bookshelf-doc/static/nav.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-02-22 04:40:14 +00:00
/**
* This module is used to implement persistent navbar expansion.
*/
2022-02-22 04:40:14 +00:00
// The variable to store the expansion information.
let expanded = {};
2022-02-22 04:40:14 +00:00
// Load expansion information from sessionStorage.
for (const e of (sessionStorage.getItem("expanded") || "").split(",")) {
if (e !== "") {
expanded[e] = true;
}
}
2022-02-22 04:40:14 +00:00
/**
* Save expansion information to sessionStorage.
*/
function saveExpanded() {
2022-02-22 04:40:14 +00:00
sessionStorage.setItem(
"expanded",
Object.getOwnPropertyNames(expanded)
.filter((e) => expanded[e])
.join(",")
);
}
2022-02-22 04:40:14 +00:00
// save expansion information when user change the expansion.
for (const elem of document.getElementsByClassName("nav_sect")) {
const id = elem.getAttribute("data-path");
if (!id) continue;
if (expanded[id]) {
elem.open = true;
}
2022-02-22 04:40:14 +00:00
elem.addEventListener("toggle", () => {
expanded[id] = elem.open;
saveExpanded();
});
}
2022-02-22 04:40:14 +00:00
// Scroll to center.
for (const currentFileLink of document.getElementsByClassName("visible")) {
currentFileLink.scrollIntoView({ block: "center" });
}