Files
ThothII/javascripts/layout-init.js
T

91 lines
2.8 KiB
JavaScript

(function () {
function toggleMenus() {
const current = document.querySelector(".bs-sidenav a.current, .bs-sidenav a[aria-current='page']");
if (!current) {
return;
}
const activeList = current.closest("ul");
document.querySelectorAll(".bs-sidenav ul").forEach((list) => {
if (list === activeList || list.contains(current)) {
list.style.display = "";
return;
}
const nested = list.parentElement && list.parentElement.parentElement;
if (nested && nested.classList.contains("bs-sidenav")) {
list.style.display = "";
}
});
}
// Keep the content pane's left padding in sync with the actual width
// of the left TOC sidebar. Fixes the windmill default where the sidebar
// can grow up to 350px but the content padding stays at 250px, so a
// wide item ends up overlapping the article body.
function syncTocSidebarWidth() {
const tocPane = document.querySelector(".wm-toc-pane");
const contentPane = document.querySelector(".wm-content-pane");
if (!tocPane || !contentPane) {
return;
}
// Skip on small screens where the sidebar is a dropdown / hidden.
if (window.matchMedia("(max-width: 600px)").matches) {
contentPane.style.paddingLeft = "";
return;
}
if (tocPane.classList.contains("wm-toc-dropdown")) {
return;
}
if (contentPane.parentElement && contentPane.parentElement.classList.contains("wm-toc-hidden")) {
contentPane.style.paddingLeft = "";
return;
}
const width = Math.ceil(tocPane.getBoundingClientRect().width);
if (width > 0) {
contentPane.style.paddingLeft = width + "px";
}
}
function installTocSidebarSync() {
const tocPane = document.querySelector(".wm-toc-pane");
if (!tocPane) {
return;
}
syncTocSidebarWidth();
if (typeof ResizeObserver === "function") {
const ro = new ResizeObserver(syncTocSidebarWidth);
ro.observe(tocPane);
}
// Re-sync after expand/collapse animations and toggle button clicks.
tocPane.addEventListener("click", function () {
// Bootstrap collapse animation defaults to 350ms.
window.setTimeout(syncTocSidebarWidth, 50);
window.setTimeout(syncTocSidebarWidth, 400);
});
const tocBtn = document.getElementById("wm-toc-button");
if (tocBtn) {
tocBtn.addEventListener("click", function () {
window.setTimeout(syncTocSidebarWidth, 50);
window.setTimeout(syncTocSidebarWidth, 400);
});
}
window.addEventListener("resize", syncTocSidebarWidth);
window.addEventListener("orientationchange", syncTocSidebarWidth);
}
function init() {
toggleMenus();
installTocSidebarSync();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();