Improve filtering
This commit is contained in:
parent
bfc158c6cb
commit
842b66def0
@ -20,15 +20,18 @@ function buttonToggle(clickedButton) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function filterSongs() {
|
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);
|
const songs = Array.from(songList.children);
|
||||||
songs.forEach(song => {
|
songs.forEach(song => {
|
||||||
const title = song.dataset.title.toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "");
|
const matching = song.dataset.title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory);
|
||||||
const matching = title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory);
|
|
||||||
song.classList.toggle("hidden", !matching);
|
song.classList.toggle("hidden", !matching);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeString(string) {
|
||||||
|
return string.trim().toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "")
|
||||||
|
}
|
||||||
|
|
||||||
// Event listeners
|
// Event listeners
|
||||||
search.addEventListener("input", filterSongs);
|
search.addEventListener("input", filterSongs);
|
||||||
// Filtering happens before the reset itself without this timeout
|
// Filtering happens before the reset itself without this timeout
|
||||||
@ -37,7 +40,10 @@ buttons.forEach(button => button.addEventListener("click", () => buttonToggle(bu
|
|||||||
|
|
||||||
// Normalize song titles
|
// Normalize song titles
|
||||||
Array.from(songList.children).forEach(song => {
|
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
|
// Display the filter section on JS-enabled browsers
|
||||||
|
Loading…
x
Reference in New Issue
Block a user