forked from php/web-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
405 lines (349 loc) · 15.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* A jQuery plugin to add typeahead search functionality to the navbar search
* box. This requires Hogan for templating and typeahead.js for the actual
* typeahead functionality.
*/
(function ($) {
/**
* A backend, which encapsulates a set of completions, such as a list of
* functions or classes.
*
* @constructor
* @param {String} label The label to show the user.
*/
var Backend = function (label) {
this.label = label;
this.elements = {};
};
/**
* Adds an item to the backend.
*
* @param {String} id The item ID. It would help if this was unique.
* @param {String} name The item name to use as a label.
* @param {Array} tokens An array of tokens that should match this item.
*/
Backend.prototype.addItem = function (id, name, description, tokens) {
this.elements[id] = {
tokens: tokens,
id: id,
name: name,
description: description
};
};
/**
* Returns the backend contents formatted as an array that typeahead.js can
* digest as a local data source.
*
* @return {Array}
*/
Backend.prototype.toTypeaheadArray = function () {
var array = [];
$.each(this.elements, function (_, element) {
array.push(element);
});
/* This is a rather convoluted sorting function, but the idea is to
* make the results as useful as possible, since only a few are shown
* at any one time. In general, we favour shorter names over longer
* ones, and favour regular functions over methods when sorting
* functions. Ideally, this would actually sort based on function
* popularity, but this is a simpler algorithmic approach for now that
* seems to result in generally useful results. */
array.sort(function (a, b) {
var a = a.name;
var b = b.name;
var aIsMethod = (a.indexOf("::") != -1);
var bIsMethod = (b.indexOf("::") != -1);
// Methods are always after regular functions.
if (aIsMethod && !bIsMethod) {
return 1;
} else if (bIsMethod && !aIsMethod) {
return -1;
}
/* If one function name is the exact prefix of the other, we want
* to sort the shorter version first (mostly for things like date()
* versus date_format()). */
if (a.length > b.length) {
if (a.indexOf(b) == 0) {
return 1;
}
} else {
if (b.indexOf(a) == 0) {
return -1;
}
}
// Otherwise, sort normally.
if (a > b) {
return 1;
} else if (a < b) {
return -1;
}
return 0;
});
return array;
};
/**
* The actual search plugin. Should be applied to the input that needs
* typeahead functionality.
*
* @param {Object} options The options object. This should include
* "language": the language to try to load,
* "limit": the maximum number of results
*/
$.fn.search = function (options) {
var element = this;
options.language = options.language || "en";
options.limit = options.limit || 30;
/**
* Utility function to check if the user's browser supports local
* storage and native JSON, in which case we'll use it to cache the
* search JSON.
*
* @return {Boolean}
*/
var canCache = function () {
try {
return ('localStorage' in window && window['localStorage'] !== null && "JSON" in window && window["JSON"] !== null);
} catch (e) {
return false;
}
};
/**
* Processes a data structure in the format of our search-index.php
* files and returns an object containing multiple Backend objects.
*
* @param {Object} index
* @return {Object}
*/
var processIndex = function (index) {
// The search types we want to support.
var backends = {
"function": new Backend("Functions"),
"variable": new Backend("Variables"),
"class": new Backend("Classes"),
"exception": new Backend("Exceptions"),
"extension": new Backend("Extensions"),
"general": new Backend("Other Matches")
};
$.each(index, function (id, item) {
/* If the item has a name, then we should figure out what type
* of data this is, and hence which backend this should go
* into. */
if (item[0]) {
var tokens = [item[0]];
var type = null;
if (item[0].indexOf("_") != -1) {
tokens.push(item[0].replace("_", ""));
}
if (item[0].indexOf("::") != -1) {
/* We'll add tokens to make the autocompletion more
* useful: users can search for method names and can
* specify that they only want method names by
* prefixing their search with ::. */
tokens.push(item[0].split("::")[1]);
tokens.push("::" + item[0].split("::")[1]);
}
switch(item[2]) {
case "phpdoc:varentry":
type = "variable";
break;
case "refentry":
type = "function";
break;
case "phpdoc:exceptionref":
type = "exception";
break;
case "phpdoc:classref":
type = "class";
break;
case "set":
case "book":
case "reference":
type = "extension";
break;
case "section":
case "chapter":
case "appendix":
case "article":
default:
type = "general";
}
if (type) {
backends[type].addItem(id, item[0], item[1], tokens);
}
}
});
return backends;
};
/**
* Attempt to asynchronously load the search JSON for a given language.
*
* @param {String} language The language to search for.
* @param {Function} success Success handler, which will be given an
* object containing multiple Backend
* objects on success.
* @param {Function} failure An optional failure handler.
*/
var loadLanguage = function (language, success, failure) {
var key = "search-" + language;
// Check if the cache has a recent enough search index.
if (canCache()) {
var cache = window.localStorage.getItem(key);
if (cache) {
var since = new Date();
// Parse the stored JSON.
cache = JSON.parse(cache);
// We'll use anything that's less than two weeks old.
since.setDate(since.getDate() - 14);
if (cache.time > since.getTime()) {
success($.map(cache.data, function (dataset, name) {
// Rehydrate the Backend objects.
var backend = new Backend(dataset.label);
backend.elements = dataset.elements;
return backend;
}));
return;
}
}
}
// OK, nothing cached.
$.ajax({
dataType: "json",
error: failure,
success: function (data) {
// Transform the data into something useful.
var backends = processIndex(data);
// Cache the data if we can.
if (canCache()) {
/* This may fail in IE 8 due to exceeding the local
* storage limit. If so, squash the exception: this
* isn't a required part of the system. */
try {
window.localStorage.setItem(key,
JSON.stringify({
data: backends,
time: new Date().getTime()
})
);
} catch (e) {
// Derp.
}
}
success(backends);
},
url: "/js/search-index.php?lang=" + language
});
};
/**
* Actually enables the typeahead on the DOM element.
*
* @param {Object} backends An array-like object containing backends.
*/
var enableSearchTypeahead = function (backends) {
var template = "<h4>{{ name }}</h4>" +
"<span title='{{ description }}' class='description'>{{ description }}</span>";
// Build the typeahead options array.
var typeaheadOptions = $.map(backends, function (backend, name) {
var local = backend instanceof Backend ? backend.toTypeaheadArray() : backend;
return {
name: name,
local: backend.toTypeaheadArray(),
header: '<h3 class="result-heading"><span class="collapsible"></span>' + backend.label + '</h3>',
limit: options.limit,
valueKey: "name",
engine: Hogan,
template: template
};
});
/* Construct a global that we can use to track the total number of
* results from each backend. */
var results = {};
// Set up the typeahead and the various listeners we need.
var searchTypeahead = $(element).typeahead(typeaheadOptions);
// Delegate click events to result-heading collapsible icons, and trigger the accordion action
$('.tt-dropdown-menu').delegate('.result-heading .collapsible', 'click', function() {
var el = $(this), suggestions = el.parent().parent().find('.tt-suggestions');
suggestions.stop();
if(!el.hasClass('closed')) {
suggestions.slideUp();
el.addClass('closed');
} else {
suggestions.slideDown();
el.removeClass('closed');
}
});
// If the user has selected an autocomplete item and hits enter, we should take them straight to the page.
searchTypeahead.on("typeahead:selected", function (_, item) {
window.location = "/manual/" + options.language + "/" + item.id;
});
searchTypeahead.on("keyup", (function () {
/* typeahead.js doesn't give us a reliable event for the
* dropdown entries having been updated, so we'll hook into the
* input element's keyup instead. The aim here is to put in
* fake entries so that the user has a discoverable way to
* perform different searches based on what he or she has
* entered. */
// Precompile the templates we need for the fake entries.
var moreTemplate = Hogan.compile("<a class='more' href='{{ url }}'>» {{ num }} more result{{ plural }}</a>");
var searchTemplate = Hogan.compile("<a class='search' href='{{ url }}'>» Search php.net for {{ pattern }}</a>");
/* Now we'll return the actual function that should be invoked
* when the user has typed something into the search box after
* typeahead.js has done its thing. */
return function () {
// Add result totals to each section heading.
$.each(results, function (name, numResults) {
var container = $(".tt-dataset-" + name, $(element).parent()),
resultHeading = container.find('.result-heading'),
resultCount = container.find('.result-count');
// Does a result count already exist in this resultHeading?
if(resultCount.length == 0) {
var results = $("<span class='result-count'>").text(numResults);
resultHeading.append(results);
} else {
resultCount.text(numResults);
}
});
// Grab what the user entered.
var pattern = $(element).val();
/* Add a global search option. Note that, as above, the
* link is only displayed if more than 2 characters have
* been entered: this is due to our search functionality
* requiring at least 3 characters in the pattern. */
$(".tt-dropdown-menu .search", $(element).parent()).remove();
if (pattern.length > 2) {
var dropdown = $(".tt-dropdown-menu", $(element).parent());
dropdown.append(searchTemplate.render({
pattern: pattern,
url: "/search.php?pattern=" + encodeURIComponent(pattern)
}));
/* If the dropdown is hidden (because there are no
* results), show it anyway. */
dropdown.show();
}
};
})());
/* Override the dataset._getLocalSuggestions() method to grab the
* number of results each dataset returns when a search occurs. */
$.each($(element).data().ttView.datasets, function (_, dataset) {
var originalGetLocal = dataset._getLocalSuggestions;
dataset._getLocalSuggestions = function () {
var suggestions = originalGetLocal.apply(dataset, arguments);
results[dataset.name] = suggestions.length;
return suggestions;
};
});
/* typeahead.js adds another input element as part of its DOM
* manipulation, which breaks the auto-submit functionality we
* previously relied upon for enter keypresses in the input box to
* work. Adding a hidden submit button re-enables it. */
$("<input type='submit' style='visibility: hidden; position: fixed'>").insertAfter(element);
// Fix for a styling issue on the created input element.
$(".tt-hint", $(element).parent()).addClass("search-query");
};
// Look for the user's language, then fall back to English.
loadLanguage(options.language, enableSearchTypeahead, function () {
loadLanguage("en", enableSearchTypeahead);
});
return this;
};
})(jQuery);
// vim: set ts=4 sw=4 et: