2
2
// Local js definitions:
3
3
/* global addClass, getSettingValue, hasClass */
4
4
/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
5
- /* global hideThemeButtonState, showThemeButtonState */
5
+ /* global switchTheme, useSystemTheme */
6
6
7
7
if ( ! String . prototype . startsWith ) {
8
8
String . prototype . startsWith = function ( searchString , position ) {
@@ -85,12 +85,15 @@ function getSearchElement() {
85
85
return document . getElementById ( "search" ) ;
86
86
}
87
87
88
+ var THEME_PICKER_ELEMENT_ID = "theme-picker" ;
89
+ var THEMES_ELEMENT_ID = "theme-choices" ;
90
+
88
91
function getThemesElement ( ) {
89
- return document . getElementById ( "theme-choices" ) ;
92
+ return document . getElementById ( THEMES_ELEMENT_ID ) ;
90
93
}
91
94
92
95
function getThemePickerElement ( ) {
93
- return document . getElementById ( "theme-picker" ) ;
96
+ return document . getElementById ( THEME_PICKER_ELEMENT_ID ) ;
94
97
}
95
98
96
99
// Returns the current URL without any query parameter or hash.
@@ -108,6 +111,65 @@ function defocusSearchBar() {
108
111
getSearchInput ( ) . blur ( ) ;
109
112
}
110
113
114
+ function showThemeButtonState ( ) {
115
+ var themePicker = getThemePickerElement ( ) ;
116
+ var themeChoices = getThemesElement ( ) ;
117
+
118
+ themeChoices . style . display = "block" ;
119
+ themePicker . style . borderBottomRightRadius = "0" ;
120
+ themePicker . style . borderBottomLeftRadius = "0" ;
121
+ }
122
+
123
+ function hideThemeButtonState ( ) {
124
+ var themePicker = getThemePickerElement ( ) ;
125
+ var themeChoices = getThemesElement ( ) ;
126
+
127
+ themeChoices . style . display = "none" ;
128
+ themePicker . style . borderBottomRightRadius = "3px" ;
129
+ themePicker . style . borderBottomLeftRadius = "3px" ;
130
+ }
131
+
132
+ // Set up the theme picker list.
133
+ ( function ( ) {
134
+ var themeChoices = getThemesElement ( ) ;
135
+ var themePicker = getThemePickerElement ( ) ;
136
+ var availableThemes /* INSERT THEMES HERE */ ;
137
+
138
+ function switchThemeButtonState ( ) {
139
+ if ( themeChoices . style . display === "block" ) {
140
+ hideThemeButtonState ( ) ;
141
+ } else {
142
+ showThemeButtonState ( ) ;
143
+ }
144
+ }
145
+
146
+ function handleThemeButtonsBlur ( e ) {
147
+ var active = document . activeElement ;
148
+ var related = e . relatedTarget ;
149
+
150
+ if ( active . id !== THEME_PICKER_ELEMENT_ID &&
151
+ ( ! active . parentNode || active . parentNode . id !== THEMES_ELEMENT_ID ) &&
152
+ ( ! related ||
153
+ ( related . id !== THEME_PICKER_ELEMENT_ID &&
154
+ ( ! related . parentNode || related . parentNode . id !== THEMES_ELEMENT_ID ) ) ) ) {
155
+ hideThemeButtonState ( ) ;
156
+ }
157
+ }
158
+
159
+ themePicker . onclick = switchThemeButtonState ;
160
+ themePicker . onblur = handleThemeButtonsBlur ;
161
+ availableThemes . forEach ( function ( item ) {
162
+ var but = document . createElement ( "button" ) ;
163
+ but . textContent = item ;
164
+ but . onclick = function ( ) {
165
+ switchTheme ( window . currentTheme , window . mainTheme , item , true ) ;
166
+ useSystemTheme ( false ) ;
167
+ } ;
168
+ but . onblur = handleThemeButtonsBlur ;
169
+ themeChoices . appendChild ( but ) ;
170
+ } ) ;
171
+ } ( ) ) ;
172
+
111
173
( function ( ) {
112
174
"use strict" ;
113
175
@@ -461,8 +523,7 @@ function defocusSearchBar() {
461
523
break ;
462
524
463
525
default :
464
- var themePicker = getThemePickerElement ( ) ;
465
- if ( themePicker . parentNode . contains ( ev . target ) ) {
526
+ if ( getThemePickerElement ( ) . parentNode . contains ( ev . target ) ) {
466
527
handleThemeKeyDown ( ev ) ;
467
528
}
468
529
}
@@ -475,7 +536,7 @@ function defocusSearchBar() {
475
536
switch ( getVirtualKey ( ev ) ) {
476
537
case "ArrowUp" :
477
538
ev . preventDefault ( ) ;
478
- if ( active . previousElementSibling && ev . target . id !== "theme-picker" ) {
539
+ if ( active . previousElementSibling && ev . target . id !== THEME_PICKER_ELEMENT_ID ) {
479
540
active . previousElementSibling . focus ( ) ;
480
541
} else {
481
542
showThemeButtonState ( ) ;
@@ -484,7 +545,7 @@ function defocusSearchBar() {
484
545
break ;
485
546
case "ArrowDown" :
486
547
ev . preventDefault ( ) ;
487
- if ( active . nextElementSibling && ev . target . id !== "theme-picker" ) {
548
+ if ( active . nextElementSibling && ev . target . id !== THEME_PICKER_ELEMENT_ID ) {
488
549
active . nextElementSibling . focus ( ) ;
489
550
} else {
490
551
showThemeButtonState ( ) ;
@@ -494,7 +555,7 @@ function defocusSearchBar() {
494
555
case "Enter" :
495
556
case "Return" :
496
557
case "Space" :
497
- if ( ev . target . id === "theme-picker" && themes . style . display === "none" ) {
558
+ if ( ev . target . id === THEME_PICKER_ELEMENT_ID && themes . style . display === "none" ) {
498
559
ev . preventDefault ( ) ;
499
560
showThemeButtonState ( ) ;
500
561
themes . firstElementChild . focus ( ) ;
0 commit comments