@@ -381,56 +381,9 @@ function hideThemeButtonState() {
381381 }
382382 }
383383
384- function highlightSourceLines ( match , ev ) {
385- if ( typeof match === "undefined" ) {
386- // If we're in mobile mode, we should hide the sidebar in any case.
387- hideSidebar ( ) ;
388- match = window . location . hash . match ( / ^ # ? ( \d + ) (?: - ( \d + ) ) ? $ / ) ;
389- }
390- if ( ! match ) {
391- return ;
392- }
393- var from = parseInt ( match [ 1 ] , 10 ) ;
394- var to = from ;
395- if ( typeof match [ 2 ] !== "undefined" ) {
396- to = parseInt ( match [ 2 ] , 10 ) ;
397- }
398- if ( to < from ) {
399- var tmp = to ;
400- to = from ;
401- from = tmp ;
402- }
403- var elem = document . getElementById ( from ) ;
404- if ( ! elem ) {
405- return ;
406- }
407- if ( ! ev ) {
408- var x = document . getElementById ( from ) ;
409- if ( x ) {
410- x . scrollIntoView ( ) ;
411- }
412- }
413- onEachLazy ( document . getElementsByClassName ( "line-numbers" ) , function ( e ) {
414- onEachLazy ( e . getElementsByTagName ( "span" ) , function ( i_e ) {
415- removeClass ( i_e , "line-highlighted" ) ;
416- } ) ;
417- } ) ;
418- for ( var i = from ; i <= to ; ++ i ) {
419- elem = document . getElementById ( i ) ;
420- if ( ! elem ) {
421- break ;
422- }
423- addClass ( elem , "line-highlighted" ) ;
424- }
425- }
426-
427384 function onHashChange ( ev ) {
428385 // If we're in mobile mode, we should hide the sidebar in any case.
429386 hideSidebar ( ) ;
430- var match = window . location . hash . match ( / ^ # ? ( \d + ) (?: - ( \d + ) ) ? $ / ) ;
431- if ( match ) {
432- return highlightSourceLines ( match , ev ) ;
433- }
434387 handleHashes ( ev ) ;
435388 }
436389
@@ -585,78 +538,9 @@ function hideThemeButtonState() {
585538 }
586539 }
587540
588- function findParentElement ( elem , tagName ) {
589- do {
590- if ( elem && elem . tagName === tagName ) {
591- return elem ;
592- }
593- elem = elem . parentNode ;
594- } while ( elem ) ;
595- return null ;
596- }
597-
598541 document . addEventListener ( "keypress" , handleShortcut ) ;
599542 document . addEventListener ( "keydown" , handleShortcut ) ;
600543
601- var handleSourceHighlight = ( function ( ) {
602- var prev_line_id = 0 ;
603-
604- var set_fragment = function ( name ) {
605- var x = window . scrollX ,
606- y = window . scrollY ;
607- if ( searchState . browserSupportsHistoryApi ( ) ) {
608- history . replaceState ( null , null , "#" + name ) ;
609- highlightSourceLines ( ) ;
610- } else {
611- location . replace ( "#" + name ) ;
612- }
613- // Prevent jumps when selecting one or many lines
614- window . scrollTo ( x , y ) ;
615- } ;
616-
617- return function ( ev ) {
618- var cur_line_id = parseInt ( ev . target . id , 10 ) ;
619- ev . preventDefault ( ) ;
620-
621- if ( ev . shiftKey && prev_line_id ) {
622- // Swap selection if needed
623- if ( prev_line_id > cur_line_id ) {
624- var tmp = prev_line_id ;
625- prev_line_id = cur_line_id ;
626- cur_line_id = tmp ;
627- }
628-
629- set_fragment ( prev_line_id + "-" + cur_line_id ) ;
630- } else {
631- prev_line_id = cur_line_id ;
632-
633- set_fragment ( cur_line_id ) ;
634- }
635- } ;
636- } ( ) ) ;
637-
638- document . addEventListener ( "click" , function ( ev ) {
639- var helpElem = getHelpElement ( false ) ;
640- if ( hasClass ( ev . target , "help-button" ) ) {
641- displayHelp ( true , ev ) ;
642- } else if ( ev . target . tagName === "SPAN" && hasClass ( ev . target . parentNode , "line-numbers" ) ) {
643- handleSourceHighlight ( ev ) ;
644- } else if ( helpElem && hasClass ( helpElem , "hidden" ) === false ) {
645- var is_inside_help_popup = ev . target !== helpElem && helpElem . contains ( ev . target ) ;
646- if ( is_inside_help_popup === false ) {
647- addClass ( helpElem , "hidden" ) ;
648- removeClass ( document . body , "blur" ) ;
649- }
650- } else {
651- // Making a collapsed element visible on onhashchange seems
652- // too late
653- var a = findParentElement ( ev . target , "A" ) ;
654- if ( a && a . hash ) {
655- expandSection ( a . hash . replace ( / ^ # / , "" ) ) ;
656- }
657- }
658- } ) ;
659-
660544 ( function ( ) {
661545 var x = document . getElementsByClassName ( "version-selector" ) ;
662546 if ( x . length > 0 ) {
@@ -1121,6 +1005,27 @@ function hideThemeButtonState() {
11211005 } ) ;
11221006 } ( ) ) ;
11231007
1008+ function handleClick ( id , f ) {
1009+ var elem = document . getElementById ( id ) ;
1010+ if ( elem ) {
1011+ elem . addEventListener ( "click" , f ) ;
1012+ }
1013+ }
1014+ handleClick ( "help-button" , function ( ev ) {
1015+ displayHelp ( true , ev ) ;
1016+ } ) ;
1017+
1018+ onEachLazy ( document . getElementsByTagName ( "a" ) , function ( el ) {
1019+ // For clicks on internal links (<A> tags with a hash property), we expand the section we're
1020+ // jumping to *before* jumping there. We can't do this in onHashChange, because it changes
1021+ // the height of the document so we wind up scrolled to the wrong place.
1022+ if ( el . hash ) {
1023+ el . addEventListener ( "click" , function ( ) {
1024+ expandSection ( el . hash . slice ( 1 ) ) ;
1025+ } ) ;
1026+ }
1027+ } ) ;
1028+
11241029 onEachLazy ( document . getElementsByClassName ( "notable-traits" ) , function ( e ) {
11251030 e . onclick = function ( ) {
11261031 this . getElementsByClassName ( 'notable-traits-tooltiptext' ) [ 0 ]
@@ -1165,6 +1070,13 @@ function hideThemeButtonState() {
11651070 addClass ( popup , "hidden" ) ;
11661071 popup . id = "help" ;
11671072
1073+ popup . addEventListener ( "click" , function ( ev ) {
1074+ if ( ev . target === popup ) {
1075+ // Clicked the blurred zone outside the help popup; dismiss help.
1076+ displayHelp ( false , ev ) ;
1077+ }
1078+ } ) ;
1079+
11681080 var book_info = document . createElement ( "span" ) ;
11691081 book_info . innerHTML = "You can find more information in \
11701082 <a href=\"https://doc.rust-lang.org/rustdoc/\">the rustdoc book</a>." ;
@@ -1223,7 +1135,7 @@ function hideThemeButtonState() {
12231135 }
12241136
12251137 onHashChange ( null ) ;
1226- window . onhashchange = onHashChange ;
1138+ window . addEventListener ( "hashchange" , onHashChange ) ;
12271139 searchState . setup ( ) ;
12281140} ( ) ) ;
12291141
0 commit comments