@@ -81,109 +81,153 @@ function createLightbox (id) {
8181 * ======================
8282 */
8383
84- document . getElementById ( 'searchInput' ) . addEventListener ( 'keydown' , e => {
84+ const search = /** @type { HTMLInputElement } */ ( document . getElementById ( 'searchInput' ) )
8585
86- if ( e . key === "Enter" ) toggleSortType ( false )
86+ search . addEventListener ( 'keydown' , e => {
87+
88+ if ( e . key === "Enter" )
89+ sort ( search . value )
8790
8891 } )
8992
90- document . getElementById ( 'searchButton' ) . addEventListener ( 'click' , ( ) => toggleSortType ( false ) )
93+ document . getElementById ( 'searchButton' ) . addEventListener ( 'click' , ( ) => ( false ) )
9194
9295 /* Load Content
9396 * ============
9497 */
9598
96- // add our sorting button
97- const sortTrigger = document . getElementById ( 'js-sortSwitcher' )
98- sortTrigger . addEventListener ( 'click' , ( ) => toggleSortType ( true ) )
99-
100- // When localstorage is not set, use "latest" order type
101- if ( ! localStorage [ 'sort' ] ) localStorage [ 'sort' ] = 'latest'
102-
103- function repeatToggle ( nextType ) {
104-
105- localStorage [ 'sort' ] = nextType
106- return toggleSortType ( false )
107-
108- }
109-
110- function toggleSortType ( change ) {
111-
112- if ( document . querySelectorAll ( '.card' ) )
113- document . querySelectorAll ( '.card' ) . forEach ( e => e . remove ( ) ) ;
114-
115- fetch ( 'themes.json' )
116- . then ( data => data . json ( ) )
117- . then ( parsedData => {
118-
119- const search = document . getElementById ( 'searchInput' ) . value
120-
121- if ( search ) {
122-
123- function matches ( text , partial ) { return text . toLowerCase ( ) . indexOf ( partial . toLowerCase ( ) ) > - 1 }
124-
125- const parsedAsArray = Object . entries ( parsedData )
126- let searchResults = parsedAsArray . filter ( element => matches ( `${ element [ 1 ] . title } , ${ element [ 1 ] . tags } ` , search ) )
127-
128- searchResults . forEach ( result => {
129-
130- const card = new Card ( result [ 1 ] , + result [ 0 ] )
131- card . render ( outputContainer )
132-
133- } )
134-
135- sortTrigger . title = `"${ search } "`
136-
137- return
138-
139- }
140-
141- switch ( localStorage [ 'sort' ] ) {
142-
143- // sort from the oldest theme added
144- case 'latest' :
145- if ( change ) return repeatToggle ( 'random' )
146- parsedData . reverse ( )
147- break ;
148-
149- // sort randomly
150- case 'random' :
151- if ( change ) return repeatToggle ( 'oldest' )
152- for ( let i = parsedData . length - 1 ; i > 0 ; i -- ) {
153-
154- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
155- [ parsedData [ i ] , parsedData [ j ] ] = [ parsedData [ j ] , parsedData [ i ] ]
156-
157- }
158- break ;
159-
160- // sort from the most recent theme added
161- default :
162- if ( change ) return repeatToggle ( 'latest' ) ;
163-
164- }
165-
166- // TODO: make a better way to preview the current sorting
167- sortTrigger . title = localStorage [ 'sort' ]
168-
169- parsedData . forEach ( ( entry , index ) => {
170-
171- const card = new Card ( entry , index )
172- card . render ( outputContainer )
99+ /*
100+ * If sorting is not set yet in `localStorage`,
101+ * then use as default `latest` kind.
102+ */
103+ if ( ! localStorage . sort )
104+ localStorage . sort = 'latest'
105+
106+ /*
107+ * Make the sort icon a button.
108+ */
109+ const sort_trigger = /** @type {HTMLElement } */ ( document . getElementById ( 'js-sortSwitcher' ) )
110+ sort_trigger . addEventListener ( 'click' , ( ) => toggle_sorting ( ) )
111+ sort ( )
112+
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+ }
139+
140+ /**
141+ * Toggle the sorting type of the themes.
142+ *
143+ * @param {string= } filter Term to filter the themes.
144+ **/
145+ function sort ( filter ) {
146+
147+ sort_trigger . title = `"${ localStorage . sort } "`
148+
149+ // Remove all themes cards from the page.
150+ const cards_container = document . getElementById ( 'themes_container' )
151+ if ( cards_container )
152+ cards_container . innerHTML = ''
153+
154+ fetch ( 'themes.json' )
155+ . then ( data => data . json ( ) )
156+ . then ( data => {
157+
158+ data = Object . entries ( data )
159+
160+ if ( filter ) {
161+
162+ /**
163+ * Match any substring (partial) from a string (text).
164+ * @param {string } text
165+ * @param {string } partial
166+ */
167+ function matches ( text , partial ) {
168+ return text . toLowerCase ( ) . indexOf ( partial . toLowerCase ( ) ) > - 1
169+ }
170+
171+ data = data . filter ( element => matches ( `${ element [ 1 ] . title } , ${ element [ 1 ] . tags } ` , search . value ) )
172+
173+ }
174+
175+ switch ( localStorage . sort ) {
176+
177+ /*
178+ * Sort from the most recent theme added.
179+ */
180+ case 'latest' :
181+ data . reverse ( )
182+ break
183+
184+ /*
185+ * Ascending sorting of stars from repositories.
186+ */
187+ case 'updated' :
188+ // item1.attr.localeCompare(item2.attr);
189+ data . sort ( ( a , b ) => b [ 1 ] . pushed_at . localeCompare ( a [ 1 ] . pushed_at ) )
190+ break
191+
192+ /*
193+ * Ascending sorting of stars from repositories.
194+ */
195+ case 'stars' :
196+ data . sort ( ( a , b ) => b [ 1 ] . stargazers_count - a [ 1 ] . stargazers_count )
197+ break
198+
199+ /*
200+ * Randomly sorting of themes.
201+ */
202+ case 'random' :
203+ for ( let i = data . length - 1 ; i > 0 ; i -- ) {
204+
205+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
206+ [ data [ i ] , data [ j ] ] = [ data [ j ] , data [ i ] ]
207+
208+ }
209+ break
210+
211+ /*
212+ * Sort from the least recent theme added (oldest).
213+ * Since it's sorted like this by default from the file, do nothing.
214+ */
215+ default :
216+
217+ }
218+
219+ for ( const [ index , entry ] of data )
220+ {
221+ const card = new Card ( entry , index )
222+ card . render ( outputContainer )
223+ }
224+
225+ } )
226+ }
173227
174- } )
175-
176- } )
177- }
178-
179228 // add themes
180229 const outputContainer = document . getElementById ( 'themes_container' )
181230
182- if ( outputContainer ) toggleSortType ( false ) ;
183-
184-
185-
186-
187231 /* Theme Handling
188232 * ==============
189233 */
0 commit comments