@@ -42,6 +42,7 @@ if (!DOMTokenList.prototype.remove) {
42
42
if ( rustdocVars ) {
43
43
window . rootPath = rustdocVars . attributes [ "data-root-path" ] . value ;
44
44
window . currentCrate = rustdocVars . attributes [ "data-current-crate" ] . value ;
45
+ window . searchJS = rustdocVars . attributes [ "data-search-js" ] . value ;
45
46
}
46
47
var sidebarVars = document . getElementById ( "sidebar-vars" ) ;
47
48
if ( sidebarVars ) {
@@ -1922,8 +1923,8 @@ function defocusSearchBar() {
1922
1923
return searchWords ;
1923
1924
}
1924
1925
1925
- function startSearch ( ) {
1926
- var callback = function ( ) {
1926
+ function registerSearchEvents ( ) {
1927
+ var searchAfter500ms = function ( ) {
1927
1928
clearInputTimeout ( ) ;
1928
1929
if ( search_input . value . length === 0 ) {
1929
1930
if ( browserSupportsHistoryApi ( ) ) {
@@ -1935,8 +1936,8 @@ function defocusSearchBar() {
1935
1936
searchTimeout = setTimeout ( search , 500 ) ;
1936
1937
}
1937
1938
} ;
1938
- search_input . onkeyup = callback ;
1939
- search_input . oninput = callback ;
1939
+ search_input . onkeyup = searchAfter500ms ;
1940
+ search_input . oninput = searchAfter500ms ;
1940
1941
document . getElementsByClassName ( "search-form" ) [ 0 ] . onsubmit = function ( e ) {
1941
1942
e . preventDefault ( ) ;
1942
1943
clearInputTimeout ( ) ;
@@ -1999,7 +2000,6 @@ function defocusSearchBar() {
1999
2000
}
2000
2001
} ) ;
2001
2002
}
2002
- search ( ) ;
2003
2003
2004
2004
// This is required in firefox to avoid this problem: Navigating to a search result
2005
2005
// with the keyboard, hitting enter, and then hitting back would take you back to
@@ -2017,8 +2017,14 @@ function defocusSearchBar() {
2017
2017
}
2018
2018
2019
2019
index = buildIndex ( rawSearchIndex ) ;
2020
- startSearch ( ) ;
2020
+ registerSearchEvents ( ) ;
2021
+ // If there's a search term in the URL, execute the search now.
2022
+ if ( getQueryStringParams ( ) . search ) {
2023
+ search ( ) ;
2024
+ }
2025
+ } ;
2021
2026
2027
+ function addSidebarCrates ( crates ) {
2022
2028
// Draw a convenient sidebar of known crates if we have a listing
2023
2029
if ( window . rootPath === "../" || window . rootPath === "./" ) {
2024
2030
var sidebar = document . getElementsByClassName ( "sidebar-elems" ) [ 0 ] ;
@@ -2029,24 +2035,13 @@ function defocusSearchBar() {
2029
2035
var ul = document . createElement ( "ul" ) ;
2030
2036
div . appendChild ( ul ) ;
2031
2037
2032
- var crates = [ ] ;
2033
- for ( var crate in rawSearchIndex ) {
2034
- if ( ! hasOwnProperty ( rawSearchIndex , crate ) ) {
2035
- continue ;
2036
- }
2037
- crates . push ( crate ) ;
2038
- }
2039
- crates . sort ( ) ;
2040
2038
for ( var i = 0 ; i < crates . length ; ++ i ) {
2041
2039
var klass = "crate" ;
2042
2040
if ( window . rootPath !== "./" && crates [ i ] === window . currentCrate ) {
2043
2041
klass += " current" ;
2044
2042
}
2045
2043
var link = document . createElement ( "a" ) ;
2046
2044
link . href = window . rootPath + crates [ i ] + "/index.html" ;
2047
- // The summary in the search index has HTML, so we need to
2048
- // dynamically render it as plaintext.
2049
- link . title = convertHTMLToPlaintext ( rawSearchIndex [ crates [ i ] ] . doc ) ;
2050
2045
link . className = klass ;
2051
2046
link . textContent = crates [ i ] ;
2052
2047
@@ -2057,7 +2052,7 @@ function defocusSearchBar() {
2057
2052
sidebar . appendChild ( div ) ;
2058
2053
}
2059
2054
}
2060
- } ;
2055
+ }
2061
2056
2062
2057
/**
2063
2058
* Convert HTML to plaintext:
@@ -2862,45 +2857,26 @@ function defocusSearchBar() {
2862
2857
}
2863
2858
}
2864
2859
2865
- window . addSearchOptions = function ( crates ) {
2860
+ function addSearchOptions ( crates ) {
2866
2861
var elem = document . getElementById ( "crate-search" ) ;
2867
2862
2868
2863
if ( ! elem ) {
2869
2864
enableSearchInput ( ) ;
2870
2865
return ;
2871
2866
}
2872
- var crates_text = [ ] ;
2873
- if ( Object . keys ( crates ) . length > 1 ) {
2874
- for ( var crate in crates ) {
2875
- if ( hasOwnProperty ( crates , crate ) ) {
2876
- crates_text . push ( crate ) ;
2877
- }
2878
- }
2879
- }
2880
- crates_text . sort ( function ( a , b ) {
2881
- var lower_a = a . toLowerCase ( ) ;
2882
- var lower_b = b . toLowerCase ( ) ;
2883
-
2884
- if ( lower_a < lower_b ) {
2885
- return - 1 ;
2886
- } else if ( lower_a > lower_b ) {
2887
- return 1 ;
2888
- }
2889
- return 0 ;
2890
- } ) ;
2891
2867
var savedCrate = getSettingValue ( "saved-filter-crate" ) ;
2892
- for ( var i = 0 , len = crates_text . length ; i < len ; ++ i ) {
2868
+ for ( var i = 0 , len = crates . length ; i < len ; ++ i ) {
2893
2869
var option = document . createElement ( "option" ) ;
2894
- option . value = crates_text [ i ] ;
2895
- option . innerText = crates_text [ i ] ;
2870
+ option . value = crates [ i ] ;
2871
+ option . innerText = crates [ i ] ;
2896
2872
elem . appendChild ( option ) ;
2897
2873
// Set the crate filter from saved storage, if the current page has the saved crate
2898
2874
// filter.
2899
2875
//
2900
2876
// If not, ignore the crate filter -- we want to support filtering for crates on sites
2901
2877
// like doc.rust-lang.org where the crates may differ from page to page while on the
2902
2878
// same domain.
2903
- if ( crates_text [ i ] === savedCrate ) {
2879
+ if ( crates [ i ] === savedCrate ) {
2904
2880
elem . value = savedCrate ;
2905
2881
}
2906
2882
}
@@ -2969,6 +2945,44 @@ function defocusSearchBar() {
2969
2945
buildHelperPopup = function ( ) { } ;
2970
2946
}
2971
2947
2948
+ function loadScript ( url ) {
2949
+ var script = document . createElement ( 'script' ) ;
2950
+ script . src = url ;
2951
+ document . head . append ( script ) ;
2952
+ }
2953
+
2954
+ function setupSearchLoader ( ) {
2955
+ var searchLoaded = false ;
2956
+ function loadSearch ( ) {
2957
+ if ( ! searchLoaded ) {
2958
+ searchLoaded = true ;
2959
+ loadScript ( window . searchJS ) ;
2960
+ }
2961
+ }
2962
+
2963
+ // `crates{version}.js` should always be loaded before this script, so we can use it safely.
2964
+ addSearchOptions ( window . ALL_CRATES ) ;
2965
+ addSidebarCrates ( window . ALL_CRATES ) ;
2966
+
2967
+ search_input . addEventListener ( "focus" , function ( ) {
2968
+ search_input . origPlaceholder = search_input . placeholder ;
2969
+ search_input . placeholder = "Type your search here." ;
2970
+ loadSearch ( ) ;
2971
+ } ) ;
2972
+ search_input . addEventListener ( "blur" , function ( ) {
2973
+ search_input . placeholder = search_input . origPlaceholder ;
2974
+ } ) ;
2975
+ enableSearchInput ( ) ;
2976
+
2977
+ var crateSearchDropDown = document . getElementById ( "crate-search" ) ;
2978
+ crateSearchDropDown . addEventListener ( "focus" , loadSearch ) ;
2979
+ var params = getQueryStringParams ( ) ;
2980
+ if ( params . search !== undefined ) {
2981
+ loadSearch ( ) ;
2982
+ }
2983
+ }
2984
+
2972
2985
onHashChange ( null ) ;
2973
2986
window . onhashchange = onHashChange ;
2987
+ setupSearchLoader ( ) ;
2974
2988
} ( ) ) ;
0 commit comments