await fetch("/api/files/get", {'Accept': 'application/json', 'Content-type': 'application/json'}).then(async (response) => {
let body = await response.json()
songFileList = body
- console.log(body)
})
}
* @param {*} url - The URL that should be checked for in the song file list
*/
function SongURLExists(url) {
- console.log(url)
return songFileList.indexOf(url) != -1
}
function getSongList() {
// Empty the List
innerSongList.innerHTML = ""
+ songList = []
fetch("/api/songs/get", {'Accept': 'application/json', 'Content-type': 'application/json'}).then(async (response) => {
let body = await response.json()
- console.log(body)
for (let i = 0; i < body.length; i++) {
let song = body[i]
innerSongList.appendChild(NewSongElement(
fileAvailable, song.DisplayTrackName, song.DisplayArtistNames, song.ArtworkURL60, song.Key
))
+
+ songList.push(song)
}
// using the elements in the body, create all the new songs in the list
let songFilterName = document.getElementById("songFilterName")
let songFilterArtist = document.getElementById("songFilterArtist")
let songFilterInDB = document.getElementById("songFilterInDB")
-let songFilterImageExist = document.getElementById("songFilterImageExist")
/**
- * Get the current state of filters, with fields NameFilter, ArtistFilter, DBFilter, ImageFilter
+ * Get the current state of filters, with fields NameFilter, ArtistFilter, DBFilter
* @returns filterstates with fields mentioned above
**/
function getFilterStates() {
- let filterStates
+ let filterStates = {}
filterStates.NameFilter = songFilterName.value.toLowerCase()
filterStates.ArtistFilter = songFilterArtist.value.toLowerCase()
filterStates.DBFilter = songFilterInDB.checked
- filterStates.ImageExist = songFilterImageExist.checked
return filterStates
}
+function updateSongListWithFilter(_) {
+ // get the filter states
+ let filters = getFilterStates()
+
+ // deep copy
+ let filteredList = [...songList]
+
+ // filter the song list using that
+ if (filters.DBFilter) {
+ filteredList = filteredList.filter((v, _1, _2) => { return SongURLExists(v.PreviewURL)})
+ }
+
+ if (filters.NameFilter != "") {
+ filteredList = filteredList.filter((v, _1, _2) => {
+ return v.TrackName.toLowerCase().includes(filters.NameFilter.toLowerCase())
+ })
+ }
+
+ if (filters.ArtistFilter != "") {
+ filteredList = filteredList.filter((v, _1, _2) => {
+ return v.ArtistName.toLowerCase().includes(filters.ArtistFilter.toLowerCase())
+ })
+ }
+
+ // now we have the filtered song list
+ innerSongList.innerHTML = ""
+ makeSongList(filteredList, innerSongList)
+}
+
+songFilterName.oninput = (event) => {updateSongListWithFilter(event)}
+songFilterArtist.oninput = (event) => {updateSongListWithFilter(event)}
+songFilterInDB.oninput = (event) => {updateSongListWithFilter(event)}
+
/**
* SETUP / RUN AT START SECTION
**/