1
- var darkThemes = [ "dark" , "ayu" ] ;
1
+ /* eslint-env es6 */
2
+ /* eslint no-var: "error" */
3
+ /* eslint prefer-const: "error" */
4
+
5
+ const darkThemes = [ "dark" , "ayu" ] ;
2
6
window . currentTheme = document . getElementById ( "themeStyle" ) ;
3
7
window . mainTheme = document . getElementById ( "mainThemeStyle" ) ;
4
8
5
- var settingsDataset = ( function ( ) {
6
- var settingsElement = document . getElementById ( "default-settings" ) ;
9
+ const settingsDataset = ( function ( ) {
10
+ const settingsElement = document . getElementById ( "default-settings" ) ;
7
11
if ( settingsElement === null ) {
8
12
return null ;
9
13
}
10
- var dataset = settingsElement . dataset ;
14
+ const dataset = settingsElement . dataset ;
11
15
if ( dataset === undefined ) {
12
16
return null ;
13
17
}
14
18
return dataset ;
15
19
} ) ( ) ;
16
20
17
21
function getSettingValue ( settingName ) {
18
- var current = getCurrentValue ( settingName ) ;
22
+ const current = getCurrentValue ( settingName ) ;
19
23
if ( current !== null ) {
20
24
return current ;
21
25
}
22
26
if ( settingsDataset !== null ) {
23
27
// See the comment for `default_settings.into_iter()` etc. in
24
28
// `Options::from_matches` in `librustdoc/config.rs`.
25
- var def = settingsDataset [ settingName . replace ( / - / g, '_' ) ] ;
29
+ const def = settingsDataset [ settingName . replace ( / - / g, '_' ) ] ;
26
30
if ( def !== undefined ) {
27
31
return def ;
28
32
}
29
33
}
30
34
return null ;
31
35
}
32
36
33
- var localStoredTheme = getSettingValue ( "theme" ) ;
37
+ const localStoredTheme = getSettingValue ( "theme" ) ;
34
38
35
- var savedHref = [ ] ;
39
+ const savedHref = [ ] ;
36
40
37
41
// eslint-disable-next-line no-unused-vars
38
42
function hasClass ( elem , className ) {
@@ -63,17 +67,16 @@ function removeClass(elem, className) {
63
67
*/
64
68
function onEach ( arr , func , reversed ) {
65
69
if ( arr && arr . length > 0 && func ) {
66
- var length = arr . length ;
67
- var i ;
68
70
if ( reversed ) {
69
- for ( i = length - 1 ; i >= 0 ; -- i ) {
71
+ const length = arr . length ;
72
+ for ( let i = length - 1 ; i >= 0 ; -- i ) {
70
73
if ( func ( arr [ i ] ) ) {
71
74
return true ;
72
75
}
73
76
}
74
77
} else {
75
- for ( i = 0 ; i < length ; ++ i ) {
76
- if ( func ( arr [ i ] ) ) {
78
+ for ( const elem of arr ) {
79
+ if ( func ( elem ) ) {
77
80
return true ;
78
81
}
79
82
}
@@ -121,7 +124,7 @@ function getCurrentValue(name) {
121
124
}
122
125
123
126
function switchTheme ( styleElem , mainStyleElem , newTheme , saveTheme ) {
124
- var newHref = mainStyleElem . href . replace (
127
+ const newHref = mainStyleElem . href . replace (
125
128
/ \/ r u s t d o c ( [ ^ / ] * ) \. c s s / , "/" + newTheme + "$1" + ".css" ) ;
126
129
127
130
// If this new value comes from a system setting or from the previously
@@ -134,7 +137,7 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
134
137
return ;
135
138
}
136
139
137
- var found = false ;
140
+ let found = false ;
138
141
if ( savedHref . length === 0 ) {
139
142
onEachLazy ( document . getElementsByTagName ( "link" ) , function ( el ) {
140
143
savedHref . push ( el . href ) ;
@@ -161,17 +164,17 @@ function useSystemTheme(value) {
161
164
updateLocalStorage ( "use-system-theme" , value ) ;
162
165
163
166
// update the toggle if we're on the settings page
164
- var toggle = document . getElementById ( "use-system-theme" ) ;
167
+ const toggle = document . getElementById ( "use-system-theme" ) ;
165
168
if ( toggle && toggle instanceof HTMLInputElement ) {
166
169
toggle . checked = value ;
167
170
}
168
171
}
169
172
170
- var updateSystemTheme = ( function ( ) {
173
+ const updateSystemTheme = ( function ( ) {
171
174
if ( ! window . matchMedia ) {
172
175
// fallback to the CSS computed value
173
176
return function ( ) {
174
- var cssTheme = getComputedStyle ( document . documentElement )
177
+ const cssTheme = getComputedStyle ( document . documentElement )
175
178
. getPropertyValue ( 'content' ) ;
176
179
177
180
switchTheme (
@@ -184,16 +187,16 @@ var updateSystemTheme = (function() {
184
187
}
185
188
186
189
// only listen to (prefers-color-scheme: dark) because light is the default
187
- var mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
190
+ const mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
188
191
189
192
function handlePreferenceChange ( mql ) {
190
- let use = function ( theme ) {
193
+ const use = function ( theme ) {
191
194
switchTheme ( window . currentTheme , window . mainTheme , theme , true ) ;
192
195
} ;
193
196
// maybe the user has disabled the setting in the meantime!
194
197
if ( getSettingValue ( "use-system-theme" ) !== "false" ) {
195
- var lightTheme = getSettingValue ( "preferred-light-theme" ) || "light" ;
196
- var darkTheme = getSettingValue ( "preferred-dark-theme" ) || "dark" ;
198
+ const lightTheme = getSettingValue ( "preferred-light-theme" ) || "light" ;
199
+ const darkTheme = getSettingValue ( "preferred-dark-theme" ) || "dark" ;
197
200
198
201
if ( mql . matches ) {
199
202
use ( darkTheme ) ;
0 commit comments