@@ -86,11 +86,12 @@ function createLightbox (id) {
8686 search . addEventListener ( 'keydown' , e => {
8787
8888 if ( e . key === "Enter" )
89- sort ( search . value )
89+ sort ( localStorage . sort , search . value )
9090
9191 } )
9292
93- document . getElementById ( 'searchButton' ) . addEventListener ( 'click' , ( ) => ( false ) )
93+ const search_button = /** @type {HTMLInputElement } */ ( document . getElementById ( 'searchButton' ) )
94+ search_button . addEventListener ( 'click' , ( ) => sort ( localStorage . sort , search . value ) )
9495
9596 /* Load Content
9697 * ============
@@ -104,47 +105,28 @@ function createLightbox (id) {
104105 localStorage . sort = 'latest'
105106
106107 /*
107- * Make the sort icon a button .
108+ * Add event to sort when an option is chosen. .
108109 */
109- const sort_trigger = /** @type {HTMLElement } */ ( document . getElementById ( 'js-sortSwitcher' ) )
110- sort_trigger . addEventListener ( 'click' , ( ) => toggle_sorting ( ) )
111- sort ( )
110+ const sort_menu = /** @type {HTMLSelectElement } */ ( document . getElementById ( 'js-sort-menu' ) )
111+ sort_menu . addEventListener ( 'change' , ( ) => {
112+ const name = /** @type {string } */ ( sort_menu . selectedOptions [ 0 ] . getAttribute ( 'name' ) )
113+ sort ( name )
114+ } )
112115
113- /**
114- * Toggle the sorting type of the themes.
115- **/
116- function toggle_sorting ( ) {
117-
118- switch ( localStorage . sort )
119- {
120- case 'latest' :
121- localStorage . sort = 'updated'
122- break
123- case 'updated' :
124- localStorage . sort = 'stars'
125- break
126- case 'stars' :
127- localStorage . sort = 'random'
128- break ;
129- case 'random' :
130- localStorage . sort = 'oldest'
131- break
132- default :
133- localStorage . sort = 'latest'
134- }
135-
136- return sort ( )
137-
138- }
116+ sort ( localStorage . sort )
117+ const current_option = sort_menu . options . namedItem ( localStorage . sort )
118+ if ( current_option )
119+ current_option . selected = true
139120
140121 /**
141122 * Toggle the sorting type of the themes.
142123 *
124+ * @param {string } kind How to sort the themes.
143125 * @param {string= } filter Term to filter the themes.
144126 **/
145- function sort ( filter ) {
127+ function sort ( kind , filter ) {
146128
147- sort_trigger . title = `" ${ localStorage . sort } "`
129+ localStorage . sort = kind
148130
149131 // Remove all themes cards from the page.
150132 const cards_container = document . getElementById ( 'themes_container' )
0 commit comments