BlueGit

timfa 66wiki

Files

66wiki / web / inlineJump.js

inlineJump.js

forgot script
timfa ruud · pushed by timfa · 11 months ago
cb495f7e History
inlineJump.js 2.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 function resolveScope(root, scopeSpec) {
11 // Default: search in the whole document
12 if (!scopeSpec) return document;
13
14 // scopeSpec examples:
15 // "document"
16 // "parent"
17 // "closest:section"
18 // "closest:.chapter"
19 // "query:#someContainer"
20 if (scopeSpec === "document") return document;
21 if (scopeSpec === "parent") return root.parentElement || document;
22
23 if (scopeSpec.startsWith("closest:")) {
24 const sel = scopeSpec.slice("closest:".length).trim();
25 return root.closest(sel) || document;
26 }
27
28 if (scopeSpec.startsWith("query:")) {
29 const sel = scopeSpec.slice("query:".length).trim();
30 return document.querySelector(sel) || document;
31 }
32
33 return document;
34 }
35
36 function smoothScrollToId(id) {
37 const el = document.getElementById(id);
38 if (!el) return;
39
40 // If you have a sticky header, this helps avoid hiding the target under it:
41 // Use CSS: scroll-margin-top on the target elements (recommended).
42 el.scrollIntoView({ behavior: "smooth", block: "start" });
43 history.replaceState(null, "", "#" + encodeURIComponent(id));
44 }
45
46 document.querySelectorAll(".inlineNavigator").forEach(nav => {
47 const prefix = nav.dataset.targetPrefix || "";
48 if (!prefix) return;
49
50 const scope = nav.parentElement;//resolveScope(nav, nav.dataset.scope);
51 const select = nav.querySelector(".inlineNavigatorSelect");
52 const button = nav.querySelector(".inlineNavigatorBtn");
53 if (!select) return;
54
55 // Find elements with id starting with prefix, within scope
56 const candidates = scope.querySelectorAll(`[id^="${CSS.escape(prefix)}"]`);
57
58 // Clear old options (keep first "Select…" option if present)
59 select.querySelectorAll("option:not(:first-child)").forEach(o => o.remove());
60
61 // Add options
62 candidates.forEach(el => {
63 const id = el.id;
64 const label = (el.getAttribute("title") || "").trim() || id;
65
66 const opt = document.createElement("option");
67 opt.value = id;
68 opt.textContent = label;
69 select.appendChild(opt);
70 });
71
72 // If there are no targets, hide the whole widget
73 if (candidates.length === 0) {
74 nav.style.display = "none";
75 return;
76 }
77
78 // Button toggles the select visibility (optional UX)
79 if (button) {
80 button.addEventListener("click", () => {
81 select.focus();
82 // some browsers will open the dropdown on focus, others won’t
83 });
84 }
85
86 // Scroll on selection
87 select.addEventListener("change", () => {
88 if (!select.value) return;
89 smoothScrollToId(select.value);
90 select.value = ""; // reset
91 });
92 });
93})();