X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustdoc%2Fhtml%2Fstatic%2Fmain.js;h=3ffb72ba3ee8b645f85ee6400c176052214ad883;hb=0b1870e7b98f6319a38750c41738c3ad35f01a27;hp=ec8024ffca5294894206c0b6acdbd0d7456954aa;hpb=6163bfdcce6404c5d61d6ab441bf49a831082194;p=rust.git diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index ec8024ffca5..7e613220f2d 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1,9 +1,6 @@ -// From rust: -/* global ALIASES, currentCrate, rootPath */ - // Local js definitions: -/* global addClass, getCurrentValue, hasClass */ -/* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ +/* global addClass, getSettingValue, hasClass */ +/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ /* global hideThemeButtonState, showThemeButtonState */ if (!String.prototype.startsWith) { @@ -40,6 +37,21 @@ if (!DOMTokenList.prototype.remove) { }; } +(function () { + var rustdocVars = document.getElementById("rustdoc-vars"); + if (rustdocVars) { + window.rootPath = rustdocVars.attributes["data-root-path"].value; + window.currentCrate = rustdocVars.attributes["data-current-crate"].value; + } + var sidebarVars = document.getElementById("sidebar-vars"); + if (sidebarVars) { + window.sidebarCurrent = { + name: sidebarVars.attributes["data-name"].value, + ty: sidebarVars.attributes["data-ty"].value, + relpath: sidebarVars.attributes["data-relpath"].value, + }; + } +}()); // Gets the human-readable string for the virtual-key code of the // given KeyboardEvent, ev. @@ -79,6 +91,11 @@ function getThemePickerElement() { return document.getElementById("theme-picker"); } +// Returns the current URL without any query parameter or hash. +function getNakedUrl() { + return window.location.href.split("?")[0].split("#")[0]; +} + // Sets the focus on the search bar at the top of the page function focusSearchBar() { getSearchInput().focus(); @@ -240,7 +257,9 @@ function defocusSearchBar() { hideSearchResults(search); var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1); if (browserSupportsHistoryApi()) { - history.replaceState(hash, "", "?search=#" + hash); + // `window.location.search`` contains all the query parameters, not just `search`. + history.replaceState(hash, "", + getNakedUrl() + window.location.search + "#" + hash); } elem = document.getElementById(hash); if (elem) { @@ -565,7 +584,7 @@ function defocusSearchBar() { var i, match, url = document.location.href, stripped = "", - len = rootPath.match(/\.\.\//g).length + 1; + len = window.rootPath.match(/\.\.\//g).length + 1; for (i = 0; i < len; ++i) { match = url.match(/\/[^\/]*$/); @@ -648,8 +667,7 @@ function defocusSearchBar() { */ function execQuery(query, searchWords, filterCrates) { function itemTypeFromName(typename) { - var length = itemTypes.length; - for (var i = 0; i < length; ++i) { + for (var i = 0, len = itemTypes.length; i < len; ++i) { if (itemTypes[i] === typename) { return i; } @@ -667,8 +685,7 @@ function defocusSearchBar() { function transformResults(results, isType) { var out = []; - var length = results.length; - for (var i = 0; i < length; ++i) { + for (var i = 0, len = results.length; i < len; ++i) { if (results[i].id > -1) { var obj = searchIndex[results[i].id]; obj.lev = results[i].lev; @@ -697,11 +714,11 @@ function defocusSearchBar() { } } results = ar; - var i; - var nresults = results.length; - for (i = 0; i < nresults; ++i) { - results[i].word = searchWords[results[i].id]; - results[i].item = searchIndex[results[i].id] || {}; + var i, len, result; + for (i = 0, len = results.length; i < len; ++i) { + result = results[i]; + result.word = searchWords[result.id]; + result.item = searchIndex[result.id] || {}; } // if there are no results then return to default and fail if (results.length === 0) { @@ -775,8 +792,7 @@ function defocusSearchBar() { return 0; }); - var length = results.length; - for (i = 0; i < length; ++i) { + for (i = 0, len = results.length; i < len; ++i) { var result = results[i]; // this validation does not make sense when searching by types @@ -833,11 +849,10 @@ function defocusSearchBar() { var vlength = val.generics.length; for (var y = 0; y < vlength; ++y) { var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1}; - var elength = elems.length; var firstGeneric = getObjectFromId(val.generics[y]).name; - for (var x = 0; x < elength; ++x) { + for (var x = 0, elength = elems.length; x < elength; ++x) { var tmp_lev = levenshtein(getObjectFromId(elems[x]).name, - firstGeneric); + firstGeneric); if (tmp_lev < lev.lev) { lev.lev = tmp_lev; lev.pos = x; @@ -861,7 +876,7 @@ function defocusSearchBar() { // Check for type name and type generics (if any). function checkType(obj, val, literalSearch) { var lev_distance = MAX_LEV_DISTANCE + 1; - var x; + var len, x, y, e_len, firstGeneric; if (obj[NAME] === val.name) { if (literalSearch === true) { if (val.generics && val.generics.length !== 0) { @@ -870,10 +885,12 @@ function defocusSearchBar() { var elems = obj[GENERICS_DATA].slice(0); var allFound = true; - for (var y = 0; allFound === true && y < val.generics.length; ++y) { + len = val.generics.length; + for (y = 0; allFound === true && y < len; ++y) { allFound = false; - var firstGeneric = getObjectFromId(val.generics[y]).name; - for (x = 0; allFound === false && x < elems.length; ++x) { + firstGeneric = getObjectFromId(val.generics[y]).name; + e_len = elems.length; + for (x = 0; allFound === false && x < e_len; ++x) { allFound = getObjectFromId(elems[x]).name === firstGeneric; } if (allFound === true) { @@ -903,12 +920,10 @@ function defocusSearchBar() { // Names didn't match so let's check if one of the generic types could. if (literalSearch === true) { if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) { - var length = obj[GENERICS_DATA].length; - for (x = 0; x < length; ++x) { - if (obj[GENERICS_DATA][x] === val.name) { - return true; - } - } + return obj[GENERICS_DATA].some( + function(name) { + return name === val.name; + }); } return false; } @@ -965,7 +980,7 @@ function defocusSearchBar() { if (typeof ret[0] === "string") { ret = [ret]; } - for (var x = 0; x < ret.length; ++x) { + for (var x = 0, len = ret.length; x < len; ++x) { var tmp = ret[x]; if (typePassesFilter(typeFilter, tmp[1]) === false) { continue; @@ -1072,23 +1087,22 @@ function defocusSearchBar() { // aliases to be before the others in the displayed results. var aliases = []; var crateAliases = []; - var i; if (filterCrates !== undefined) { if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) { - for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) { - aliases.push( - createAliasFromItem( - searchIndex[ALIASES[filterCrates][query.search][i]])); + var query_aliases = ALIASES[filterCrates][query.search]; + var len = query_aliases.length; + for (var i = 0; i < len; ++i) { + aliases.push(createAliasFromItem(searchIndex[query_aliases[i]])); } } } else { Object.keys(ALIASES).forEach(function(crate) { if (ALIASES[crate][query.search]) { var pushTo = crate === window.currentCrate ? crateAliases : aliases; - for (i = 0; i < ALIASES[crate][query.search].length; ++i) { - pushTo.push( - createAliasFromItem( - searchIndex[ALIASES[crate][query.search][i]])); + var query_aliases = ALIASES[crate][query.search]; + var len = query_aliases.length; + for (var i = 0; i < len; ++i) { + pushTo.push(createAliasFromItem(searchIndex[query_aliases[i]])); } } }); @@ -1123,11 +1137,12 @@ function defocusSearchBar() { // quoted values mean literal search var nSearchWords = searchWords.length; - var i; + var i, it; var ty; var fullId; var returned; var in_args; + var len; if ((val.charAt(0) === "\"" || val.charAt(0) === "'") && val.charAt(val.length - 1) === val.charAt(0)) { @@ -1175,7 +1190,7 @@ function defocusSearchBar() { var input = parts[0]; // sort inputs so that order does not matter var inputs = input.split(",").map(trimmer).sort(); - for (i = 0; i < inputs.length; ++i) { + for (i = 0, len = inputs.length; i < len; ++i) { inputs[i] = extractGenerics(inputs[i]); } var output = extractGenerics(parts[1]); @@ -1200,7 +1215,7 @@ function defocusSearchBar() { is_module = true; } else { var allFound = true; - for (var it = 0; allFound === true && it < inputs.length; it++) { + for (it = 0, len = inputs.length; allFound === true && it < len; it++) { allFound = checkType(type, inputs[it], true); } in_args = allFound; @@ -1243,7 +1258,7 @@ function defocusSearchBar() { var paths = valLower.split("::"); var j; - for (j = 0; j < paths.length; ++j) { + for (j = 0, len = paths.length; j < len; ++j) { if (paths[j] === "") { paths.splice(j, 1); j -= 1; @@ -1365,7 +1380,7 @@ function defocusSearchBar() { * @return {[boolean]} [Whether the result is valid or not] */ function validateResult(name, path, keys, parent) { - for (var i = 0; i < keys.length; ++i) { + for (var i = 0, len = keys.length; i < len; ++i) { // each check is for validation so we negate the conditions and invalidate if (!( // check for an exact name match @@ -1508,15 +1523,15 @@ function defocusSearchBar() { if (type === "mod") { displayPath = path + "::"; - href = rootPath + path.replace(/::/g, "/") + "/" + + href = window.rootPath + path.replace(/::/g, "/") + "/" + name + "/index.html"; } else if (type === "primitive" || type === "keyword") { displayPath = ""; - href = rootPath + path.replace(/::/g, "/") + + href = window.rootPath + path.replace(/::/g, "/") + "/" + type + "." + name + ".html"; } else if (type === "externcrate") { displayPath = ""; - href = rootPath + name + "/index.html"; + href = window.rootPath + name + "/index.html"; } else if (item.parent !== undefined) { var myparent = item.parent; var anchor = "#" + type + "." + name; @@ -1539,13 +1554,13 @@ function defocusSearchBar() { } else { displayPath = path + "::" + myparent.name + "::"; } - href = rootPath + path.replace(/::/g, "/") + + href = window.rootPath + path.replace(/::/g, "/") + "/" + pageType + "." + pageName + ".html" + anchor; } else { displayPath = item.path + "::"; - href = rootPath + item.path.replace(/::/g, "/") + + href = window.rootPath + item.path.replace(/::/g, "/") + "/" + type + "." + name + ".html"; } return [displayPath, href]; @@ -1654,6 +1669,21 @@ function defocusSearchBar() { var ret_in_args = addTab(results.in_args, query, false); var ret_returned = addTab(results.returned, query, false); + // Navigate to the relevant tab if the current tab is empty, like in case users search + // for "-> String". If they had selected another tab previously, they have to click on + // it again. + if ((currentTab === 0 && ret_others[1] === 0) || + (currentTab === 1 && ret_in_args[1] === 0) || + (currentTab === 2 && ret_returned[1] === 0)) { + if (ret_others[1] !== 0) { + currentTab = 0; + } else if (ret_in_args[1] !== 0) { + currentTab = 1; + } else if (ret_returned[1] !== 0) { + currentTab = 2; + } + } + var output = "