[ADD] Total notification counter to sidebar

This commit is contained in:
Peter Pfeufer 2025-10-28 09:38:20 +01:00
parent 513b7b88f4
commit 80145b313f
No known key found for this signature in database
2 changed files with 57 additions and 7 deletions

View File

@ -47,6 +47,12 @@
/* Side Navigation /* Side Navigation
------------------------------------------------------------------------------------- */ ------------------------------------------------------------------------------------- */
@media all { @media all {
.sidemenu-total-notifications-badge {
position: absolute;
left: 28px;
font-size: 42.5% !important;
}
#sidebar > div { #sidebar > div {
width: 325px; width: 325px;
} }

View File

@ -1,13 +1,57 @@
$(document).ready(() => { $(document).ready(() => {
'use strict'; 'use strict';
const activeChildMenuItem = document.querySelector('ul#sidebar-menu ul.collapse a.active'); /**
* Collect all badges in the sidebar menu that are not part of a collapsible submenu, and calculate the total notification count.
* Show a total notification badge in the navbar if there are any notifications.
*/
const totalNotificationsBadge = () => {
const badges = [];
let notificationCount = 0;
if (activeChildMenuItem) { document.querySelectorAll('#sidebar-menu .badge').forEach(b => {
const activeChildMenuUl = activeChildMenuItem.closest('ul'); const li = b.closest('li');
activeChildMenuUl.classList.add('show');
document.querySelectorAll(`[data-bs-target^="#${activeChildMenuUl.id}"]`) if (!li || !li.querySelector('ul.collapse')) {
.forEach(element => element.setAttribute('aria-expanded', true)); badges.push(b);
} notificationCount += parseInt(b.textContent);
}
});
if (badges.length > 0 && notificationCount > 0) {
const notificationBadge = document.createElement('span');
notificationBadge.classList.add(
'badge',
'text-bg-danger',
'align-self-center',
'sidemenu-notification-badge',
'sidemenu-total-notifications-badge'
);
notificationBadge.textContent = String(notificationCount);
document.querySelector('a.navbar-brand i.fa-solid').prepend(notificationBadge);
}
};
/**
* Find the active child menu item in the sidebar menu, if any, and ensure its parent submenu is expanded.
*/
const expandChildMenu = () => {
const activeChildMenuItem = document.querySelector('ul#sidebar-menu ul.collapse a.active');
if (activeChildMenuItem) {
const activeChildMenuUl = activeChildMenuItem.closest('ul');
activeChildMenuUl.classList.add('show');
document.querySelectorAll(`[data-bs-target^="#${activeChildMenuUl.id}"]`)
.forEach(element => element.setAttribute('aria-expanded', 'true'));
}
};
// Execute functions on document ready
[
totalNotificationsBadge,
expandChildMenu
].forEach(fn => fn());
}); });