Further consolidate search-related vars and funcs
This commit is contained in:
parent
95d652ebce
commit
11f854db4f
1 changed files with 61 additions and 76 deletions
|
@ -76,10 +76,6 @@ function getVirtualKey(ev) {
|
||||||
return String.fromCharCode(c);
|
return String.fromCharCode(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSearchInput() {
|
|
||||||
return document.getElementsByClassName("search-input")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
var THEME_PICKER_ELEMENT_ID = "theme-picker";
|
var THEME_PICKER_ELEMENT_ID = "theme-picker";
|
||||||
var THEMES_ELEMENT_ID = "theme-choices";
|
var THEMES_ELEMENT_ID = "theme-choices";
|
||||||
|
|
||||||
|
@ -96,16 +92,6 @@ function getNakedUrl() {
|
||||||
return window.location.href.split("?")[0].split("#")[0];
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Removes the focus from the search bar.
|
|
||||||
function defocusSearchBar() {
|
|
||||||
getSearchInput().blur();
|
|
||||||
}
|
|
||||||
|
|
||||||
function showThemeButtonState() {
|
function showThemeButtonState() {
|
||||||
var themePicker = getThemePickerElement();
|
var themePicker = getThemePickerElement();
|
||||||
var themeChoices = getThemesElement();
|
var themeChoices = getThemesElement();
|
||||||
|
@ -170,7 +156,7 @@ function hideThemeButtonState() {
|
||||||
|
|
||||||
window.searchState = {
|
window.searchState = {
|
||||||
loadingText: "Loading search results...",
|
loadingText: "Loading search results...",
|
||||||
input: getSearchInput(),
|
input: document.getElementsByClassName("search-input")[0],
|
||||||
outputElement: function() {
|
outputElement: function() {
|
||||||
return document.getElementById("search");
|
return document.getElementById("search");
|
||||||
},
|
},
|
||||||
|
@ -190,6 +176,14 @@ function hideThemeButtonState() {
|
||||||
searchState.timeout = null;
|
searchState.timeout = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// Sets the focus on the search bar at the top of the page
|
||||||
|
focus: function() {
|
||||||
|
searchState.input.focus();
|
||||||
|
},
|
||||||
|
// Removes the focus from the search bar.
|
||||||
|
defocus: function() {
|
||||||
|
searchState.input.blur();
|
||||||
|
},
|
||||||
showResults: function(search) {
|
showResults: function(search) {
|
||||||
if (search === null || typeof search === 'undefined') {
|
if (search === null || typeof search === 'undefined') {
|
||||||
search = searchState.outputElement();
|
search = searchState.outputElement();
|
||||||
|
@ -237,7 +231,11 @@ function hideThemeButtonState() {
|
||||||
browserSupportsHistoryApi: function() {
|
browserSupportsHistoryApi: function() {
|
||||||
return window.history && typeof window.history.pushState === "function";
|
return window.history && typeof window.history.pushState === "function";
|
||||||
},
|
},
|
||||||
setupLoader: function() {
|
setup: function() {
|
||||||
|
var search_input = searchState.input;
|
||||||
|
if (!searchState.input) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
function loadScript(url) {
|
function loadScript(url) {
|
||||||
var script = document.createElement('script');
|
var script = document.createElement('script');
|
||||||
script.src = url;
|
script.src = url;
|
||||||
|
@ -252,38 +250,57 @@ function hideThemeButtonState() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
|
search_input.addEventListener("focus", function() {
|
||||||
addSearchOptions(window.ALL_CRATES);
|
searchState.putBackSearch(this);
|
||||||
addSidebarCrates(window.ALL_CRATES);
|
search_input.origPlaceholder = searchState.input.placeholder;
|
||||||
|
search_input.placeholder = "Type your search here.";
|
||||||
searchState.input.addEventListener("focus", function() {
|
|
||||||
searchState.input.origPlaceholder = searchState.input.placeholder;
|
|
||||||
searchState.input.placeholder = "Type your search here.";
|
|
||||||
loadSearch();
|
loadSearch();
|
||||||
});
|
});
|
||||||
searchState.input.addEventListener("blur", function() {
|
search_input.addEventListener("blur", function() {
|
||||||
searchState.input.placeholder = searchState.input.origPlaceholder;
|
search_input.placeholder = searchState.input.origPlaceholder;
|
||||||
});
|
});
|
||||||
searchState.input.removeAttribute('disabled');
|
|
||||||
|
|
||||||
var crateSearchDropDown = document.getElementById("crate-search");
|
document.addEventListener("mousemove", function() {
|
||||||
// `crateSearchDropDown` can be null in case there is only crate because in that case, the
|
searchState.mouseMovedAfterSearch = true;
|
||||||
// crate filter dropdown is removed.
|
});
|
||||||
if (crateSearchDropDown) {
|
|
||||||
crateSearchDropDown.addEventListener("focus", loadSearch);
|
search_input.removeAttribute('disabled');
|
||||||
}
|
|
||||||
|
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
|
||||||
|
searchState.addCrateDropdown(window.ALL_CRATES);
|
||||||
var params = searchState.getQueryStringParams();
|
var params = searchState.getQueryStringParams();
|
||||||
if (params.search !== undefined) {
|
if (params.search !== undefined) {
|
||||||
|
var search = searchState.outputElement();
|
||||||
|
search.innerHTML = "<h3 style=\"text-align: center;\">" +
|
||||||
|
searchState.loadingText + "</h3>";
|
||||||
|
searchState.showResults(search);
|
||||||
loadSearch();
|
loadSearch();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
addCrateDropdown: function(crates) {
|
||||||
|
var elem = document.getElementById("crate-search");
|
||||||
|
|
||||||
if (searchState.input) {
|
if (!elem) {
|
||||||
searchState.input.onfocus = function() {
|
return;
|
||||||
searchState.putBackSearch(this);
|
}
|
||||||
};
|
var savedCrate = getSettingValue("saved-filter-crate");
|
||||||
}
|
for (var i = 0, len = crates.length; i < len; ++i) {
|
||||||
|
var option = document.createElement("option");
|
||||||
|
option.value = crates[i];
|
||||||
|
option.innerText = crates[i];
|
||||||
|
elem.appendChild(option);
|
||||||
|
// Set the crate filter from saved storage, if the current page has the saved crate
|
||||||
|
// filter.
|
||||||
|
//
|
||||||
|
// If not, ignore the crate filter -- we want to support filtering for crates on sites
|
||||||
|
// like doc.rust-lang.org where the crates may differ from page to page while on the
|
||||||
|
// same domain.
|
||||||
|
if (crates[i] === savedCrate) {
|
||||||
|
elem.value = savedCrate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
function getPageId() {
|
function getPageId() {
|
||||||
if (window.location.hash) {
|
if (window.location.hash) {
|
||||||
|
@ -491,7 +508,7 @@ function hideThemeButtonState() {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
searchState.hideResults(search);
|
searchState.hideResults(search);
|
||||||
}
|
}
|
||||||
defocusSearchBar();
|
searchState.defocus();
|
||||||
hideThemeButtonState();
|
hideThemeButtonState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -518,7 +535,7 @@ function hideThemeButtonState() {
|
||||||
case "S":
|
case "S":
|
||||||
displayHelp(false, ev);
|
displayHelp(false, ev);
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
focusSearchBar();
|
searchState.focus();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "+":
|
case "+":
|
||||||
|
@ -605,10 +622,6 @@ function hideThemeButtonState() {
|
||||||
document.addEventListener("keypress", handleShortcut);
|
document.addEventListener("keypress", handleShortcut);
|
||||||
document.addEventListener("keydown", handleShortcut);
|
document.addEventListener("keydown", handleShortcut);
|
||||||
|
|
||||||
document.addEventListener("mousemove", function() {
|
|
||||||
searchState.mouseMovedAfterSearch = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
var handleSourceHighlight = (function() {
|
var handleSourceHighlight = (function() {
|
||||||
var prev_line_id = 0;
|
var prev_line_id = 0;
|
||||||
|
|
||||||
|
@ -789,6 +802,9 @@ function hideThemeButtonState() {
|
||||||
block("foreigntype", "Foreign Types");
|
block("foreigntype", "Foreign Types");
|
||||||
block("keyword", "Keywords");
|
block("keyword", "Keywords");
|
||||||
block("traitalias", "Trait Aliases");
|
block("traitalias", "Trait Aliases");
|
||||||
|
|
||||||
|
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
|
||||||
|
addSidebarCrates(window.ALL_CRATES);
|
||||||
};
|
};
|
||||||
|
|
||||||
window.register_implementors = function(imp) {
|
window.register_implementors = function(imp) {
|
||||||
|
@ -1420,13 +1436,6 @@ function hideThemeButtonState() {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
var params = searchState.getQueryStringParams();
|
|
||||||
if (params && params.search) {
|
|
||||||
var search = searchState.outputElement();
|
|
||||||
search.innerHTML = "<h3 style=\"text-align: center;\">" + searchState.loadingText + "</h3>";
|
|
||||||
searchState.showResults(search);
|
|
||||||
}
|
|
||||||
|
|
||||||
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
|
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
|
||||||
if (sidebar_menu) {
|
if (sidebar_menu) {
|
||||||
sidebar_menu.onclick = function() {
|
sidebar_menu.onclick = function() {
|
||||||
|
@ -1459,30 +1468,6 @@ function hideThemeButtonState() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSearchOptions(crates) {
|
|
||||||
var elem = document.getElementById("crate-search");
|
|
||||||
|
|
||||||
if (!elem) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var savedCrate = getSettingValue("saved-filter-crate");
|
|
||||||
for (var i = 0, len = crates.length; i < len; ++i) {
|
|
||||||
var option = document.createElement("option");
|
|
||||||
option.value = crates[i];
|
|
||||||
option.innerText = crates[i];
|
|
||||||
elem.appendChild(option);
|
|
||||||
// Set the crate filter from saved storage, if the current page has the saved crate
|
|
||||||
// filter.
|
|
||||||
//
|
|
||||||
// If not, ignore the crate filter -- we want to support filtering for crates on sites
|
|
||||||
// like doc.rust-lang.org where the crates may differ from page to page while on the
|
|
||||||
// same domain.
|
|
||||||
if (crates[i] === savedCrate) {
|
|
||||||
elem.value = savedCrate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function buildHelperPopup() {
|
function buildHelperPopup() {
|
||||||
var popup = document.createElement("aside");
|
var popup = document.createElement("aside");
|
||||||
addClass(popup, "hidden");
|
addClass(popup, "hidden");
|
||||||
|
@ -1547,7 +1532,7 @@ function hideThemeButtonState() {
|
||||||
|
|
||||||
onHashChange(null);
|
onHashChange(null);
|
||||||
window.onhashchange = onHashChange;
|
window.onhashchange = onHashChange;
|
||||||
searchState.setupLoader();
|
searchState.setup();
|
||||||
}());
|
}());
|
||||||
|
|
||||||
function copy_path(but) {
|
function copy_path(but) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue