/**
* Builds dropdown navigators for elements like:
*
...
*
* - Finds child elements whose id starts with prefix
* - Uses their `title` attribute as label
* - Scrolls to them on selection
*/
(function initInlineNavigators(){
function resolveScope(root, scopeSpec) {
// Default: search in the whole document
if (!scopeSpec) return document;
// scopeSpec examples:
// "document"
// "parent"
// "closest:section"
// "closest:.chapter"
// "query:#someContainer"
if (scopeSpec === "document") return document;
if (scopeSpec === "parent") return root.parentElement || document;
if (scopeSpec.startsWith("closest:")) {
const sel = scopeSpec.slice("closest:".length).trim();
return root.closest(sel) || document;
}
if (scopeSpec.startsWith("query:")) {
const sel = scopeSpec.slice("query:".length).trim();
return document.querySelector(sel) || document;
}
return document;
}
function smoothScrollToId(id) {
const el = document.getElementById(id);
if (!el) return;
// If you have a sticky header, this helps avoid hiding the target under it:
// Use CSS: scroll-margin-top on the target elements (recommended).
el.scrollIntoView({ behavior: "smooth", block: "start" });
history.replaceState(null, "", "#" + encodeURIComponent(id));
}
document.querySelectorAll(".inlineNavigator").forEach(nav => {
const prefix = nav.dataset.targetPrefix || "";
if (!prefix) return;
const scope = nav.parentElement;//resolveScope(nav, nav.dataset.scope);
const select = nav.querySelector(".inlineNavigatorSelect");
const button = nav.querySelector(".inlineNavigatorBtn");
if (!select) return;
// Find elements with id starting with prefix, within scope
const candidates = scope.querySelectorAll(`[id^="${CSS.escape(prefix)}"]`);
// Clear old options (keep first "Select…" option if present)
select.querySelectorAll("option:not(:first-child)").forEach(o => o.remove());
// Add options
candidates.forEach(el => {
const id = el.id;
const label = (el.getAttribute("title") || "").trim() || id;
const opt = document.createElement("option");
opt.value = id;
opt.textContent = label;
select.appendChild(opt);
});
// If there are no targets, hide the whole widget
if (candidates.length === 0) {
nav.style.display = "none";
return;
}
// Button toggles the select visibility (optional UX)
if (button) {
button.addEventListener("click", () => {
select.focus();
// some browsers will open the dropdown on focus, others won’t
});
}
// Scroll on selection
select.addEventListener("change", () => {
if (!select.value) return;
smoothScrollToId(select.value);
select.value = ""; // reset
});
});
})();