Move secondary menu items to "more" dropdown dynamically
This commit is contained in:
parent
c8757cf1d0
commit
cdd156137b
3 changed files with 57 additions and 27 deletions
|
@ -90,45 +90,74 @@ $(document).ready(function () {
|
||||||
// See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
|
// See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
|
||||||
Turbo.session.drive = false;
|
Turbo.session.drive = false;
|
||||||
|
|
||||||
let headerWidth = 0;
|
const $expandedSecondaryMenu = $("header nav.secondary > ul"),
|
||||||
const breakpointWidth = 768;
|
$collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
|
||||||
|
secondaryMenuItems = [],
|
||||||
|
breakpointWidth = 768;
|
||||||
|
let moreItemWidth = 0;
|
||||||
|
|
||||||
function updateHeader() {
|
function updateHeader() {
|
||||||
var windowWidth = $(window).width();
|
var windowWidth = $(window).width();
|
||||||
|
|
||||||
if (windowWidth < breakpointWidth) {
|
if (windowWidth < breakpointWidth) {
|
||||||
$("body").addClass("small-nav");
|
$("body").addClass("small-nav");
|
||||||
expandSecondaryMenu();
|
expandAllSecondaryMenuItems();
|
||||||
} else if (windowWidth < headerWidth) {
|
|
||||||
$("body").removeClass("small-nav");
|
|
||||||
collapseSecondaryMenu();
|
|
||||||
} else {
|
} else {
|
||||||
$("body").removeClass("small-nav");
|
$("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() {
|
function expandAllSecondaryMenuItems() {
|
||||||
$("#compact-secondary-nav > ul").find("li").children("a")
|
secondaryMenuItems.forEach(function (item) {
|
||||||
|
expandSecondaryMenuItem($(item[0]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function expandSecondaryMenuItem($item) {
|
||||||
|
$item.children("a")
|
||||||
.removeClass("dropdown-item")
|
.removeClass("dropdown-item")
|
||||||
.addClass("nav-link")
|
.addClass("nav-link")
|
||||||
.addClass(function () {
|
.addClass(function () {
|
||||||
return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
|
return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
|
||||||
});
|
});
|
||||||
$("#compact-secondary-nav > ul").find("li")
|
$item.addClass("nav-item").insertBefore("#compact-secondary-nav");
|
||||||
.addClass("nav-item")
|
toggleCompactSecondaryNav();
|
||||||
.prependTo("header nav.secondary > ul");
|
|
||||||
$("#compact-secondary-nav").hide();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function collapseSecondaryMenu() {
|
function collapseSecondaryMenuItem($item) {
|
||||||
$("header nav.secondary > ul").find("li:not(#compact-secondary-nav)").children("a")
|
$item.children("a")
|
||||||
.addClass("dropdown-item")
|
.addClass("dropdown-item")
|
||||||
.removeClass("nav-link text-secondary text-secondary-emphasis");
|
.removeClass("nav-link text-secondary text-secondary-emphasis");
|
||||||
$("header nav.secondary > ul").find("li:not(#compact-secondary-nav)")
|
$item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
|
||||||
.removeClass("nav-item")
|
toggleCompactSecondaryNav();
|
||||||
.prependTo("#compact-secondary-nav > ul");
|
}
|
||||||
$("#compact-secondary-nav").show();
|
|
||||||
|
function toggleCompactSecondaryNav() {
|
||||||
|
$("#compact-secondary-nav").toggle(
|
||||||
|
$collapsedSecondaryMenu.find("li").length > 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -138,9 +167,10 @@ $(document).ready(function () {
|
||||||
* to defer the measurement slightly as a workaround.
|
* to defer the measurement slightly as a workaround.
|
||||||
*/
|
*/
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
$("header").children(":visible").each(function (i, e) {
|
$expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
|
||||||
headerWidth += $(e).outerWidth();
|
secondaryMenuItems.push([this, $(this).width()]);
|
||||||
});
|
});
|
||||||
|
moreItemWidth = $("#compact-secondary-nav").width();
|
||||||
|
|
||||||
$("header").removeClass("text-nowrap");
|
$("header").removeClass("text-nowrap");
|
||||||
$("header nav.secondary > ul").removeClass("flex-nowrap");
|
$("header nav.secondary > ul").removeClass("flex-nowrap");
|
||||||
|
|
|
@ -131,10 +131,6 @@ header {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
nav.primary {
|
|
||||||
margin-right: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.username {
|
.username {
|
||||||
max-width: 12em;
|
max-width: 12em;
|
||||||
}
|
}
|
||||||
|
@ -231,6 +227,10 @@ body.small-nav {
|
||||||
nav.secondary {
|
nav.secondary {
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
|
> ul {
|
||||||
|
justify-content: unset !important;
|
||||||
|
}
|
||||||
|
|
||||||
.user-menu, .login-menu {
|
.user-menu, .login-menu {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
<nav class='secondary d-flex gap-2 align-items-center'>
|
<nav class='secondary d-flex gap-2 flex-grow-1 align-items-center'>
|
||||||
<ul class='nav flex-nowrap'>
|
<ul class='nav flex-grow-1 justify-content-end flex-nowrap'>
|
||||||
<% if Settings.status != "database_offline" && can?(:index, Issue) %>
|
<% if Settings.status != "database_offline" && can?(:index, Issue) %>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<%= link_to issues_path(:status => "open"), :class => header_nav_link_class(issues_path) do %>
|
<%= link_to issues_path(:status => "open"), :class => header_nav_link_class(issues_path) do %>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue