22// Local js definitions:
33/* global addClass, getSettingValue, hasClass */
44/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
5- /* global hideThemeButtonState, showThemeButtonState */
5+ /* global switchTheme, useSystemTheme */
66
77if ( ! String . prototype . startsWith ) {
88 String . prototype . startsWith = function ( searchString , position ) {
@@ -85,12 +85,15 @@ function getSearchElement() {
8585 return document . getElementById ( "search" ) ;
8686}
8787
88+ var THEME_PICKER_ELEMENT_ID = "theme-picker" ;
89+ var THEMES_ELEMENT_ID = "theme-choices" ;
90+
8891function getThemesElement ( ) {
89- return document . getElementById ( "theme-choices" ) ;
92+ return document . getElementById ( THEMES_ELEMENT_ID ) ;
9093}
9194
9295function getThemePickerElement ( ) {
93- return document . getElementById ( "theme-picker" ) ;
96+ return document . getElementById ( THEME_PICKER_ELEMENT_ID ) ;
9497}
9598
9699// Returns the current URL without any query parameter or hash.
@@ -108,6 +111,65 @@ function defocusSearchBar() {
108111 getSearchInput ( ) . blur ( ) ;
109112}
110113
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+
111173( function ( ) {
112174 "use strict" ;
113175
@@ -461,8 +523,7 @@ function defocusSearchBar() {
461523 break ;
462524
463525 default :
464- var themePicker = getThemePickerElement ( ) ;
465- if ( themePicker . parentNode . contains ( ev . target ) ) {
526+ if ( getThemePickerElement ( ) . parentNode . contains ( ev . target ) ) {
466527 handleThemeKeyDown ( ev ) ;
467528 }
468529 }
@@ -475,7 +536,7 @@ function defocusSearchBar() {
475536 switch ( getVirtualKey ( ev ) ) {
476537 case "ArrowUp" :
477538 ev . preventDefault ( ) ;
478- if ( active . previousElementSibling && ev . target . id !== "theme-picker" ) {
539+ if ( active . previousElementSibling && ev . target . id !== THEME_PICKER_ELEMENT_ID ) {
479540 active . previousElementSibling . focus ( ) ;
480541 } else {
481542 showThemeButtonState ( ) ;
@@ -484,7 +545,7 @@ function defocusSearchBar() {
484545 break ;
485546 case "ArrowDown" :
486547 ev . preventDefault ( ) ;
487- if ( active . nextElementSibling && ev . target . id !== "theme-picker" ) {
548+ if ( active . nextElementSibling && ev . target . id !== THEME_PICKER_ELEMENT_ID ) {
488549 active . nextElementSibling . focus ( ) ;
489550 } else {
490551 showThemeButtonState ( ) ;
@@ -494,7 +555,7 @@ function defocusSearchBar() {
494555 case "Enter" :
495556 case "Return" :
496557 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" ) {
498559 ev . preventDefault ( ) ;
499560 showThemeButtonState ( ) ;
500561 themes . firstElementChild . focus ( ) ;
0 commit comments