-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
378 lines (373 loc) · 12.9 KB
/
search.js
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
var noResult = {l: "No results found"};
var loading = {l: "Loading search index..."};
var catModules = "Modules";
var catPackages = "Packages";
var catTypes = "Types";
var catMembers = "Members";
var catSearchTags = "SearchTags";
var highlight = "<span class=\"result-highlight\">$&</span>";
var searchPattern = "";
var RANKING_THRESHOLD = 2;
var NO_MATCH = 0xffff;
var MAX_RESULTS_PER_CATEGORY = 500;
var UNNAMED = "<Unnamed>";
function escapeHtml(str) {
return str.replace(/</g, "<").replace(/>/g, ">");
}
function getHighlightedText(item, matcher) {
var escapedItem = escapeHtml(item);
return escapedItem.replace(matcher, highlight);
}
function getURLPrefix(ui) {
var urlPrefix="";
var slash = "/";
if (ui.item.category === catModules) {
return ui.item.l + slash;
} else if (ui.item.category === catPackages && ui.item.m) {
return ui.item.m + slash;
} else if (ui.item.category === catTypes || ui.item.category === catMembers) {
if (ui.item.m) {
urlPrefix = ui.item.m + slash;
} else {
$.each(packageSearchIndex, function(index, item) {
if (item.m && ui.item.p === item.l) {
urlPrefix = item.m + slash;
}
});
}
return urlPrefix;
}
return urlPrefix;
}
function makeCamelCaseRegex(term) {
var pattern = "";
var isWordToken = false;
term.replace(/,\s*/g, ", ").trim().split(/\s+/).forEach(function(w, index) {
if (index > 0) {
// whitespace between identifiers is significant
pattern += (isWordToken && /^\w/.test(w)) ? "\\s+" : "\\s*";
}
var tokens = w.split(/(?=[A-Z,.()<>[\/])/);
for (var i = 0; i < tokens.length; i++) {
var s = tokens[i];
if (s === "") {
continue;
}
pattern += $.ui.autocomplete.escapeRegex(s);
isWordToken = /\w$/.test(s);
if (isWordToken) {
pattern += "([a-z0-9_$<>\\[\\]]*?)";
}
}
});
return pattern;
}
function createMatcher(pattern, flags) {
var isCamelCase = /[A-Z]/.test(pattern);
return new RegExp(pattern, flags + (isCamelCase ? "" : "i"));
}
var watermark = 'Search';
$(function() {
$("#search").val('');
$("#search").prop("disabled", false);
$("#reset").prop("disabled", false);
$("#search").val(watermark).addClass('watermark');
$("#search").blur(function() {
if ($(this).val().length == 0) {
$(this).val(watermark).addClass('watermark');
}
});
$("#search").on('click keydown paste', function() {
if ($(this).val() == watermark) {
$(this).val('').removeClass('watermark');
}
});
$("#reset").click(function() {
$("#search").val('');
$("#search").focus();
});
$("#search").focus();
$("#search")[0].setSelectionRange(0, 0);
});
$.widget("custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
},
_renderMenu: function(ul, items) {
var rMenu = this;
var currentCategory = "";
rMenu.menu.bindings = $();
$.each(items, function(index, item) {
var li;
if (item.category && item.category !== currentCategory) {
ul.append("<li class=\"ui-autocomplete-category\">" + item.category + "</li>");
currentCategory = item.category;
}
li = rMenu._renderItemData(ul, item);
if (item.category) {
li.attr("aria-label", item.category + " : " + item.l);
li.attr("class", "result-item");
} else {
li.attr("aria-label", item.l);
li.attr("class", "result-item");
}
});
},
_renderItem: function(ul, item) {
var label = "";
var matcher = createMatcher(escapeHtml(searchPattern), "g");
if (item.category === catModules) {
label = getHighlightedText(item.l, matcher);
} else if (item.category === catPackages) {
label = getHighlightedText(item.l, matcher);
} else if (item.category === catTypes) {
label = (item.p && item.p !== UNNAMED)
? getHighlightedText(item.p + "." + item.l, matcher)
: getHighlightedText(item.l, matcher);
} else if (item.category === catMembers) {
label = (item.p && item.p !== UNNAMED)
? getHighlightedText(item.p + "." + item.c + "." + item.l, matcher)
: getHighlightedText(item.c + "." + item.l, matcher);
} else if (item.category === catSearchTags) {
label = getHighlightedText(item.l, matcher);
} else {
label = item.l;
}
var li = $("<li/>").appendTo(ul);
var div = $("<div/>").appendTo(li);
if (item.category === catSearchTags) {
if (item.d) {
div.html(label + "<span class=\"search-tag-holder-result\"> (" + item.h + ")</span><br><span class=\"search-tag-desc-result\">"
+ item.d + "</span><br>");
} else {
div.html(label + "<span class=\"search-tag-holder-result\"> (" + item.h + ")</span>");
}
} else {
if (item.m) {
div.html(item.m + "/" + label);
} else {
div.html(label);
}
}
return li;
}
});
function rankMatch(match, category) {
if (!match) {
return NO_MATCH;
}
var index = match.index;
var input = match.input;
var leftBoundaryMatch = 2;
var periferalMatch = 0;
var delta = 0;
// make sure match is anchored on a left word boundary
if (index === 0 || /\W/.test(input[index - 1]) || "_" === input[index - 1] || "_" === input[index]) {
leftBoundaryMatch = 0;
} else if (input[index] === input[index].toUpperCase() && !/^[A-Z0-9_$]+$/.test(input)) {
leftBoundaryMatch = 1;
}
var matchEnd = index + match[0].length;
var leftParen = input.indexOf("(");
// exclude peripheral matches
if (category !== catModules && category !== catSearchTags) {
var endOfName = leftParen > -1 ? leftParen : input.length;
var delim = category === catPackages ? "/" : ".";
if (leftParen > -1 && leftParen < index) {
periferalMatch += 2;
} else if (input.lastIndexOf(delim, endOfName) >= matchEnd) {
periferalMatch += 2;
}
}
for (var i = 1; i < match.length; i++) {
// lower ranking if parts of the name are missing
if (match[i])
delta += match[i].length;
}
if (category === catTypes) {
// lower ranking if a type name contains unmatched camel-case parts
if (/[A-Z]/.test(input.substring(matchEnd)))
delta += 5;
if (/[A-Z]/.test(input.substring(0, index)))
delta += 5;
}
return leftBoundaryMatch + periferalMatch + (delta / 200);
}
function doSearch(request, response) {
var result = [];
var newResults = [];
searchPattern = makeCamelCaseRegex(request.term);
if (searchPattern === "") {
return this.close();
}
var camelCaseMatcher = createMatcher(searchPattern, "");
var boundaryMatcher = createMatcher("\\b" + searchPattern, "");
function concatResults(a1, a2) {
a2.sort(function(e1, e2) {
return e1.ranking - e2.ranking;
});
a1 = a1.concat(a2.map(function(e) { return e.item; }));
a2.length = 0;
return a1;
}
if (moduleSearchIndex) {
$.each(moduleSearchIndex, function(index, item) {
item.category = catModules;
var ranking = rankMatch(boundaryMatcher.exec(item.l), catModules);
if (ranking < RANKING_THRESHOLD) {
newResults.push({ ranking: ranking, item: item });
}
return newResults.length < MAX_RESULTS_PER_CATEGORY;
});
result = concatResults(result, newResults);
}
if (packageSearchIndex) {
$.each(packageSearchIndex, function(index, item) {
item.category = catPackages;
var name = (item.m && request.term.indexOf("/") > -1)
? (item.m + "/" + item.l)
: item.l;
var ranking = rankMatch(boundaryMatcher.exec(name), catPackages);
if (ranking < RANKING_THRESHOLD) {
newResults.push({ ranking: ranking, item: item });
}
return newResults.length < MAX_RESULTS_PER_CATEGORY;
});
result = concatResults(result, newResults);
}
if (typeSearchIndex) {
$.each(typeSearchIndex, function(index, item) {
item.category = catTypes;
var name = request.term.indexOf(".") > -1
? item.p + "." + item.l
: item.l;
var ranking = rankMatch(camelCaseMatcher.exec(name), catTypes);
if (ranking < RANKING_THRESHOLD) {
newResults.push({ ranking: ranking, item: item });
}
return newResults.length < MAX_RESULTS_PER_CATEGORY;
});
result = concatResults(result, newResults);
}
if (memberSearchIndex) {
$.each(memberSearchIndex, function(index, item) {
item.category = catMembers;
var name = request.term.indexOf(".") > -1
? item.p + "." + item.c + "." + item.l
: item.l;
var ranking = rankMatch(camelCaseMatcher.exec(name), catMembers);
if (ranking < RANKING_THRESHOLD) {
newResults.push({ ranking: ranking, item: item });
}
return newResults.length < MAX_RESULTS_PER_CATEGORY;
});
result = concatResults(result, newResults);
}
if (tagSearchIndex) {
$.each(tagSearchIndex, function(index, item) {
item.category = catSearchTags;
var ranking = rankMatch(boundaryMatcher.exec(item.l), catSearchTags);
if (ranking < RANKING_THRESHOLD) {
newResults.push({ ranking: ranking, item: item });
}
return newResults.length < MAX_RESULTS_PER_CATEGORY;
});
result = concatResults(result, newResults);
}
if (!indexFilesLoaded()) {
updateSearchResults = function() {
doSearch(request, response);
}
result.unshift(loading);
} else {
updateSearchResults = function() {};
}
response(result);
}
$(function() {
$("#search").catcomplete({
minLength: 1,
delay: 300,
source: doSearch,
response: function(event, ui) {
if (!ui.content.length) {
ui.content.push(noResult);
} else {
$("#search").empty();
}
},
autoFocus: true,
focus: function(event, ui) {
return false;
},
position: {
collision: "flip"
},
select: function(event, ui) {
if (ui.item.category) {
var url = getURLPrefix(ui);
if (ui.item.category === catModules) {
url += "module-summary.html";
} else if (ui.item.category === catPackages) {
if (ui.item.u) {
url = ui.item.u;
} else {
url += ui.item.l.replace(/\./g, '/') + "/package-summary.html";
}
} else if (ui.item.category === catTypes) {
if (ui.item.u) {
url = ui.item.u;
} else if (ui.item.p === UNNAMED) {
url += ui.item.l + ".html";
} else {
url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
}
} else if (ui.item.category === catMembers) {
if (ui.item.p === UNNAMED) {
url += ui.item.c + ".html" + "#";
} else {
url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
}
if (ui.item.u) {
url += ui.item.u;
} else {
url += ui.item.l;
}
} else if (ui.item.category === catSearchTags) {
url += ui.item.u;
}
if (top !== window) {
parent.classFrame.location = pathtoroot + url;
} else {
window.location.href = pathtoroot + url;
}
$("#search").focus();
}
}
});
});