diff --git a/OpenBench/static/ui.js b/OpenBench/static/ui.js new file mode 100644 index 0000000..99494d1 --- /dev/null +++ b/OpenBench/static/ui.js @@ -0,0 +1,136 @@ +/* ========================================================================== + OpenBench - small UI helpers (progressive enhancement only). + Nothing here changes what the site does: it highlights the current page in + the navigation, closes the mobile menu, shows a toast after copying text and + lets message banners be dismissed. + ========================================================================== */ + +(function () { + 'use strict'; + + function ready(fn) { + if (document.readyState !== 'loading') + fn(); + else + document.addEventListener('DOMContentLoaded', fn); + } + + function normalise(path) { + path = path.replace(/\/+$/, ''); + return path === '' ? '/' : path; + } + + /* ---------- Navigation: mark the link for the current page ---------- */ + + function highlightNavigation() { + + var here = normalise(window.location.pathname); + + // The front page is served from both / and /index + if (here === '/') + here = '/index'; + + var best = null; + var bestLength = -1; + + document.querySelectorAll('#sidebar a[href^="/"]').forEach(function (link) { + + var target = normalise(new URL(link.getAttribute('href'), window.location.origin).pathname); + var matches = here === target || here.indexOf(target + '/') === 0; + + if (matches && target.length > bestLength) { + best = link; + bestLength = target.length; + } + }); + + if (best) { + best.classList.add('active'); + best.setAttribute('aria-current', 'page'); + } + } + + /* ---------- Mobile navigation ---------- */ + + function setSidebar(open) { + document.body.classList.toggle('sidebar-open', open); + var toggle = document.getElementById('sidebar-toggle'); + if (toggle) + toggle.setAttribute('aria-expanded', String(open)); + } + + function initSidebar() { + + var backdrop = document.getElementById('sidebar-backdrop'); + if (backdrop) + backdrop.addEventListener('click', function () { setSidebar(false); }); + + document.addEventListener('keydown', function (event) { + if (event.key === 'Escape') + setSidebar(false); + }); + + document.querySelectorAll('#sidebar a').forEach(function (link) { + link.addEventListener('click', function () { setSidebar(false); }); + }); + } + + /* ---------- Toast ---------- */ + + var toastTimer = null; + + function toast(message) { + + var element = document.getElementById('toast'); + if (!element) + return; + + element.textContent = message; + element.classList.add('show'); + + window.clearTimeout(toastTimer); + toastTimer = window.setTimeout(function () { + element.classList.remove('show'); + }, 1800); + } + + // Give copy_text() (see copy_text.js) some visible feedback + function wrapCopyText() { + + if (typeof window.copy_text !== 'function' || window.copy_text.wrapped) + return; + + var original = window.copy_text; + + window.copy_text = function () { + var result = original.apply(this, arguments); + toast('Copied to clipboard'); + return result; + }; + + window.copy_text.wrapped = true; + } + + /* ---------- Dismissible banners ---------- */ + + function initBanners() { + document.addEventListener('click', function (event) { + + var button = event.target.closest('.banner-close'); + if (!button) + return; + + var banner = button.closest('.banner'); + if (banner) + banner.remove(); + }); + } + + ready(function () { + highlightNavigation(); + initSidebar(); + initBanners(); + wrapCopyText(); + }); + +})(); diff --git a/Templates/OpenBench/Blocks/pagebrowser.html b/Templates/OpenBench/Blocks/pagebrowser.html index fba0a39..da27f82 100644 --- a/Templates/OpenBench/Blocks/pagebrowser.html +++ b/Templates/OpenBench/Blocks/pagebrowser.html @@ -1,7 +1,7 @@ -