servala-portal/src/servala/static/js/sidebar.js
2025-04-07 22:47:00 +02:00

30 lines
934 B
JavaScript

/**
* This script marks the current path as active in the menu.
*/
const markActive = (link) => {
const parentItem = link.closest('.menu-item');
link.classList.add('active');
if (parentItem) {
parentItem.classList.add('active');
}
}
const checkLink = (fuzzy) => {
}
document.addEventListener('DOMContentLoaded', () => {
const currentPath = window.location.pathname;
const menuLinks = [...document.querySelectorAll('a.menu-link')]
const exactMatches = menuLinks.filter(link => link.getAttribute('href') === currentPath)
if (exactMatches.length > 0) {
markActive(exactMatches[0])
} else {
fuzzyMatches = menuLinks.filter(link => currentPath.startsWith(link.getAttribute('href')))
if (fuzzyMatches.length > 0) {
const longestMatch = fuzzyMatches.sort((a, b) => b.href.length - a.href.length)[0]
markActive(longestMatch)
}
}
})