@@ -76,10 +76,6 @@ function getVirtualKey(ev) {
76
76
return String . fromCharCode ( c ) ;
77
77
}
78
78
79
- function getSearchInput ( ) {
80
- return document . getElementsByClassName ( "search-input" ) [ 0 ] ;
81
- }
82
-
83
79
var THEME_PICKER_ELEMENT_ID = "theme-picker" ;
84
80
var THEMES_ELEMENT_ID = "theme-choices" ;
85
81
@@ -96,16 +92,6 @@ function getNakedUrl() {
96
92
return window . location . href . split ( "?" ) [ 0 ] . split ( "#" ) [ 0 ] ;
97
93
}
98
94
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
-
109
95
function showThemeButtonState ( ) {
110
96
var themePicker = getThemePickerElement ( ) ;
111
97
var themeChoices = getThemesElement ( ) ;
@@ -170,7 +156,7 @@ function hideThemeButtonState() {
170
156
171
157
window . searchState = {
172
158
loadingText : "Loading search results..." ,
173
- input : getSearchInput ( ) ,
159
+ input : document . getElementsByClassName ( "search-input" ) [ 0 ] ,
174
160
outputElement : function ( ) {
175
161
return document . getElementById ( "search" ) ;
176
162
} ,
@@ -190,6 +176,14 @@ function hideThemeButtonState() {
190
176
searchState . timeout = null ;
191
177
}
192
178
} ,
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
+ } ,
193
187
showResults : function ( search ) {
194
188
if ( search === null || typeof search === 'undefined' ) {
195
189
search = searchState . outputElement ( ) ;
@@ -237,7 +231,11 @@ function hideThemeButtonState() {
237
231
browserSupportsHistoryApi : function ( ) {
238
232
return window . history && typeof window . history . pushState === "function" ;
239
233
} ,
240
- setupLoader : function ( ) {
234
+ setup : function ( ) {
235
+ var search_input = searchState . input ;
236
+ if ( ! searchState . input ) {
237
+ return ;
238
+ }
241
239
function loadScript ( url ) {
242
240
var script = document . createElement ( 'script' ) ;
243
241
script . src = url ;
@@ -252,38 +250,57 @@ function hideThemeButtonState() {
252
250
}
253
251
}
254
252
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." ;
262
257
loadSearch ( ) ;
263
258
} ) ;
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 ;
266
261
} ) ;
267
- searchState . input . removeAttribute ( 'disabled' ) ;
268
262
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 ) ;
275
271
var params = searchState . getQueryStringParams ( ) ;
276
272
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 ) ;
277
277
loadSearch ( ) ;
278
278
}
279
279
} ,
280
- } ;
280
+ addCrateDropdown : function ( crates ) {
281
+ var elem = document . getElementById ( "crate-search" ) ;
281
282
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
+ } ;
287
304
288
305
function getPageId ( ) {
289
306
if ( window . location . hash ) {
@@ -491,7 +508,7 @@ function hideThemeButtonState() {
491
508
ev . preventDefault ( ) ;
492
509
searchState . hideResults ( search ) ;
493
510
}
494
- defocusSearchBar ( ) ;
511
+ searchState . defocus ( ) ;
495
512
hideThemeButtonState ( ) ;
496
513
}
497
514
@@ -518,7 +535,7 @@ function hideThemeButtonState() {
518
535
case "S" :
519
536
displayHelp ( false , ev ) ;
520
537
ev . preventDefault ( ) ;
521
- focusSearchBar ( ) ;
538
+ searchState . focus ( ) ;
522
539
break ;
523
540
524
541
case "+" :
@@ -605,10 +622,6 @@ function hideThemeButtonState() {
605
622
document . addEventListener ( "keypress" , handleShortcut ) ;
606
623
document . addEventListener ( "keydown" , handleShortcut ) ;
607
624
608
- document . addEventListener ( "mousemove" , function ( ) {
609
- searchState . mouseMovedAfterSearch = true ;
610
- } ) ;
611
-
612
625
var handleSourceHighlight = ( function ( ) {
613
626
var prev_line_id = 0 ;
614
627
@@ -789,6 +802,9 @@ function hideThemeButtonState() {
789
802
block ( "foreigntype" , "Foreign Types" ) ;
790
803
block ( "keyword" , "Keywords" ) ;
791
804
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 ) ;
792
808
} ;
793
809
794
810
window . register_implementors = function ( imp ) {
@@ -1420,13 +1436,6 @@ function hideThemeButtonState() {
1420
1436
} ;
1421
1437
} ) ;
1422
1438
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
-
1430
1439
var sidebar_menu = document . getElementsByClassName ( "sidebar-menu" ) [ 0 ] ;
1431
1440
if ( sidebar_menu ) {
1432
1441
sidebar_menu . onclick = function ( ) {
@@ -1459,30 +1468,6 @@ function hideThemeButtonState() {
1459
1468
} ) ;
1460
1469
}
1461
1470
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
-
1486
1471
function buildHelperPopup ( ) {
1487
1472
var popup = document . createElement ( "aside" ) ;
1488
1473
addClass ( popup , "hidden" ) ;
@@ -1547,7 +1532,7 @@ function hideThemeButtonState() {
1547
1532
1548
1533
onHashChange ( null ) ;
1549
1534
window . onhashchange = onHashChange ;
1550
- searchState . setupLoader ( ) ;
1535
+ searchState . setup ( ) ;
1551
1536
} ( ) ) ;
1552
1537
1553
1538
function copy_path ( but ) {
0 commit comments