@@ -15,6 +15,9 @@ const preview = {
1515 type : "svg" ,
1616 exclude_days : "" ,
1717 card_width : "495" ,
18+ hide_total_contributions : "false" ,
19+ hide_current_streak : "false" ,
20+ hide_longest_streak : "false" ,
1821 } ,
1922
2023 /**
@@ -23,6 +26,11 @@ const preview = {
2326 update ( ) {
2427 // get parameter values from all .param elements
2528 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 ;
2634 // convert parameters to query string
2735 const query = Object . keys ( params )
2836 . filter ( ( key ) => params [ key ] !== this . defaults [ key ] )
@@ -301,6 +309,29 @@ const preview = {
301309 this . update ( ) ;
302310 } ,
303311
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+
304335 /**
305336 * Assign values to input boxes based on the query string
306337 *
@@ -334,15 +365,9 @@ const preview = {
334365 preview . checkColor ( backgroundParams [ 2 ] , "background-color2" ) ;
335366 }
336367 // 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" ) ;
346371 } ,
347372} ;
348373
@@ -401,12 +426,22 @@ window.addEventListener(
401426 } ;
402427 document . querySelector ( "#background-type-solid" ) . addEventListener ( "change" , toggleBackgroundType , false ) ;
403428 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+ } ;
404435 // 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 ) => {
406443 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" ) ;
410445 } ) ;
411446 } ) ;
412447 // when mode is set to "weekly", disable checkboxes, otherwise enable them
0 commit comments