(function() {
'use strict';
var STORAGE_KEY = 'guidebook-expanded-chapters';
var sidebar = document.querySelector('.book-summary');
if (!sidebar) return;
function getExpandedState() {
try {
var stored = localStorage.getItem(STORAGE_KEY);
return stored ? JSON.parse(stored) : {};
} catch (e) {
return {};
}
}
function saveExpandedState(state) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
} catch (e) {}
}
function getChapterId(chapter) {
var link = chapter.querySelector(':scope > a');
if (link) {
return link.getAttribute('href');
}
var title = chapter.querySelector(':scope > .chapter-title');
if (title) {
return 'title:' + title.textContent.trim();
}
return null;
}
function restoreExpandedState() {
var state = getExpandedState();
var chapters = sidebar.querySelectorAll('.chapter.expandable');
chapters.forEach(function(chapter) {
var id = getChapterId(chapter);
if (id && state[id] !== undefined) {
if (state[id]) {
chapter.classList.add('expanded');
} else {
if (!chapter.classList.contains('active') &&
!chapter.querySelector('.chapter.active')) {
chapter.classList.remove('expanded');
}
}
}
});
}
function saveCurrentState() {
var state = {};
var chapters = sidebar.querySelectorAll('.chapter.expandable');
chapters.forEach(function(chapter) {
var id = getChapterId(chapter);
if (id) {
state[id] = chapter.classList.contains('expanded');
}
});
saveExpandedState(state);
}
restoreExpandedState();
sidebar.addEventListener('click', function(e) {
var link = e.target.closest('a');
var titleSpan = e.target.closest('.chapter-title');
var directChapter = (link || titleSpan)?.closest('.chapter');
if (!directChapter) return;
if (!directChapter.classList.contains('expandable')) return;
var articles = directChapter.querySelector(':scope > .articles');
if (!articles) return;
if (titleSpan && !link) {
e.preventDefault();
e.stopImmediatePropagation();
directChapter.classList.toggle('expanded');
saveCurrentState();
return;
}
if (link) {
e.preventDefault();
e.stopImmediatePropagation();
directChapter.classList.toggle('expanded');
saveCurrentState();
}
});
})();