diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index cb7408502eb..80d6997751a 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -76,10 +76,6 @@ function getVirtualKey(ev) { return String.fromCharCode(c); } -function getSearchInput() { - return document.getElementsByClassName("search-input")[0]; -} - var THEME_PICKER_ELEMENT_ID = "theme-picker"; var THEMES_ELEMENT_ID = "theme-choices"; @@ -96,16 +92,6 @@ 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(); -} - -// Removes the focus from the search bar. -function defocusSearchBar() { - getSearchInput().blur(); -} - function showThemeButtonState() { var themePicker = getThemePickerElement(); var themeChoices = getThemesElement(); @@ -170,7 +156,7 @@ function hideThemeButtonState() { window.searchState = { loadingText: "Loading search results...", - input: getSearchInput(), + input: document.getElementsByClassName("search-input")[0], outputElement: function() { return document.getElementById("search"); }, @@ -190,6 +176,14 @@ function hideThemeButtonState() { 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) { if (search === null || typeof search === 'undefined') { search = searchState.outputElement(); @@ -237,7 +231,11 @@ function hideThemeButtonState() { browserSupportsHistoryApi: 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) { var script = document.createElement('script'); 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. - addSearchOptions(window.ALL_CRATES); - addSidebarCrates(window.ALL_CRATES); - - searchState.input.addEventListener("focus", function() { - searchState.input.origPlaceholder = searchState.input.placeholder; - searchState.input.placeholder = "Type your search here."; + search_input.addEventListener("focus", function() { + searchState.putBackSearch(this); + search_input.origPlaceholder = searchState.input.placeholder; + search_input.placeholder = "Type your search here."; loadSearch(); }); - searchState.input.addEventListener("blur", function() { - searchState.input.placeholder = searchState.input.origPlaceholder; + search_input.addEventListener("blur", function() { + search_input.placeholder = searchState.input.origPlaceholder; }); - searchState.input.removeAttribute('disabled'); - var crateSearchDropDown = document.getElementById("crate-search"); - // `crateSearchDropDown` can be null in case there is only crate because in that case, the - // crate filter dropdown is removed. - if (crateSearchDropDown) { - crateSearchDropDown.addEventListener("focus", loadSearch); - } + document.addEventListener("mousemove", function() { + searchState.mouseMovedAfterSearch = true; + }); + + 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(); if (params.search !== undefined) { + var search = searchState.outputElement(); + search.innerHTML = "