-
-
Notifications
You must be signed in to change notification settings - Fork 557
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
base: main
Are you sure you want to change the base?
Changes from all commits
34aee14
0b401bf
3762540
08f6a47
1e4c7e3
6945378
529ecdc
e6adce2
511eef0
2f490a5
f56fdf7
cb8dc8c
0c9a3c5
edb1f05
d97747c
2ea8d08
e8915ba
f7eec09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 && | ||
|
@@ -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)) { | ||
|
@@ -42,7 +42,7 @@ function processChecklistClick(checkboxSelector) { | |
storeChecklistItem(target.id); | ||
} else { | ||
removeChecklistItem(target.id); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
@@ -58,10 +58,45 @@ 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"); | ||
buttonEl.addEventListener("click", function handleToggleButtonClick(event) { | ||
var target = event.target; | ||
if (target.hasAttribute("data-open") === true) { | ||
target.removeAttribute("data-open"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead use |
||
} else { | ||
target.setAttribute("data-open", true); | ||
} | ||
details.forEach(function (item) { | ||
if (target.hasAttribute("data-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]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
toggleCategoryButtons.forEach(function (button) { | ||
var parentSection = button.closest("section"); | ||
registerToggleButton(button, parentSection); | ||
}); | ||
} | ||
|
||
renderToggle(); |
There was a problem hiding this comment.
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.