Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/toggle checklist groups #1280

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
34aee14
toogle-checklist button created + next steps outlined in checklist.js…
rachel-fischoff May 6, 2021
0b401bf
toggle content button created
rachel-fischoff May 13, 2021
3762540
toggle content button created
rachel-fischoff May 13, 2021
08f6a47
toggle content button created
rachel-fischoff May 13, 2021
1e4c7e3
updated feat/toggle-checklist-groups with main
rachel-fischoff May 13, 2021
6945378
toggle content button renders summary for details in content group
rachel-fischoff May 13, 2021
529ecdc
feat: toggle all & toggle category opening and closing details when c…
rachel-fischoff May 16, 2021
e6adce2
feat: refactor create, created reusable function registerToggleButton
rachel-fischoff May 16, 2021
511eef0
refactor: removed unncessary semicolons and warnings
rachel-fischoff May 21, 2021
2f490a5
merged with current main
rachel-fischoff Jul 6, 2021
f56fdf7
updated code with main & combined renderToggleAll & renderToggleCater…
rachel-fischoff Aug 16, 2021
cb8dc8c
changed function from anonymous event listener to a named function: c…
rachel-fischoff Aug 16, 2021
0c9a3c5
refactored and added correct event handler function name 'handleToggl…
rachel-fischoff Aug 16, 2021
edb1f05
fix: toggle category button & toggle all open or close all the elemen…
rachel-fischoff Aug 26, 2021
d97747c
fix: removed git merge remanant in package-lock.json
rachel-fischoff Aug 26, 2021
2ea8d08
fix: removed details attribute open and changed to data-open
rachel-fischoff Aug 26, 2021
e8915ba
fix: changed data attribute to class for button
rachel-fischoff Aug 31, 2021
f7eec09
Merge branch 'main' into feat/toggle-checklist-groups
rachel-fischoff Apr 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/checklist.njk
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ templateClass: template-checklist

<hr class="c-divider" />

<button id="toggle-all" >Toggle All</button>

{% for category, content in checklists %}
{# 6 May 2021: We don't publish our guidelines about SVGs #}
{% if category !== 'svg' %}
Expand All @@ -93,6 +95,7 @@ templateClass: template-checklist
<p class="c-preface">
{{ content.preface | safe }}
</p>
<button data-toggle-category="{{ category }}">Toggle {{ category }}</button>
rachel-fischoff marked this conversation as resolved.
Show resolved Hide resolved
<div class="c-card__wrapper">
{% set tasks = content.tasks %}
{% for task in tasks %}
Expand Down
49 changes: 43 additions & 6 deletions src/js/checklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* by a DOM selector.
* @see processChecklistClick
*/
if (!Element.prototype.matches) {
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector;
}

/**
* If someone opens the checklist page using a checklist item's "Share link" (ex: a11yproject.com/checklist/#validate-your-html) the item with the corresponding id will scroll into view. Then, if JS is enabled, this function will open its associated <details> element
*/
function openLinkedCheckListItem() {
function openLinkedCheckListItem() {
var hash = window.location.hash.substr(1);
var checklistItem =
hash.length > 0 &&
Expand All @@ -23,15 +23,15 @@
}
// Store checklist status ---------------------------------------------------
function storeChecklistItem(checkboxId) {
localStorage.setItem(checkboxId, 'checked');
localStorage.setItem(checkboxId, "checked");
}

function removeChecklistItem(checkboxId) {
localStorage.removeItem(checkboxId);
}

function processChecklistClick(checkboxSelector) {
document.addEventListener("change", function(event) {
document.addEventListener("change", function (event) {
var target = event.target;

if (!target.matches(checkboxSelector)) {
Expand All @@ -42,7 +42,7 @@ function processChecklistClick(checkboxSelector) {
storeChecklistItem(target.id);
} else {
removeChecklistItem(target.id);
}
}
});
}

Expand All @@ -58,10 +58,47 @@ function populateChecklistFromLocalStorage(checkboxSelector) {

function processChecklist() {
var checkboxSelector = '.c-checklist__checkbox input[type="checkbox"]';

populateChecklistFromLocalStorage(checkboxSelector);
processChecklistClick(checkboxSelector);
}

openLinkedCheckListItem();
processChecklist();

function registerToggleButton(buttonEl, parentEl) {
if (buttonEl && parentEl) {
var details = parentEl.querySelectorAll("details");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this selector a little more specific? Like details.c-checklist to ensure that we are not toggle any other <details> that might be there. Just to future-proof it a bit.


buttonEl.addEventListener("click", function handleToggleButtonClick(event) {
var target = event.target;
if (target.hasAttribute("open") === true) {
target.removeAttribute("open");
} else {
target.setAttribute("open", true);
}
rachel-fischoff marked this conversation as resolved.
Show resolved Hide resolved
details.forEach(function (item) {

if (target.hasAttribute("open") === true) {
item.setAttribute("open", true);
} else {
item.removeAttribute("open");
}
});
});
}
}

function renderToggle() {
var toggleAllButton = document.querySelector("#toggle-all");
registerToggleButton(toggleAllButton, document);

var toggleCategoryButtons = document.querySelectorAll(
"[data-toggle-category]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this selector is not working anymore because the njk code no longer has this data attribute. Probably can use .toggle-category instead? I think because of this the category toggles are not working anymore.

);
toggleCategoryButtons.forEach(function (button) {
var parentSection = button.closest("section");
registerToggleButton(button, parentSection);
});
}

renderToggle();