1+ "use strict" ;
2+
13window . searchState = {
24 timeout : null ,
35 inputElem : document . getElementById ( "search-input" ) ,
@@ -124,31 +126,16 @@ function toggleElements(filter, value) {
124126 }
125127}
126128
127- function changeSetting ( elem ) {
128- if ( elem . id === "disable-shortcuts" ) {
129- disableShortcuts = elem . checked ;
130- storeValue ( elem . id , elem . checked ) ;
131- }
132- }
133-
134129function onEachLazy ( lazyArray , func ) {
135130 const arr = Array . prototype . slice . call ( lazyArray ) ;
136131 for ( const el of arr ) {
137132 func ( el ) ;
138133 }
139134}
140135
141- function highlightIfNeeded ( lintId ) {
142- onEachLazy ( document . querySelectorAll ( `#${ lintId } pre > code:not(.hljs)` ) , el => {
143- hljs . highlightElement ( el . parentElement )
144- el . classList . add ( "highlighted" ) ;
145- } ) ;
146- }
147-
148136function expandLint ( lintId ) {
149137 const elem = document . querySelector ( `#${ lintId } > input[type="checkbox"]` ) ;
150138 elem . checked = true ;
151- highlightIfNeeded ( lintId ) ;
152139}
153140
154141function lintAnchor ( event ) {
@@ -194,13 +181,9 @@ function handleBlur(event, elementId) {
194181}
195182
196183function toggleExpansion ( expand ) {
197- onEachLazy (
198- document . querySelectorAll ( "article" ) ,
199- expand ? el => {
200- el . classList . remove ( "collapsed" ) ;
201- highlightIfNeeded ( el ) ;
202- } : el => el . classList . add ( "collapsed" ) ,
203- ) ;
184+ for ( const checkbox of document . querySelectorAll ( "article input[type=checkbox]" ) ) {
185+ checkbox . checked = expand ;
186+ }
204187}
205188
206189// Returns the current URL without any query parameter or hash.
@@ -535,7 +518,7 @@ function parseURLFilters() {
535518 for ( const [ corres_key , corres_value ] of Object . entries ( URL_PARAMS_CORRESPONDENCE ) ) {
536519 if ( corres_value === key ) {
537520 if ( key !== "versions" ) {
538- const settings = new Set ( value . split ( "," ) ) ;
521+ const settings = new Set ( value . split ( "," ) ) ;
539522 onEachLazy ( document . querySelectorAll ( `#lint-${ key } ul input` ) , elem => {
540523 elem . checked = settings . has ( elem . getAttribute ( "data-value" ) ) ;
541524 updateFilter ( elem , corres_key , true ) ;
@@ -555,12 +538,60 @@ function parseURLFilters() {
555538 }
556539}
557540
558- document . getElementById ( `theme-choice` ) . value = loadValue ( "theme" ) ;
559- let disableShortcuts = loadValue ( 'disable-shortcuts' ) === "true" ;
560- document . getElementById ( "disable-shortcuts" ) . checked = disableShortcuts ;
541+ function addListeners ( ) {
542+ disableShortcutsButton . addEventListener ( "change" , ( ) => {
543+ disableShortcuts = disableShortcutsButton . checked ;
544+ storeValue ( "disable-shortcuts" , disableShortcuts ) ;
545+ } ) ;
546+
547+ document . getElementById ( "expand-all" ) . addEventListener ( "click" , ( ) => toggleExpansion ( true ) ) ;
548+ document . getElementById ( "collapse-all" ) . addEventListener ( "click" , ( ) => toggleExpansion ( false ) ) ;
549+
550+ // A delegated listener to avoid the upfront cost of >1000 listeners
551+ document . addEventListener ( "click" , event => {
552+ if ( ! event . target instanceof HTMLAnchorElement ) {
553+ return ;
554+ }
555+
556+ if ( event . target . classList . contains ( "lint-anchor" ) ) {
557+ lintAnchor ( event ) ;
558+ } else if ( event . target . classList . contains ( "copy-to-clipboard" ) ) {
559+ copyToClipboard ( event ) ;
560+ }
561+ } ) ;
562+
563+ document . addEventListener ( "keypress" , handleShortcut ) ;
564+ document . addEventListener ( "keydown" , handleShortcut ) ;
565+ }
566+
567+ // Highlight code blocks only when they approach the viewport so that clicking the "Expand All"
568+ // button doesn't take a long time
569+ function highlightLazily ( ) {
570+ if ( ! 'IntersectionObserver' in window ) {
571+ return ;
572+ }
573+ const observer = new IntersectionObserver ( ( entries ) => {
574+ for ( const entry of entries ) {
575+ if ( entry . isIntersecting ) {
576+ observer . unobserve ( entry . target ) ;
577+ for ( const code of entry . target . querySelectorAll ( "pre code" ) ) {
578+ hljs . highlightElement ( code ) ;
579+ }
580+ }
581+ }
582+ } ) ;
583+ for ( const docs of document . querySelectorAll ( ".lint-docs" ) ) {
584+ observer . observe ( docs ) ;
585+ }
586+ }
587+
588+ let disableShortcuts = loadValue ( "disable-shortcuts" ) === "true" ;
589+
590+ const disableShortcutsButton = document . getElementById ( "disable-shortcuts" ) ;
591+ disableShortcutsButton . checked = disableShortcuts ;
561592
562- document . addEventListener ( "keypress" , handleShortcut ) ;
563- document . addEventListener ( "keydown" , handleShortcut ) ;
593+ addListeners ( ) ;
594+ highlightLazily ( ) ;
564595
565596generateSettings ( ) ;
566597generateSearch ( ) ;
0 commit comments