|
7 | 7 | document.querySelectorAll("span.brackets").forEach(el => {
|
8 | 8 | if (!el.children.length) el.innerText = "[" + el.innerText + "]"
|
9 | 9 | })
|
10 |
| - |
11 |
| -const ready = (callback) => { |
12 |
| - if (document.readyState !== "loading") callback(); |
13 |
| - else document.addEventListener("DOMContentLoaded", callback); |
14 |
| -} |
15 |
| - |
16 |
| -const removeElements = (elms) => {for (let el of elms) el.remove()} |
17 |
| - |
18 |
| -const highlight = (text, className, node, addItems) => { |
19 |
| - if (node.nodeType === 3) { // Text node |
20 |
| - const val = node.nodeValue; |
21 |
| - const parent = node.parentNode |
22 |
| - const pos = val.toLowerCase().indexOf(text); |
23 |
| - if (pos >= 0 |
24 |
| - && !parent.classList.contains(className) |
25 |
| - && !parent.classList.contains("nohighlight") |
26 |
| - ) { |
27 |
| - let span |
28 |
| - const closestNode = node.parentNode.closest("body, svg, foreignObject"); |
29 |
| - const isInSVG = closestNode && closestNode.matches("svg") |
30 |
| - if (isInSVG) { |
31 |
| - span = document.createElementNS("http://www.w3.org/2000/svg", "tspan"); |
32 |
| - } else { |
33 |
| - span = document.createElement("span"); |
34 |
| - span.classList.add(className); |
35 |
| - } |
36 |
| - |
37 |
| - span.appendChild(document.createTextNode(val.substr(pos, text.length))); |
38 |
| - parent.insertBefore(span, parent.insertBefore( |
39 |
| - document.createTextNode(val.substr(pos + text.length)), |
40 |
| - node.nextSibling |
41 |
| - )) |
42 |
| - node.nodeValue = val.substr(0, pos); |
43 |
| - if (isInSVG) { |
44 |
| - const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); |
45 |
| - const bbox = parent.getBBox(); |
46 |
| - rect.x.baseVal.value = bbox.x; |
47 |
| - rect.y.baseVal.value = bbox.y; |
48 |
| - rect.width.baseVal.value = bbox.width; |
49 |
| - rect.height.baseVal.value = bbox.height; |
50 |
| - rect.setAttribute('class', className); |
51 |
| - addItems.push({ |
52 |
| - "parent": node.parentNode, |
53 |
| - "target": rect}); |
54 |
| - } |
55 |
| - } |
56 |
| - } |
57 |
| - else if (node.matches("button, select, textarea")) { |
58 |
| - node.childNodes.forEach(el => highlight(el, addItems)); |
59 |
| - } |
60 |
| - else if (node.nodeType === 1) |
61 |
| - { |
62 |
| - node.childNodes.forEach(el => highlight(el, addItems)); |
63 |
| - } |
64 |
| -} |
65 |
| - |
66 |
| -// highlight a given string on a node by wrapping it in |
67 |
| -// span elements with the given class name. |
68 |
| -const highlightText = (text, className, curNode) => { |
69 |
| - let addItems = []; |
70 |
| - highlight(text, className, curNode, addItems) |
71 |
| - for (let i = 0; i < addItems.length; ++i) addItems[i].parent.insertAdjacentHTML("beforebegin", addItems[i].target) |
72 |
| - return curNode |
73 |
| -} |
74 |
| - |
75 |
| -// Small JavaScript module for the documentation. |
76 |
| -const Documentation = { |
77 |
| - init: () => this.highlightSearchWords(), |
78 |
| - |
79 |
| - gettext: string => string, |
80 |
| - |
81 |
| - // highlight the search words provided in the url in the text |
82 |
| - highlightSearchWords: () => { |
83 |
| - const urlParams = new URLSearchParams(window.location.search); |
84 |
| - const terms = urlParams.get("highlight") ? urlParams.get("highlight").split(/\s+/) : []; |
85 |
| - if (terms.length) { |
86 |
| - window.setTimeout(() => { |
87 |
| - terms.forEach(term => highlightText(term.toLowerCase(), 'highlighted', document.querySelector("body"))) |
88 |
| - }, 10); |
89 |
| - const hideMatchesLink = document.createElement("a") |
90 |
| - hideMatchesLink.href = "javascript:Documentation.hideSearchWords()" |
91 |
| - hideMatchesLink.innerText = 'Hide Search Matches' |
92 |
| - hideMatchesLink.style.fontStyle = "italic" |
93 |
| - const hideMatches = document.createElement("p") |
94 |
| - hideMatches.classList.add("highlight-link") |
95 |
| - hideMatches.appendChild(hideMatchesLink) |
96 |
| - document.getElementById("searchbox").appendChild(hideMatches) |
97 |
| - } |
98 |
| - }, |
99 |
| - |
100 |
| - // helper function to hide the search marks again |
101 |
| - hideSearchWords: () => { |
102 |
| - removeElements(document.querySelectorAll("#searchbox .highlight-link")) |
103 |
| - document.querySelectorAll("span.highlighted").forEach(el => el.classList.remove("highlighted")) |
104 |
| - }, |
105 |
| -} |
106 |
| - |
107 |
| -// quick alias for translations |
108 |
| -_ = Documentation.gettext |
109 |
| - |
110 |
| -ready(Documentation.init) |
0 commit comments