mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-03-30 05:53:33 +02:00
finished mvp for js menu
This commit is contained in:
parent
12df136ccf
commit
3a32b65454
@ -63,100 +63,103 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// Initialize the current navigation
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
function initCurrentNav() {
|
// Initialization: wait for window load and then trigger current nav detection.
|
||||||
// If Alpine is available, wait until the DOM is updated.
|
window.addEventListener("load", function() {
|
||||||
if (window.Alpine && typeof window.Alpine.nextTick === 'function') {
|
console.log("Window loaded, initializing current nav...");
|
||||||
window.Alpine.nextTick(processNav);
|
initCurrentNav();
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// After processing, open all submenus under current items.
|
|
||||||
openCurrentSubmenus();
|
// Re-trigger when the hash changes.
|
||||||
}
|
window.addEventListener("hashchange", function() {
|
||||||
|
console.log("Hash changed, reinitializing current nav...");
|
||||||
// Mark the link's parent li and all ancestor li elements as current.
|
initCurrentNav();
|
||||||
function markAsCurrent(link) {
|
});
|
||||||
var li = link.closest("li");
|
|
||||||
if (!li) {
|
function initCurrentNav() {
|
||||||
console.log("markAsCurrent: No parent li found for link:", link);
|
// If Alpine.js is available and provides nextTick, use it.
|
||||||
return;
|
if (window.Alpine && typeof window.Alpine.nextTick === 'function') {
|
||||||
}
|
window.Alpine.nextTick(processNav);
|
||||||
li.classList.add("current");
|
} else {
|
||||||
console.log("markAsCurrent: Marked li as current:", li);
|
processNav();
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
console.log("markAsCurrent: Propagated current to ancestor li:", parentLi);
|
|
||||||
parentLi = parentLi.parentElement.closest("li");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
function processNav() {
|
||||||
// Open all submenu elements under list items marked as current.
|
var currentHash = window.location.hash;
|
||||||
function openCurrentSubmenus() {
|
console.log("initCurrentNav: Current hash:", currentHash);
|
||||||
document.querySelectorAll('.current-index li.current').forEach(function(li) {
|
if (!currentHash) return;
|
||||||
li.querySelectorAll("[x-show]").forEach(function(elem) {
|
|
||||||
if (elem.style.display === "none") {
|
// Select all internal links within the .current-index container.
|
||||||
elem.style.display = "block";
|
var links = document.querySelectorAll('.current-index a.reference.internal');
|
||||||
console.log("openCurrentSubmenus: Opened submenu element:", elem);
|
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();
|
||||||
// Re-trigger when the hash changes.
|
}
|
||||||
window.addEventListener("hashchange", function() {
|
|
||||||
console.log("Hash changed, reinitializing current nav...");
|
// Mark the link's parent li and all its ancestor li elements as current.
|
||||||
initCurrentNav();
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
</script>
|
||||||
// Wait until the window fully loads, then initialize the navigation.
|
|
||||||
window.addEventListener("load", function() {
|
|
||||||
console.log("Window loaded, initializing current nav...");
|
|
||||||
initCurrentNav();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user