finished mvp for js menu

This commit is contained in:
Kevin Veen-Birkenbach 2025-03-17 16:16:29 +01:00
parent 12df136ccf
commit 3a32b65454
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

@ -63,100 +63,103 @@
{% endif %}
<script>
// Initialize the current navigation
function initCurrentNav() {
// If Alpine is available, wait until the DOM is updated.
if (window.Alpine && typeof window.Alpine.nextTick === 'function') {
window.Alpine.nextTick(processNav);
} else {
processNav();
}
}
// Process all navigation links within the .current-index container.
function processNav() {
var currentHash = window.location.hash;
console.log("initCurrentNav: Current hash:", currentHash);
if (!currentHash) return;
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 only 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);
}
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();
});
// After processing, open all submenus under current items.
openCurrentSubmenus();
}
// Mark the link's parent li and all 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 in use, 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;
// 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();
}
console.log("markAsCurrent: Propagated current to ancestor li:", parentLi);
parentLi = parentLi.parentElement.closest("li");
}
}
// Open all submenu elements under list items marked as current.
function openCurrentSubmenus() {
document.querySelectorAll('.current-index li.current').forEach(function(li) {
li.querySelectorAll("[x-show]").forEach(function(elem) {
if (elem.style.display === "none") {
elem.style.display = "block";
console.log("openCurrentSubmenus: Opened submenu element:", elem);
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);
}
});
});
}
// Re-trigger when the hash changes.
window.addEventListener("hashchange", function() {
console.log("Hash changed, reinitializing current nav...");
initCurrentNav();
// 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);
}
});
});
}
});
// Wait until the window fully loads, then initialize the navigation.
window.addEventListener("load", function() {
console.log("Window loaded, initializing current nav...");
initCurrentNav();
});
</script>
</script>