@@ -80,10 +80,6 @@ function getSearchInput() {
80
80
return document . getElementsByClassName ( "search-input" ) [ 0 ] ;
81
81
}
82
82
83
- function getSearchElement ( ) {
84
- return document . getElementById ( "search" ) ;
85
- }
86
-
87
83
var THEME_PICKER_ELEMENT_ID = "theme-picker" ;
88
84
var THEMES_ELEMENT_ID = "theme-choices" ;
89
85
@@ -172,30 +168,123 @@ function hideThemeButtonState() {
172
168
( function ( ) {
173
169
"use strict" ;
174
170
175
- var disableShortcuts = getSettingValue ( "disable-shortcuts" ) === "true" ;
176
- window . search_input = getSearchInput ( ) ;
177
- var searchTimeout = null ;
178
- var toggleAllDocsId = "toggle-all-docs" ;
171
+ window . searchState = {
172
+ loadingText : "Loading search results..." ,
173
+ input : getSearchInput ( ) ,
174
+ outputElement : function ( ) {
175
+ return document . getElementById ( "search" ) ;
176
+ } ,
177
+ title : null ,
178
+ titleBeforeSearch : document . title ,
179
+ timeout : null ,
180
+ // On the search screen, so you remain on the last tab you opened.
181
+ //
182
+ // 0 for "In Names"
183
+ // 1 for "In Parameters"
184
+ // 2 for "In Return Types"
185
+ currentTab : 0 ,
186
+ mouseMovedAfterSearch : true ,
187
+ clearInputTimeout : function ( ) {
188
+ if ( searchState . timeout !== null ) {
189
+ clearTimeout ( searchState . timeout ) ;
190
+ searchState . timeout = null ;
191
+ }
192
+ } ,
193
+ showResults : function ( search ) {
194
+ if ( search === null || typeof search === 'undefined' ) {
195
+ search = searchState . outputElement ( ) ;
196
+ }
197
+ addClass ( main , "hidden" ) ;
198
+ removeClass ( search , "hidden" ) ;
199
+ searchState . mouseMovedAfterSearch = false ;
200
+ document . title = searchState . title ;
201
+ } ,
202
+ hideResults : function ( search ) {
203
+ if ( search === null || typeof search === 'undefined' ) {
204
+ search = searchState . outputElement ( ) ;
205
+ }
206
+ addClass ( search , "hidden" ) ;
207
+ removeClass ( main , "hidden" ) ;
208
+ document . title = searchState . titleBeforeSearch ;
209
+ // We also remove the query parameter from the URL.
210
+ if ( searchState . browserSupportsHistoryApi ( ) ) {
211
+ history . replaceState ( "" , window . currentCrate + " - Rust" ,
212
+ getNakedUrl ( ) + window . location . hash ) ;
213
+ }
214
+ } ,
215
+ getQueryStringParams : function ( ) {
216
+ var params = { } ;
217
+ window . location . search . substring ( 1 ) . split ( "&" ) .
218
+ map ( function ( s ) {
219
+ var pair = s . split ( "=" ) ;
220
+ params [ decodeURIComponent ( pair [ 0 ] ) ] =
221
+ typeof pair [ 1 ] === "undefined" ? null : decodeURIComponent ( pair [ 1 ] ) ;
222
+ } ) ;
223
+ return params ;
224
+ } ,
225
+ putBackSearch : function ( search_input ) {
226
+ var search = searchState . outputElement ( ) ;
227
+ if ( search_input . value !== "" && hasClass ( search , "hidden" ) ) {
228
+ searchState . showResults ( search ) ;
229
+ if ( searchState . browserSupportsHistoryApi ( ) ) {
230
+ var extra = "?search=" + encodeURIComponent ( search_input . value ) ;
231
+ history . replaceState ( search_input . value , "" ,
232
+ getNakedUrl ( ) + extra + window . location . hash ) ;
233
+ }
234
+ document . title = searchState . title ;
235
+ }
236
+ } ,
237
+ browserSupportsHistoryApi : function ( ) {
238
+ return window . history && typeof window . history . pushState === "function" ;
239
+ } ,
240
+ setupLoader : function ( ) {
241
+ function loadScript ( url ) {
242
+ var script = document . createElement ( 'script' ) ;
243
+ script . src = url ;
244
+ document . head . append ( script ) ;
245
+ }
179
246
180
- // On the search screen, so you remain on the last tab you opened.
181
- //
182
- // 0 for "In Names"
183
- // 1 for "In Parameters"
184
- // 2 for "In Return Types"
185
- window . currentTab = 0 ;
247
+ var searchLoaded = false ;
248
+ function loadSearch ( ) {
249
+ if ( ! searchLoaded ) {
250
+ searchLoaded = true ;
251
+ loadScript ( window . searchJS ) ;
252
+ }
253
+ }
186
254
187
- window . mouseMovedAfterSearch = true ;
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 ) ;
188
258
189
- var titleBeforeSearch = document . title ;
190
- window . searchTitle = null ;
259
+ searchState . input . addEventListener ( "focus" , function ( ) {
260
+ searchState . input . origPlaceholder = searchState . input . placeholder ;
261
+ searchState . input . placeholder = "Type your search here." ;
262
+ loadSearch ( ) ;
263
+ } ) ;
264
+ searchState . input . addEventListener ( "blur" , function ( ) {
265
+ searchState . input . placeholder = searchState . input . origPlaceholder ;
266
+ } ) ;
267
+ searchState . input . removeAttribute ( 'disabled' ) ;
191
268
192
- window . clearInputTimeout = function ( ) {
193
- if ( searchTimeout !== null ) {
194
- clearTimeout ( searchTimeout ) ;
195
- searchTimeout = null ;
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
+ }
275
+ var params = searchState . getQueryStringParams ( ) ;
276
+ if ( params . search !== undefined ) {
277
+ loadSearch ( ) ;
196
278
}
279
+ } ,
197
280
} ;
198
281
282
+ if ( searchState . input ) {
283
+ searchState . input . onfocus = function ( ) {
284
+ searchState . putBackSearch ( this ) ;
285
+ } ;
286
+ }
287
+
199
288
function getPageId ( ) {
200
289
if ( window . location . hash ) {
201
290
var tmp = window . location . hash . replace ( / ^ # / , "" ) ;
@@ -237,61 +326,23 @@ function hideThemeButtonState() {
237
326
document . getElementsByTagName ( "body" ) [ 0 ] . style . marginTop = "" ;
238
327
}
239
328
240
- window . showSearchResults = function ( search ) {
241
- if ( search === null || typeof search === 'undefined' ) {
242
- search = getSearchElement ( ) ;
243
- }
244
- addClass ( main , "hidden" ) ;
245
- removeClass ( search , "hidden" ) ;
246
- mouseMovedAfterSearch = false ;
247
- document . title = searchTitle ;
248
- } ;
249
-
250
- window . hideSearchResults = function ( search ) {
251
- if ( search === null || typeof search === 'undefined' ) {
252
- search = getSearchElement ( ) ;
253
- }
254
- addClass ( search , "hidden" ) ;
255
- removeClass ( main , "hidden" ) ;
256
- document . title = titleBeforeSearch ;
257
- // We also remove the query parameter from the URL.
258
- if ( browserSupportsHistoryApi ( ) ) {
259
- history . replaceState ( "" , window . currentCrate + " - Rust" ,
260
- getNakedUrl ( ) + window . location . hash ) ;
261
- }
262
- } ;
263
-
264
- window . getQueryStringParams = function ( ) {
265
- var params = { } ;
266
- window . location . search . substring ( 1 ) . split ( "&" ) .
267
- map ( function ( s ) {
268
- var pair = s . split ( "=" ) ;
269
- params [ decodeURIComponent ( pair [ 0 ] ) ] =
270
- typeof pair [ 1 ] === "undefined" ? null : decodeURIComponent ( pair [ 1 ] ) ;
271
- } ) ;
272
- return params ;
273
- } ;
274
-
275
- window . browserSupportsHistoryApi = function ( ) {
276
- return window . history && typeof window . history . pushState === "function" ;
277
- } ;
278
-
279
329
function isHidden ( elem ) {
280
330
return elem . offsetHeight === 0 ;
281
331
}
282
332
333
+ var toggleAllDocsId = "toggle-all-docs" ;
283
334
var main = document . getElementById ( "main" ) ;
284
335
var savedHash = "" ;
285
336
286
337
function handleHashes ( ev ) {
287
338
var elem ;
288
- var search = getSearchElement ( ) ;
339
+ var search = searchState . outputElement ( ) ;
289
340
if ( ev !== null && search && ! hasClass ( search , "hidden" ) && ev . newURL ) {
290
341
// This block occurs when clicking on an element in the navbar while
291
342
// in a search.
292
- hideSearchResults ( search ) ;
343
+ searchState . hideResults ( search ) ;
293
344
var hash = ev . newURL . slice ( ev . newURL . indexOf ( "#" ) + 1 ) ;
294
- if ( browserSupportsHistoryApi ( ) ) {
345
+ if ( searchState . browserSupportsHistoryApi ( ) ) {
295
346
// `window.location.search`` contains all the query parameters, not just `search`.
296
347
history . replaceState ( hash , "" ,
297
348
getNakedUrl ( ) + window . location . search + "#" + hash ) ;
@@ -432,18 +483,19 @@ function hideThemeButtonState() {
432
483
433
484
function handleEscape ( ev ) {
434
485
var help = getHelpElement ( false ) ;
435
- var search = getSearchElement ( ) ;
486
+ var search = searchState . outputElement ( ) ;
436
487
if ( hasClass ( help , "hidden" ) === false ) {
437
488
displayHelp ( false , ev , help ) ;
438
489
} else if ( hasClass ( search , "hidden" ) === false ) {
439
- clearInputTimeout ( ) ;
490
+ searchState . clearInputTimeout ( ) ;
440
491
ev . preventDefault ( ) ;
441
- hideSearchResults ( search ) ;
492
+ searchState . hideResults ( search ) ;
442
493
}
443
494
defocusSearchBar ( ) ;
444
495
hideThemeButtonState ( ) ;
445
496
}
446
497
498
+ var disableShortcuts = getSettingValue ( "disable-shortcuts" ) === "true" ;
447
499
function handleShortcut ( ev ) {
448
500
// Don't interfere with browser shortcuts
449
501
if ( ev . ctrlKey || ev . altKey || ev . metaKey || disableShortcuts === true ) {
@@ -553,15 +605,17 @@ function hideThemeButtonState() {
553
605
document . addEventListener ( "keypress" , handleShortcut ) ;
554
606
document . addEventListener ( "keydown" , handleShortcut ) ;
555
607
556
- document . addEventListener ( "mousemove" , function ( ) { mouseMovedAfterSearch = true ; } ) ;
608
+ document . addEventListener ( "mousemove" , function ( ) {
609
+ searchState . mouseMovedAfterSearch = true ;
610
+ } ) ;
557
611
558
612
var handleSourceHighlight = ( function ( ) {
559
613
var prev_line_id = 0 ;
560
614
561
615
var set_fragment = function ( name ) {
562
616
var x = window . scrollX ,
563
617
y = window . scrollY ;
564
- if ( browserSupportsHistoryApi ( ) ) {
618
+ if ( searchState . browserSupportsHistoryApi ( ) ) {
565
619
history . replaceState ( null , null , "#" + name ) ;
566
620
highlightSourceLines ( ) ;
567
621
} else {
@@ -1366,34 +1420,11 @@ function hideThemeButtonState() {
1366
1420
} ;
1367
1421
} ) ;
1368
1422
1369
- window . putBackSearch = function ( search_input ) {
1370
- var search = getSearchElement ( ) ;
1371
- if ( search_input . value !== "" && hasClass ( search , "hidden" ) ) {
1372
- showSearchResults ( search ) ;
1373
- if ( browserSupportsHistoryApi ( ) ) {
1374
- var extra = "?search=" + encodeURIComponent ( search_input . value ) ;
1375
- history . replaceState ( search_input . value , "" ,
1376
- getNakedUrl ( ) + extra + window . location . hash ) ;
1377
- }
1378
- document . title = searchTitle ;
1379
- }
1380
- } ;
1381
-
1382
- function getSearchLoadingText ( ) {
1383
- return "Loading search results..." ;
1384
- }
1385
-
1386
- if ( search_input ) {
1387
- search_input . onfocus = function ( ) {
1388
- putBackSearch ( this ) ;
1389
- } ;
1390
- }
1391
-
1392
- var params = getQueryStringParams ( ) ;
1423
+ var params = searchState . getQueryStringParams ( ) ;
1393
1424
if ( params && params . search ) {
1394
- var search = getSearchElement ( ) ;
1395
- search . innerHTML = "<h3 style=\"text-align: center;\">" + getSearchLoadingText ( ) + "</h3>" ;
1396
- showSearchResults ( search ) ;
1425
+ var search = searchState . outputElement ( ) ;
1426
+ search . innerHTML = "<h3 style=\"text-align: center;\">" + searchState . loadingText + "</h3>" ;
1427
+ searchState . showResults ( search ) ;
1397
1428
}
1398
1429
1399
1430
var sidebar_menu = document . getElementsByClassName ( "sidebar-menu" ) [ 0 ] ;
@@ -1509,55 +1540,14 @@ function hideThemeButtonState() {
1509
1540
container . appendChild ( div_infos ) ;
1510
1541
1511
1542
popup . appendChild ( container ) ;
1512
- insertAfter ( popup , getSearchElement ( ) ) ;
1543
+ insertAfter ( popup , searchState . outputElement ( ) ) ;
1513
1544
// So that it's only built once and then it'll do nothing when called!
1514
1545
buildHelperPopup = function ( ) { } ;
1515
1546
}
1516
1547
1517
- function loadScript ( url ) {
1518
- var script = document . createElement ( 'script' ) ;
1519
- script . src = url ;
1520
- document . head . append ( script ) ;
1521
- }
1522
-
1523
- function setupSearchLoader ( ) {
1524
- var searchLoaded = false ;
1525
- function loadSearch ( ) {
1526
- if ( ! searchLoaded ) {
1527
- searchLoaded = true ;
1528
- loadScript ( window . searchJS ) ;
1529
- }
1530
- }
1531
-
1532
- // `crates{version}.js` should always be loaded before this script, so we can use it safely.
1533
- addSearchOptions ( window . ALL_CRATES ) ;
1534
- addSidebarCrates ( window . ALL_CRATES ) ;
1535
-
1536
- search_input . addEventListener ( "focus" , function ( ) {
1537
- search_input . origPlaceholder = search_input . placeholder ;
1538
- search_input . placeholder = "Type your search here." ;
1539
- loadSearch ( ) ;
1540
- } ) ;
1541
- search_input . addEventListener ( "blur" , function ( ) {
1542
- search_input . placeholder = search_input . origPlaceholder ;
1543
- } ) ;
1544
- search_input . removeAttribute ( 'disabled' ) ;
1545
-
1546
- var crateSearchDropDown = document . getElementById ( "crate-search" ) ;
1547
- // `crateSearchDropDown` can be null in case there is only crate because in that case, the
1548
- // crate filter dropdown is removed.
1549
- if ( crateSearchDropDown ) {
1550
- crateSearchDropDown . addEventListener ( "focus" , loadSearch ) ;
1551
- }
1552
- var params = getQueryStringParams ( ) ;
1553
- if ( params . search !== undefined ) {
1554
- loadSearch ( ) ;
1555
- }
1556
- }
1557
-
1558
1548
onHashChange ( null ) ;
1559
1549
window . onhashchange = onHashChange ;
1560
- setupSearchLoader ( ) ;
1550
+ searchState . setupLoader ( ) ;
1561
1551
} ( ) ) ;
1562
1552
1563
1553
function copy_path ( but ) {
0 commit comments