Improve filtering

This commit is contained in:
Emil Miler 2024-12-30 13:54:28 +01:00
parent bfc158c6cb
commit 842b66def0

View File

@ -20,15 +20,18 @@ function buttonToggle(clickedButton) {
}
function filterSongs() {
const searchTerm = search.value.trim().toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "");
const searchTerm = sanitizeString(search.value);
const songs = Array.from(songList.children);
songs.forEach(song => {
const title = song.dataset.title.toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "");
const matching = title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory);
const matching = song.dataset.title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory);
song.classList.toggle("hidden", !matching);
});
}
function sanitizeString(string) {
return string.trim().toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "")
}
// Event listeners
search.addEventListener("input", filterSongs);
// Filtering happens before the reset itself without this timeout
@ -37,7 +40,10 @@ buttons.forEach(button => button.addEventListener("click", () => buttonToggle(bu
// Normalize song titles
Array.from(songList.children).forEach(song => {
song.dataset.title = song.dataset.title.toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "");
song.dataset.title = sanitizeString(song.dataset.title);
if (song.dataset.artist) {
song.dataset.artist = sanitizeString(song.dataset.artist);
}
});
// Display the filter section on JS-enabled browsers