-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-search.dart
347 lines (334 loc) · 8.97 KB
/
client-search.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var terms;
var path;
var timeouthandle;
searchWidget() {
terms = _terms();
if (terms != null) {
addSearchWidget();
}
}
buildsearchnav() {
if (timeouthandle != null) {
window.clearTimeout(timeouthandle);
}
var checked = document.query('input#qc');
var search = document.query('input#q');
var content = document.query('.content');
var listing = make_listing(search, checked);
var directory = document.query('.nav');
var dirlist = dir_list(listing, search, checked);
directory.nodes.clear();
directory.nodes.add(dirlist);
content_filter(listing, search, checked, content);
}
waitfornavbuilt() {
var dir = document.query('.nav');
if (!dir.classes.contains('built')) {
timeouthandle = window.setTimeout(waitfornavbuilt, 20);
} else {
buildsearchnav();
}
}
addSearchWidget() {
path = document.query('body').attributes['data-path'];
print('Search is enabled');
var nav = document.query('.header');
var content = document.query('.content');
var directory = document.query('.nav');
var query = widget();
nav.nodes.add(query);
var submit = document.query('input#qb');
var checked = document.query('input#qc');
var search = document.query('input#q');
if (document.cookie != '') {
var c = document.cookie.split(';');
for (var cv in c) {
cv = cv.trim();
var ca = cv.split('=');
if (ca[0] == 'dartdocs-search') {
search.value = ca[1];
}
if (ca[0] == 'dartdocs-exact') {
checked.checked = (ca[1] == 'true' ? true : false);
}
}
} else {
var qs = window.location.search;
if (qs != '' && qs[0] == '?') {
qs = qs.substring(1);
var qa = qs.split('&');
for (var qi in qa) {
var qis = qi.split('=');
if (qis[0] == 'q') {
search.value = qis[1];
}
if (qis[0] == 'e') {
if (qis[1] == 'true') {
checked.checked = true;
} else {
checked.checked = false;
};
}
}
}
}
var qs = window.location.search;
if (qs != '') {
waitfornavbuilt();
}
submit.on.click.add((e){
document.cookie = 'dartdocs-search=${search.value}; path=/; ';
document.cookie = 'dartdocs-exact=${checked.checked}; path=/; ';
if (search.value.trim() != '') {
var q = '?q=${search.value}&e=${checked.checked}';
window.location.href = '$path/index.html$q';
}
});
search.on.keyUp.add((e) {
if (e.keyCode == 13) {
submit.click();
}
});
checked.on.click.add((e) {
if (search.value != '') {
submit.click();
}
});
}
widget() {
return new Element.html(
'''
<div style="float:right; display:inline;padding-right:22px;">
<input id="q"> exact <input type="checkbox" id="qc">
<input type="button" id="qb" value="Search">
<style>.hidden {display: none;}
.matching li {
font: 600 13px/22px 'Open Sans', 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
list-style-type: none;
white-space: nowrap;
}
</style></div>
'''
);
}
tuple(l) {
var out = [];
var n = l.length;
for (var i = 0; i < n; i = i + 2) {
out.add([l[i], l[i+1]]);
}
return out;
}
triple(l) {
var out = [];
var n = l.length;
for (var i = 0; i < n; i = i + 3) {
out.add([l[i], l[i+1], l[i+2]]);
}
return out;
}
make_listing(search, checked) {
search = search.value.toLowerCase();
var k = terms['terms'].getKeys();
var out = [];
add_terms(item) {
item[0] = terms['filelist'][item[0]];
item[1] = terms['id'][item[1]];
out.add(item);
}
if (checked.checked == true) {
if (terms['terms'].containsKey(search)) {
tuple(terms['terms'][search]).forEach(add_terms);
}
} else {
get_terms(key) {
if (new RegExp(search).hasMatch(key)) {
tuple(terms['terms'][key]).forEach(add_terms);
}
}
k.forEach(get_terms);
}
return out;
}
dir_list(listing, search, checked) {
var nav = triple(terms['nav']);
var l = {};
for (var li in listing) {
l[li[0]] = '';
}
var out = '';
var startul = false;
var endul = false;
for (var li in nav) {
var li0 = terms['filelist'][li[0]];
var li1 = terms['typ'][li[1]];
var startuls = '';
var enduls = '';
var contained = l.containsKey(li0);
var isLib = (li1 == 'library');
var q = '';
if (search.value != '') {
q = '?q=${search.value}&e=${checked.checked}';
}
if ( isLib || contained) {
if (isLib && contained) {
if (endul == true) {
enduls = '</ul>';
}
out += '$enduls<h2><div class="icon-library"></div><a href="$path/${li0}$q">${li[2]}</a></h2>';
startul = true;
endul = false;
}
if (isLib && !contained) {
if (endul == true) {
enduls = '</ul>';
}
out += '$enduls<h2><div class="icon-library"></div>${li[2]}</h2>';
startul = true;
endul = false;
}
if (!isLib && contained) {
if (startul == true) {
startuls = '<ul>';
}
out += '$startuls<li><div class="icon-${li1}"></div><a href="$path/${li0}$q">${li[2]}</a></li>';
startul = false;
endul = true;
}
}
}
if (endul == true) {
out += '</ul>';
}
return new Element.html('<div>$out</div>');
}
content_filter(listing, search, checked, c) {
var h2 = c.query('h2');
var query = search.value.trim();
if (checked.checked == false) {
query = '*$query*';
}
var h2t = h2.innerHTML;
h2.innerHTML = 'Results for <strong>${_escape(query)}</strong> in $h2t';
var mh4 = c.queryAll('.method h4');
var fh4 = c.queryAll('.field h4');
var th4 = c.queryAll('.type h4');
var href = window.location.href;
var match = [];
for (var l in listing) {
if (href.indexOf(l[0]) > -1) {
match.add(l[1]);
}
}
for (var m in mh4) {
if (match.indexOf(m.attributes['id']) == -1 ) {
m.parent.classes.add('hidden');
}
}
for (var f in fh4) {
if (match.indexOf(f.attributes['id']) == -1 ) {
f.parent.classes.add('hidden');
}
}
for (var t in th4) {
t.parent.classes.add('hidden');
}
if (window.location.href.indexOf('/index.html') > -1) {
var h3 = c.queryAll('h3');
var h4 = c.queryAll('h4');
for (var i in h3) {
i.classes.add('hidden');
}
for (var i in h4) {
i.classes.add('hidden');
}
c.nodes.add(dir_stats(listing));
c.nodes.add(types_list(search, checked));
}
var h3s = c.queryAll('h3');
for (var h3 in h3s) {
var p = h3.parent;
var h = p.queryAll('.hidden');
var m = p.queryAll('.method');
var f = p.queryAll('.field');
var t = p.queryAll('.type');
if (h.length == (m.length + f.length + t.length)) {
p.classes.add('hidden');
}
}
return '';
}
/** Escape HTML special chars. */
String _escape(String str) {
str = str.replaceAll('&','&');
str = str.replaceAll('<','<');
str = str.replaceAll('>','>');
str = str.replaceAll('"','"');
str = str.replaceAll("'",''');
str = str.replaceAll('/','/');
return str;
}
dir_stats(listing) {
var stats = {'lib': 0, 'type': 0, 'member': listing.length};
var nav = triple(terms['nav']);
var l = {};
var llib = {};
for (var li in listing) {
l[li[0]] = '';
llib[li[0].split('/')[0].replaceAll('.html', '')] = '';
}
stats['lib'] = llib.getKeys().length;
var out = new Element.tag('div');
for (var li in nav) {
var li0 = terms['filelist'][li[0]];
var li1 = terms['typ'][li[1]];
var contained = l.containsKey(li0);
var isLib = (li1 == 'library');
if ( isLib || contained) {
if (!isLib && contained) {
stats['type'] += 1;
}
}
}
var members = (stats['member'] == 1) ? 'member' : 'members';
var libraries = (stats['lib'] == 1) ? 'library' : 'libraries';
var types = (stats['type'] == 1) ? 'type' : 'types';
out.nodes.add(new Element.html('<h4>Found ${stats['member']} $members in ' +
'${stats['lib']} $libraries and ${stats['type']} $types.</h4>'));
return out;
}
types_list(search, checked) {
var l = [];
var s = search.value.toLowerCase().trim();
var nav = triple(terms['nav']);
if (checked.checked == true) {
for (var i in nav) {
if (i[2].toLowerCase() == s) {
var i0 = terms['filelist'][i[0]];
var i1 = terms['typ'][i[1]];
l.add([i0, i1, i[2]]);
}
}
} else {
for (var i in nav) {
if (i[2].toLowerCase().indexOf(s) > -1) {
var i0 = terms['filelist'][i[0]];
var i1 = terms['typ'][i[1]];
l.add([i0, i1, i[2]]);
}
}
}
var out = new Element.tag('div');
out.classes.add('matching');
if (l.length > 0) {
out.nodes.add(new Element.tag('p'));
out.nodes.add(new Element.html('<h4>Search term also matching these library/type names:</h4>'));
var list = new Element.tag('ul');
for (var i in l) {
var e = new Element.html('<li><div class="icon-${i[1]}"></div><a href="$path/${i[0]}">${i[2]}</a></li>');
list.nodes.add(e);
}
out.nodes.add(list);
}
return out;
}