Debian Codegraph @

src/docsrc/html/search.js

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6
7 var pages = new Array();
8 var ref = new Array();
9 var ignored = '';
10 var firstLink = null;
11 var firstLinkWord = null;
12
13 String.prototype.endsWith = function(suffix) {
14 var startPos = this.length - suffix.length;
15 if (startPos < 0) {
16 return false;
17 }
18 return this.lastIndexOf(suffix, startPos) == startPos;
19 };
20
21 function listWords(value, open) {
22 value = replaceOtherChars(value);
23 value = trim(value);
24 if (pages.length == 0) {
25 load();
26 }
27 var table = document.getElementById('result');
28 while (table.rows.length > 0) {
29 table.deleteRow(0);
30 }
31 firstLink = null;
32 var clear = document.getElementById('clear');
33 if (value.length == 0) {
34 clear.style.display = 'none';
35 return;
36 }
37 clear.style.display = '';
38 var keywords = value.split(' ');
39 if (keywords.length > 1) {
40 listMultipleWords(keywords);
41 return;
42 }
43 if (value.length < 3) {
44 max = 100;
45 } else {
46 max = 1000;
47 }
48 value = value.toLowerCase();
49 var r = ref[value.substring(0, 1)];
50 if (r == undefined) {
51 return;
52 }
53 var x = 0;
54 var words = r.split(';');
55 var count = 0;
56 for (var i = 0; i < words.length; i++) {
57 var wordRef = words[i];
58 if (wordRef.toLowerCase().indexOf(value) == 0) {
59 count++;
60 }
61 }
62 for (var i = 0; i < words.length && (x <= max); i++) {
63 var wordRef = words[i];
64 if (wordRef.toLowerCase().indexOf(value) == 0) {
65 word = wordRef.split("=")[0];
66 var tr = table.insertRow(x++);
67 var td = document.createElement('td');
68 var tdClass = document.createAttribute('class');
69 tdClass.nodeValue = 'searchKeyword';
70 td.setAttributeNode(tdClass);
71
72 var ah = document.createElement('a');
73 var href = document.createAttribute('href');
74 href.nodeValue = 'javascript:set("' + word + '");';
75 var link = document.createTextNode(word);
76 ah.setAttributeNode(href);
77 ah.appendChild(link);
78 td.appendChild(ah);
79 tr.appendChild(td);
80 piList = wordRef.split("=")[1].split(",");
81 if (count < 20 || open == word) {
82 x = addReferences(x, piList, word);
83 }
84 }
85 }
86 if (x == 0) {
87 if (ignored.indexOf(';' + value + ';') >= 0) {
88 noResults(table, 'Common word (not indexed)');
89 } else {
90 noResults(table, 'No results found!');
91 }
92 }
93 }
94
95 function set(v) {
96 if (pages.length == 0) {
97 load();
98 }
99 var search = document.getElementById('search').value;
100 listWords(search, v);
101 document.getElementById('search').focus();
102 window.scrollBy(-20, 0);
103 }
104
105 function goFirst() {
106 var table = document.getElementById('result');
107 if (firstLink != null) {
108 go(firstLink, firstLinkWord);
109 }
110 return false;
111 }
112
113 function go(pageId, word) {
114 var page = pages[pageId];
115 var load = '../' + page.file + '?highlight=' + encodeURIComponent(word);
116 if (top.main) {
117 if (!top.main.location.href.endsWith(page.file)) {
118 top.main.location = load;
119 }
120 } else {
121 if (!document.location.href.endsWith(page.file)) {
122 var search = document.getElementById('search').value;
123 document.location = load + '&search=' + encodeURIComponent(search);
124 }
125 }
126 }
127
128 function listMultipleWords(keywords) {
129 var count = new Array();
130 var weight = new Array();
131 for (var i = 0; i < pages.length; i++) {
132 count[i] = 0;
133 weight[i] = 0.0;
134 }
135 for (var i = 0; i < keywords.length; i++) {
136 var value = keywords[i].toLowerCase();
137 if (value.length <= 1) {
138 continue;
139 }
140 var r = ref[value.substring(0, 1)];
141 if (r == undefined) {
142 continue;
143 }
144 var words = r.split(';');
145 for (var j = 0; j < words.length; j++) {
146 var wordRef = words[j];
147 if (wordRef.toLowerCase().indexOf(value) == 0) {
148 var word = wordRef.split("=")[0].toLowerCase();
149 piList = wordRef.split("=")[1].split(",");
150 var w = 1;
151 for (var k = 0; k < piList.length; k++) {
152 var pi = piList[k];
153 if (pi.charAt(0) == 't') {
154 pi = pi.substring(1);
155 w = 10000;
156 } else if (pi.charAt(0) == 'h') {
157 pi = pi.substring(1);
158 w = 100;
159 } else if (pi.charAt(0) == 'r') {
160 pi = pi.substring(1);
161 w = 1;
162 }
163 if (w > 0) {
164 if (word != value) {
165 // if it's only the start of the word,
166 // reduce the weight
167 w /= 10.0;
168 }
169 // higher weight for longer words
170 w += w * word.length / 10.0;
171 count[pi]++;
172 weight[pi] += w;
173 }
174 }
175 }
176 }
177 }
178 var x = 0;
179 var table = document.getElementById('result');
180 var piList = new Array();
181 var piWeight = new Array();
182 for (var i = 0; i < pages.length; i++) {
183 var w = weight[i];
184 if (w > 0) {
185 piList[x] = '' + i;
186 piWeight[x] = w * count[i];
187 x++;
188 }
189 }
190 // sort
191 for (var i = 1, j; i < x; i++) {
192 var tw = piWeight[i];
193 var ti = piList[i];
194 for (j = i - 1; j >= 0 && (piWeight[j] < tw); j--) {
195 piWeight[j + 1] = piWeight[j];
196 piList[j + 1] = piList[j];
197 }
198 piWeight[j + 1] = tw;
199 piList[j + 1] = ti;
200 }
201 addReferences(0, piList, keywords);
202 if (piList.length == 0) {
203 noResults(table, 'No results found');
204 }
205 }
206
207 function addReferences(x, piList, word) {
208 var table = document.getElementById('result');
209 for (var j = 0; j < piList.length; j++) {
210 var pi = piList[j];
211 if (pi.charAt(0) == 't') {
212 pi = pi.substring(1);
213 } else if (pi.charAt(0) == 'h') {
214 pi = pi.substring(1);
215 } else if (pi.charAt(0) == 'r') {
216 pi = pi.substring(1);
217 }
218 var tr = table.insertRow(x++);
219 var td = document.createElement('td');
220 var tdClass = document.createAttribute('class');
221 tdClass.nodeValue = 'searchLink';
222 td.setAttributeNode(tdClass);
223 var ah = document.createElement('a');
224 var href = document.createAttribute('href');
225 var thisLink = 'javascript:go(' + pi + ', "' + word + '")';
226 if (firstLink == null) {
227 firstLink = pi;
228 firstLinkWord = word;
229 }
230 href.nodeValue = thisLink;
231 ah.setAttributeNode(href);
232 var page = pages[pi];
233 var link = document.createTextNode(page.title);
234 ah.appendChild(link);
235 td.appendChild(ah);
236 tr.appendChild(td);
237 }
238 return x;
239 }
240
241 function trim(s) {
242 while (s.charAt(0) == ' ' && s.length > 0) {
243 s = s.substring(1);
244 }
245 while (s.charAt(s.length - 1) == ' ' && s.length > 0) {
246 s = s.substring(0, s.length - 1);
247 }
248 return s;
249 }
250
251 function replaceOtherChars(s) {
252 var x = "";
253 for (var i = 0; i < s.length; i++) {
254 var c = s.charAt(i);
255 if ("\t\r\n\"'.,:;!&/\\?%@`[]{}()+-=<>|*^~#$".indexOf(c) >= 0) {
256 c = " ";
257 }
258 x += c;
259 }
260 return x;
261 }
262
263 function noResults(table, message) {
264 var tr = table.insertRow(0);
265 var td = document.createElement('td');
266 var tdClass = document.createAttribute('class');
267 tdClass.nodeValue = 'searchKeyword';
268 td.setAttributeNode(tdClass);
269 var text = document.createTextNode(message);
270 td.appendChild(text);
271 tr.appendChild(td);
272 }
273