Files
wysite/__mirror/static-mirror.js

109 lines
3.0 KiB
JavaScript

(function () {
function getDirectChildSubmenuList(submenu) {
for (const child of submenu.children) {
if (child.tagName === "UL" && child.classList.contains("el-menu--inline")) {
return child;
}
}
return null;
}
function setExpanded(title, expand) {
const submenu = title.closest(".el-submenu");
if (!submenu) {
return;
}
const list = getDirectChildSubmenuList(submenu);
if (!list) {
return;
}
submenu.classList.toggle("codex-static-submenu-open", expand);
list.style.display = expand ? "block" : "none";
title.setAttribute("aria-expanded", String(expand));
}
function toggle(title) {
const submenu = title.closest(".el-submenu");
if (!submenu) {
return;
}
const list = getDirectChildSubmenuList(submenu);
if (!list) {
return;
}
const computed = window.getComputedStyle(list).display;
setExpanded(title, computed === "none");
}
function expandAncestorsForCurrentLink() {
const currentPath = window.location.pathname.replace(/index\.html$/, "");
const anchors = document.querySelectorAll("a[href]");
anchors.forEach((anchor) => {
const href = anchor.getAttribute("href");
if (!href || href.startsWith("#") || href.startsWith("http")) {
return;
}
let resolvedPath = "";
try {
resolvedPath = new URL(href, window.location.href).pathname.replace(/index\.html$/, "");
} catch (_error) {
return;
}
if (resolvedPath !== currentPath) {
return;
}
let submenu = anchor.closest(".el-submenu");
while (submenu) {
const title = submenu.querySelector(":scope > .el-submenu__title");
if (title) {
setExpanded(title, true);
}
submenu = submenu.parentElement ? submenu.parentElement.closest(".el-submenu") : null;
}
});
}
function initStaticSubmenus() {
const titles = document.querySelectorAll(".el-submenu > .el-submenu__title");
titles.forEach((title) => {
const submenu = title.closest(".el-submenu");
if (!submenu) {
return;
}
const list = getDirectChildSubmenuList(submenu);
if (!list) {
return;
}
title.setAttribute("role", "button");
title.setAttribute("tabindex", "0");
title.setAttribute("aria-expanded", window.getComputedStyle(list).display !== "none" ? "true" : "false");
title.addEventListener("click", function (event) {
event.preventDefault();
event.stopPropagation();
toggle(title);
});
title.addEventListener("keydown", function (event) {
if (event.key !== "Enter" && event.key !== " ") {
return;
}
event.preventDefault();
toggle(title);
});
});
expandAncestorsForCurrentLink();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initStaticSubmenus);
} else {
initStaticSubmenus();
}
})();