1
+ "use strict" ;
2
+
1
3
window . searchState = {
2
4
timeout : null ,
3
5
inputElem : document . getElementById ( "search-input" ) ,
@@ -124,31 +126,16 @@ function toggleElements(filter, value) {
124
126
}
125
127
}
126
128
127
- function changeSetting ( elem ) {
128
- if ( elem . id === "disable-shortcuts" ) {
129
- disableShortcuts = elem . checked ;
130
- storeValue ( elem . id , elem . checked ) ;
131
- }
132
- }
133
-
134
129
function onEachLazy ( lazyArray , func ) {
135
130
const arr = Array . prototype . slice . call ( lazyArray ) ;
136
131
for ( const el of arr ) {
137
132
func ( el ) ;
138
133
}
139
134
}
140
135
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
-
148
136
function expandLint ( lintId ) {
149
137
const elem = document . querySelector ( `#${ lintId } > input[type="checkbox"]` ) ;
150
138
elem . checked = true ;
151
- highlightIfNeeded ( lintId ) ;
152
139
}
153
140
154
141
function lintAnchor ( event ) {
@@ -194,13 +181,9 @@ function handleBlur(event, elementId) {
194
181
}
195
182
196
183
function 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
+ }
204
187
}
205
188
206
189
// Returns the current URL without any query parameter or hash.
@@ -535,7 +518,7 @@ function parseURLFilters() {
535
518
for ( const [ corres_key , corres_value ] of Object . entries ( URL_PARAMS_CORRESPONDENCE ) ) {
536
519
if ( corres_value === key ) {
537
520
if ( key !== "versions" ) {
538
- const settings = new Set ( value . split ( "," ) ) ;
521
+ const settings = new Set ( value . split ( "," ) ) ;
539
522
onEachLazy ( document . querySelectorAll ( `#lint-${ key } ul input` ) , elem => {
540
523
elem . checked = settings . has ( elem . getAttribute ( "data-value" ) ) ;
541
524
updateFilter ( elem , corres_key , true ) ;
@@ -555,12 +538,60 @@ function parseURLFilters() {
555
538
}
556
539
}
557
540
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 ;
561
592
562
- document . addEventListener ( "keypress" , handleShortcut ) ;
563
- document . addEventListener ( "keydown" , handleShortcut ) ;
593
+ addListeners ( ) ;
594
+ highlightLazily ( ) ;
564
595
565
596
generateSettings ( ) ;
566
597
generateSearch ( ) ;
0 commit comments