Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 11f854d

Browse files
committed
Further consolidate search-related vars and funcs
1 parent 95d652e commit 11f854d

File tree

1 file changed

+61
-76
lines changed

1 file changed

+61
-76
lines changed

src/librustdoc/html/static/main.js

Lines changed: 61 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ function getVirtualKey(ev) {
7676
return String.fromCharCode(c);
7777
}
7878

79-
function getSearchInput() {
80-
return document.getElementsByClassName("search-input")[0];
81-
}
82-
8379
var THEME_PICKER_ELEMENT_ID = "theme-picker";
8480
var THEMES_ELEMENT_ID = "theme-choices";
8581

@@ -96,16 +92,6 @@ function getNakedUrl() {
9692
return window.location.href.split("?")[0].split("#")[0];
9793
}
9894

99-
// Sets the focus on the search bar at the top of the page
100-
function focusSearchBar() {
101-
getSearchInput().focus();
102-
}
103-
104-
// Removes the focus from the search bar.
105-
function defocusSearchBar() {
106-
getSearchInput().blur();
107-
}
108-
10995
function showThemeButtonState() {
11096
var themePicker = getThemePickerElement();
11197
var themeChoices = getThemesElement();
@@ -170,7 +156,7 @@ function hideThemeButtonState() {
170156

171157
window.searchState = {
172158
loadingText: "Loading search results...",
173-
input: getSearchInput(),
159+
input: document.getElementsByClassName("search-input")[0],
174160
outputElement: function() {
175161
return document.getElementById("search");
176162
},
@@ -190,6 +176,14 @@ function hideThemeButtonState() {
190176
searchState.timeout = null;
191177
}
192178
},
179+
// Sets the focus on the search bar at the top of the page
180+
focus: function() {
181+
searchState.input.focus();
182+
},
183+
// Removes the focus from the search bar.
184+
defocus: function() {
185+
searchState.input.blur();
186+
},
193187
showResults: function(search) {
194188
if (search === null || typeof search === 'undefined') {
195189
search = searchState.outputElement();
@@ -237,7 +231,11 @@ function hideThemeButtonState() {
237231
browserSupportsHistoryApi: function() {
238232
return window.history && typeof window.history.pushState === "function";
239233
},
240-
setupLoader: function() {
234+
setup: function() {
235+
var search_input = searchState.input;
236+
if (!searchState.input) {
237+
return;
238+
}
241239
function loadScript(url) {
242240
var script = document.createElement('script');
243241
script.src = url;
@@ -252,38 +250,57 @@ function hideThemeButtonState() {
252250
}
253251
}
254252

255-
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
256-
addSearchOptions(window.ALL_CRATES);
257-
addSidebarCrates(window.ALL_CRATES);
258-
259-
searchState.input.addEventListener("focus", function() {
260-
searchState.input.origPlaceholder = searchState.input.placeholder;
261-
searchState.input.placeholder = "Type your search here.";
253+
search_input.addEventListener("focus", function() {
254+
searchState.putBackSearch(this);
255+
search_input.origPlaceholder = searchState.input.placeholder;
256+
search_input.placeholder = "Type your search here.";
262257
loadSearch();
263258
});
264-
searchState.input.addEventListener("blur", function() {
265-
searchState.input.placeholder = searchState.input.origPlaceholder;
259+
search_input.addEventListener("blur", function() {
260+
search_input.placeholder = searchState.input.origPlaceholder;
266261
});
267-
searchState.input.removeAttribute('disabled');
268262

269-
var crateSearchDropDown = document.getElementById("crate-search");
270-
// `crateSearchDropDown` can be null in case there is only crate because in that case, the
271-
// crate filter dropdown is removed.
272-
if (crateSearchDropDown) {
273-
crateSearchDropDown.addEventListener("focus", loadSearch);
274-
}
263+
document.addEventListener("mousemove", function() {
264+
searchState.mouseMovedAfterSearch = true;
265+
});
266+
267+
search_input.removeAttribute('disabled');
268+
269+
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
270+
searchState.addCrateDropdown(window.ALL_CRATES);
275271
var params = searchState.getQueryStringParams();
276272
if (params.search !== undefined) {
273+
var search = searchState.outputElement();
274+
search.innerHTML = "<h3 style=\"text-align: center;\">" +
275+
searchState.loadingText + "</h3>";
276+
searchState.showResults(search);
277277
loadSearch();
278278
}
279279
},
280-
};
280+
addCrateDropdown: function(crates) {
281+
var elem = document.getElementById("crate-search");
281282

282-
if (searchState.input) {
283-
searchState.input.onfocus = function() {
284-
searchState.putBackSearch(this);
285-
};
286-
}
283+
if (!elem) {
284+
return;
285+
}
286+
var savedCrate = getSettingValue("saved-filter-crate");
287+
for (var i = 0, len = crates.length; i < len; ++i) {
288+
var option = document.createElement("option");
289+
option.value = crates[i];
290+
option.innerText = crates[i];
291+
elem.appendChild(option);
292+
// Set the crate filter from saved storage, if the current page has the saved crate
293+
// filter.
294+
//
295+
// If not, ignore the crate filter -- we want to support filtering for crates on sites
296+
// like doc.rust-lang.org where the crates may differ from page to page while on the
297+
// same domain.
298+
if (crates[i] === savedCrate) {
299+
elem.value = savedCrate;
300+
}
301+
}
302+
},
303+
};
287304

288305
function getPageId() {
289306
if (window.location.hash) {
@@ -491,7 +508,7 @@ function hideThemeButtonState() {
491508
ev.preventDefault();
492509
searchState.hideResults(search);
493510
}
494-
defocusSearchBar();
511+
searchState.defocus();
495512
hideThemeButtonState();
496513
}
497514

@@ -518,7 +535,7 @@ function hideThemeButtonState() {
518535
case "S":
519536
displayHelp(false, ev);
520537
ev.preventDefault();
521-
focusSearchBar();
538+
searchState.focus();
522539
break;
523540

524541
case "+":
@@ -605,10 +622,6 @@ function hideThemeButtonState() {
605622
document.addEventListener("keypress", handleShortcut);
606623
document.addEventListener("keydown", handleShortcut);
607624

608-
document.addEventListener("mousemove", function() {
609-
searchState.mouseMovedAfterSearch = true;
610-
});
611-
612625
var handleSourceHighlight = (function() {
613626
var prev_line_id = 0;
614627

@@ -789,6 +802,9 @@ function hideThemeButtonState() {
789802
block("foreigntype", "Foreign Types");
790803
block("keyword", "Keywords");
791804
block("traitalias", "Trait Aliases");
805+
806+
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
807+
addSidebarCrates(window.ALL_CRATES);
792808
};
793809

794810
window.register_implementors = function(imp) {
@@ -1420,13 +1436,6 @@ function hideThemeButtonState() {
14201436
};
14211437
});
14221438

1423-
var params = searchState.getQueryStringParams();
1424-
if (params && params.search) {
1425-
var search = searchState.outputElement();
1426-
search.innerHTML = "<h3 style=\"text-align: center;\">" + searchState.loadingText + "</h3>";
1427-
searchState.showResults(search);
1428-
}
1429-
14301439
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
14311440
if (sidebar_menu) {
14321441
sidebar_menu.onclick = function() {
@@ -1459,30 +1468,6 @@ function hideThemeButtonState() {
14591468
});
14601469
}
14611470

1462-
function addSearchOptions(crates) {
1463-
var elem = document.getElementById("crate-search");
1464-
1465-
if (!elem) {
1466-
return;
1467-
}
1468-
var savedCrate = getSettingValue("saved-filter-crate");
1469-
for (var i = 0, len = crates.length; i < len; ++i) {
1470-
var option = document.createElement("option");
1471-
option.value = crates[i];
1472-
option.innerText = crates[i];
1473-
elem.appendChild(option);
1474-
// Set the crate filter from saved storage, if the current page has the saved crate
1475-
// filter.
1476-
//
1477-
// If not, ignore the crate filter -- we want to support filtering for crates on sites
1478-
// like doc.rust-lang.org where the crates may differ from page to page while on the
1479-
// same domain.
1480-
if (crates[i] === savedCrate) {
1481-
elem.value = savedCrate;
1482-
}
1483-
}
1484-
};
1485-
14861471
function buildHelperPopup() {
14871472
var popup = document.createElement("aside");
14881473
addClass(popup, "hidden");
@@ -1547,7 +1532,7 @@ function hideThemeButtonState() {
15471532

15481533
onHashChange(null);
15491534
window.onhashchange = onHashChange;
1550-
searchState.setupLoader();
1535+
searchState.setup();
15511536
}());
15521537

15531538
function copy_path(but) {

0 commit comments

Comments
 (0)