2020-08-03 17:11:15 -04:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2020-08-04 13:14:12 -04:00
|
|
|
let items;
|
|
|
|
let reqBoxes;
|
|
|
|
let excBoxes;
|
|
|
|
let allBoxes;
|
|
|
|
let tags;
|
2020-08-03 17:11:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
function fillSets() {
|
|
|
|
let checkedValues = boxes =>
|
|
|
|
new Set(boxes.filter(b => b.checked).map(b => b.value));
|
|
|
|
|
|
|
|
return [checkedValues(reqBoxes), checkedValues(excBoxes)];
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:20:57 -04:00
|
|
|
function updateItems() {
|
2020-08-03 17:11:15 -04:00
|
|
|
let [reqTags, excTags] = fillSets();
|
|
|
|
let anyReq = reqTags.size > 0;
|
|
|
|
|
|
|
|
for (let item of items) {
|
|
|
|
let req = tags.get(item).some(x => reqTags.has(x));
|
|
|
|
let exc = tags.get(item).some(x => excTags.has(x));
|
|
|
|
|
2020-08-04 15:10:50 -04:00
|
|
|
item.hidden = exc || (anyReq && !req);
|
2020-08-03 17:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:20:57 -04:00
|
|
|
function update() {
|
|
|
|
updateItems();
|
|
|
|
history.pushState(null, "", makeFragment());
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:11:15 -04:00
|
|
|
function converseId(id) {
|
|
|
|
if (id.match(/^require/)) {
|
|
|
|
return id.replace('require', 'exclude');
|
|
|
|
} else {
|
|
|
|
return id.replace('exclude', 'require');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggle(checkbox, thisSet, thatSet) {
|
|
|
|
if (checkbox.checked)
|
|
|
|
document.getElementById(converseId(checkbox.id)).checked = false;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function clearForm() {
|
|
|
|
allBoxes.forEach(b => b.checked = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear(e) {
|
|
|
|
clearForm();
|
|
|
|
update();
|
|
|
|
if (e) e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetForm() {
|
2020-08-04 12:22:16 -04:00
|
|
|
allBoxes.forEach(b => b.checked = b.defaultChecked);
|
2020-08-03 17:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset(e) {
|
|
|
|
resetForm();
|
|
|
|
update();
|
|
|
|
if (e) e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-03 20:20:57 -04:00
|
|
|
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(';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:46:17 -04:00
|
|
|
function useFragment(firstLoad) {
|
2020-08-03 20:20:57 -04:00
|
|
|
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));
|
2020-08-04 12:46:17 -04:00
|
|
|
if (firstLoad) document.getElementById('filters-details').open = true;
|
2020-08-03 20:20:57 -04:00
|
|
|
} else {
|
|
|
|
resetForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-03 17:11:15 -04:00
|
|
|
function setup() {
|
2020-08-04 13:14:12 -04:00
|
|
|
items = Array.from(document.querySelectorAll('.item.post'));
|
|
|
|
|
|
|
|
reqBoxes = Array.from(document.querySelectorAll('#require input'));
|
|
|
|
excBoxes = Array.from(document.querySelectorAll('#exclude input'));
|
|
|
|
allBoxes = [...reqBoxes, ...excBoxes];
|
|
|
|
|
|
|
|
tags = new Map(items.map(item => [item, item.dataset.tags.split(';')]));
|
|
|
|
|
|
|
|
|
2020-08-03 17:11:15 -04:00
|
|
|
allBoxes.forEach(b => b.addEventListener('change', () => toggle(b)));
|
|
|
|
|
|
|
|
document.getElementById('clear').addEventListener('click', clear);
|
|
|
|
document.getElementById('reset').addEventListener('click', reset);
|
|
|
|
|
2020-08-04 12:46:17 -04:00
|
|
|
window.addEventListener('popstate', () => useFragment(false));
|
2020-08-04 13:14:12 -04:00
|
|
|
|
2020-08-04 12:46:17 -04:00
|
|
|
useFragment(true);
|
2020-08-03 17:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 13:14:12 -04:00
|
|
|
window.addEventListener('DOMContentLoaded', setup);
|
2020-08-03 17:11:15 -04:00
|
|
|
|
|
|
|
})();
|