diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index eef69e7e72..d0aeedfa77 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -70,7 +70,10 @@ $(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() { @@ -78,37 +81,63 @@ $(document).ready(function () { 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 + ); } /* @@ -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"); diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index d8de7b14fb..a29349fe88 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -131,10 +131,6 @@ header { font-size: 14px; } - nav.primary { - margin-right: auto; - } - .username { max-width: 12em; } @@ -227,6 +223,10 @@ body.small-nav { nav.secondary { flex-direction: column; + > ul { + justify-content: unset !important; + } + .user-menu, .login-menu { width: 100%; } diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index c0ea32f4fe..6234058068 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -27,8 +27,8 @@ -