(function() {
'use strict';
var backToTop = document.querySelector('.back-to-top');
if (backToTop) {
window.addEventListener('scroll', function() {
if (window.scrollY > 300) {
backToTop.classList.add('visible');
} else {
backToTop.classList.remove('visible');
}
});
backToTop.addEventListener('click', function(e) {
e.preventDefault();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
var sidebarToggle = document.querySelector('.sidebar-toggle');
var book = document.querySelector('.book');
var bookSummary = document.querySelector('.book-summary');
function isMobile() {
return window.innerWidth <= 768;
}
var wasMobile = isMobile();
if (sidebarToggle && book && bookSummary) {
if (!isMobile()) {
var sidebarHidden = localStorage.getItem('guidebook-sidebar-hidden') === 'true';
if (sidebarHidden) {
book.classList.add('sidebar-hidden');
}
}
sidebarToggle.addEventListener('click', function() {
if (isMobile()) {
bookSummary.classList.toggle('open');
} else {
book.classList.add('sidebar-toggling');
book.classList.toggle('sidebar-hidden');
var isHidden = book.classList.contains('sidebar-hidden');
localStorage.setItem('guidebook-sidebar-hidden', isHidden);
setTimeout(function() {
book.classList.remove('sidebar-toggling');
}, 350);
}
});
document.addEventListener('click', function(e) {
if (isMobile() && bookSummary.classList.contains('open')) {
if (!bookSummary.contains(e.target) && !sidebarToggle.contains(e.target)) {
bookSummary.classList.remove('open');
}
}
});
window.addEventListener('resize', function() {
var nowMobile = isMobile();
if (wasMobile !== nowMobile) {
if (nowMobile) {
book.classList.remove('sidebar-hidden');
book.classList.remove('sidebar-toggling');
bookSummary.classList.remove('open');
} else {
bookSummary.classList.remove('open');
var sidebarHidden = localStorage.getItem('guidebook-sidebar-hidden') === 'true';
if (sidebarHidden) {
book.classList.add('sidebar-hidden');
} else {
book.classList.remove('sidebar-hidden');
}
}
wasMobile = nowMobile;
}
});
}
document.querySelectorAll('a[href*="#"]').forEach(function(anchor) {
anchor.addEventListener('click', function(e) {
var href = this.getAttribute('href');
var hashIndex = href.indexOf('#');
if (hashIndex === -1) return;
var hash = href.substring(hashIndex + 1);
try {
hash = decodeURIComponent(hash);
} catch (ex) {
}
var target = document.getElementById(hash);
if (target) {
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth' });
history.pushState(null, '', '#' + encodeURIComponent(hash));
}
});
});
var baseUrl = (function() {
var base = document.querySelector('base');
if (base && base.href) {
return base.href;
}
return window.location.href.replace(/[^/]*$/, '');
})();
var isNavigating = false;
function setupSpaNavigation() {
var sidebar = document.querySelector('.book-summary');
if (!sidebar) return;
sidebar.addEventListener('click', function(e) {
var link = e.target.closest('a');
if (!link) return;
var href = link.getAttribute('href');
if (!href || href.startsWith('#') || href.startsWith('http')) return;
e.preventDefault();
if (isNavigating) return;
loadPage(href, link);
});
}
function setupPageNavigation() {
document.querySelectorAll('.page-nav').forEach(function(nav) {
nav.addEventListener('click', function(e) {
e.preventDefault();
if (isNavigating) return;
var href = this.getAttribute('href');
if (!href) return;
loadPage(href, null);
});
});
}
function loadPage(url, clickedLink) {
if (isNavigating) return;
isNavigating = true;
document.body.classList.add('loading');
var absoluteUrl = new URL(url, baseUrl).href;
var hashIndex = url.indexOf('#');
var hash = hashIndex !== -1 ? url.substring(hashIndex + 1) : null;
fetch(absoluteUrl)
.then(function(response) {
if (!response.ok) throw new Error('Page not found');
return response.text();
})
.then(function(html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var newContent = doc.querySelector('.markdown-section');
var currentContent = document.querySelector('.markdown-section');
if (newContent && currentContent) {
currentContent.innerHTML = newContent.innerHTML;
}
var newTitle = doc.querySelector('title');
if (newTitle) {
document.title = newTitle.textContent;
}
document.querySelectorAll('.book-summary .chapter.active').forEach(function(ch) {
ch.classList.remove('active');
});
var newActiveHref = url.replace(/^\.\//, '').split('#')[0];
document.querySelectorAll('.book-summary .chapter a').forEach(function(link) {
var href = link.getAttribute('href');
if (href === newActiveHref || href === './' + newActiveHref) {
var chapter = link.closest('.chapter');
if (chapter) {
chapter.classList.add('active');
var parent = chapter.parentElement;
while (parent) {
if (parent.classList && parent.classList.contains('chapter')) {
parent.classList.add('expanded');
}
parent = parent.parentElement;
}
}
}
});
setTimeout(function() {
var activeItem = document.querySelector('.book-summary .chapter.active');
if (activeItem) {
activeItem.scrollIntoView({ block: 'center', behavior: 'auto' });
}
}, 50);
history.pushState(null, '', url);
if (hash) {
try {
var decodedHash = decodeURIComponent(hash);
var target = document.getElementById(decodedHash);
if (target) {
setTimeout(function() {
target.scrollIntoView({ behavior: 'auto' });
}, 50);
} else {
window.scrollTo(0, 0);
}
} catch (ex) {
window.scrollTo(0, 0);
}
} else {
window.scrollTo(0, 0);
}
if (typeof mermaid !== 'undefined') {
mermaid.init(undefined, '.markdown-section .mermaid');
}
var newPrev = doc.querySelector('.page-nav.prev');
var newNext = doc.querySelector('.page-nav.next');
var currentPrev = document.querySelector('.page-nav.prev');
var currentNext = document.querySelector('.page-nav.next');
if (currentPrev) currentPrev.remove();
if (currentNext) currentNext.remove();
var bodyInner = document.querySelector('.body-inner');
if (bodyInner) {
if (newPrev) {
var prevClone = newPrev.cloneNode(true);
bodyInner.insertBefore(prevClone, bodyInner.firstChild);
}
if (newNext) {
var nextClone = newNext.cloneNode(true);
bodyInner.insertBefore(nextClone, bodyInner.querySelector('.page-wrapper'));
}
setupPageNavigation();
}
isNavigating = false;
document.body.classList.remove('loading');
})
.catch(function(err) {
console.error('Navigation error:', err);
isNavigating = false;
document.body.classList.remove('loading');
window.location.href = url;
});
}
window.addEventListener('popstate', function() {
loadPage(location.pathname + location.hash, null);
});
setupSpaNavigation();
setupPageNavigation();
function scrollToHashOnLoad() {
if (!window.location.hash) return;
var hash = window.location.hash.substring(1);
try {
hash = decodeURIComponent(hash);
} catch (ex) {
}
var target = document.getElementById(hash);
if (target) {
setTimeout(function() {
target.scrollIntoView({ behavior: 'auto' });
}, 100);
}
}
if (document.readyState === 'complete') {
scrollToHashOnLoad();
} else {
window.addEventListener('load', scrollToHashOnLoad);
}
function scrollActiveIntoView() {
var activeItem = document.querySelector('.book-summary .chapter.active');
if (activeItem) {
var sidebar = document.querySelector('.book-summary');
if (sidebar) {
var itemRect = activeItem.getBoundingClientRect();
var sidebarRect = sidebar.getBoundingClientRect();
if (itemRect.top < sidebarRect.top || itemRect.bottom > sidebarRect.bottom) {
activeItem.scrollIntoView({ block: 'center', behavior: 'auto' });
}
}
}
}
setTimeout(scrollActiveIntoView, 50);
})();