Skip to content

Commit 8dd6df7

Browse files
committed
Simplify the filtering, use native hidden property instead
1 parent 96f5f07 commit 8dd6df7

File tree

2 files changed

+33
-78
lines changed

2 files changed

+33
-78
lines changed

ts/WoltLabSuite/Core/Component/ItemList/Categorized.ts

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @sice 6.3
88
*/
99

10-
import { innerError, show, hide, isHidden } from "WoltLabSuite/Core/Dom/Util";
10+
import { innerError } from "WoltLabSuite/Core/Dom/Util";
1111
import { getPhrase } from "WoltLabSuite/Core/Language";
1212
import { escapeRegExp } from "WoltLabSuite/Core/StringUtil";
1313

@@ -86,10 +86,6 @@ export class CategorizedItemList {
8686
}
8787
}
8888

89-
#categoryIsOpen(category: HTMLLIElement): boolean {
90-
return category.dataset.open === "true";
91-
}
92-
9389
#keyup(): void {
9490
const value = this.#input.value.trim();
9591
if (this.#value === value) {
@@ -109,56 +105,36 @@ export class CategorizedItemList {
109105
this.#fragment.appendChild(this.#elementList);
110106

111107
this.#categories.forEach((category) => {
112-
this.#showItems(category);
108+
this.#filterItems(category);
113109
});
114110

115-
const hasVisibleItems = Array.from(
116-
this.#elementList.querySelectorAll<HTMLLIElement>(".scrollableCheckboxList > li"),
117-
).some((li) => {
118-
return !isHidden(li);
119-
});
111+
const hasVisibleItem = this.#elementList.querySelector(".scrollableCheckboxList > li:not([hidden])") !== null;
120112

121113
this.#container.insertAdjacentElement("beforeend", this.#elementList);
122114

123-
innerError(this.#container, hasVisibleItems ? false : getPhrase("wcf.global.filter.error.noMatches"));
115+
innerError(this.#container, hasVisibleItem ? false : getPhrase("wcf.global.filter.error.noMatches"));
124116
}
125117

126-
#showItems(category: Category): void {
127-
const categoryIsOpen = this.#categoryIsOpen(category.element);
118+
#filterItems(category: Category): void {
128119
const regexp = new RegExp("(" + escapeRegExp(this.#value) + ")", "i");
129120

130-
if (this.#value === "") {
131-
show(category.element);
132-
133-
category.items.forEach((item) => {
121+
let hasMatchingItem = false;
122+
for (const item of category.items) {
123+
if (this.#value === "") {
134124
item.span.innerHTML = item.text; // Reset highlighting
135125

136-
if (categoryIsOpen) {
137-
show(item.element);
138-
} else {
139-
hide(item.element);
140-
}
141-
});
142-
} else {
143-
if (category.items.some((item) => regexp.test(item.text))) {
144-
show(category.element);
126+
hasMatchingItem = true;
127+
item.element.hidden = false;
128+
} else if (regexp.test(item.text)) {
129+
item.span.innerHTML = item.text.replace(regexp, "<u>$1</u>");
145130

146-
category.items.forEach((item) => {
147-
if (categoryIsOpen && regexp.test(item.text)) {
148-
item.span.innerHTML = item.text.replace(regexp, "<u>$1</u>");
149-
150-
show(item.element);
151-
} else {
152-
hide(item.element);
153-
}
154-
});
131+
item.element.hidden = false;
132+
hasMatchingItem = true;
155133
} else {
156-
hide(category.element);
157-
158-
category.items.forEach((item) => {
159-
hide(item.element);
160-
});
134+
item.element.hidden = true;
161135
}
162136
}
137+
138+
category.element.hidden = !hasMatchingItem;
163139
}
164140
}

wcfsetup/install/files/js/WoltLabSuite/Core/Component/ItemList/Categorized.js

Lines changed: 16 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)