From 4cde1ed9e418adefb2c3e40bbbdab85a9ff23d6e Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 17 Mar 2025 16:31:07 +0100 Subject: [PATCH] Refactored js --- sphinx/_static/current-nav.js | 97 ++++++++++++++++++++++++++++++ sphinx/conf.py | 2 +- sphinx/templates/structure.html | 102 +------------------------------- 3 files changed, 99 insertions(+), 102 deletions(-) create mode 100644 sphinx/_static/current-nav.js diff --git a/sphinx/_static/current-nav.js b/sphinx/_static/current-nav.js new file mode 100644 index 00000000..67d189de --- /dev/null +++ b/sphinx/_static/current-nav.js @@ -0,0 +1,97 @@ + document.addEventListener("DOMContentLoaded", function() { + // Initialization: wait for window load and then trigger current nav detection. + window.addEventListener("load", function() { + console.log("Window loaded, initializing current nav..."); + initCurrentNav(); + }); + + // Re-trigger when the hash changes. + window.addEventListener("hashchange", function() { + console.log("Hash changed, reinitializing current nav..."); + initCurrentNav(); + }); + + function initCurrentNav() { + // If Alpine.js is available and provides nextTick, use it. + if (window.Alpine && typeof window.Alpine.nextTick === 'function') { + window.Alpine.nextTick(processNav); + } else { + processNav(); + } + } + + function processNav() { + var currentHash = window.location.hash; + console.log("initCurrentNav: Current hash:", currentHash); + if (!currentHash) return; + + // Select all internal links within the .current-index container. + var links = document.querySelectorAll('.current-index a.reference.internal'); + links.forEach(function(link) { + var href = link.getAttribute("href"); + console.log("initCurrentNav: Checking link:", href); + // If the link is hash-only (e.g. "#setup-guide") + if (href && href.trim().startsWith("#")) { + if (href.trim() === currentHash.trim()) { + console.log("initCurrentNav: Match found for hash-only link:", href); + markAsCurrent(link); + } + } + // Otherwise, if the link includes a file and a hash, compare the hash part. + else if (href && href.indexOf('#') !== -1) { + var parts = href.split('#'); + var linkHash = "#" + parts[1].trim(); + console.log("initCurrentNav: Extracted link hash:", linkHash); + if (linkHash === currentHash.trim()) { + console.log("initCurrentNav: Match found for link with file and hash:", href); + markAsCurrent(link); + } + } + else { + console.log("initCurrentNav: No match for link:", href); + } + }); + + // After processing links, open submenus only for those li elements marked as current. + openCurrentSubmenus(); + } + + // Mark the link's parent li and all its ancestor li elements as current. + function markAsCurrent(link) { + var li = link.closest("li"); + if (!li) { + console.log("markAsCurrent: No parent li found for link:", link); + return; + } + li.classList.add("current"); + console.log("markAsCurrent: Marked li as current:", li); + // If Alpine.js is used, set its "expanded" property to true. + if (li.__x && li.__x.$data) { + li.__x.$data.expanded = true; + console.log("markAsCurrent: Set Alpine expanded on li:", li); + } + // Propagate upward: mark all ancestor li elements as current. + var parentLi = li.parentElement.closest("li"); + while (parentLi) { + parentLi.classList.add("current"); + if (parentLi.__x && parentLi.__x.$data) { + parentLi.__x.$data.expanded = true; + } + console.log("markAsCurrent: Propagated current to ancestor li:", parentLi); + parentLi = parentLi.parentElement.closest("li"); + } + } + + // Open immediate submenu elements (the direct children with x-show) of li.current. + function openCurrentSubmenus() { + document.querySelectorAll('.current-index li.current').forEach(function(li) { + // Only target immediate child elements that have x-show. + li.querySelectorAll(":scope > [x-show]").forEach(function(elem) { + if (elem.style.display === "none" || elem.style.display === "") { + elem.style.display = "block"; + console.log("openCurrentSubmenus: Opened submenu element:", elem); + } + }); + }); + } + }); \ No newline at end of file diff --git a/sphinx/conf.py b/sphinx/conf.py index dba4f26e..1df4b694 100644 --- a/sphinx/conf.py +++ b/sphinx/conf.py @@ -22,7 +22,7 @@ exclude_patterns = ['docs', 'venv', 'venv/**'] # -- Options for HTML output ------------------------------------------------- html_theme = 'sphinxawesome_theme' -html_static_path = ['static'] +html_static_path = ['_static'] html_sidebars = { '**': [ diff --git a/sphinx/templates/structure.html b/sphinx/templates/structure.html index ba75972f..778204d8 100644 --- a/sphinx/templates/structure.html +++ b/sphinx/templates/structure.html @@ -61,105 +61,5 @@ {% endif %} {% endif %} - - - + \ No newline at end of file