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