// deck-it - music panel // Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. function clock(s) { s = Math.max(0, Math.floor(s || 0)); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; } async function post(action, body = {}) { try { await fetch(`/api/music/${action}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); } catch { // the next poll shows the real state } } // Nerd Font glyphs, as on the other tabs. const G = { prev: "\uf048", play: "\uf04b", pause: "\uf04c", next: "\uf051", vol: "\uf028", muted: "\uf026" }; function button(glyph, action, label) { const b = document.createElement("button"); b.className = `nf ${action}`; b.textContent = glyph; b.setAttribute("aria-label", label); b.onclick = () => post(action); return b; } export default { title: "Music", interval: 2, mount(el) { el.innerHTML = `
♪
Nothing playing
0:00
`; el.querySelector(".controls").prepend( button(G.prev, "prev", "Previous"), button(G.play, "play", "Play/pause"), button(G.next, "next", "Next")); el.querySelector(".vol").prepend(button(G.vol, "mute", "Mute")); const vol = el.querySelector(".vol input"); vol.addEventListener("pointerdown", () => { el._sliding = true; }); vol.addEventListener("pointerup", () => { el._sliding = false; }); vol.addEventListener("input", () => { el.querySelector(".level").textContent = `${vol.value}%`; post("volume", { level: Number(vol.value) }); }); const seek = el.querySelector(".seek input"); seek.addEventListener("pointerdown", () => { el._dragging = true; }); seek.addEventListener("pointerup", () => { el._dragging = false; }); seek.addEventListener("change", () => post("seek", { position: Number(seek.value) })); }, update(st, el) { if (st.art) { let img = el.querySelector("img.art"); if (!img) { img = document.createElement("img"); img.className = "art"; img.alt = ""; el.querySelector(".art").replaceWith(img); } if (img.getAttribute("src") !== st.art) img.src = st.art; } el.querySelector(".title").textContent = st.title || "Nothing playing"; el.querySelector(".artist").textContent = st.artist || ""; el.querySelector(".album").textContent = st.album || ""; el.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play; const muted = Boolean(st.volume?.muted); const mute = el.querySelector("button.mute"); mute.classList.toggle("on", muted); mute.textContent = muted ? G.muted : G.vol; mute.setAttribute("aria-label", muted ? "Unmute" : "Mute"); const vol = el.querySelector(".vol input"); vol.classList.toggle("muted", muted); if (!el._sliding) { vol.value = st.volume?.level ?? 0; el.querySelector(".level").textContent = `${vol.value}%`; } const seek = el.querySelector(".seek input"); seek.max = Math.max(1, Math.floor(st.length || 0)); if (!el._dragging) seek.value = Math.min(Number(seek.max), Math.floor(st.position || 0)); el.querySelector(".time").textContent = `${clock(st.position)} / ${clock(st.length)}`; }, };