Skip to content

Commit

Permalink
[IMPROVEMENT] Rewrite filter from jQuery to regular JavaScript #486
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm committed Mar 5, 2024
2 parents 1822788 + 30fd3a5 commit 3d1ac57
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions promgen/static/js/promgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ function update_page(data) {
}
}

// https://blog.bitsrc.io/debounce-understand-and-learn-how-to-use-this-essential-javascript-skill-9db0c9afbfc1
function debounce(func, delay = 250) {
let timerId;
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}

// Wait until DOM is loaded to add our extra listeners
document.addEventListener("DOMContentLoaded", function () {
/*
Expand All @@ -45,6 +56,27 @@ document.addEventListener("DOMContentLoaded", function () {
dstElement.value = srcElement.innerText;
});
});

/*
Filter Element
Example: <input data-filter="div.auto-grid div">
*/
document.querySelectorAll("[data-filter]").forEach(srcElement => {
const filterTarget = document.querySelectorAll(srcElement.dataset.filter);

srcElement.addEventListener(
"input",
debounce(e => {
const search = srcElement.value.toUpperCase();
console.debug("Searching for", search);
for (const child of filterTarget) {
const txt = child.innerText.toUpperCase();
child.style.display = txt.indexOf(search) > -1 ? "block" : "none";
}
})
);
});
});

$(document).ready(function() {
Expand Down Expand Up @@ -76,12 +108,4 @@ $(document).ready(function() {
$(this).bootstrapSwitch('state', !state, true);
}
});

$('[data-filter]').change(function(){
var search = this.value.toUpperCase();
$(this.dataset.filter).each(function(i, ele){
var txt = $(this).text().toUpperCase();
ele.style.display = txt.indexOf(search) > -1 ? "" : "none"
})
});
});

0 comments on commit 3d1ac57

Please sign in to comment.