2024-02-25 11:09:44 +01:00
|
|
|
// Configuration
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
const API_URL = `${document.location.protocol}//${document.location.host}/api`;
|
|
|
|
const VOLUME_STEP = 5;
|
2023-12-29 16:55:51 +01:00
|
|
|
|
|
|
|
// Get control elements
|
|
|
|
|
2024-01-21 20:29:00 +01:00
|
|
|
const dialog_save_playlist = document.getElementById("save-playlist");
|
|
|
|
const control_playlist_name = document.getElementById("control-playlist-name");
|
2024-04-18 15:06:12 +02:00
|
|
|
const dialog_save_playlist_submit = document.querySelector("#save-playlist form button");
|
2024-01-21 20:29:00 +01:00
|
|
|
const dialog_save_playlist_close = document.querySelector("#save-playlist .close");
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
const connection_state = document.getElementById("connection-state");
|
2023-12-29 16:55:51 +01:00
|
|
|
const control_update_db = document.getElementById("control-update-db");
|
|
|
|
const control_previous = document.getElementById("control-previous");
|
|
|
|
const control_play_pause = document.getElementById("control-play-pause");
|
|
|
|
const control_stop = document.getElementById("control-stop");
|
|
|
|
const control_next = document.getElementById("control-next");
|
|
|
|
const control_progress = document.getElementById("control-progress");
|
|
|
|
const control_repeat = document.getElementById("control-repeat");
|
|
|
|
const control_shuffle = document.getElementById("control-shuffle");
|
|
|
|
const control_xfade = document.getElementById("control-xfade");
|
|
|
|
const control_xfade_minus = document.getElementById("control-xfade-minus");
|
|
|
|
const control_xfade_plus = document.getElementById("control-xfade-plus");
|
2023-12-30 15:02:13 +01:00
|
|
|
const control_volume = document.getElementById("control-volume");
|
|
|
|
const control_volume_up = document.getElementById("control-volume-up");
|
|
|
|
const control_volume_down = document.getElementById("control-volume-down");
|
2023-12-29 16:55:51 +01:00
|
|
|
const queue_table = document.querySelector("#queue tbody");
|
2023-12-30 15:02:13 +01:00
|
|
|
const control_track = document.getElementById("control-track");
|
|
|
|
const control_time = document.getElementById("control-time");
|
2024-01-20 13:48:03 +01:00
|
|
|
const tabs = document.getElementById("tabs");
|
|
|
|
const tab_browser = document.getElementById("tab-browser");
|
|
|
|
const tab_search = document.getElementById("tab-search");
|
|
|
|
const tab_playlists = document.getElementById("tab-playlists");
|
2024-01-21 20:29:00 +01:00
|
|
|
const control_playlist_list = document.getElementById("control-playlist-list");
|
2024-07-15 20:00:18 +02:00
|
|
|
const control_refresh_playlists = document.getElementById("control-refresh-playlists");
|
2024-01-21 20:29:00 +01:00
|
|
|
const control_replace_playlist = document.getElementById("control-replace-playlist");
|
|
|
|
const control_attach_playlist = document.getElementById("control-attach-playlist");
|
|
|
|
const control_save_playlist = document.getElementById("control-save-playlist");
|
|
|
|
const control_delete_playlist = document.getElementById("control-delete-playlist");
|
2024-04-17 00:24:18 +02:00
|
|
|
const result_table = document.querySelector("#result tbody");
|
2024-04-29 00:14:39 +02:00
|
|
|
const control_search_pattern = document.getElementById("control-search-pattern");
|
|
|
|
const control_search_submit = document.getElementById("control-search-submit");
|
2024-01-20 13:48:03 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
// Utility functions
|
2024-01-20 13:48:03 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
secondsToTrackTime = (t) => {
|
|
|
|
const hours = Math.floor(t / 3600);
|
|
|
|
const minutes = Math.floor((t - hours * 3600) / 60);
|
|
|
|
const seconds = Math.floor(t - hours * 3600 - minutes * 60);
|
2024-01-21 20:29:00 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
|
|
|
|
}
|
2024-01-21 20:29:00 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
removeTrackFromQueue = (event) => {
|
|
|
|
const song_id = event.target.parentElement.parentElement.dataset.song_id;
|
2024-01-21 20:29:00 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
console.log(`DEBUG: remove song id ${song_id} from queue`);
|
|
|
|
fetch(`${API_URL}/queue/${song_id}/delete`).then(r => {
|
|
|
|
console.log(r.text());
|
2024-01-21 20:29:00 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
}
|
2024-01-21 20:29:00 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
moveTrackInQueue = (event, direction) => {
|
|
|
|
const song_id = event.target.parentElement.parentElement.dataset.song_id;
|
|
|
|
// TODO: figure out position in queue by counting HTML elements?
|
|
|
|
const position = parseInt(event.target.parentElement.parentElement.firstChild.innerText);
|
|
|
|
|
|
|
|
console.log(`DEBUG: move song ${song_id} down in queue to position ${position + direction}`);
|
|
|
|
fetch(`${API_URL}/queue/${song_id}/move/${position + direction}`).then(r => {
|
|
|
|
console.log(r.text());
|
2024-01-21 20:29:00 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
}
|
2024-01-21 20:29:00 +01:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
refreshPlaylists = () => {
|
|
|
|
console.log("Refreshing playlists from MPD server")
|
|
|
|
fetch(`${API_URL}/playlists`).then(async r => {
|
|
|
|
if (r.status === 200) {
|
|
|
|
const playlists = await r.json();
|
|
|
|
control_playlist_list.options.length = 0; // clear playlists
|
|
|
|
playlists.forEach(p => {
|
|
|
|
const option = document.createElement("option")
|
|
|
|
option.innerText = p["playlist"];
|
|
|
|
option.value = p["playlist"];
|
|
|
|
option.addEventListener("click", () => {
|
|
|
|
fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => {
|
2024-07-15 20:00:18 +02:00
|
|
|
if (r.status === 200) fillResultTable(await r.json());
|
2024-04-18 14:28:02 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
control_playlist_list.appendChild(option)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-29 00:14:39 +02:00
|
|
|
fillResultTable = (songs) => {
|
|
|
|
result_table.innerHTML = "";
|
|
|
|
songs.forEach(song => {
|
|
|
|
const tr = document.createElement("tr");
|
|
|
|
const artist = document.createElement("td");
|
|
|
|
artist.innerText = song["Artist"];
|
|
|
|
const title = document.createElement("td");
|
|
|
|
title.innerText = song["Title"];
|
|
|
|
const time = document.createElement("td");
|
|
|
|
time.innerText = secondsToTrackTime(parseInt(song["Time"]))
|
|
|
|
tr.appendChild(artist);
|
|
|
|
tr.appendChild(title);
|
|
|
|
tr.appendChild(document.createElement("td")); // album
|
|
|
|
tr.appendChild(document.createElement("td")); // genre
|
|
|
|
tr.appendChild(time);
|
|
|
|
result_table.appendChild(tr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
// UI controls
|
|
|
|
|
|
|
|
tab_browser.addEventListener("click", () => {
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_browser.classList.contains("active")) {
|
|
|
|
tab_browser.classList.add("active");
|
|
|
|
tab_search.classList.remove("active")
|
|
|
|
tab_playlists.classList.remove("active")
|
|
|
|
document.getElementById("file-browser").style.display = "block";
|
|
|
|
document.getElementById("search").style.display = "none";
|
|
|
|
document.getElementById("playlist-browser").style.display = "none";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
tab_search.addEventListener("click", () => {
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_search.classList.contains("active")) {
|
|
|
|
tab_browser.classList.remove("active");
|
|
|
|
tab_search.classList.add("active")
|
|
|
|
tab_playlists.classList.remove("active")
|
|
|
|
document.getElementById("file-browser").style.display = "none";
|
|
|
|
document.getElementById("search").style.display = "block";
|
|
|
|
document.getElementById("playlist-browser").style.display = "none";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
tab_playlists.addEventListener("click", () => {
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_playlists.classList.contains("active")) {
|
|
|
|
tab_browser.classList.remove("active");
|
|
|
|
tab_search.classList.remove("active")
|
|
|
|
tab_playlists.classList.add("active")
|
|
|
|
document.getElementById("file-browser").style.display = "none";
|
|
|
|
document.getElementById("search").style.display = "none";
|
|
|
|
document.getElementById("playlist-browser").style.display = "block";
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
|
2024-04-18 15:06:12 +02:00
|
|
|
// Show "Save playlist" modal
|
2024-04-06 18:57:43 +02:00
|
|
|
control_save_playlist.addEventListener("click", () => {
|
|
|
|
dialog_save_playlist.showModal()
|
|
|
|
});
|
|
|
|
|
2024-04-18 15:06:12 +02:00
|
|
|
// Close "Save playlist" modal
|
2024-04-06 18:57:43 +02:00
|
|
|
dialog_save_playlist_close.addEventListener("click", () => {
|
|
|
|
dialog_save_playlist.close()
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-12-29 16:55:51 +01:00
|
|
|
// Add API calls to controls
|
|
|
|
|
2024-04-29 00:14:39 +02:00
|
|
|
control_search_submit.addEventListener("click", event => {
|
|
|
|
event.preventDefault()
|
|
|
|
fetch(`${API_URL}/database/${control_search_pattern.value}`).then(async r => {
|
|
|
|
if (r.status === 200) {
|
|
|
|
fillResultTable([...new Set(await r.json())]);
|
|
|
|
} else {
|
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2024-07-15 20:00:18 +02:00
|
|
|
control_refresh_playlists.addEventListener("click", () => {
|
|
|
|
refreshPlaylists();
|
|
|
|
});
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
control_replace_playlist.addEventListener("click", () => {
|
|
|
|
fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => {
|
2024-04-06 18:57:43 +02:00
|
|
|
if (r.status !== 200) {
|
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
control_attach_playlist.addEventListener("click", () => {
|
2024-04-18 12:12:52 +02:00
|
|
|
fetch(`${API_URL}/queue/attach/${control_playlist_list.value}`).then(async r => {
|
2024-04-06 18:57:43 +02:00
|
|
|
if (r.status !== 200) {
|
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-18 15:06:12 +02:00
|
|
|
// Save current queue as new playlist and refresh playlist list
|
2024-04-06 18:57:43 +02:00
|
|
|
dialog_save_playlist_submit.addEventListener("click", () => {
|
2024-04-18 15:06:12 +02:00
|
|
|
fetch(`${API_URL}/playlists/${control_playlist_name.value}`, {method: "POST"}).then(async r => {
|
2024-04-06 18:57:43 +02:00
|
|
|
if (r.status === 201) {
|
|
|
|
console.log(`Playlist "${control_playlist_name.value}" saved`)
|
2024-04-18 15:06:12 +02:00
|
|
|
refreshPlaylists()
|
|
|
|
} else {
|
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
2024-04-06 18:57:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
control_delete_playlist.addEventListener("click", () => {
|
2024-04-18 14:28:02 +02:00
|
|
|
const playlist_name = control_playlist_list.value;
|
|
|
|
fetch(`${API_URL}/playlists/${control_playlist_list.value}`, {method: "DELETE"}).then(r => {
|
2024-04-06 18:57:43 +02:00
|
|
|
if (r.status === 204) {
|
2024-04-18 14:28:02 +02:00
|
|
|
console.log(`Playlist "${playlist_name}" successfully deleted.`);
|
|
|
|
refreshPlaylists();
|
2024-04-06 18:57:43 +02:00
|
|
|
} else {
|
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
tab_browser.addEventListener("click", () => {
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_browser.classList.contains("active")) {
|
|
|
|
tab_browser.classList.add("active");
|
|
|
|
tab_search.classList.remove("active")
|
|
|
|
tab_playlists.classList.remove("active")
|
|
|
|
document.getElementById("file-browser").style.display = "block";
|
|
|
|
document.getElementById("search").style.display = "none";
|
|
|
|
document.getElementById("playlist-browser").style.display = "none";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
tab_search.addEventListener("click", () => {
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_search.classList.contains("active")) {
|
|
|
|
tab_browser.classList.remove("active");
|
|
|
|
tab_search.classList.add("active")
|
|
|
|
tab_playlists.classList.remove("active")
|
|
|
|
document.getElementById("file-browser").style.display = "none";
|
|
|
|
document.getElementById("search").style.display = "block";
|
|
|
|
document.getElementById("playlist-browser").style.display = "none";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
tab_playlists.addEventListener("click", () => {
|
2024-04-18 14:28:02 +02:00
|
|
|
refreshPlaylists();
|
2024-01-20 13:48:03 +01:00
|
|
|
if (!tab_playlists.classList.contains("active")) {
|
|
|
|
tab_browser.classList.remove("active");
|
|
|
|
tab_search.classList.remove("active")
|
|
|
|
tab_playlists.classList.add("active")
|
|
|
|
document.getElementById("file-browser").style.display = "none";
|
|
|
|
document.getElementById("search").style.display = "none";
|
|
|
|
document.getElementById("playlist-browser").style.display = "block";
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
|
|
|
|
// Add API calls to controls
|
|
|
|
|
2024-04-18 11:46:09 +02:00
|
|
|
control_update_db.addEventListener("click", () => {
|
2023-12-30 15:02:13 +01:00
|
|
|
console.log("Issuing database update")
|
|
|
|
fetch(`${API_URL}/update_db`).then(async r => {
|
|
|
|
if (r.status === 200) {
|
2024-04-06 18:57:43 +02:00
|
|
|
console.log(await r.text());
|
2024-04-06 19:27:59 +02:00
|
|
|
event.target.disabled = true;
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_previous.addEventListener("click", () => {
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/previous_track`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_play_pause.addEventListener("click", event => {
|
|
|
|
if (event.target.innerHTML === "⏸︎") { // Resume playback
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/pause`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
} else { // Pause playback
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/play`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
}
|
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_stop.addEventListener("click", () => {
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/stop`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_next.addEventListener("click", () => {
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/next_track`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_progress.addEventListener("change", event => {
|
|
|
|
fetch(`${API_URL}/seek/${event.target.value}`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_progress.addEventListener("input", event => {
|
|
|
|
control_time.value = `${secondsToTrackTime(event.target.value)}/${secondsToTrackTime(event.target.max)}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
control_repeat.addEventListener("click", event => {
|
|
|
|
if (event.target.dataset.state === "on") { // TODO: check is never true
|
|
|
|
event.target.innerHTML = "🔘 repeat";
|
|
|
|
event.target.dataset.state = "off";
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
2024-04-06 18:57:43 +02:00
|
|
|
event.target.innerHTML = "🔴 repeat";
|
|
|
|
event.target.dataset.state = "on";
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/repeat`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_shuffle.addEventListener("click", event => {
|
|
|
|
if (event.target.dataset.state === "on") { // TODO: check is never true
|
|
|
|
event.target.innerHTML = "🔘 shuffle";
|
|
|
|
event.target.dataset.state = "off";
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
2024-04-06 18:57:43 +02:00
|
|
|
event.target.innerHTML = "🔴 shuffle";
|
|
|
|
event.target.dataset.state = "on";
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/random`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_xfade_minus.addEventListener("click", () => {
|
2023-12-29 16:55:51 +01:00
|
|
|
// TODO: not yet implemented
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/xfade`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_xfade_plus.addEventListener("click", () => {
|
2023-12-29 16:55:51 +01:00
|
|
|
// TODO: not yet implemented
|
2024-01-21 20:29:00 +01:00
|
|
|
fetch(`${API_URL}/xfade`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_volume_up.addEventListener("click", () => {
|
|
|
|
const volume = Math.min(parseInt(control_volume.value) + VOLUME_STEP, 100);
|
|
|
|
fetch(`${API_URL}/volume/${volume}`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
control_volume.value = volume;
|
2023-12-30 15:02:13 +01:00
|
|
|
|
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_volume_down.addEventListener("click", () => {
|
|
|
|
const volume = Math.max(parseInt(control_volume.value) - VOLUME_STEP, 0);
|
|
|
|
fetch(`${API_URL}/volume/${volume}`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
control_volume.value = volume;
|
2023-12-30 15:02:13 +01:00
|
|
|
});
|
2024-04-06 18:57:43 +02:00
|
|
|
|
|
|
|
control_volume.addEventListener("change", event => {
|
|
|
|
fetch(`${API_URL}/volume/${event.target.value}`).then(async r => {
|
2024-02-25 11:09:44 +01:00
|
|
|
if (r.status >= 400) {
|
2024-01-21 20:29:00 +01:00
|
|
|
console.error(`API returned ${r.status}: ${r.statusText}`);
|
|
|
|
}
|
|
|
|
});
|
2023-12-30 15:02:13 +01:00
|
|
|
});
|
2023-12-29 16:55:51 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
// Websocket logic
|
|
|
|
|
2023-12-29 16:55:51 +01:00
|
|
|
// Create WebSocket connection.
|
2023-12-30 15:02:13 +01:00
|
|
|
const socket = new WebSocket(`${document.location.protocol === "https:" ? "wss" : "ws"}://${document.location.host}/ws`);
|
2023-12-29 16:55:51 +01:00
|
|
|
|
|
|
|
// Connection opened
|
2024-04-06 18:57:43 +02:00
|
|
|
socket.addEventListener("open", () => {
|
2023-12-29 16:55:51 +01:00
|
|
|
socket.send("Hello Server!");
|
|
|
|
});
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
// Listen for messages and update UI state
|
2024-04-06 18:57:43 +02:00
|
|
|
socket.addEventListener("message", event => {
|
2023-12-30 15:02:13 +01:00
|
|
|
// Print out mpd response
|
2024-04-06 18:57:43 +02:00
|
|
|
console.log(`DEBUG: ${event.data}`); // DEBUG
|
2023-12-30 15:02:13 +01:00
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
const msg = JSON.parse(event.data);
|
2023-12-29 16:55:51 +01:00
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
if ("mpd_status" in msg) {
|
|
|
|
if (msg.mpd_status == null) {
|
|
|
|
connection_state.innerHTML = "❌ Disconnected"; // ✅ Check Mark Button
|
|
|
|
} else {
|
|
|
|
// print error if present
|
|
|
|
if ("error" in msg.mpd_status) {
|
|
|
|
console.error(msg.mpd_status.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update "Update DB" button
|
|
|
|
if ("updating_db" in msg.mpd_status) {
|
|
|
|
control_update_db.disabled = true;
|
|
|
|
} else {
|
|
|
|
if (control_update_db.disabled) {
|
|
|
|
console.log("Database update done.")
|
|
|
|
}
|
|
|
|
control_update_db.disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update play/pause button
|
2024-02-25 21:48:18 +01:00
|
|
|
if ("state" in msg.mpd_status && msg.mpd_status.state !== "play") { // TODO: only update DOM if necessary
|
2023-12-30 15:02:13 +01:00
|
|
|
control_play_pause.innerHTML = "⏵︎"; // Play
|
2024-02-25 21:48:18 +01:00
|
|
|
} else {
|
|
|
|
control_play_pause.innerHTML = "⏸︎"; // Pause
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update playback time
|
2024-04-06 18:57:43 +02:00
|
|
|
if ("time" in msg.mpd_status) {
|
|
|
|
const [elapsed, duration] = msg.mpd_status.time.split(":", 2)
|
|
|
|
control_progress.value = elapsed;
|
|
|
|
control_progress.max = duration;
|
|
|
|
// triggers the update of control_time element
|
|
|
|
const e = new Event("input");
|
|
|
|
control_progress.dispatchEvent(e);
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update repeat state
|
|
|
|
if ("repeat" in msg.mpd_status) {
|
|
|
|
if (msg.mpd_status.repeat === "1") {
|
|
|
|
control_repeat.innerHTML = "🔴 repeat"; // 🔴 Red Circle
|
2024-01-21 14:55:31 +01:00
|
|
|
control_repeat.dataset.state = "on";
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
|
|
|
control_repeat.innerHTML = "🔘 repeat"; // 🔘 Radio Button
|
2024-01-21 14:55:31 +01:00
|
|
|
control_repeat.dataset.state = "off";
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update shuffle state
|
|
|
|
if ("random" in msg.mpd_status) {
|
|
|
|
if (msg.mpd_status.random === "1") {
|
|
|
|
control_shuffle.innerHTML = "🔴 shuffle"; // 🔴 Red Circle
|
2024-01-21 14:55:31 +01:00
|
|
|
control_shuffle.dataset.state = "on";
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
|
|
|
control_shuffle.innerHTML = "🔘 shuffle"; // 🔘 Radio Button
|
2024-01-21 14:55:31 +01:00
|
|
|
control_shuffle.dataset.state = "off";
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update crossfade state
|
|
|
|
if ("xfade" in msg.mpd_status) {
|
|
|
|
control_xfade.value = msg.mpd_status.xfade;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update volume
|
|
|
|
if ("volume" in msg.mpd_status) {
|
|
|
|
control_volume.value = msg.mpd_status.volume;
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 16:55:51 +01:00
|
|
|
}
|
2023-12-30 15:02:13 +01:00
|
|
|
|
|
|
|
// update song info
|
|
|
|
if ("mpd_current_song" in msg && msg.mpd_current_song != null) {
|
2024-01-21 20:29:00 +01:00
|
|
|
let track;
|
2023-12-30 15:02:13 +01:00
|
|
|
if ("Artist" in msg.mpd_current_song && "Title" in msg.mpd_current_song) {
|
2024-04-06 18:57:43 +02:00
|
|
|
track = `${msg.mpd_current_song.Artist} - ${msg.mpd_current_song.Title}`
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
2024-04-06 18:57:43 +02:00
|
|
|
track = msg.mpd_current_song.file;
|
2024-01-21 20:29:00 +01:00
|
|
|
}
|
2024-04-06 18:57:43 +02:00
|
|
|
if (control_track.innerHTML !== `<span>${track}</span>`) {
|
|
|
|
control_track.innerHTML = `<span>${track}</span>`;
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
2023-12-29 16:55:51 +01:00
|
|
|
}
|
2023-12-30 15:02:13 +01:00
|
|
|
|
2024-01-20 11:40:13 +01:00
|
|
|
// update queue
|
|
|
|
if ("mpd_queue" in msg && msg.mpd_queue != null) {
|
|
|
|
const tbody = document.createElement("tbody");
|
2024-04-06 18:57:43 +02:00
|
|
|
msg.mpd_queue.forEach(song => {
|
2024-01-20 11:40:13 +01:00
|
|
|
const tr = document.createElement("tr");
|
2024-04-06 18:57:43 +02:00
|
|
|
tr.dataset.song_id = song.Id;
|
|
|
|
if ("songid" in msg.mpd_status && msg.mpd_status.songid === song.Id) {
|
2024-02-25 21:48:18 +01:00
|
|
|
tr.classList.add("playing");
|
|
|
|
}
|
2024-01-20 11:40:13 +01:00
|
|
|
const pos = document.createElement("td");
|
2024-04-06 18:57:43 +02:00
|
|
|
pos.innerText = song.Pos;
|
2024-01-20 11:40:13 +01:00
|
|
|
const artist = document.createElement("td");
|
2024-04-06 18:57:43 +02:00
|
|
|
if ("Artist" in song) {
|
|
|
|
artist.innerText = song.Artist;
|
2024-01-20 11:40:13 +01:00
|
|
|
}
|
|
|
|
const track = document.createElement("td");
|
2024-04-06 18:57:43 +02:00
|
|
|
if ("Title" in song) {
|
|
|
|
track.innerText = song.Title;
|
2024-01-20 11:40:13 +01:00
|
|
|
} else {
|
2024-04-06 18:57:43 +02:00
|
|
|
track.innerText = song.file;
|
2024-01-20 11:40:13 +01:00
|
|
|
}
|
|
|
|
const album = document.createElement("td");
|
2024-04-06 18:57:43 +02:00
|
|
|
// TODO: Do songs have album info attached to them?
|
|
|
|
album.innerText = "";
|
2024-01-20 11:40:13 +01:00
|
|
|
const length = document.createElement("td");
|
2024-04-06 18:57:43 +02:00
|
|
|
length.innerText = secondsToTrackTime(song.duration);
|
2024-01-20 11:40:13 +01:00
|
|
|
const actions = document.createElement("td");
|
2024-04-06 19:27:59 +02:00
|
|
|
const moveUp = document.createElement("button");
|
|
|
|
moveUp.classList.add("borderless");
|
2024-04-06 18:57:43 +02:00
|
|
|
if (parseInt(song.Pos) !== 0) {
|
|
|
|
moveUp.innerHTML = "🔺"; // 🔺 Red Triangle Pointed Down
|
|
|
|
moveUp.addEventListener("click", event => { moveTrackInQueue(event, -1) });
|
|
|
|
} else {
|
2024-04-06 19:27:59 +02:00
|
|
|
moveUp.innerHTML = " ";
|
2024-04-06 18:57:43 +02:00
|
|
|
}
|
2024-04-06 19:27:59 +02:00
|
|
|
const moveDown = document.createElement("button");
|
|
|
|
moveDown.classList.add("borderless");
|
2024-04-06 18:57:43 +02:00
|
|
|
if (parseInt(song.Pos) !== msg.mpd_queue.length - 1) {
|
|
|
|
moveDown.innerHTML = "🔻"; // 🔻 Red Triangle Pointed Up
|
|
|
|
moveDown.addEventListener("click", event => {moveTrackInQueue(event, 1)});
|
|
|
|
} else {
|
2024-04-06 19:27:59 +02:00
|
|
|
moveDown.innerHTML = " ";
|
2024-04-06 18:57:43 +02:00
|
|
|
}
|
2024-01-21 14:55:31 +01:00
|
|
|
const remove = document.createElement("button");
|
|
|
|
remove.classList.add("borderless");
|
|
|
|
remove.innerHTML = "❌"; // ❌ Cross mark
|
2024-04-06 18:57:43 +02:00
|
|
|
remove.addEventListener("click", removeTrackFromQueue);
|
2024-04-06 19:27:59 +02:00
|
|
|
actions.appendChild(moveUp);
|
|
|
|
actions.appendChild(moveDown);
|
2024-01-21 14:55:31 +01:00
|
|
|
actions.appendChild(remove);
|
2024-01-20 11:40:13 +01:00
|
|
|
tr.appendChild(pos);
|
|
|
|
tr.appendChild(artist);
|
|
|
|
tr.appendChild(track);
|
|
|
|
tr.appendChild(album);
|
|
|
|
tr.appendChild(length);
|
|
|
|
tr.appendChild(actions);
|
|
|
|
tbody.appendChild(tr);
|
|
|
|
});
|
|
|
|
const currentQueue = document.querySelector("#queue tbody")
|
|
|
|
if (currentQueue.innerHTML !== tbody.innerHTML) {
|
|
|
|
console.log("Updating queue")
|
|
|
|
currentQueue.outerHTML = tbody.outerHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
if ("mpd_error" in msg) {
|
|
|
|
console.error(`MPD Error: ${msg.mpd_error}`)
|
2023-12-29 16:55:51 +01:00
|
|
|
}
|
|
|
|
});
|
2023-12-30 15:02:13 +01:00
|
|
|
|
|
|
|
// Request MPD status every second
|
|
|
|
window.setInterval(() => {
|
|
|
|
if (socket.readyState === socket.OPEN) {
|
|
|
|
socket.send("#status");
|
2024-04-06 18:57:43 +02:00
|
|
|
connection_state.innerHTML = "✅ Connected"; // ✅ Check Mark Button
|
2023-12-30 15:02:13 +01:00
|
|
|
} else {
|
2024-04-06 18:57:43 +02:00
|
|
|
connection_state.innerHTML = "❌ Disconnected"; // ❌ Cross Mark
|
2023-12-30 15:02:13 +01:00
|
|
|
}
|
|
|
|
}, 1000);
|
2024-04-17 15:12:12 +02:00
|
|
|
|