@@ -15,13 +15,23 @@ const ajax = (endpoint, callback, data = null) => {
1515 request . send ( data ) ;
1616}
1717
18- const replace_query_param = ( param , value ) => {
18+ const query_params = ( ... args ) => {
1919 const url = new URL ( location . href ) ;
20- const params = new URLSearchParams ( url . search ) ;
21-
22- params . set ( param , value ) ;
20+ const search_params = new URLSearchParams ( url . search ) ;
21+
22+ if ( args . length === 2 && typeof args [ 0 ] === 'string' ) {
23+ search_params . set ( args [ 0 ] , String ( args [ 1 ] ) ) ;
24+ } else if ( args . length === 1 && typeof args [ 0 ] === 'object' ) {
25+ Object . entries ( args [ 0 ] ) . forEach ( ( [ key , value ] ) => {
26+ if ( value === null ) {
27+ search_params . delete ( key ) ;
28+ } else {
29+ search_params . set ( key , String ( value ) ) ;
30+ }
31+ } ) ;
32+ }
2333
24- url . search = params . toString ( ) ;
34+ url . search = search_params . toString ( ) ;
2535 location . href = url . toString ( ) ;
2636}
2737
@@ -30,7 +40,7 @@ const select_and_redirect = (id, param) => {
3040
3141 if ( select ) {
3242 select . addEventListener ( 'change' , e => {
33- replace_query_param ( param , e . target . value ) ;
43+ query_params ( param , e . target . value ) ;
3444 } ) ;
3545 }
3646}
@@ -180,7 +190,7 @@ const search_form = document.getElementById('search_form');
180190if ( search_form ) {
181191 const submit_search = document . getElementById ( 'submit_search' ) ;
182192 submit_search . addEventListener ( 'click' , ( ) => {
183- replace_query_param ( 's' , document . getElementById ( 'search_key' ) . value )
193+ query_params ( 's' , document . getElementById ( 'search_key' ) . value )
184194 } ) ;
185195
186196 const search_key = document . getElementById ( 'search_key' ) ;
@@ -198,6 +208,28 @@ if (search_form) {
198208 } ) ;
199209}
200210
211+ /**
212+ * Table sorting
213+ */
214+ document . querySelectorAll ( '[data-sortcol]' ) . forEach ( element => {
215+ element . addEventListener ( 'click' , ( ) => {
216+ const sort_col = element . getAttribute ( 'data-sortcol' ) ;
217+ const search_params = new URLSearchParams ( window . location . search ) ;
218+ const current_sort_dir = search_params . get ( 'sortcol' ) === sort_col ? search_params . get ( 'sortdir' ) || 'none' : 'none' ;
219+
220+ const sort_dir_cycle = [ 'none' , 'asc' , 'desc' ] ;
221+ const current_index = sort_dir_cycle . indexOf ( current_sort_dir ) ;
222+ const new_sort_dir = sort_dir_cycle [ ( current_index + 1 ) % sort_dir_cycle . length ] ;
223+ element . setAttribute ( 'data-sortdir' , new_sort_dir ) ;
224+
225+ if ( new_sort_dir === 'none' ) {
226+ query_params ( { sortdir : null , sortcol : null } ) ;
227+ } else {
228+ query_params ( { p : null , sortdir : new_sort_dir , sortcol : sort_col } ) ;
229+ }
230+ } ) ;
231+ } ) ;
232+
201233/**
202234 * Light / Dark mode
203235 */
0 commit comments