@@ -138,6 +138,22 @@ function getSearchElement() {
138
138
}
139
139
}
140
140
141
+ function showSearchResults ( search ) {
142
+ if ( search === null || typeof search === 'undefined' ) {
143
+ search = getSearchElement ( ) ;
144
+ }
145
+ addClass ( main , "hidden" ) ;
146
+ removeClass ( search , "hidden" ) ;
147
+ }
148
+
149
+ function hideSearchResults ( search ) {
150
+ if ( search === null || typeof search === 'undefined' ) {
151
+ search = getSearchElement ( ) ;
152
+ }
153
+ addClass ( search , "hidden" ) ;
154
+ removeClass ( main , "hidden" ) ;
155
+ }
156
+
141
157
// used for special search precedence
142
158
var TY_PRIMITIVE = itemTypes . indexOf ( "primitive" ) ;
143
159
var TY_KEYWORD = itemTypes . indexOf ( "keyword" ) ;
@@ -169,8 +185,7 @@ function getSearchElement() {
169
185
if ( ev !== null && search && ! hasClass ( search , "hidden" ) && ev . newURL ) {
170
186
// This block occurs when clicking on an element in the navbar while
171
187
// in a search.
172
- addClass ( search , "hidden" ) ;
173
- removeClass ( main , "hidden" ) ;
188
+ hideSearchResults ( search ) ;
174
189
var hash = ev . newURL . slice ( ev . newURL . indexOf ( "#" ) + 1 ) ;
175
190
if ( browserSupportsHistoryApi ( ) ) {
176
191
history . replaceState ( hash , "" , "?search=#" + hash ) ;
@@ -331,8 +346,7 @@ function getSearchElement() {
331
346
displayHelp ( false , ev , help ) ;
332
347
} else if ( hasClass ( search , "hidden" ) === false ) {
333
348
ev . preventDefault ( ) ;
334
- addClass ( search , "hidden" ) ;
335
- removeClass ( main , "hidden" ) ;
349
+ hideSearchResults ( search ) ;
336
350
document . title = titleBeforeSearch ;
337
351
}
338
352
defocusSearchBar ( ) ;
@@ -390,8 +404,8 @@ function getSearchElement() {
390
404
return null ;
391
405
}
392
406
393
- document . onkeypress = handleShortcut ;
394
- document . onkeydown = handleShortcut ;
407
+ document . addEventListener ( "keypress" , handleShortcut ) ;
408
+ document . addEventListener ( "keydown" , handleShortcut ) ;
395
409
396
410
var handleSourceHighlight = ( function ( ) {
397
411
var prev_line_id = 0 ;
@@ -430,7 +444,7 @@ function getSearchElement() {
430
444
}
431
445
} ) ( ) ;
432
446
433
- document . onclick = function ( ev ) {
447
+ document . addEventListener ( "click" , function ( ev ) {
434
448
if ( hasClass ( ev . target , "collapse-toggle" ) ) {
435
449
collapseDocs ( ev . target , "toggle" ) ;
436
450
} else if ( hasClass ( ev . target . parentNode , "collapse-toggle" ) ) {
@@ -452,7 +466,7 @@ function getSearchElement() {
452
466
expandSection ( a . hash . replace ( / ^ # / , "" ) ) ;
453
467
}
454
468
}
455
- } ;
469
+ } ) ;
456
470
457
471
var x = document . getElementsByClassName ( "version-selector" ) ;
458
472
if ( x . length > 0 ) {
@@ -1264,8 +1278,7 @@ function getSearchElement() {
1264
1278
}
1265
1279
dst = dst [ 0 ] ;
1266
1280
if ( window . location . pathname === dst . pathname ) {
1267
- addClass ( getSearchElement ( ) , "hidden" ) ;
1268
- removeClass ( main , "hidden" ) ;
1281
+ hideSearchResults ( ) ;
1269
1282
document . location . href = dst . href ;
1270
1283
}
1271
1284
} ;
@@ -1340,8 +1353,6 @@ function getSearchElement() {
1340
1353
e . preventDefault ( ) ;
1341
1354
} else if ( e . which === 16 ) { // shift
1342
1355
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
1343
- } else if ( e . which === 27 ) { // escape
1344
- handleEscape ( e ) ;
1345
1356
} else if ( actives [ currentTab ] . length > 0 ) {
1346
1357
removeClass ( actives [ currentTab ] [ 0 ] , "highlighted" ) ;
1347
1358
}
@@ -1491,10 +1502,9 @@ function getSearchElement() {
1491
1502
"</div><div id=\"results\">" +
1492
1503
ret_others [ 0 ] + ret_in_args [ 0 ] + ret_returned [ 0 ] + "</div>" ;
1493
1504
1494
- addClass ( main , "hidden" ) ;
1495
1505
var search = getSearchElement ( ) ;
1496
- removeClass ( search , "hidden" ) ;
1497
1506
search . innerHTML = output ;
1507
+ showSearchResults ( search ) ;
1498
1508
var tds = search . getElementsByTagName ( "td" ) ;
1499
1509
var td_width = 0 ;
1500
1510
if ( tds . length > 0 ) {
@@ -1699,13 +1709,7 @@ function getSearchElement() {
1699
1709
if ( browserSupportsHistoryApi ( ) ) {
1700
1710
history . replaceState ( "" , window . currentCrate + " - Rust" , "?search=" ) ;
1701
1711
}
1702
- if ( hasClass ( main , "content" ) ) {
1703
- removeClass ( main , "hidden" ) ;
1704
- }
1705
- var search_c = getSearchElement ( ) ;
1706
- if ( hasClass ( search_c , "content" ) ) {
1707
- addClass ( search_c , "hidden" ) ;
1708
- }
1712
+ hideSearchResults ( ) ;
1709
1713
} else {
1710
1714
searchTimeout = setTimeout ( search , 500 ) ;
1711
1715
}
@@ -1718,6 +1722,10 @@ function getSearchElement() {
1718
1722
search ( ) ;
1719
1723
} ;
1720
1724
search_input . onchange = function ( e ) {
1725
+ if ( e . target !== document . activeElement ) {
1726
+ // To prevent doing anything when it's from a blur event.
1727
+ return ;
1728
+ }
1721
1729
// Do NOT e.preventDefault() here. It will prevent pasting.
1722
1730
clearTimeout ( searchTimeout ) ;
1723
1731
// zero-timeout necessary here because at the time of event handler execution the
@@ -1741,19 +1749,8 @@ function getSearchElement() {
1741
1749
// Store the previous <title> so we can revert back to it later.
1742
1750
var previousTitle = document . title ;
1743
1751
1744
- window . onpopstate = function ( e ) {
1752
+ window . addEventListener ( "popstate" , function ( e ) {
1745
1753
var params = getQueryStringParams ( ) ;
1746
- // When browsing back from search results the main page
1747
- // visibility must be reset.
1748
- if ( ! params . search ) {
1749
- if ( hasClass ( main , "content" ) ) {
1750
- removeClass ( main , "hidden" ) ;
1751
- }
1752
- var search_c = getSearchElement ( ) ;
1753
- if ( hasClass ( search_c , "content" ) ) {
1754
- addClass ( search_c , "hidden" ) ;
1755
- }
1756
- }
1757
1754
// Revert to the previous title manually since the History
1758
1755
// API ignores the title parameter.
1759
1756
document . title = previousTitle ;
@@ -1765,18 +1762,21 @@ function getSearchElement() {
1765
1762
// perform the search. This will empty the bar if there's
1766
1763
// nothing there, which lets you really go back to a
1767
1764
// previous state with nothing in the bar.
1768
- if ( params . search ) {
1765
+ if ( params . search && params . search . length > 0 ) {
1769
1766
search_input . value = params . search ;
1767
+ // Some browsers fire "onpopstate" for every page load
1768
+ // (Chrome), while others fire the event only when actually
1769
+ // popping a state (Firefox), which is why search() is
1770
+ // called both here and at the end of the startSearch()
1771
+ // function.
1772
+ search ( e ) ;
1770
1773
} else {
1771
1774
search_input . value = "" ;
1775
+ // When browsing back from search results the main page
1776
+ // visibility must be reset.
1777
+ hideSearchResults ( ) ;
1772
1778
}
1773
- // Some browsers fire "onpopstate" for every page load
1774
- // (Chrome), while others fire the event only when actually
1775
- // popping a state (Firefox), which is why search() is
1776
- // called both here and at the end of the startSearch()
1777
- // function.
1778
- search ( ) ;
1779
- } ;
1779
+ } ) ;
1780
1780
}
1781
1781
search ( ) ;
1782
1782
}
@@ -2522,9 +2522,9 @@ function getSearchElement() {
2522
2522
}
2523
2523
2524
2524
function putBackSearch ( search_input ) {
2525
- if ( search_input . value !== "" ) {
2526
- addClass ( main , "hidden" ) ;
2527
- removeClass ( getSearchElement ( ) , "hidden" ) ;
2525
+ var search = getSearchElement ( ) ;
2526
+ if ( search_input . value !== "" && hasClass ( search , "hidden" ) ) {
2527
+ showSearchResults ( search ) ;
2528
2528
if ( browserSupportsHistoryApi ( ) ) {
2529
2529
history . replaceState ( search_input . value ,
2530
2530
"" ,
@@ -2541,10 +2541,9 @@ function getSearchElement() {
2541
2541
2542
2542
var params = getQueryStringParams ( ) ;
2543
2543
if ( params && params . search ) {
2544
- addClass ( main , "hidden" ) ;
2545
2544
var search = getSearchElement ( ) ;
2546
- removeClass ( search , "hidden" ) ;
2547
2545
search . innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>" ;
2546
+ showSearchResults ( search ) ;
2548
2547
}
2549
2548
2550
2549
var sidebar_menu = document . getElementsByClassName ( "sidebar-menu" ) [ 0 ] ;
0 commit comments