var sidebarStorageKey = "dove-sidebar-expanded"; function getSidebarState() { try { var stored = localStorage.getItem(sidebarStorageKey); return stored ? JSON.parse(stored) : {}; } catch (error) { return {}; } } function saveSidebarState(state) { localStorage.setItem(sidebarStorageKey, JSON.stringify(state)); } function toggleSection(section, expanded) { var children = section.querySelector("[data-sidebar-children]"); var chevron = section.querySelector("[data-sidebar-chevron]"); if (expanded) { children.classList.remove("hidden"); chevron.style.transform = "rotate(90deg)"; } else { children.classList.add("hidden"); chevron.style.transform = ""; } } function initSidebar() { var currentPath = window.location.pathname; var state = getSidebarState(); document.querySelectorAll("#sidebar-nav > a.nav-link").forEach(function (link) { link.classList.remove("active"); if (link.getAttribute("href") === currentPath) { link.classList.add("active"); } }); document.querySelectorAll("[data-sidebar-section]").forEach(function (section) { var prefix = section.dataset.sectionPrefix; var trigger = section.querySelector("[data-sidebar-trigger]"); var chevron = section.querySelector("[data-sidebar-chevron]"); var pathMatchesSection = currentPath.indexOf(prefix) === 0; if (pathMatchesSection && state[prefix] === undefined) { state[prefix] = true; } toggleSection(section, !!state[prefix]); trigger.classList.remove("active"); if (currentPath === prefix || currentPath === prefix + "/") { trigger.classList.add("active"); } section.querySelectorAll("[data-sidebar-children] .nav-link").forEach(function (link) { link.classList.remove("active"); var href = link.getAttribute("href"); if (currentPath.indexOf(href) === 0 && href !== prefix + "/") { link.classList.add("active"); } }); if (!trigger.dataset.sidebarBound) { trigger.dataset.sidebarBound = "true"; trigger.addEventListener("click", function () { state[prefix] = true; saveSidebarState(state); toggleSection(section, true); }); chevron.addEventListener("click", function (event) { event.preventDefault(); event.stopPropagation(); state[prefix] = !state[prefix]; saveSidebarState(state); toggleSection(section, state[prefix]); }); } }); saveSidebarState(state); } document.addEventListener("DOMContentLoaded", initSidebar); document.body.addEventListener("htmx:afterSwap", initSidebar);