mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 08:44:35 +00:00
Rollup merge of #72294 - GuillaumeGomez:js-cleanup, r=kinnison
JS cleanup The goal here is just to improve the source code a bit. I recommend to review one commit at a time, otherwise it might not make much sense. :) The biggest commit is the second one: to prevent to have "global" variables declared in `main.js` (and thus prevent name conflict or overwriting), I moved such code into anonymous functions. r? @kinnison cc @rust-lang/rustdoc
This commit is contained in:
commit
0747f586eb
@ -3,7 +3,7 @@
|
||||
|
||||
// Local js definitions:
|
||||
/* global addClass, getCurrentValue, hasClass */
|
||||
/* global onEach, removeClass, updateLocalStorage */
|
||||
/* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
|
||||
|
||||
if (!String.prototype.startsWith) {
|
||||
String.prototype.startsWith = function(searchString, position) {
|
||||
@ -47,6 +47,16 @@ function getSearchElement() {
|
||||
return document.getElementById("search");
|
||||
}
|
||||
|
||||
// Sets the focus on the search bar at the top of the page
|
||||
function focusSearchBar() {
|
||||
getSearchInput().focus();
|
||||
}
|
||||
|
||||
// Removes the focus from the search bar
|
||||
function defocusSearchBar() {
|
||||
getSearchInput().blur();
|
||||
}
|
||||
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
@ -181,6 +191,7 @@ function getSearchElement() {
|
||||
var savedHash = "";
|
||||
|
||||
function handleHashes(ev) {
|
||||
var elem;
|
||||
var search = getSearchElement();
|
||||
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
|
||||
// This block occurs when clicking on an element in the navbar while
|
||||
@ -190,7 +201,7 @@ function getSearchElement() {
|
||||
if (browserSupportsHistoryApi()) {
|
||||
history.replaceState(hash, "", "?search=#" + hash);
|
||||
}
|
||||
var elem = document.getElementById(hash);
|
||||
elem = document.getElementById(hash);
|
||||
if (elem) {
|
||||
elem.scrollIntoView();
|
||||
}
|
||||
@ -201,7 +212,7 @@ function getSearchElement() {
|
||||
if (savedHash.length === 0) {
|
||||
return;
|
||||
}
|
||||
var elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
|
||||
elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
|
||||
if (!elem || !isHidden(elem)) {
|
||||
return;
|
||||
}
|
||||
@ -324,7 +335,7 @@ function getSearchElement() {
|
||||
}
|
||||
|
||||
function displayHelp(display, ev, help) {
|
||||
var help = help ? help : getHelpElement();
|
||||
help = help ? help : getHelpElement();
|
||||
if (display === true) {
|
||||
if (hasClass(help, "hidden")) {
|
||||
ev.preventDefault();
|
||||
@ -438,8 +449,8 @@ function getSearchElement() {
|
||||
|
||||
set_fragment(cur_line_id);
|
||||
}
|
||||
}
|
||||
})();
|
||||
};
|
||||
}());
|
||||
|
||||
document.addEventListener("click", function(ev) {
|
||||
if (hasClass(ev.target, "collapse-toggle")) {
|
||||
@ -465,6 +476,7 @@ function getSearchElement() {
|
||||
}
|
||||
});
|
||||
|
||||
(function() {
|
||||
var x = document.getElementsByClassName("version-selector");
|
||||
if (x.length > 0) {
|
||||
x[0].onchange = function() {
|
||||
@ -481,11 +493,13 @@ function getSearchElement() {
|
||||
url = url.substring(0, url.length - match[0].length);
|
||||
}
|
||||
|
||||
url += "/" + document.getElementsByClassName("version-selector")[0].value + stripped;
|
||||
var selectedVersion = document.getElementsByClassName("version-selector")[0].value;
|
||||
url += "/" + selectedVersion + stripped;
|
||||
|
||||
document.location.href = url;
|
||||
};
|
||||
}
|
||||
}());
|
||||
|
||||
/**
|
||||
* A function to compute the Levenshtein distance between two strings
|
||||
@ -522,7 +536,7 @@ function getSearchElement() {
|
||||
return s1_len + s2_len;
|
||||
}
|
||||
|
||||
function initSearch(rawSearchIndex) {
|
||||
window.initSearch = function(rawSearchIndex) {
|
||||
var MAX_LEV_DISTANCE = 3;
|
||||
var MAX_RESULTS = 200;
|
||||
var GENERICS_DATA = 1;
|
||||
@ -602,7 +616,7 @@ function getSearchElement() {
|
||||
function sortResults(results, isType) {
|
||||
var ar = [];
|
||||
for (var entry in results) {
|
||||
if (results.hasOwnProperty(entry)) {
|
||||
if (hasOwnProperty(results, entry)) {
|
||||
ar.push(results[entry]);
|
||||
}
|
||||
}
|
||||
@ -1100,8 +1114,6 @@ function getSearchElement() {
|
||||
}
|
||||
fullId = generateId(ty);
|
||||
|
||||
// allow searching for void (no output) functions as well
|
||||
var typeOutput = type.length > OUTPUT_DATA ? type[OUTPUT_DATA].name : "";
|
||||
returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
|
||||
if (output.name === "*" || returned === true) {
|
||||
in_args = false;
|
||||
@ -1164,7 +1176,6 @@ function getSearchElement() {
|
||||
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
|
||||
|
||||
var lev;
|
||||
var lev_distance;
|
||||
for (j = 0; j < nSearchWords; ++j) {
|
||||
ty = searchIndex[j];
|
||||
if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
|
||||
@ -1661,7 +1672,7 @@ function getSearchElement() {
|
||||
function getFilterCrates() {
|
||||
var elem = document.getElementById("crate-search");
|
||||
|
||||
if (elem && elem.value !== "All crates" && rawSearchIndex.hasOwnProperty(elem.value)) {
|
||||
if (elem && elem.value !== "All crates" && hasOwnProperty(rawSearchIndex, elem.value)) {
|
||||
return elem.value;
|
||||
}
|
||||
return undefined;
|
||||
@ -1709,7 +1720,7 @@ function getSearchElement() {
|
||||
var currentIndex = 0;
|
||||
|
||||
for (var crate in rawSearchIndex) {
|
||||
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
|
||||
if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
|
||||
|
||||
var crateSize = 0;
|
||||
|
||||
@ -1893,7 +1904,7 @@ function getSearchElement() {
|
||||
|
||||
var crates = [];
|
||||
for (var crate in rawSearchIndex) {
|
||||
if (!rawSearchIndex.hasOwnProperty(crate)) {
|
||||
if (!hasOwnProperty(rawSearchIndex, crate)) {
|
||||
continue;
|
||||
}
|
||||
crates.push(crate);
|
||||
@ -1917,12 +1928,11 @@ function getSearchElement() {
|
||||
sidebar.appendChild(div);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.initSearch = initSearch;
|
||||
|
||||
// delayed sidebar rendering.
|
||||
function initSidebarItems(items) {
|
||||
window.initSidebarItems = function(items) {
|
||||
var sidebar = document.getElementsByClassName("sidebar-elems")[0];
|
||||
var current = window.sidebarCurrent;
|
||||
|
||||
@ -1984,9 +1994,7 @@ function getSearchElement() {
|
||||
block("foreigntype", "Foreign Types");
|
||||
block("keyword", "Keywords");
|
||||
block("traitalias", "Trait Aliases");
|
||||
}
|
||||
|
||||
window.initSidebarItems = initSidebarItems;
|
||||
};
|
||||
|
||||
window.register_implementors = function(imp) {
|
||||
var implementors = document.getElementById("implementors-list");
|
||||
@ -2163,19 +2171,13 @@ function getSearchElement() {
|
||||
}
|
||||
}
|
||||
var ns = n.nextElementSibling;
|
||||
while (true) {
|
||||
if (ns && (
|
||||
hasClass(ns, "docblock") ||
|
||||
hasClass(ns, "stability"))) {
|
||||
while (ns && (hasClass(ns, "docblock") || hasClass(ns, "stability"))) {
|
||||
if (addOrRemove) {
|
||||
addClass(ns, "hidden-by-impl-hider");
|
||||
} else {
|
||||
removeClass(ns, "hidden-by-impl-hider");
|
||||
}
|
||||
ns = ns.nextElementSibling;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -2248,7 +2250,7 @@ function getSearchElement() {
|
||||
}
|
||||
}
|
||||
|
||||
function collapser(e, collapse) {
|
||||
function collapser(pageId, e, collapse) {
|
||||
// inherent impl ids are like "impl" or impl-<number>'.
|
||||
// they will never be hidden by default.
|
||||
var n = e.parentElement;
|
||||
@ -2268,7 +2270,7 @@ function getSearchElement() {
|
||||
|
||||
if (impl_list !== null) {
|
||||
onEachLazy(impl_list.getElementsByClassName("collapse-toggle"), function(e) {
|
||||
collapser(e, collapse);
|
||||
collapser(pageId, e, collapse);
|
||||
});
|
||||
}
|
||||
|
||||
@ -2276,7 +2278,7 @@ function getSearchElement() {
|
||||
|
||||
if (blanket_list !== null) {
|
||||
onEachLazy(blanket_list.getElementsByClassName("collapse-toggle"), function(e) {
|
||||
collapser(e, collapse);
|
||||
collapser(pageId, e, collapse);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -2300,6 +2302,42 @@ function getSearchElement() {
|
||||
return toggle;
|
||||
}
|
||||
|
||||
function createToggle(toggle, otherMessage, fontSize, extraClass, show) {
|
||||
var span = document.createElement("span");
|
||||
span.className = "toggle-label";
|
||||
if (show) {
|
||||
span.style.display = "none";
|
||||
}
|
||||
if (!otherMessage) {
|
||||
span.innerHTML = " Expand description";
|
||||
} else {
|
||||
span.innerHTML = otherMessage;
|
||||
}
|
||||
|
||||
if (fontSize) {
|
||||
span.style.fontSize = fontSize;
|
||||
}
|
||||
|
||||
var mainToggle = toggle.cloneNode(true);
|
||||
mainToggle.appendChild(span);
|
||||
|
||||
var wrapper = document.createElement("div");
|
||||
wrapper.className = "toggle-wrapper";
|
||||
if (!show) {
|
||||
addClass(wrapper, "collapsed");
|
||||
var inner = mainToggle.getElementsByClassName("inner");
|
||||
if (inner && inner.length > 0) {
|
||||
inner[0].innerHTML = "+";
|
||||
}
|
||||
}
|
||||
if (extraClass) {
|
||||
addClass(wrapper, extraClass);
|
||||
}
|
||||
wrapper.appendChild(mainToggle);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
(function() {
|
||||
var toggle = createSimpleToggle(false);
|
||||
var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
|
||||
var pageId = getPageId();
|
||||
@ -2338,7 +2376,7 @@ function getSearchElement() {
|
||||
onEachLazy(document.getElementsByClassName("impl"), funcImpl);
|
||||
var impl_call = function() {};
|
||||
if (hideMethodDocs === true) {
|
||||
impl_call = function(e, newToggle, pageId) {
|
||||
impl_call = function(e, newToggle) {
|
||||
if (e.id.match(/^impl(?:-\d+)?$/) === null) {
|
||||
// Automatically minimize all non-inherent impls
|
||||
if (hasClass(e, "impl") === true) {
|
||||
@ -2392,45 +2430,10 @@ function getSearchElement() {
|
||||
var inner_toggle = newToggle.cloneNode(true);
|
||||
inner_toggle.onclick = toggleClicked;
|
||||
e.insertBefore(inner_toggle, e.firstChild);
|
||||
impl_call(e.previousSibling, inner_toggle, pageId);
|
||||
impl_call(e.previousSibling, inner_toggle);
|
||||
}
|
||||
});
|
||||
|
||||
function createToggle(otherMessage, fontSize, extraClass, show) {
|
||||
var span = document.createElement("span");
|
||||
span.className = "toggle-label";
|
||||
if (show) {
|
||||
span.style.display = "none";
|
||||
}
|
||||
if (!otherMessage) {
|
||||
span.innerHTML = " Expand description";
|
||||
} else {
|
||||
span.innerHTML = otherMessage;
|
||||
}
|
||||
|
||||
if (fontSize) {
|
||||
span.style.fontSize = fontSize;
|
||||
}
|
||||
|
||||
var mainToggle = toggle.cloneNode(true);
|
||||
mainToggle.appendChild(span);
|
||||
|
||||
var wrapper = document.createElement("div");
|
||||
wrapper.className = "toggle-wrapper";
|
||||
if (!show) {
|
||||
addClass(wrapper, "collapsed");
|
||||
var inner = mainToggle.getElementsByClassName("inner");
|
||||
if (inner && inner.length > 0) {
|
||||
inner[0].innerHTML = "+";
|
||||
}
|
||||
}
|
||||
if (extraClass) {
|
||||
addClass(wrapper, extraClass);
|
||||
}
|
||||
wrapper.appendChild(mainToggle);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
var currentType = document.getElementsByClassName("type-decl")[0];
|
||||
var className = null;
|
||||
if (currentType) {
|
||||
@ -2471,7 +2474,7 @@ function getSearchElement() {
|
||||
onEachLazy(inner_toggle.getElementsByClassName("toggle-label"), function(e) {
|
||||
e.style.display = "inline-block";
|
||||
if (extra === true) {
|
||||
i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
|
||||
e.innerHTML = " Show " + e.childNodes[0].innerHTML;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -2506,7 +2509,9 @@ function getSearchElement() {
|
||||
}
|
||||
|
||||
e.parentNode.insertBefore(
|
||||
createToggle(otherMessage,
|
||||
createToggle(
|
||||
toggle,
|
||||
otherMessage,
|
||||
fontSize,
|
||||
extraClass,
|
||||
hasClass(e, "type-decl") === false || showItemDeclarations === true),
|
||||
@ -2522,6 +2527,7 @@ function getSearchElement() {
|
||||
|
||||
onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
|
||||
onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
|
||||
}());
|
||||
|
||||
function createToggleWrapper(tog) {
|
||||
var span = document.createElement("span");
|
||||
@ -2536,6 +2542,7 @@ function getSearchElement() {
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
(function() {
|
||||
// To avoid checking on "rustdoc-item-attributes" value on every loop...
|
||||
var itemAttributesFunc = function() {};
|
||||
if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
|
||||
@ -2552,7 +2559,9 @@ function getSearchElement() {
|
||||
i_e.parentNode.insertBefore(attr_tog, i_e);
|
||||
itemAttributesFunc(i_e);
|
||||
});
|
||||
}());
|
||||
|
||||
(function() {
|
||||
// To avoid checking on "rustdoc-line-numbers" value on every loop...
|
||||
var lineNumbersFunc = function() {};
|
||||
if (getCurrentValue("rustdoc-line-numbers") === "true") {
|
||||
@ -2570,22 +2579,23 @@ function getSearchElement() {
|
||||
}
|
||||
onEachLazy(document.getElementsByClassName("rust-example-rendered"), function(e) {
|
||||
if (hasClass(e, "compile_fail")) {
|
||||
e.addEventListener("mouseover", function(event) {
|
||||
e.addEventListener("mouseover", function() {
|
||||
this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
|
||||
});
|
||||
e.addEventListener("mouseout", function(event) {
|
||||
e.addEventListener("mouseout", function() {
|
||||
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
|
||||
});
|
||||
} else if (hasClass(e, "ignore")) {
|
||||
e.addEventListener("mouseover", function(event) {
|
||||
e.addEventListener("mouseover", function() {
|
||||
this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
|
||||
});
|
||||
e.addEventListener("mouseout", function(event) {
|
||||
e.addEventListener("mouseout", function() {
|
||||
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
|
||||
});
|
||||
}
|
||||
lineNumbersFunc(e);
|
||||
});
|
||||
}());
|
||||
|
||||
// In the search display, allows to switch between tabs.
|
||||
function printTab(nb) {
|
||||
@ -2678,7 +2688,7 @@ function getSearchElement() {
|
||||
});
|
||||
}
|
||||
|
||||
function addSearchOptions(crates) {
|
||||
window.addSearchOptions = function(crates) {
|
||||
var elem = document.getElementById("crate-search");
|
||||
|
||||
if (!elem) {
|
||||
@ -2687,7 +2697,7 @@ function getSearchElement() {
|
||||
var crates_text = [];
|
||||
if (Object.keys(crates).length > 1) {
|
||||
for (var crate in crates) {
|
||||
if (crates.hasOwnProperty(crate)) {
|
||||
if (hasOwnProperty(crates, crate)) {
|
||||
crates_text.push(crate);
|
||||
}
|
||||
}
|
||||
@ -2722,10 +2732,8 @@ function getSearchElement() {
|
||||
|
||||
if (search_input) {
|
||||
search_input.removeAttribute('disabled');
|
||||
};
|
||||
}
|
||||
|
||||
window.addSearchOptions = addSearchOptions;
|
||||
};
|
||||
|
||||
function buildHelperPopup() {
|
||||
var popup = document.createElement("aside");
|
||||
@ -2778,16 +2786,6 @@ function getSearchElement() {
|
||||
buildHelperPopup();
|
||||
}());
|
||||
|
||||
// Sets the focus on the search bar at the top of the page
|
||||
function focusSearchBar() {
|
||||
getSearchInput().focus();
|
||||
}
|
||||
|
||||
// Removes the focus from the search bar
|
||||
function defocusSearchBar() {
|
||||
getSearchInput().blur();
|
||||
}
|
||||
|
||||
// This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
|
||||
// the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
|
||||
// previous search results (if you navigated to a search result with the keyboard, pressed enter on
|
||||
|
@ -1,3 +1,6 @@
|
||||
// Local js definitions:
|
||||
/* global getCurrentValue, updateLocalStorage */
|
||||
|
||||
(function () {
|
||||
function changeSetting(settingName, isEnabled) {
|
||||
updateLocalStorage('rustdoc-' + settingName, isEnabled);
|
||||
|
@ -1,5 +1,5 @@
|
||||
// From rust:
|
||||
/* global sourcesIndex */
|
||||
/* global search, sourcesIndex */
|
||||
|
||||
// Local js definitions:
|
||||
/* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */
|
||||
|
@ -27,14 +27,15 @@ function removeClass(elem, className) {
|
||||
function onEach(arr, func, reversed) {
|
||||
if (arr && arr.length > 0 && func) {
|
||||
var length = arr.length;
|
||||
var i;
|
||||
if (reversed !== true) {
|
||||
for (var i = 0; i < length; ++i) {
|
||||
for (i = 0; i < length; ++i) {
|
||||
if (func(arr[i]) === true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var i = length - 1; i >= 0; --i) {
|
||||
for (i = length - 1; i >= 0; --i) {
|
||||
if (func(arr[i]) === true) {
|
||||
return true;
|
||||
}
|
||||
@ -51,6 +52,10 @@ function onEachLazy(lazyArray, func, reversed) {
|
||||
reversed);
|
||||
}
|
||||
|
||||
function hasOwnProperty(obj, property) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, property);
|
||||
}
|
||||
|
||||
function usableLocalStorage() {
|
||||
// Check if the browser supports localStorage at all:
|
||||
if (typeof Storage === "undefined") {
|
||||
|
@ -241,7 +241,7 @@ function loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate) {
|
||||
ALIASES = {};
|
||||
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
||||
finalJS += 'var rootPath = "../";\n';
|
||||
finalJS += loadThings(["onEach"], 'function', extractFunction, storageJs);
|
||||
finalJS += loadThings(["hasOwnProperty", "onEach"], 'function', extractFunction, storageJs);
|
||||
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
|
||||
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
|
||||
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
||||
|
Loading…
Reference in New Issue
Block a user