use push/popstate for filters

This commit is contained in:
Rhiannon Morris 2020-08-04 02:20:57 +02:00
parent 45077d5eec
commit 161cb48d3a

View file

@ -17,7 +17,7 @@ function fillSets() {
return [checkedValues(reqBoxes), checkedValues(excBoxes)]; return [checkedValues(reqBoxes), checkedValues(excBoxes)];
} }
function update() { function updateItems() {
let [reqTags, excTags] = fillSets(); let [reqTags, excTags] = fillSets();
let anyReq = reqTags.size > 0; let anyReq = reqTags.size > 0;
@ -33,6 +33,11 @@ function update() {
} }
} }
function update() {
updateItems();
history.pushState(null, "", makeFragment());
}
function converseId(id) { function converseId(id) {
if (id.match(/^require/)) { if (id.match(/^require/)) {
return id.replace('require', 'exclude'); return id.replace('require', 'exclude');
@ -70,13 +75,41 @@ function reset(e) {
} }
function makeFragment() {
let ids = allBoxes.filter(b => b.checked).map(b => b.id);
if (ids.length == 0) {
return '#all';
} else if (allBoxes.every(b => b.checked == b.defaultChecked)) {
return '#';
} else {
return '#' + ids.join(';');
}
}
function useFragment() {
let frag = document.location.hash.replace(/^#/, '');
if (frag == 'all') {
clearForm();
} else if (frag) {
let set = new Set(frag.split(';'));
allBoxes.forEach(b => b.checked = set.has(b.id));
} else {
resetForm();
}
updateItems();
}
function setup() { function setup() {
allBoxes.forEach(b => b.addEventListener('change', () => toggle(b))); allBoxes.forEach(b => b.addEventListener('change', () => toggle(b)));
document.getElementById('clear').addEventListener('click', clear); document.getElementById('clear').addEventListener('click', clear);
document.getElementById('reset').addEventListener('click', reset); document.getElementById('reset').addEventListener('click', reset);
update(); window.addEventListener('popstate', useFragment);
useFragment();
} }
window.addEventListener('load', setup); window.addEventListener('load', setup);