2024-07-15 18:38:50 -07:00
|
|
|
// Populate the sidebar
|
|
|
|
|
//
|
|
|
|
|
// This is a script, and not included directly in the page, to control the total size of the book.
|
|
|
|
|
// The TOC contains an entry for each page, so if each page includes a copy of the TOC,
|
|
|
|
|
// the total size of the page becomes O(n**2).
|
2024-11-06 14:56:49 -07:00
|
|
|
class MDBookSidebarScrollbox extends HTMLElement {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2024-07-15 18:38:50 -07:00
|
|
|
}
|
2024-11-06 14:56:49 -07:00
|
|
|
connectedCallback() {
|
|
|
|
|
this.innerHTML = '{{#toc}}{{/toc}}';
|
|
|
|
|
// Set the current, active page, and reveal it if it's hidden
|
2025-08-25 15:15:47 -07:00
|
|
|
let current_page = document.location.href.toString().split('#')[0].split('?')[0];
|
|
|
|
|
if (current_page.endsWith('/')) {
|
|
|
|
|
current_page += 'index.html';
|
2024-07-15 18:38:50 -07:00
|
|
|
}
|
2025-08-25 15:15:47 -07:00
|
|
|
const links = Array.prototype.slice.call(this.querySelectorAll('a'));
|
|
|
|
|
const l = links.length;
|
|
|
|
|
for (let i = 0; i < l; ++i) {
|
|
|
|
|
const link = links[i];
|
|
|
|
|
const href = link.getAttribute('href');
|
|
|
|
|
if (href && !href.startsWith('#') && !/^(?:[a-z+]+:)?\/\//.test(href)) {
|
2024-11-06 14:56:49 -07:00
|
|
|
link.href = path_to_root + href;
|
|
|
|
|
}
|
2025-08-25 15:15:47 -07:00
|
|
|
// The 'index' page is supposed to alias the first chapter in the book.
|
|
|
|
|
if (link.href === current_page
|
|
|
|
|
|| i === 0
|
|
|
|
|
&& path_to_root === ''
|
|
|
|
|
&& current_page.endsWith('/index.html')) {
|
|
|
|
|
link.classList.add('active');
|
|
|
|
|
let parent = link.parentElement;
|
|
|
|
|
if (parent && parent.classList.contains('chapter-item')) {
|
|
|
|
|
parent.classList.add('expanded');
|
2024-11-06 14:56:49 -07:00
|
|
|
}
|
|
|
|
|
while (parent) {
|
2025-08-25 15:15:47 -07:00
|
|
|
if (parent.tagName === 'LI' && parent.previousElementSibling) {
|
|
|
|
|
if (parent.previousElementSibling.classList.contains('chapter-item')) {
|
|
|
|
|
parent.previousElementSibling.classList.add('expanded');
|
2024-11-06 14:56:49 -07:00
|
|
|
}
|
2024-07-15 18:38:50 -07:00
|
|
|
}
|
2024-11-06 14:56:49 -07:00
|
|
|
parent = parent.parentElement;
|
2024-07-15 18:38:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-06 14:56:49 -07:00
|
|
|
// Track and set sidebar scroll position
|
2025-08-25 15:15:47 -07:00
|
|
|
this.addEventListener('click', e => {
|
2024-11-06 14:56:49 -07:00
|
|
|
if (e.target.tagName === 'A') {
|
|
|
|
|
sessionStorage.setItem('sidebar-scroll', this.scrollTop);
|
|
|
|
|
}
|
|
|
|
|
}, { passive: true });
|
2025-08-25 15:15:47 -07:00
|
|
|
const sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
|
2024-11-06 14:56:49 -07:00
|
|
|
sessionStorage.removeItem('sidebar-scroll');
|
|
|
|
|
if (sidebarScrollTop) {
|
|
|
|
|
// preserve sidebar scroll position when navigating via links within sidebar
|
|
|
|
|
this.scrollTop = sidebarScrollTop;
|
|
|
|
|
} else {
|
2025-08-25 15:15:47 -07:00
|
|
|
// scroll sidebar to current active section when navigating via
|
|
|
|
|
// 'next/previous chapter' buttons
|
|
|
|
|
const activeSection = document.querySelector('#mdbook-sidebar .active');
|
2024-11-06 14:56:49 -07:00
|
|
|
if (activeSection) {
|
|
|
|
|
activeSection.scrollIntoView({ block: 'center' });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Toggle buttons
|
2025-08-25 15:15:47 -07:00
|
|
|
const sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
|
2024-11-06 14:56:49 -07:00
|
|
|
function toggleSection(ev) {
|
|
|
|
|
ev.currentTarget.parentElement.classList.toggle('expanded');
|
|
|
|
|
}
|
2025-08-25 15:15:47 -07:00
|
|
|
Array.from(sidebarAnchorToggles).forEach(el => {
|
2024-11-06 14:56:49 -07:00
|
|
|
el.addEventListener('click', toggleSection);
|
|
|
|
|
});
|
2024-07-15 18:38:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-25 15:15:47 -07:00
|
|
|
window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox);
|