Skip to content

Commit

Permalink
Move secondary menu items to "more" dropdown dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 2, 2024
1 parent b696a18 commit 8973383
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
70 changes: 50 additions & 20 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,74 @@ $(document).ready(function () {
// See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
Turbo.session.drive = false;

var headerWidth = 0,
var $expandedSecondaryMenu = $("header nav.secondary > ul"),
$collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
secondaryMenuItems = [],
moreItemWidth = 0,
breakpointWidth = 768;

function updateHeader() {
var windowWidth = $(window).width();

if (windowWidth < breakpointWidth) {
$("body").addClass("small-nav");
expandSecondaryMenu();
} else if (windowWidth < headerWidth) {
$("body").removeClass("small-nav");
collapseSecondaryMenu();
expandAllSecondaryMenuItems();
} else {
$("body").removeClass("small-nav");
expandSecondaryMenu();
var availableWidth = $expandedSecondaryMenu.width();
secondaryMenuItems.forEach(function (item) {
$(item[0]).remove();
});
var runningWidth = 0,
i = 0,
requiredWidth;
for (; i < secondaryMenuItems.length; i++) {
runningWidth += secondaryMenuItems[i][1];
if (i < secondaryMenuItems.length - 1) {
requiredWidth = runningWidth + moreItemWidth;
} else {
requiredWidth = runningWidth;
}
if (requiredWidth > availableWidth) {
break;
}
expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
}
for (; i < secondaryMenuItems.length; i++) {
collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
}
}
}

function expandSecondaryMenu() {
$("#compact-secondary-nav > ul").find("li").children("a")
function expandAllSecondaryMenuItems() {
secondaryMenuItems.forEach(function (item) {
expandSecondaryMenuItem($(item[0]));
});
}

function expandSecondaryMenuItem($item) {
$item.children("a")
.removeClass("dropdown-item")
.addClass("nav-link")
.addClass(function () {
return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
});
$("#compact-secondary-nav > ul").find("li")
.addClass("nav-item")
.prependTo("header nav.secondary > ul");
$("#compact-secondary-nav").hide();
$item.addClass("nav-item").insertBefore("#compact-secondary-nav");
toggleCompactSecondaryNav();
}

function collapseSecondaryMenu() {
$("header nav.secondary > ul").find("li:not(#compact-secondary-nav)").children("a")
function collapseSecondaryMenuItem($item) {
$item.children("a")
.addClass("dropdown-item")
.removeClass("nav-link text-secondary text-secondary-emphasis");
$("header nav.secondary > ul").find("li:not(#compact-secondary-nav)")
.removeClass("nav-item")
.prependTo("#compact-secondary-nav > ul");
$("#compact-secondary-nav").show();
$item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
toggleCompactSecondaryNav();
}

function toggleCompactSecondaryNav() {
$("#compact-secondary-nav").toggle(
$collapsedSecondaryMenu.find("li").length > 0
);
}

/*
Expand All @@ -118,9 +147,10 @@ $(document).ready(function () {
* to defer the measurement slightly as a workaround.
*/
setTimeout(function () {
$("header").children(":visible").each(function (i, e) {
headerWidth = headerWidth + $(e).outerWidth();
$expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
secondaryMenuItems.push([this, $(this).width()]);
});
moreItemWidth = $("#compact-secondary-nav").width();

$("header").removeClass("text-nowrap");
$("header nav.secondary > ul").removeClass("flex-nowrap");
Expand Down
8 changes: 4 additions & 4 deletions app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ header {
font-size: 14px;
}

nav.primary {
margin-right: auto;
}

.username {
max-width: 12em;
}
Expand Down Expand Up @@ -227,6 +223,10 @@ body.small-nav {
nav.secondary {
flex-direction: column;

> ul {
justify-content: unset !important;
}

.user-menu, .login-menu {
width: 100%;
}
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</ul>
</div>
</nav>
<nav class='secondary d-flex gap-2 align-items-center'>
<ul class='nav flex-nowrap'>
<nav class='secondary d-flex gap-2 flex-grow-1 align-items-center'>
<ul class='nav flex-grow-1 justify-content-end flex-nowrap'>
<% if Settings.status != "database_offline" && can?(:index, Issue) %>
<li class="nav-item">
<%= link_to issues_path(:status => "open"), :class => header_nav_link_class(issues_path) do %>
Expand Down

0 comments on commit 8973383

Please sign in to comment.