X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/424316b41c155af251597ce4993accd819fe797a..e6b0e1ae7ef45b87a61f18bb50296273d6dd1bfb:/app/assets/javascripts/application.js?ds=sidebyside diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 005ab6217..a3fd93e27 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -11,7 +11,6 @@ //= require leaflet.zoom //= require leaflet.locationfilter //= require i18n -//= require oauth //= require matomo //= require richtext @@ -76,7 +75,7 @@ window.updateLinks = function (loc, zoom, layers, object) { // Disable the button group and also the buttons to avoid // inconsistent behaviour when zooming - var editDisabled = zoom < 13; + const editDisabled = zoom < 13; $("#edit_tab") .tooltip({ placement: "bottom" }) .tooltip(editDisabled ? "enable" : "disable") @@ -90,45 +89,74 @@ $(document).ready(function () { // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive Turbo.session.drive = false; - var headerWidth = 0, - compactWidth = 0; + const $expandedSecondaryMenu = $("header nav.secondary > ul"), + $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"), + secondaryMenuItems = [], + breakpointWidth = 768; + let moreItemWidth = 0; function updateHeader() { - var windowWidth = $(window).width(); + const windowWidth = $(window).width(); - if (windowWidth < compactWidth) { + if (windowWidth < breakpointWidth) { $("body").addClass("small-nav"); - expandSecondaryMenu(); - } else if (windowWidth < headerWidth) { - $("body").removeClass("small-nav"); - collapseSecondaryMenu(); + expandAllSecondaryMenuItems(); } else { $("body").removeClass("small-nav"); - expandSecondaryMenu(); + const availableWidth = $expandedSecondaryMenu.width(); + secondaryMenuItems.forEach(function (item) { + $(item[0]).remove(); + }); + let 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 + ); } /* @@ -138,18 +166,10 @@ $(document).ready(function () { * to defer the measurement slightly as a workaround. */ setTimeout(function () { - $("header").children(":visible").each(function (i, e) { - headerWidth += $(e).outerWidth(); + $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () { + secondaryMenuItems.push([this, $(this).width()]); }); - - collapseSecondaryMenu(); - - $("header").children(":visible").each(function (i, e) { - compactWidth += $(e).outerWidth(); - }); - - $("header").removeClass("text-nowrap"); - $("header nav.secondary > ul").removeClass("flex-nowrap"); + moreItemWidth = $("#compact-secondary-nav").width(); updateHeader();