/** * WP Multi Wiki – TOC Script * Highlights active TOC entry while scrolling + toggle functionality. */ (function () { 'use strict'; document.addEventListener('DOMContentLoaded', function () { var toc = document.getElementById('wmw-toc'); if (!toc) return; // ── Toggle ──────────────────────────────────────────────────────────── var toggleBtn = toc.querySelector('.wmw-toc-toggle'); var tocList = document.getElementById('wmw-toc-list'); if (toggleBtn && tocList) { toggleBtn.addEventListener('click', function () { var expanded = toggleBtn.getAttribute('aria-expanded') === 'true'; toggleBtn.setAttribute('aria-expanded', String(!expanded)); tocList.style.display = expanded ? 'none' : ''; var icon = toggleBtn.querySelector('.wmw-toc-toggle-icon'); if (icon) icon.innerHTML = expanded ? '▶' : '▼'; }); } // ── Active Highlight on Scroll ──────────────────────────────────────── var links = toc.querySelectorAll('a[href^="#"]'); if (!links.length) return; var headings = []; links.forEach(function (link) { var id = link.getAttribute('href').substring(1); var el = document.getElementById(id); if (el) headings.push({ el: el, link: link }); }); function setActive() { var scrollY = window.scrollY || window.pageYOffset; var offset = 80; // sticky header offset var activeIdx = -1; headings.forEach(function (h, i) { if (h.el.getBoundingClientRect().top + scrollY - offset <= scrollY) { activeIdx = i; } }); links.forEach(function (l) { l.classList.remove('wmw-toc-active'); }); if (activeIdx >= 0) { headings[activeIdx].link.classList.add('wmw-toc-active'); } } var ticking = false; window.addEventListener('scroll', function () { if (!ticking) { requestAnimationFrame(function () { setActive(); ticking = false; }); ticking = true; } }, { passive: true }); setActive(); // run once on load // ── Sidebar: mark current article ──────────────────────────────────── var currentUrl = window.location.href.replace(/\/$/, ''); document.querySelectorAll('.wmw-sidebar .wmw-article-list a').forEach(function (a) { if (a.href.replace(/\/$/, '') === currentUrl) { a.classList.add('wmw-current'); // Scroll link into view inside sidebar var sidebar = document.getElementById('wmw-sidebar'); if (sidebar) { var linkTop = a.offsetTop; sidebar.scrollTop = linkTop - 80; } } }); }); })();