@@ -72,27 +72,34 @@ class MDBookSidebarScrollbox extends HTMLElement {
7272 Array.from(sidebarAnchorToggles).forEach(el => {
7373 el.addEventListener('click', toggleSection);
7474 });
75- {{ #if fold_enable }}
75+ {{ #if fold_enable }}
7676 // Collapse All / Expand All button
77- // Deferred to DOMContentLoaded because the button and icon < template >
78- // elements appear later in the HTML body than this custom element .
77+ // Deferred to DOMContentLoaded because the button and icon
78+ // < template > elements appear later in the HTML body.
7979 const scrollbox = this;
80- const toggles = sidebarAnchorToggles;
8180 document.addEventListener('DOMContentLoaded', () => {
82- const collapseAllBtn = document.getElementById('mdbook-collapse-all');
83- const collapseIconHTML = document.getElementById('fa-square-minus').innerHTML;
84- const expandIconHTML = document.getElementById('fa-square-plus').innerHTML;
81+ const collapseAllBtn =
82+ document.getElementById('mdbook-collapse-all');
83+ const collapseIconHTML =
84+ document.getElementById('fa-square-minus').innerHTML;
85+ const expandIconHTML =
86+ document.getElementById('fa-square-plus').innerHTML;
8587 function getFoldableItems() {
86- return Array.from(scrollbox.querySelectorAll('li.chapter-item, li.header-item')).filter(li => li.querySelector(':scope > ol.section'));
88+ const selector = 'li.chapter-item, li.header-item';
89+ return Array.from(scrollbox.querySelectorAll(selector))
90+ .filter(li => li.querySelector(':scope > ol.section'));
8791 }
8892 function updateCollapseAllButton() {
8993 const foldable = getFoldableItems();
90- const anyExpanded = foldable.some(li => li.classList.contains('expanded'));
91- collapseAllBtn.innerHTML = anyExpanded ? collapseIconHTML : expandIconHTML;
94+ const anyExpanded = foldable.some(
95+ li => li.classList.contains('expanded'));
96+ collapseAllBtn.innerHTML = anyExpanded
97+ ? collapseIconHTML : expandIconHTML;
9298 }
9399 collapseAllBtn.addEventListener('click', () => {
94100 const foldable = getFoldableItems();
95- const anyExpanded = foldable.some(li => li.classList.contains('expanded'));
101+ const anyExpanded = foldable.some(
102+ li => li.classList.contains('expanded'));
96103 foldable.forEach(li => {
97104 if (anyExpanded) {
98105 li.classList.remove('expanded');
@@ -103,12 +110,16 @@ class MDBookSidebarScrollbox extends HTMLElement {
103110 updateCollapseAllButton();
104111 });
105112 updateCollapseAllButton();
106- // Hook all fold toggles (including header-nav ones added dynamically)
107- Array.from(scrollbox.querySelectorAll('.chapter-fold-toggle')).forEach(el => {
108- el.addEventListener('click', () => setTimeout(updateCollapseAllButton, 0));
113+ // Hook all fold toggles (including dynamically added ones)
114+ const allToggles =
115+ scrollbox.querySelectorAll('.chapter-fold-toggle');
116+ Array.from(allToggles).forEach(el => {
117+ el.addEventListener('click', () => {
118+ setTimeout(updateCollapseAllButton, 0);
119+ });
109120 });
110121 });
111- {{ /if }}
122+ {{ /if }}
112123 }
113124}
114125window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox);
0 commit comments