@@ -10,61 +10,65 @@ class MDBookSidebarScrollbox extends HTMLElement {
1010 connectedCallback() {
1111 this.innerHTML = '{{ #toc }} {{ /toc }} ';
1212 // Set the current, active page, and reveal it if it's hidden
13- let current_page = document.location.href.toString().split("#" )[0].split("?" )[0];
14- if (current_page.endsWith("/" )) {
15- current_page += " index.html" ;
13+ let current_page = document.location.href.toString().split('#' )[0].split('?' )[0];
14+ if (current_page.endsWith('/' )) {
15+ current_page += ' index.html' ;
1616 }
17- var links = Array.prototype.slice.call(this.querySelectorAll("a" ));
18- var l = links.length;
19- for (var i = 0; i < l; ++i) {
20- var link = links[i];
21- var href = link.getAttribute(" href" );
22- if (href && !href.startsWith("#" ) && !/^(?:[a-z+]+:)?\/\//.test(href)) {
17+ const links = Array.prototype.slice.call(this.querySelectorAll('a' ));
18+ const l = links.length;
19+ for (let i = 0; i < l; ++i) {
20+ const link = links[i];
21+ const href = link.getAttribute(' href' );
22+ if (href && !href.startsWith('#' ) && !/^(?:[a-z+]+:)?\/\//.test(href)) {
2323 link.href = path_to_root + href;
2424 }
25- // The "index" page is supposed to alias the first chapter in the book.
26- if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
27- link.classList.add("active");
28- var parent = link.parentElement;
29- if (parent && parent.classList.contains("chapter-item")) {
30- parent.classList.add("expanded");
25+ // The 'index' page is supposed to alias the first chapter in the book.
26+ if (link.href === current_page
27+ || i === 0
28+ && path_to_root === ''
29+ && current_page.endsWith('/index.html')) {
30+ link.classList.add('active');
31+ let parent = link.parentElement;
32+ if (parent && parent.classList.contains('chapter-item')) {
33+ parent.classList.add('expanded');
3134 }
3235 while (parent) {
33- if (parent.tagName === "LI" && parent.previousElementSibling) {
34- if (parent.previousElementSibling.classList.contains(" chapter-item" )) {
35- parent.previousElementSibling.classList.add(" expanded" );
36+ if (parent.tagName === 'LI' && parent.previousElementSibling) {
37+ if (parent.previousElementSibling.classList.contains(' chapter-item' )) {
38+ parent.previousElementSibling.classList.add(' expanded' );
3639 }
3740 }
3841 parent = parent.parentElement;
3942 }
4043 }
4144 }
4245 // Track and set sidebar scroll position
43- this.addEventListener('click', function(e) {
46+ this.addEventListener('click', e => {
4447 if (e.target.tagName === 'A') {
4548 sessionStorage.setItem('sidebar-scroll', this.scrollTop);
4649 }
4750 }, { passive: true });
48- var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
51+ const sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
4952 sessionStorage.removeItem('sidebar-scroll');
5053 if (sidebarScrollTop) {
5154 // preserve sidebar scroll position when navigating via links within sidebar
5255 this.scrollTop = sidebarScrollTop;
5356 } else {
54- // scroll sidebar to current active section when navigating via "next/previous chapter" buttons
55- var activeSection = document.querySelector('#mdbook-sidebar .active');
57+ // scroll sidebar to current active section when navigating via
58+ // 'next/previous chapter' buttons
59+ const activeSection = document.querySelector('#mdbook-sidebar .active');
5660 if (activeSection) {
5761 activeSection.scrollIntoView({ block: 'center' });
5862 }
5963 }
6064 // Toggle buttons
61- var sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
65+ const sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
6266 function toggleSection(ev) {
6367 ev.currentTarget.parentElement.classList.toggle('expanded');
6468 }
65- Array.from(sidebarAnchorToggles).forEach(function (el) {
69+ Array.from(sidebarAnchorToggles).forEach(el => {
6670 el.addEventListener('click', toggleSection);
6771 });
6872 }
6973}
70- window.customElements.define(" mdbook-sidebar-scrollbox" , MDBookSidebarScrollbox);
74+ window.customElements.define(' mdbook-sidebar-scrollbox' , MDBookSidebarScrollbox);
0 commit comments