diff --git a/src/servala/static/js/sidebar.js b/src/servala/static/js/sidebar.js index 1f79795..f541788 100644 --- a/src/servala/static/js/sidebar.js +++ b/src/servala/static/js/sidebar.js @@ -2,21 +2,30 @@ * This script marks the current path as active in the sidebar. */ +const markActive = (link) => { + const parentItem = link.closest('.sidebar-item'); + if (parentItem) { + parentItem.classList.add('active'); + } else { + link.classList.add('active'); + } +} + +const checkLink = (fuzzy) => { +} + document.addEventListener('DOMContentLoaded', () => { const currentPath = window.location.pathname; - const sidebarLinks = document.querySelectorAll('.sidebar-link'); + const sidebarLinks = [...document.querySelectorAll('a.sidebar-link')] - sidebarLinks.forEach(link => { - // Skip links that are inside buttons (like logout) - if (link.tagName === 'BUTTON') return; - - if (link.getAttribute('href') === currentPath) { - const parentItem = link.closest('.sidebar-item'); - if (parentItem) { - parentItem.classList.add('active'); - } else { - link.classList.add('active'); - } + const exactMatches = sidebarLinks.filter(link => link.getAttribute('href') === currentPath) + if (exactMatches.length > 0) { + markActive(exactMatches[0]) + } else { + fuzzyMatches = sidebarLinks.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) } - }) + } })