release nouri 0.6.0 polish backup and pwa
This commit is contained in:
+47
-2
@@ -51,12 +51,57 @@
|
||||
if (!container) return;
|
||||
|
||||
const items = Array.from(container.querySelectorAll("[data-filter-label]"));
|
||||
const filterGroups = Array.from(container.querySelectorAll("[data-filter-group]"));
|
||||
const resultLimit = Number.parseInt(input.getAttribute("data-filter-limit") || "", 10);
|
||||
const hasLimit = Number.isFinite(resultLimit) && resultLimit > 0;
|
||||
|
||||
const scoreItem = (label, term) => {
|
||||
if (label === term) return 0;
|
||||
if (label.startsWith(term)) return 1;
|
||||
if (label.split(/\s+/).some((part) => part.startsWith(term))) return 2;
|
||||
if (label.includes(term)) return 3;
|
||||
return 99;
|
||||
};
|
||||
|
||||
const syncGroups = () => {
|
||||
filterGroups.forEach((group) => {
|
||||
const visibleChildren = Array.from(group.querySelectorAll("[data-filter-label]")).some((item) => !item.hidden);
|
||||
const card = group.closest(".component-group, .template-list-card, .panel, .planner-subsection");
|
||||
if (card) {
|
||||
card.hidden = !visibleChildren;
|
||||
} else {
|
||||
group.hidden = !visibleChildren;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const applyFilter = () => {
|
||||
const term = input.value.trim().toLowerCase();
|
||||
if (!term) {
|
||||
items.forEach((item) => {
|
||||
item.hidden = false;
|
||||
});
|
||||
syncGroups();
|
||||
return;
|
||||
}
|
||||
|
||||
const rankedMatches = items
|
||||
.map((item, index) => {
|
||||
const haystack = (item.getAttribute("data-filter-label") || "").toLowerCase();
|
||||
const score = scoreItem(haystack, term);
|
||||
return { item, index, score, matches: score < 99 };
|
||||
})
|
||||
.filter((entry) => entry.matches)
|
||||
.sort((left, right) => left.score - right.score || left.index - right.index);
|
||||
|
||||
const allowedItems = new Set(
|
||||
(hasLimit ? rankedMatches.slice(0, resultLimit) : rankedMatches).map((entry) => entry.item)
|
||||
);
|
||||
|
||||
items.forEach((item) => {
|
||||
const haystack = (item.getAttribute("data-filter-label") || "").toLowerCase();
|
||||
item.hidden = Boolean(term) && !haystack.includes(term);
|
||||
item.hidden = !allowedItems.has(item);
|
||||
});
|
||||
syncGroups();
|
||||
};
|
||||
|
||||
input.addEventListener("input", applyFilter);
|
||||
|
||||
Reference in New Issue
Block a user