Skip to content

Commit ec6490f

Browse files
committed
Improve performance of addFilter() up to 300%
1 parent 03f504d commit ec6490f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pydis_site/static/js/resources/resources.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ function updateUI() {
147147
let resourceBox = $(this);
148148

149149
// Validate the filters
150+
150151
$.each(activeFilters, function(filterType, filters) {
151152
// If the filter list is empty, this passes validation.
152153
if (filters.length === 0) {
@@ -171,6 +172,7 @@ function updateUI() {
171172
}
172173
}).show();
173174

175+
174176
// If there are no matches, show the no matches message
175177
if (!hasMatches) {
176178
$(".no-resources-found").show();
@@ -219,14 +221,18 @@ document.addEventListener("DOMContentLoaded", function () {
219221
}
220222

221223
// If you click on the div surrounding the filter checkbox, it clicks the corresponding checkbox.
222-
$('.filter-panel').click(function() {
223-
let checkbox = $(this).find(".filter-checkbox");
224-
checkbox.prop("checked", !checkbox.prop("checked"));
225-
checkbox.change();
224+
$('.filter-panel').on("click",function(event) {
225+
let hitsCheckbox = Boolean(String(event.target));
226+
227+
if (!hitsCheckbox) {
228+
let checkbox = $(this).find(".filter-checkbox");
229+
checkbox.prop("checked", !checkbox.prop("checked"));
230+
checkbox.trigger("change");
231+
}
226232
});
227233

228234
// If you click on one of the tags in the filter box, it unchecks the corresponding checkbox.
229-
$('.filter-box-tag').click(function() {
235+
$('.filter-box-tag').on("click", function() {
230236
let filterItem = this.dataset.filterItem;
231237
let filterName = this.dataset.filterName;
232238
let checkbox = $(`.filter-checkbox[data-filter-name='${filterName}'][data-filter-item='${filterItem}']`);
@@ -236,7 +242,7 @@ document.addEventListener("DOMContentLoaded", function () {
236242
});
237243

238244
// If you click on one of the tags in the resource cards, it clicks the corresponding checkbox.
239-
$('.resource-tag').click(function() {
245+
$('.resource-tag').on("click", function() {
240246
let filterItem = this.dataset.filterItem;
241247
let filterName = this.dataset.filterName;
242248
let checkbox = $(`.filter-checkbox[data-filter-name='${filterName}'][data-filter-item='${filterItem}']`);
@@ -251,13 +257,13 @@ document.addEventListener("DOMContentLoaded", function () {
251257
});
252258

253259
// When checkboxes are toggled, trigger a filter update.
254-
$('.filter-checkbox').change(function () {
260+
$('.filter-checkbox').on("change", function (event) {
255261
let filterItem = this.dataset.filterItem;
256262
let filterName = this.dataset.filterName;
257263

258-
if (this.checked) {
264+
if (this.checked && !activeFilters[filterName].includes(filterItem)) {
259265
addFilter(filterName, filterItem);
260-
} else {
266+
} else if (!this.checked && activeFilters[filterName].includes(filterItem)) {
261267
removeFilter(filterName, filterItem);
262268
}
263269
});

0 commit comments

Comments
 (0)