Skip to content

Commit

Permalink
Merge pull request #4 from barredterra/patch-1
Browse files Browse the repository at this point in the history
feat: combine filters
  • Loading branch information
knadh committed Feb 13, 2023
2 parents 2a5d15b + 9344028 commit ae0deca
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions dirmaker/example/static/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function intersection(setA, setB) {
const _intersection = new Set();
for (const elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}

function filterList(sel, itemSelector) {
const noRes = document.querySelector("#no-results");
// Hide all items.
Expand All @@ -11,12 +21,18 @@ function filterList(sel, itemSelector) {
}
noRes.style.display = 'none';

// List of attribute selector queries for each value. eg:
// #items li[data-language*=malayalam|], #items li[data-language*=kannada|] ...
const q = sel.map(v => `${itemSelector}[data-${v.taxonomy}*='${v.value}|']`);
let visible = new Set(document.querySelectorAll(itemSelector));
for (taxonomy of new Set(sel.map((e) => e.taxonomy))) {
// List of attribute selector queries for each value. eg:
// #items li[data-language*=malayalam|], #items li[data-language*=kannada|] ...
let q = sel.filter((e) => e.taxonomy === taxonomy).map((e) => `${itemSelector}[data-${taxonomy}*='${e.value}|']`)
visible = intersection(visible, new Set(document.querySelectorAll(q.join(", "))));

// const q = sel.map(v => `${itemSelector}[data-${v.taxonomy}*='${v.value}|']`);
}

// Show the matched items.
document.querySelectorAll(q.join(", ")).forEach(e => {
visible.forEach(e => {
e.style.display = "block";
});
}
Expand Down

0 comments on commit ae0deca

Please sign in to comment.