]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | /*jslint browser: true, es5: true */ | |
12 | /*globals $: true, rootPath: true */ | |
13 | ||
14 | (function() { | |
15 | "use strict"; | |
16 | var resizeTimeout, interval; | |
17 | ||
c34b1796 AL |
18 | // This mapping table should match the discriminants of |
19 | // `rustdoc::html::item_type::ItemType` type in Rust. | |
20 | var itemTypes = ["mod", | |
21 | "externcrate", | |
22 | "import", | |
23 | "struct", | |
24 | "enum", | |
25 | "fn", | |
26 | "type", | |
27 | "static", | |
28 | "trait", | |
29 | "impl", | |
30 | "tymethod", | |
31 | "method", | |
32 | "structfield", | |
33 | "variant", | |
34 | "macro", | |
35 | "primitive", | |
36 | "associatedtype", | |
d9579d0f AL |
37 | "constant", |
38 | "associatedconstant"]; | |
c34b1796 | 39 | |
1a4d82fc JJ |
40 | $('.js-only').removeClass('js-only'); |
41 | ||
42 | function getQueryStringParams() { | |
43 | var params = {}; | |
44 | window.location.search.substring(1).split("&"). | |
45 | map(function(s) { | |
46 | var pair = s.split("="); | |
47 | params[decodeURIComponent(pair[0])] = | |
48 | typeof pair[1] === "undefined" ? | |
49 | null : decodeURIComponent(pair[1]); | |
50 | }); | |
51 | return params; | |
52 | } | |
53 | ||
54 | function browserSupportsHistoryApi() { | |
55 | return window.history && typeof window.history.pushState === "function"; | |
56 | } | |
57 | ||
1a4d82fc JJ |
58 | function highlightSourceLines(ev) { |
59 | var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/); | |
60 | if (match) { | |
61 | from = parseInt(match[1], 10); | |
62 | to = Math.min(50000, parseInt(match[2] || match[1], 10)); | |
63 | from = Math.min(from, to); | |
64 | if ($('#' + from).length === 0) { | |
65 | return; | |
66 | } | |
67 | if (ev === null) $('#' + from)[0].scrollIntoView(); | |
68 | $('.line-numbers span').removeClass('line-highlighted'); | |
69 | for (i = from; i <= to; ++i) { | |
70 | $('#' + i).addClass('line-highlighted'); | |
71 | } | |
72 | } | |
73 | } | |
74 | highlightSourceLines(null); | |
75 | $(window).on('hashchange', highlightSourceLines); | |
76 | ||
77 | $(document).on('keyup', function(e) { | |
78 | if (document.activeElement.tagName === 'INPUT') { | |
79 | return; | |
80 | } | |
81 | ||
9346a6ac AL |
82 | if (e.which === 191) { // question mark |
83 | if (e.shiftKey && $('#help').hasClass('hidden')) { | |
84 | e.preventDefault(); | |
85 | $('#help').removeClass('hidden'); | |
86 | } | |
1a4d82fc JJ |
87 | } else if (e.which === 27) { // esc |
88 | if (!$('#help').hasClass('hidden')) { | |
89 | e.preventDefault(); | |
90 | $('#help').addClass('hidden'); | |
91 | } else if (!$('#search').hasClass('hidden')) { | |
92 | e.preventDefault(); | |
93 | $('#search').addClass('hidden'); | |
94 | $('#main').removeClass('hidden'); | |
95 | } | |
96 | } else if (e.which === 83) { // S | |
97 | e.preventDefault(); | |
98 | $('.search-input').focus(); | |
99 | } | |
100 | }).on('click', function(e) { | |
101 | if (!$(e.target).closest('#help').length) { | |
102 | $('#help').addClass('hidden'); | |
103 | } | |
104 | }); | |
105 | ||
106 | $('.version-selector').on('change', function() { | |
107 | var i, match, | |
108 | url = document.location.href, | |
109 | stripped = '', | |
110 | len = rootPath.match(/\.\.\//g).length + 1; | |
111 | ||
112 | for (i = 0; i < len; ++i) { | |
113 | match = url.match(/\/[^\/]*$/); | |
114 | if (i < len - 1) { | |
115 | stripped = match[0] + stripped; | |
116 | } | |
117 | url = url.substring(0, url.length - match[0].length); | |
118 | } | |
119 | ||
120 | url += '/' + $('.version-selector').val() + stripped; | |
121 | ||
122 | document.location.href = url; | |
123 | }); | |
124 | /** | |
125 | * A function to compute the Levenshtein distance between two strings | |
126 | * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported | |
127 | * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode | |
128 | * This code is an unmodified version of the code written by Marco de Wit | |
129 | * and was found at http://stackoverflow.com/a/18514751/745719 | |
130 | */ | |
131 | var levenshtein = (function() { | |
132 | var row2 = []; | |
133 | return function(s1, s2) { | |
134 | if (s1 === s2) { | |
135 | return 0; | |
136 | } else { | |
137 | var s1_len = s1.length, s2_len = s2.length; | |
138 | if (s1_len && s2_len) { | |
139 | var i1 = 0, i2 = 0, a, b, c, c2, row = row2; | |
140 | while (i1 < s1_len) | |
141 | row[i1] = ++i1; | |
142 | while (i2 < s2_len) { | |
143 | c2 = s2.charCodeAt(i2); | |
144 | a = i2; | |
145 | ++i2; | |
146 | b = i2; | |
147 | for (i1 = 0; i1 < s1_len; ++i1) { | |
148 | c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0); | |
149 | a = row[i1]; | |
150 | b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c); | |
151 | row[i1] = b; | |
152 | } | |
153 | } | |
154 | return b; | |
155 | } else { | |
156 | return s1_len + s2_len; | |
157 | } | |
158 | } | |
159 | }; | |
160 | })(); | |
161 | ||
162 | function initSearch(rawSearchIndex) { | |
163 | var currentResults, index, searchIndex; | |
164 | var MAX_LEV_DISTANCE = 3; | |
165 | var params = getQueryStringParams(); | |
166 | ||
167 | // Populate search bar with query string search term when provided, | |
168 | // but only if the input bar is empty. This avoid the obnoxious issue | |
169 | // where you start trying to do a search, and the index loads, and | |
170 | // suddenly your search is gone! | |
171 | if ($(".search-input")[0].value === "") { | |
172 | $(".search-input")[0].value = params.search || ''; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Executes the query and builds an index of results | |
177 | * @param {[Object]} query [The user query] | |
178 | * @param {[type]} max [The maximum results returned] | |
179 | * @param {[type]} searchWords [The list of search words to query | |
180 | * against] | |
181 | * @return {[type]} [A search index of results] | |
182 | */ | |
183 | function execQuery(query, max, searchWords) { | |
184 | var valLower = query.query.toLowerCase(), | |
185 | val = valLower, | |
186 | typeFilter = itemTypeFromName(query.type), | |
187 | results = [], | |
188 | split = valLower.split("::"); | |
189 | ||
190 | //remove empty keywords | |
191 | for (var j = 0; j < split.length; ++j) { | |
192 | split[j].toLowerCase(); | |
193 | if (split[j] === "") { | |
194 | split.splice(j, 1); | |
195 | } | |
196 | } | |
197 | ||
198 | // quoted values mean literal search | |
199 | var nSearchWords = searchWords.length; | |
200 | if ((val.charAt(0) === "\"" || val.charAt(0) === "'") && | |
201 | val.charAt(val.length - 1) === val.charAt(0)) | |
202 | { | |
203 | val = val.substr(1, val.length - 2); | |
204 | for (var i = 0; i < nSearchWords; ++i) { | |
205 | if (searchWords[i] === val) { | |
206 | // filter type: ... queries | |
207 | if (typeFilter < 0 || typeFilter === searchIndex[i].ty) { | |
208 | results.push({id: i, index: -1}); | |
209 | } | |
210 | } | |
211 | if (results.length === max) { | |
212 | break; | |
213 | } | |
214 | } | |
c34b1796 AL |
215 | // searching by type |
216 | } else if (val.search("->") > -1) { | |
217 | var trimmer = function (s) { return s.trim(); }; | |
218 | var parts = val.split("->").map(trimmer); | |
219 | var input = parts[0]; | |
220 | // sort inputs so that order does not matter | |
221 | var inputs = input.split(",").map(trimmer).sort(); | |
222 | var output = parts[1]; | |
223 | ||
224 | for (var i = 0; i < nSearchWords; ++i) { | |
225 | var type = searchIndex[i].type; | |
226 | if (!type) { | |
227 | continue; | |
228 | } | |
229 | ||
230 | // sort index inputs so that order does not matter | |
231 | var typeInputs = type.inputs.map(function (input) { | |
232 | return input.name; | |
233 | }).sort(); | |
234 | ||
235 | // allow searching for void (no output) functions as well | |
236 | var typeOutput = type.output ? type.output.name : ""; | |
237 | if (inputs.toString() === typeInputs.toString() && | |
238 | output == typeOutput) { | |
239 | results.push({id: i, index: -1, dontValidate: true}); | |
240 | } | |
241 | } | |
1a4d82fc JJ |
242 | } else { |
243 | // gather matching search results up to a certain maximum | |
244 | val = val.replace(/\_/g, ""); | |
245 | for (var i = 0; i < split.length; ++i) { | |
246 | for (var j = 0; j < nSearchWords; ++j) { | |
247 | var lev_distance; | |
248 | if (searchWords[j].indexOf(split[i]) > -1 || | |
249 | searchWords[j].indexOf(val) > -1 || | |
250 | searchWords[j].replace(/_/g, "").indexOf(val) > -1) | |
251 | { | |
252 | // filter type: ... queries | |
253 | if (typeFilter < 0 || typeFilter === searchIndex[j].ty) { | |
254 | results.push({ | |
255 | id: j, | |
256 | index: searchWords[j].replace(/_/g, "").indexOf(val), | |
257 | lev: 0, | |
258 | }); | |
259 | } | |
260 | } else if ( | |
261 | (lev_distance = levenshtein(searchWords[j], val)) <= | |
262 | MAX_LEV_DISTANCE) { | |
263 | if (typeFilter < 0 || typeFilter === searchIndex[j].ty) { | |
264 | results.push({ | |
265 | id: j, | |
266 | index: 0, | |
267 | // we want lev results to go lower than others | |
268 | lev: lev_distance, | |
269 | }); | |
270 | } | |
271 | } | |
272 | if (results.length === max) { | |
273 | break; | |
274 | } | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | var nresults = results.length; | |
280 | for (var i = 0; i < nresults; ++i) { | |
281 | results[i].word = searchWords[results[i].id]; | |
282 | results[i].item = searchIndex[results[i].id] || {}; | |
283 | } | |
284 | // if there are no results then return to default and fail | |
285 | if (results.length === 0) { | |
286 | return []; | |
287 | } | |
288 | ||
289 | results.sort(function(aaa, bbb) { | |
290 | var a, b; | |
291 | ||
292 | // Sort by non levenshtein results and then levenshtein results by the distance | |
293 | // (less changes required to match means higher rankings) | |
294 | a = (aaa.lev); | |
295 | b = (bbb.lev); | |
296 | if (a !== b) return a - b; | |
297 | ||
298 | // sort by crate (non-current crate goes later) | |
299 | a = (aaa.item.crate !== window.currentCrate); | |
300 | b = (bbb.item.crate !== window.currentCrate); | |
301 | if (a !== b) return a - b; | |
302 | ||
303 | // sort by exact match (mismatch goes later) | |
304 | a = (aaa.word !== valLower); | |
305 | b = (bbb.word !== valLower); | |
306 | if (a !== b) return a - b; | |
307 | ||
308 | // sort by item name length (longer goes later) | |
309 | a = aaa.word.length; | |
310 | b = bbb.word.length; | |
311 | if (a !== b) return a - b; | |
312 | ||
313 | // sort by item name (lexicographically larger goes later) | |
314 | a = aaa.word; | |
315 | b = bbb.word; | |
316 | if (a !== b) return (a > b ? +1 : -1); | |
317 | ||
318 | // sort by index of keyword in item name (no literal occurrence goes later) | |
319 | a = (aaa.index < 0); | |
320 | b = (bbb.index < 0); | |
321 | if (a !== b) return a - b; | |
322 | // (later literal occurrence, if any, goes later) | |
323 | a = aaa.index; | |
324 | b = bbb.index; | |
325 | if (a !== b) return a - b; | |
326 | ||
327 | // sort by description (no description goes later) | |
328 | a = (aaa.item.desc === ''); | |
329 | b = (bbb.item.desc === ''); | |
330 | if (a !== b) return a - b; | |
331 | ||
332 | // sort by type (later occurrence in `itemTypes` goes later) | |
333 | a = aaa.item.ty; | |
334 | b = bbb.item.ty; | |
335 | if (a !== b) return a - b; | |
336 | ||
337 | // sort by path (lexicographically larger goes later) | |
338 | a = aaa.item.path; | |
339 | b = bbb.item.path; | |
340 | if (a !== b) return (a > b ? +1 : -1); | |
341 | ||
342 | // que sera, sera | |
343 | return 0; | |
344 | }); | |
345 | ||
346 | // remove duplicates, according to the data provided | |
347 | for (var i = results.length - 1; i > 0; i -= 1) { | |
348 | if (results[i].word === results[i - 1].word && | |
349 | results[i].item.ty === results[i - 1].item.ty && | |
350 | results[i].item.path === results[i - 1].item.path && | |
351 | (results[i].item.parent || {}).name === (results[i - 1].item.parent || {}).name) | |
352 | { | |
353 | results[i].id = -1; | |
354 | } | |
355 | } | |
356 | for (var i = 0; i < results.length; ++i) { | |
357 | var result = results[i], | |
358 | name = result.item.name.toLowerCase(), | |
359 | path = result.item.path.toLowerCase(), | |
360 | parent = result.item.parent; | |
361 | ||
c34b1796 AL |
362 | // this validation does not make sense when searching by types |
363 | if (result.dontValidate) { | |
364 | continue; | |
365 | } | |
366 | ||
1a4d82fc JJ |
367 | var valid = validateResult(name, path, split, parent); |
368 | if (!valid) { | |
369 | result.id = -1; | |
370 | } | |
371 | } | |
372 | return results; | |
373 | } | |
374 | ||
375 | /** | |
376 | * Validate performs the following boolean logic. For example: | |
377 | * "File::open" will give IF A PARENT EXISTS => ("file" && "open") | |
378 | * exists in (name || path || parent) OR => ("file" && "open") exists in | |
379 | * (name || path ) | |
380 | * | |
381 | * This could be written functionally, but I wanted to minimise | |
382 | * functions on stack. | |
383 | * | |
384 | * @param {[string]} name [The name of the result] | |
385 | * @param {[string]} path [The path of the result] | |
386 | * @param {[string]} keys [The keys to be used (["file", "open"])] | |
387 | * @param {[object]} parent [The parent of the result] | |
388 | * @return {[boolean]} [Whether the result is valid or not] | |
389 | */ | |
390 | function validateResult(name, path, keys, parent) { | |
391 | for (var i=0; i < keys.length; ++i) { | |
392 | // each check is for validation so we negate the conditions and invalidate | |
393 | if (!( | |
394 | // check for an exact name match | |
395 | name.toLowerCase().indexOf(keys[i]) > -1 || | |
396 | // then an exact path match | |
397 | path.toLowerCase().indexOf(keys[i]) > -1 || | |
398 | // next if there is a parent, check for exact parent match | |
399 | (parent !== undefined && | |
400 | parent.name.toLowerCase().indexOf(keys[i]) > -1) || | |
401 | // lastly check to see if the name was a levenshtein match | |
402 | levenshtein(name.toLowerCase(), keys[i]) <= | |
403 | MAX_LEV_DISTANCE)) { | |
404 | return false; | |
405 | } | |
406 | } | |
407 | return true; | |
408 | } | |
409 | ||
410 | function getQuery() { | |
411 | var matches, type, query, raw = $('.search-input').val(); | |
412 | query = raw; | |
413 | ||
414 | matches = query.match(/^(fn|mod|struct|enum|trait|t(ype)?d(ef)?)\s*:\s*/i); | |
415 | if (matches) { | |
416 | type = matches[1].replace(/^td$/, 'typedef') | |
417 | .replace(/^tdef$/, 'typedef') | |
418 | .replace(/^typed$/, 'typedef'); | |
419 | query = query.substring(matches[0].length); | |
420 | } | |
421 | ||
422 | return { | |
423 | raw: raw, | |
424 | query: query, | |
425 | type: type, | |
426 | id: query + type, | |
427 | }; | |
428 | } | |
429 | ||
430 | function initSearchNav() { | |
431 | var hoverTimeout, $results = $('.search-results .result'); | |
432 | ||
433 | $results.on('click', function() { | |
434 | var dst = $(this).find('a')[0]; | |
435 | if (window.location.pathname == dst.pathname) { | |
436 | $('#search').addClass('hidden'); | |
437 | $('#main').removeClass('hidden'); | |
438 | document.location.href = dst.href; | |
439 | } | |
440 | }).on('mouseover', function() { | |
441 | var $el = $(this); | |
442 | clearTimeout(hoverTimeout); | |
443 | hoverTimeout = setTimeout(function() { | |
444 | $results.removeClass('highlighted'); | |
445 | $el.addClass('highlighted'); | |
446 | }, 20); | |
447 | }); | |
448 | ||
449 | $(document).off('keydown.searchnav'); | |
450 | $(document).on('keydown.searchnav', function(e) { | |
451 | var $active = $results.filter('.highlighted'); | |
452 | ||
453 | if (e.which === 38) { // up | |
454 | e.preventDefault(); | |
455 | if (!$active.length || !$active.prev()) { | |
456 | return; | |
457 | } | |
458 | ||
459 | $active.prev().addClass('highlighted'); | |
460 | $active.removeClass('highlighted'); | |
461 | } else if (e.which === 40) { // down | |
462 | e.preventDefault(); | |
463 | if (!$active.length) { | |
464 | $results.first().addClass('highlighted'); | |
465 | } else if ($active.next().length) { | |
466 | $active.next().addClass('highlighted'); | |
467 | $active.removeClass('highlighted'); | |
468 | } | |
469 | } else if (e.which === 13) { // return | |
470 | e.preventDefault(); | |
471 | if ($active.length) { | |
472 | document.location.href = $active.find('a').prop('href'); | |
473 | } | |
9346a6ac AL |
474 | } else { |
475 | $active.removeClass('highlighted'); | |
1a4d82fc JJ |
476 | } |
477 | }); | |
478 | } | |
479 | ||
480 | function escape(content) { | |
481 | return $('<h1/>').text(content).html(); | |
482 | } | |
483 | ||
484 | function showResults(results) { | |
485 | var output, shown, query = getQuery(); | |
486 | ||
487 | currentResults = query.id; | |
488 | output = '<h1>Results for ' + escape(query.query) + | |
489 | (query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>'; | |
490 | output += '<table class="search-results">'; | |
491 | ||
492 | if (results.length > 0) { | |
493 | shown = []; | |
494 | ||
495 | results.forEach(function(item) { | |
496 | var name, type, href, displayPath; | |
497 | ||
498 | if (shown.indexOf(item) !== -1) { | |
499 | return; | |
500 | } | |
501 | ||
502 | shown.push(item); | |
503 | name = item.name; | |
504 | type = itemTypes[item.ty]; | |
505 | ||
506 | if (type === 'mod') { | |
507 | displayPath = item.path + '::'; | |
508 | href = rootPath + item.path.replace(/::/g, '/') + '/' + | |
509 | name + '/index.html'; | |
510 | } else if (type === 'static' || type === 'reexport') { | |
511 | displayPath = item.path + '::'; | |
512 | href = rootPath + item.path.replace(/::/g, '/') + | |
513 | '/index.html'; | |
514 | } else if (item.parent !== undefined) { | |
515 | var myparent = item.parent; | |
516 | var anchor = '#' + type + '.' + name; | |
517 | displayPath = item.path + '::' + myparent.name + '::'; | |
518 | href = rootPath + item.path.replace(/::/g, '/') + | |
519 | '/' + itemTypes[myparent.ty] + | |
520 | '.' + myparent.name + | |
521 | '.html' + anchor; | |
522 | } else { | |
523 | displayPath = item.path + '::'; | |
524 | href = rootPath + item.path.replace(/::/g, '/') + | |
525 | '/' + type + '.' + name + '.html'; | |
526 | } | |
527 | ||
528 | output += '<tr class="' + type + ' result"><td>' + | |
529 | '<a href="' + href + '">' + | |
530 | displayPath + '<span class="' + type + '">' + | |
531 | name + '</span></a></td><td>' + | |
532 | '<a href="' + href + '">' + | |
533 | '<span class="desc">' + item.desc + | |
534 | ' </span></a></td></tr>'; | |
535 | }); | |
536 | } else { | |
537 | output += 'No results :( <a href="https://duckduckgo.com/?q=' + | |
538 | encodeURIComponent('rust ' + query.query) + | |
539 | '">Try on DuckDuckGo?</a>'; | |
540 | } | |
541 | ||
542 | output += "</p>"; | |
543 | $('#main.content').addClass('hidden'); | |
544 | $('#search.content').removeClass('hidden').html(output); | |
545 | $('#search .desc').width($('#search').width() - 40 - | |
546 | $('#search td:first-child').first().width()); | |
547 | initSearchNav(); | |
548 | } | |
549 | ||
550 | function search(e) { | |
551 | var query, | |
552 | filterdata = [], | |
553 | obj, i, len, | |
554 | results = [], | |
555 | maxResults = 200, | |
556 | resultIndex; | |
557 | var params = getQueryStringParams(); | |
558 | ||
559 | query = getQuery(); | |
560 | if (e) { | |
561 | e.preventDefault(); | |
562 | } | |
563 | ||
564 | if (!query.query || query.id === currentResults) { | |
565 | return; | |
566 | } | |
567 | ||
568 | // Because searching is incremental by character, only the most | |
569 | // recent search query is added to the browser history. | |
570 | if (browserSupportsHistoryApi()) { | |
571 | if (!history.state && !params.search) { | |
572 | history.pushState(query, "", "?search=" + | |
573 | encodeURIComponent(query.raw)); | |
574 | } else { | |
575 | history.replaceState(query, "", "?search=" + | |
576 | encodeURIComponent(query.raw)); | |
577 | } | |
578 | } | |
579 | ||
580 | resultIndex = execQuery(query, 20000, index); | |
581 | len = resultIndex.length; | |
582 | for (i = 0; i < len; ++i) { | |
583 | if (resultIndex[i].id > -1) { | |
584 | obj = searchIndex[resultIndex[i].id]; | |
585 | filterdata.push([obj.name, obj.ty, obj.path, obj.desc]); | |
586 | results.push(obj); | |
587 | } | |
588 | if (results.length >= maxResults) { | |
589 | break; | |
590 | } | |
591 | } | |
592 | ||
593 | showResults(results); | |
594 | } | |
595 | ||
1a4d82fc JJ |
596 | function itemTypeFromName(typename) { |
597 | for (var i = 0; i < itemTypes.length; ++i) { | |
598 | if (itemTypes[i] === typename) return i; | |
599 | } | |
600 | return -1; | |
601 | } | |
602 | ||
603 | function buildIndex(rawSearchIndex) { | |
604 | searchIndex = []; | |
605 | var searchWords = []; | |
606 | for (var crate in rawSearchIndex) { | |
607 | if (!rawSearchIndex.hasOwnProperty(crate)) { continue } | |
608 | ||
609 | // an array of [(Number) item type, | |
610 | // (String) name, | |
611 | // (String) full path or empty string for previous path, | |
612 | // (String) description, | |
c34b1796 AL |
613 | // (Number | null) the parent path index to `paths`] |
614 | // (Object | null) the type of the function (if any) | |
1a4d82fc JJ |
615 | var items = rawSearchIndex[crate].items; |
616 | // an array of [(Number) item type, | |
617 | // (String) name] | |
618 | var paths = rawSearchIndex[crate].paths; | |
619 | ||
620 | // convert `paths` into an object form | |
621 | var len = paths.length; | |
622 | for (var i = 0; i < len; ++i) { | |
623 | paths[i] = {ty: paths[i][0], name: paths[i][1]}; | |
624 | } | |
625 | ||
626 | // convert `items` into an object form, and construct word indices. | |
627 | // | |
628 | // before any analysis is performed lets gather the search terms to | |
629 | // search against apart from the rest of the data. This is a quick | |
630 | // operation that is cached for the life of the page state so that | |
631 | // all other search operations have access to this cached data for | |
632 | // faster analysis operations | |
633 | var len = items.length; | |
634 | var lastPath = ""; | |
635 | for (var i = 0; i < len; ++i) { | |
636 | var rawRow = items[i]; | |
637 | var row = {crate: crate, ty: rawRow[0], name: rawRow[1], | |
638 | path: rawRow[2] || lastPath, desc: rawRow[3], | |
c34b1796 | 639 | parent: paths[rawRow[4]], type: rawRow[5]}; |
1a4d82fc JJ |
640 | searchIndex.push(row); |
641 | if (typeof row.name === "string") { | |
642 | var word = row.name.toLowerCase(); | |
643 | searchWords.push(word); | |
644 | } else { | |
645 | searchWords.push(""); | |
646 | } | |
647 | lastPath = row.path; | |
648 | } | |
649 | } | |
650 | return searchWords; | |
651 | } | |
652 | ||
653 | function startSearch() { | |
654 | var keyUpTimeout; | |
655 | $('.do-search').on('click', search); | |
656 | $('.search-input').on('keyup', function() { | |
657 | clearTimeout(keyUpTimeout); | |
85aaf69f | 658 | keyUpTimeout = setTimeout(search, 500); |
1a4d82fc JJ |
659 | }); |
660 | ||
661 | // Push and pop states are used to add search results to the browser | |
662 | // history. | |
663 | if (browserSupportsHistoryApi()) { | |
664 | $(window).on('popstate', function(e) { | |
665 | var params = getQueryStringParams(); | |
666 | // When browsing back from search results the main page | |
667 | // visibility must be reset. | |
668 | if (!params.search) { | |
669 | $('#main.content').removeClass('hidden'); | |
670 | $('#search.content').addClass('hidden'); | |
671 | } | |
672 | // When browsing forward to search results the previous | |
673 | // search will be repeated, so the currentResults are | |
674 | // cleared to ensure the search is successful. | |
675 | currentResults = null; | |
676 | // Synchronize search bar with query string state and | |
677 | // perform the search. This will empty the bar if there's | |
678 | // nothing there, which lets you really go back to a | |
679 | // previous state with nothing in the bar. | |
680 | $('.search-input').val(params.search); | |
681 | // Some browsers fire 'onpopstate' for every page load | |
682 | // (Chrome), while others fire the event only when actually | |
683 | // popping a state (Firefox), which is why search() is | |
684 | // called both here and at the end of the startSearch() | |
685 | // function. | |
686 | search(); | |
687 | }); | |
688 | } | |
689 | search(); | |
690 | } | |
691 | ||
85aaf69f SL |
692 | function plainSummaryLine(markdown) { |
693 | var str = markdown.replace(/\n/g, ' ') | |
694 | str = str.replace(/'/g, "\'") | |
695 | str = str.replace(/^#+? (.+?)/, "$1") | |
696 | str = str.replace(/\[(.*?)\]\(.*?\)/g, "$1") | |
697 | str = str.replace(/\[(.*?)\]\[.*?\]/g, "$1") | |
698 | return str; | |
699 | } | |
700 | ||
1a4d82fc JJ |
701 | index = buildIndex(rawSearchIndex); |
702 | startSearch(); | |
703 | ||
704 | // Draw a convenient sidebar of known crates if we have a listing | |
705 | if (rootPath == '../') { | |
706 | var sidebar = $('.sidebar'); | |
707 | var div = $('<div>').attr('class', 'block crate'); | |
708 | div.append($('<h2>').text('Crates')); | |
709 | ||
710 | var crates = []; | |
711 | for (var crate in rawSearchIndex) { | |
712 | if (!rawSearchIndex.hasOwnProperty(crate)) { continue } | |
713 | crates.push(crate); | |
714 | } | |
715 | crates.sort(); | |
716 | for (var i = 0; i < crates.length; ++i) { | |
717 | var klass = 'crate'; | |
718 | if (crates[i] == window.currentCrate) { | |
719 | klass += ' current'; | |
720 | } | |
9346a6ac AL |
721 | if (rawSearchIndex[crates[i]].items[0]) { |
722 | var desc = rawSearchIndex[crates[i]].items[0][3]; | |
723 | div.append($('<a>', {'href': '../' + crates[i] + '/index.html', | |
724 | 'title': plainSummaryLine(desc), | |
725 | 'class': klass}).text(crates[i])); | |
726 | } | |
1a4d82fc JJ |
727 | } |
728 | sidebar.append(div); | |
729 | } | |
730 | } | |
731 | ||
732 | window.initSearch = initSearch; | |
733 | ||
c34b1796 AL |
734 | // delayed sidebar rendering. |
735 | function initSidebarItems(items) { | |
736 | var sidebar = $('.sidebar'); | |
737 | var current = window.sidebarCurrent; | |
738 | ||
739 | function block(shortty, longty) { | |
740 | var filtered = items[shortty]; | |
741 | if (!filtered) return; | |
742 | ||
743 | var div = $('<div>').attr('class', 'block ' + shortty); | |
744 | div.append($('<h2>').text(longty)); | |
745 | ||
746 | for (var i = 0; i < filtered.length; ++i) { | |
747 | var item = filtered[i]; | |
748 | var name = item[0]; | |
749 | var desc = item[1]; // can be null | |
750 | ||
751 | var klass = shortty; | |
752 | if (name === current.name && shortty == current.ty) { | |
753 | klass += ' current'; | |
754 | } | |
755 | var path; | |
756 | if (shortty === 'mod') { | |
757 | path = name + '/index.html'; | |
758 | } else { | |
759 | path = shortty + '.' + name + '.html'; | |
760 | } | |
761 | div.append($('<a>', {'href': current.relpath + path, | |
762 | 'title': desc, | |
763 | 'class': klass}).text(name)); | |
764 | } | |
765 | sidebar.append(div); | |
766 | } | |
767 | ||
768 | block("mod", "Modules"); | |
769 | block("struct", "Structs"); | |
770 | block("enum", "Enums"); | |
771 | block("trait", "Traits"); | |
772 | block("fn", "Functions"); | |
773 | block("macro", "Macros"); | |
774 | } | |
775 | ||
776 | window.initSidebarItems = initSidebarItems; | |
777 | ||
1a4d82fc JJ |
778 | window.register_implementors = function(imp) { |
779 | var list = $('#implementors-list'); | |
780 | var libs = Object.getOwnPropertyNames(imp); | |
781 | for (var i = 0; i < libs.length; ++i) { | |
782 | if (libs[i] == currentCrate) continue; | |
783 | var structs = imp[libs[i]]; | |
784 | for (var j = 0; j < structs.length; ++j) { | |
785 | var code = $('<code>').append(structs[j]); | |
786 | $.each(code.find('a'), function(idx, a) { | |
787 | var href = $(a).attr('href'); | |
788 | if (href && href.indexOf('http') !== 0) { | |
789 | $(a).attr('href', rootPath + href); | |
790 | } | |
791 | }); | |
792 | var li = $('<li>').append(code); | |
793 | list.append(li); | |
794 | } | |
795 | } | |
796 | }; | |
797 | if (window.pending_implementors) { | |
798 | window.register_implementors(window.pending_implementors); | |
799 | } | |
800 | ||
801 | // See documentation in html/render.rs for what this is doing. | |
802 | var query = getQueryStringParams(); | |
803 | if (query['gotosrc']) { | |
804 | window.location = $('#src-' + query['gotosrc']).attr('href'); | |
805 | } | |
d9579d0f AL |
806 | if (query['gotomacrosrc']) { |
807 | window.location = $('.srclink').attr('href'); | |
808 | } | |
1a4d82fc | 809 | |
d9579d0f AL |
810 | function labelForToggleButton(sectionIsCollapsed) { |
811 | if (sectionIsCollapsed) { | |
812 | // button will expand the section | |
813 | return "+"; | |
814 | } else { | |
815 | // button will collapse the section | |
816 | // note that this text is also set in the HTML template in render.rs | |
817 | return "\u2212"; // "\u2212" is '−' minus sign | |
818 | } | |
819 | } | |
1a4d82fc | 820 | |
d9579d0f AL |
821 | $("#toggle-all-docs").on("click", function() { |
822 | var toggle = $("#toggle-all-docs"); | |
823 | if (toggle.hasClass("will-expand")) { | |
824 | toggle.removeClass("will-expand"); | |
825 | toggle.children(".inner").text(labelForToggleButton(false)); | |
826 | toggle.attr("title", "collapse all docs"); | |
827 | $(".docblock").show(); | |
828 | $(".toggle-label").hide(); | |
829 | $(".toggle-wrapper").removeClass("collapsed"); | |
830 | $(".collapse-toggle").children(".inner").text(labelForToggleButton(false)); | |
831 | } else { | |
832 | toggle.addClass("will-expand"); | |
833 | toggle.children(".inner").text(labelForToggleButton(true)); | |
834 | toggle.attr("title", "expand all docs"); | |
835 | $(".docblock").hide(); | |
836 | $(".toggle-label").show(); | |
837 | $(".toggle-wrapper").addClass("collapsed"); | |
838 | $(".collapse-toggle").children(".inner").text(labelForToggleButton(true)); | |
839 | } | |
1a4d82fc JJ |
840 | }); |
841 | ||
842 | $(document).on("click", ".collapse-toggle", function() { | |
843 | var toggle = $(this); | |
844 | var relatedDoc = toggle.parent().next(); | |
d9579d0f AL |
845 | if (relatedDoc.is(".stability")) { |
846 | relatedDoc = relatedDoc.next(); | |
847 | } | |
1a4d82fc JJ |
848 | if (relatedDoc.is(".docblock")) { |
849 | if (relatedDoc.is(":visible")) { | |
850 | relatedDoc.slideUp({duration:'fast', easing:'linear'}); | |
851 | toggle.parent(".toggle-wrapper").addClass("collapsed"); | |
d9579d0f | 852 | toggle.children(".inner").text(labelForToggleButton(true)); |
1a4d82fc JJ |
853 | toggle.children(".toggle-label").fadeIn(); |
854 | } else { | |
855 | relatedDoc.slideDown({duration:'fast', easing:'linear'}); | |
856 | toggle.parent(".toggle-wrapper").removeClass("collapsed"); | |
d9579d0f | 857 | toggle.children(".inner").text(labelForToggleButton(false)); |
1a4d82fc JJ |
858 | toggle.children(".toggle-label").hide(); |
859 | } | |
860 | } | |
861 | }); | |
862 | ||
863 | $(function() { | |
864 | var toggle = $("<a/>", {'href': 'javascript:void(0)', 'class': 'collapse-toggle'}) | |
d9579d0f AL |
865 | .html("[<span class='inner'></span>]"); |
866 | toggle.children(".inner").text(labelForToggleButton(false)); | |
1a4d82fc JJ |
867 | |
868 | $(".method").each(function() { | |
d9579d0f AL |
869 | if ($(this).next().is(".docblock") || |
870 | ($(this).next().is(".stability") && $(this).next().next().is(".docblock"))) { | |
871 | $(this).children().first().after(toggle.clone()); | |
872 | } | |
1a4d82fc JJ |
873 | }); |
874 | ||
875 | var mainToggle = | |
876 | $(toggle).append( | |
877 | $('<span/>', {'class': 'toggle-label'}) | |
878 | .css('display', 'none') | |
879 | .html(' Expand description')); | |
880 | var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle); | |
881 | $("#main > .docblock").before(wrapper); | |
882 | }); | |
883 | ||
884 | $('pre.line-numbers').on('click', 'span', function() { | |
885 | var prev_id = 0; | |
886 | ||
887 | function set_fragment(name) { | |
888 | if (history.replaceState) { | |
889 | history.replaceState(null, null, '#' + name); | |
890 | $(window).trigger('hashchange'); | |
891 | } else { | |
892 | location.replace('#' + name); | |
893 | } | |
894 | } | |
895 | ||
896 | return function(ev) { | |
897 | var cur_id = parseInt(ev.target.id); | |
898 | ||
899 | if (ev.shiftKey && prev_id) { | |
900 | if (prev_id > cur_id) { | |
901 | var tmp = prev_id; | |
902 | prev_id = cur_id; | |
903 | cur_id = tmp; | |
904 | } | |
905 | ||
906 | set_fragment(prev_id + '-' + cur_id); | |
907 | } else { | |
908 | prev_id = cur_id; | |
909 | ||
910 | set_fragment(cur_id); | |
911 | } | |
912 | }; | |
913 | }()); | |
914 | ||
915 | }()); |