Skip to content

Commit b677013

Browse files
Improve code readability
1 parent 4ce2384 commit b677013

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

Diff for: src/librustdoc/html/static/main.js

+37-43
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ function getSearchElement() {
138138
}
139139
}
140140

141+
function showSearchResults(search) {
142+
if (search === null || typeof search === 'undefined') {
143+
search = getSearchElement();
144+
}
145+
addClass(main, "hidden");
146+
removeClass(search, "hidden");
147+
}
148+
149+
function hideSearchResults(search) {
150+
if (search === null || typeof search === 'undefined') {
151+
search = getSearchElement();
152+
}
153+
addClass(search, "hidden");
154+
removeClass(main, "hidden");
155+
}
156+
141157
// used for special search precedence
142158
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
143159
var TY_KEYWORD = itemTypes.indexOf("keyword");
@@ -169,8 +185,7 @@ function getSearchElement() {
169185
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
170186
// This block occurs when clicking on an element in the navbar while
171187
// in a search.
172-
addClass(search, "hidden");
173-
removeClass(main, "hidden");
188+
hideSearchResults(search);
174189
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
175190
if (browserSupportsHistoryApi()) {
176191
history.replaceState(hash, "", "?search=#" + hash);
@@ -324,16 +339,14 @@ function getSearchElement() {
324339
}
325340

326341
function handleEscape(ev) {
327-
debugger;
328342
var help = getHelpElement();
329343
var search = getSearchElement();
330344
hideModal();
331345
if (hasClass(help, "hidden") === false) {
332346
displayHelp(false, ev, help);
333347
} else if (hasClass(search, "hidden") === false) {
334348
ev.preventDefault();
335-
addClass(search, "hidden");
336-
removeClass(main, "hidden");
349+
hideSearchResults(search);
337350
document.title = titleBeforeSearch;
338351
}
339352
defocusSearchBar();
@@ -1265,8 +1278,7 @@ function getSearchElement() {
12651278
}
12661279
dst = dst[0];
12671280
if (window.location.pathname === dst.pathname) {
1268-
addClass(getSearchElement(), "hidden");
1269-
removeClass(main, "hidden");
1281+
hideSearchResults();
12701282
document.location.href = dst.href;
12711283
}
12721284
};
@@ -1341,8 +1353,6 @@ function getSearchElement() {
13411353
e.preventDefault();
13421354
} else if (e.which === 16) { // shift
13431355
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
1344-
} else if (e.which === 27) { // escape
1345-
handleEscape(e);
13461356
} else if (actives[currentTab].length > 0) {
13471357
removeClass(actives[currentTab][0], "highlighted");
13481358
}
@@ -1492,10 +1502,9 @@ function getSearchElement() {
14921502
"</div><div id=\"results\">" +
14931503
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
14941504

1495-
addClass(main, "hidden");
14961505
var search = getSearchElement();
1497-
removeClass(search, "hidden");
14981506
search.innerHTML = output;
1507+
showSearchResults(search);
14991508
var tds = search.getElementsByTagName("td");
15001509
var td_width = 0;
15011510
if (tds.length > 0) {
@@ -1700,13 +1709,7 @@ function getSearchElement() {
17001709
if (browserSupportsHistoryApi()) {
17011710
history.replaceState("", window.currentCrate + " - Rust", "?search=");
17021711
}
1703-
if (hasClass(main, "content")) {
1704-
removeClass(main, "hidden");
1705-
}
1706-
var search_c = getSearchElement();
1707-
if (hasClass(search_c, "content")) {
1708-
addClass(search_c, "hidden");
1709-
}
1712+
hideSearchResults();
17101713
} else {
17111714
searchTimeout = setTimeout(search, 500);
17121715
}
@@ -1742,19 +1745,8 @@ function getSearchElement() {
17421745
// Store the previous <title> so we can revert back to it later.
17431746
var previousTitle = document.title;
17441747

1745-
window.onpopstate = function(e) {
1748+
window.addEventListener("popstate", function(e) {
17461749
var params = getQueryStringParams();
1747-
// When browsing back from search results the main page
1748-
// visibility must be reset.
1749-
if (!params.search) {
1750-
if (hasClass(main, "content")) {
1751-
removeClass(main, "hidden");
1752-
}
1753-
var search_c = getSearchElement();
1754-
if (hasClass(search_c, "content")) {
1755-
addClass(search_c, "hidden");
1756-
}
1757-
}
17581750
// Revert to the previous title manually since the History
17591751
// API ignores the title parameter.
17601752
document.title = previousTitle;
@@ -1766,18 +1758,21 @@ function getSearchElement() {
17661758
// perform the search. This will empty the bar if there's
17671759
// nothing there, which lets you really go back to a
17681760
// previous state with nothing in the bar.
1769-
if (params.search) {
1761+
if (params.search && params.search.length > 0) {
17701762
search_input.value = params.search;
1763+
// Some browsers fire "onpopstate" for every page load
1764+
// (Chrome), while others fire the event only when actually
1765+
// popping a state (Firefox), which is why search() is
1766+
// called both here and at the end of the startSearch()
1767+
// function.
1768+
search(e);
17711769
} else {
17721770
search_input.value = "";
1771+
// When browsing back from search results the main page
1772+
// visibility must be reset.
1773+
hideSearchResults();
17731774
}
1774-
// Some browsers fire "onpopstate" for every page load
1775-
// (Chrome), while others fire the event only when actually
1776-
// popping a state (Firefox), which is why search() is
1777-
// called both here and at the end of the startSearch()
1778-
// function.
1779-
search();
1780-
};
1775+
});
17811776
}
17821777
search();
17831778
}
@@ -2523,9 +2518,9 @@ function getSearchElement() {
25232518
}
25242519

25252520
function putBackSearch(search_input) {
2526-
if (search_input.value !== "") {
2527-
addClass(main, "hidden");
2528-
removeClass(getSearchElement(), "hidden");
2521+
var search = getSearchElement();
2522+
if (search_input.value !== "" && hasClass(search, "hidden")) {
2523+
showSearchResults(search);
25292524
if (browserSupportsHistoryApi()) {
25302525
history.replaceState(search_input.value,
25312526
"",
@@ -2542,10 +2537,9 @@ function getSearchElement() {
25422537

25432538
var params = getQueryStringParams();
25442539
if (params && params.search) {
2545-
addClass(main, "hidden");
25462540
var search = getSearchElement();
2547-
removeClass(search, "hidden");
25482541
search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
2542+
showSearchResults(search);
25492543
}
25502544

25512545
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];

0 commit comments

Comments
 (0)