Skip to content

Commit dd474ae

Browse files
authored
feat(demo): Support hiding sections on demo site (#519)
1 parent e53f951 commit dd474ae

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

src/demo/css/style.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,31 @@ h2 {
186186
text-transform: capitalize;
187187
}
188188

189-
.weekdays input {
189+
.checkbox-buttons input {
190190
display: none !important;
191191
}
192192

193-
.weekdays input[type="checkbox"] + label {
193+
.checkbox-buttons input[type="checkbox"] + label {
194194
font-size: 90%;
195195
display: inline-block;
196196
border-radius: 6px;
197197
height: 30px;
198-
width: 30px;
199198
margin: 2px 3px 2px 0;
200199
line-height: 28px;
201200
text-align: center;
202201
cursor: pointer;
203202
background: var(--card-background);
204203
color: var(--text);
205204
border: 1px solid var(--border);
205+
padding: 0 10px;
206206
}
207207

208-
.weekdays input[type="checkbox"]:checked + label {
208+
.checkbox-buttons input[type="checkbox"]:checked + label {
209209
background: var(--text);
210210
color: var(--background);
211211
}
212212

213-
.weekdays input[type="checkbox"]:disabled + label {
213+
.checkbox-buttons input[type="checkbox"]:disabled + label {
214214
background: var(--card-background);
215215
color: var(--stroke);
216216
}

src/demo/index.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function gtag() {
138138
</select>
139139

140140
<span id="exclude-days-label">Exclude Days</span>
141-
<div class="weekdays" role="group" aria-labelledby="exclude-days-label">
141+
<div class="checkbox-buttons weekdays" role="group" aria-labelledby="exclude-days-label">
142142
<input type="checkbox" value="Sun" id="weekday-sun" />
143143
<label for="weekday-sun" data-tooltip="Exclude Sunday" title="Exclude Sunday">S</label>
144144
<input type="checkbox" value="Mon" id="weekday-mon" />
@@ -156,16 +156,27 @@ function gtag() {
156156
<input type="hidden" id="exclude-days" name="exclude_days" class="param" />
157157
</div>
158158

159+
<span id="show-sections-label">Show Sections</span>
160+
<div class="checkbox-buttons sections" role="group" aria-labelledby="show-sections-label">
161+
<input type="checkbox" value="total" id="section-total" checked />
162+
<label for="section-total" data-tooltip="Total Contributions" title="Total Contributions">Total</label>
163+
<input type="checkbox" value="current" id="section-current" checked />
164+
<label for="section-current" data-tooltip="Current Streak" title="Current Streak">Current</label>
165+
<input type="checkbox" value="longest" id="section-longest" checked />
166+
<label for="section-longest" data-tooltip="Longest Streak" title="Longest Streak">Longest</label>
167+
<input type="hidden" id="sections" name="sections" class="param" value="total,current,longest" />
168+
</div>
169+
170+
<label for="card-width">Card Width</label>
171+
<input class="param" type="number" id="card-width" name="card_width" placeholder="495" value="495" step="1" min="300" />
172+
159173
<label for="type">Output Type</label>
160174
<select class="param" id="type" name="type">
161175
<option value="svg">SVG</option>
162176
<option value="png">PNG</option>
163177
<option value="json">JSON</option>
164178
</select>
165179

166-
<label for="card-width">Card Width</label>
167-
<input class="param" type="number" id="card-width" name="card_width" placeholder="495" value="495" step="1" min="300" />
168-
169180
<details class="advanced">
170181
<summary>⚙ Advanced Options</summary>
171182
<div class="content">

src/demo/js/script.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)