BlueGit

timfa 66wiki

Files

66wiki / web / inlineJump.js

inlineJump.js

inlineJump.js 1.9 KiB Download Raw
1/**
2 * Builds dropdown navigators for elements like:
3 * <div class="inlineNavigator" data-target-prefix="listitem-" data-scope="closest:section">...</div>
4 *
5 * - Finds child elements whose id starts with prefix
6 * - Uses their `title` attribute as label
7 * - Scrolls to them on selection
8 */
9(function initInlineNavigators ()
10{
11 function smoothScrollToId (id)
12 {
13 const el = document.getElementById(id);
14 if (!el) return;
15
16 // If you have a sticky header, this helps avoid hiding the target under it:
17 // Use CSS: scroll-margin-top on the target elements (recommended).
18 el.scrollIntoView({ behavior: "smooth", block: "start" });
19 history.replaceState(null, "", "#" + encodeURIComponent(id));
20 }
21
22 document.querySelectorAll(".inlineNavigator").forEach(nav =>
23 {
24 const prefix = nav.dataset.targetPrefix || "";
25 if (!prefix) return;
26
27 const scope = nav.parentElement;
28 const select = nav.querySelector(".inlineNavigatorSelect");
29 if (!select) return;
30
31 // Find elements with id starting with prefix, within scope
32 const candidates = scope.querySelectorAll(`[id^="${CSS.escape(prefix)}"]`);
33
34 // Clear old options (keep first "Select…" option if present)
35 select.querySelectorAll("option:not(:first-child)").forEach(o => o.remove());
36
37 // Add options
38 candidates.forEach(el =>
39 {
40 const id = el.id;
41 const label = (el.getAttribute("title") || "").trim() || id;
42
43 const opt = document.createElement("option");
44 opt.value = id;
45 opt.textContent = label;
46 select.appendChild(opt);
47 });
48
49 // If there are no targets, hide the whole widget
50 if (candidates.length === 0)
51 {
52 nav.style.display = "none";
53 return;
54 }
55
56 // Scroll on selection
57 select.addEventListener("change", () =>
58 {
59 if (!select.value) return;
60 smoothScrollToId(select.value);
61 select.value = ""; // reset
62 });
63 });
64})();