@@ -15,6 +15,9 @@ const preview = {
15
15
type : "svg" ,
16
16
exclude_days : "" ,
17
17
card_width : "495" ,
18
+ hide_total_contributions : "false" ,
19
+ hide_current_streak : "false" ,
20
+ hide_longest_streak : "false" ,
18
21
} ,
19
22
20
23
/**
@@ -23,6 +26,11 @@ const preview = {
23
26
update ( ) {
24
27
// get parameter values from all .param elements
25
28
const params = this . objectFromElements ( document . querySelectorAll ( ".param" ) ) ;
29
+ // convert sections to hide_... parameters
30
+ params . hide_total_contributions = String ( ! params . sections . includes ( "total" ) ) ;
31
+ params . hide_current_streak = String ( ! params . sections . includes ( "current" ) ) ;
32
+ params . hide_longest_streak = String ( ! params . sections . includes ( "longest" ) ) ;
33
+ delete params . sections ;
26
34
// convert parameters to query string
27
35
const query = Object . keys ( params )
28
36
. filter ( ( key ) => params [ key ] !== this . defaults [ key ] )
@@ -301,6 +309,29 @@ const preview = {
301
309
this . update ( ) ;
302
310
} ,
303
311
312
+ /**
313
+ * Update checkboxes based on the query string parameter
314
+ *
315
+ * @param {string|null } param - the query string parameter to read
316
+ * @param {string } selector - the selector of the parent container to find the checkboxes
317
+ */
318
+ updateCheckboxes ( param , selector ) {
319
+ if ( ! param ) {
320
+ return ;
321
+ }
322
+ // uncheck all checkboxes
323
+ [ ...document . querySelectorAll ( `${ selector } input[value]` ) ] . forEach ( ( checkbox ) => {
324
+ checkbox . checked = false ;
325
+ } ) ;
326
+ // check checkboxes based on values in the query string
327
+ param . split ( "," ) . forEach ( ( value ) => {
328
+ const checkbox = document . querySelector ( `${ selector } input[value="${ value } "]` ) ;
329
+ if ( checkbox ) {
330
+ checkbox . checked = true ;
331
+ }
332
+ } ) ;
333
+ } ,
334
+
304
335
/**
305
336
* Assign values to input boxes based on the query string
306
337
*
@@ -334,15 +365,9 @@ const preview = {
334
365
preview . checkColor ( backgroundParams [ 2 ] , "background-color2" ) ;
335
366
}
336
367
// set weekday checkboxes
337
- const excludeDays = searchParams . get ( "exclude_days" ) ;
338
- if ( excludeDays ) {
339
- excludeDays . split ( "," ) . forEach ( ( day ) => {
340
- const checkbox = document . querySelector ( `.weekdays input[value="${ day } "]` ) ;
341
- if ( checkbox ) {
342
- checkbox . checked = true ;
343
- }
344
- } ) ;
345
- }
368
+ this . updateCheckboxes ( searchParams . get ( "exclude_days" ) , ".weekdays" ) ;
369
+ // set show sections checkboxes
370
+ this . updateCheckboxes ( searchParams . get ( "sections" ) , ".sections" ) ;
346
371
} ,
347
372
} ;
348
373
@@ -401,12 +426,22 @@ window.addEventListener(
401
426
} ;
402
427
document . querySelector ( "#background-type-solid" ) . addEventListener ( "change" , toggleBackgroundType , false ) ;
403
428
document . querySelector ( "#background-type-gradient" ) . addEventListener ( "change" , toggleBackgroundType , false ) ;
429
+ // function to update the hidden input box when checkboxes are clicked
430
+ const updateCheckboxTextField = ( parentSelector , inputSelector ) => {
431
+ const checked = document . querySelectorAll ( `${ parentSelector } input:checked` ) ;
432
+ document . querySelector ( inputSelector ) . value = [ ...checked ] . map ( ( node ) => node . value ) . join ( "," ) ;
433
+ preview . update ( ) ;
434
+ } ;
404
435
// when weekdays are toggled, update the input field
405
- document . querySelectorAll ( '.weekdays input[type="checkbox"]' ) . forEach ( ( el ) => {
436
+ document . querySelectorAll ( ".weekdays input[type='checkbox']" ) . forEach ( ( el ) => {
437
+ el . addEventListener ( "click" , ( ) => {
438
+ updateCheckboxTextField ( ".weekdays" , "#exclude-days" ) ;
439
+ } ) ;
440
+ } ) ;
441
+ // when sections are toggled, update the input field
442
+ document . querySelectorAll ( ".sections input[type='checkbox']" ) . forEach ( ( el ) => {
406
443
el . addEventListener ( "click" , ( ) => {
407
- const checked = document . querySelectorAll ( ".weekdays input:checked" ) ;
408
- document . querySelector ( "#exclude-days" ) . value = [ ...checked ] . map ( ( node ) => node . value ) . join ( "," ) ;
409
- preview . update ( ) ;
444
+ updateCheckboxTextField ( ".sections" , "#sections" ) ;
410
445
} ) ;
411
446
} ) ;
412
447
// when mode is set to "weekly", disable checkboxes, otherwise enable them
0 commit comments