diff options
Diffstat (limited to 'static/modules/music.js')
| -rw-r--r-- | static/modules/music.js | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/static/modules/music.js b/static/modules/music.js index e127969..4a4ce05 100644 --- a/static/modules/music.js +++ b/static/modules/music.js @@ -19,6 +19,25 @@ function clock(s) { return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; } +// Minutes as "2h 05m", or "59m" under an hour. +export function hours(min) { + min = Math.max(0, Math.floor(min || 0)); + return min < 60 ? `${min}m` : `${Math.floor(min / 60)}h ${String(min % 60).padStart(2, "0")}m`; +} + +// The game the card shows instead of the music, from /api/games and +// /api/xbox: a PC game first, then the Xbox's. +export function pickGame(pc, xbox) { + if (pc?.playing) return pc.playing; + const g = xbox?.playing; + if (!g) return null; + const t = (xbox.titles ?? []).find((x) => x.id === g.id); + return { platform: "xbox", name: g.name, cover: t?.cover ?? null, minutes: t?.minutes ?? null, + session: xbox.session }; +} + +const PLATFORM = { steam: "Steam", ps1: "PlayStation", ps2: "PlayStation 2", xbox: "Xbox" }; + async function post(action, body = {}) { try { await fetch(`/api/music/${action}`, { @@ -69,6 +88,16 @@ export default { <span class="level num"></span> </div> </div> + </div> + <div class="music game" hidden> + <div class="top"> + <img class="art" alt="" hidden><div class="art empty nf"></div> + <div class="meta"> + <div class="artist plat muted"></div> + <div class="title"></div> + <div class="album times num"></div> + </div> + </div> </div>`; el.querySelector(".controls").prepend( button(G.prev, "prev", "Previous"), button(G.play, "play", "Play/pause"), @@ -87,18 +116,33 @@ export default { seek.addEventListener("change", () => post("seek", { position: Number(seek.value) })); }, - update(st, el) { + update(st, el, game = null) { + const [musicEl, gameEl] = el.querySelectorAll(":scope > .music"); + musicEl.hidden = Boolean(game); + gameEl.hidden = !game; + if (game) { + const img = gameEl.querySelector("img.art"); + img.hidden = !game.cover; + gameEl.querySelector(".art.empty").hidden = Boolean(game.cover); + if (game.cover && img.getAttribute("src") !== game.cover) img.src = game.cover; + gameEl.querySelector(".plat").textContent = PLATFORM[game.platform] ?? ""; + gameEl.querySelector(".title").textContent = game.name ?? ""; + const session = game.session ? (Date.now() / 1000 - game.session) / 60 : 0; + gameEl.querySelector(".times").textContent = game.minutes != null + ? `${hours(session)} now · ${hours(game.minutes + session)} total` : `${hours(session)} now`; + return; + } if (st.art) { - let img = el.querySelector("img.art"); + let img = musicEl.querySelector("img.art"); if (!img) { img = document.createElement("img"); img.className = "art"; img.alt = ""; - el.querySelector(".art").replaceWith(img); + musicEl.querySelector(".art").replaceWith(img); } if (img.getAttribute("src") !== st.art) img.src = st.art; } else { - const img = el.querySelector("img.art"); + const img = musicEl.querySelector("img.art"); if (img) { const empty = document.createElement("div"); empty.className = "art empty"; @@ -106,24 +150,24 @@ export default { img.replaceWith(empty); } } - 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; + musicEl.querySelector(".title").textContent = st.title || "Nothing playing"; + musicEl.querySelector(".artist").textContent = st.artist || ""; + musicEl.querySelector(".album").textContent = st.album || ""; + musicEl.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play; const muted = Boolean(st.volume?.muted); - const mute = el.querySelector("button.mute"); + const mute = musicEl.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"); + const vol = musicEl.querySelector(".vol input"); vol.classList.toggle("muted", muted); if (!el._sliding) { vol.value = st.volume?.level ?? 0; - el.querySelector(".level").textContent = `${vol.value}%`; + musicEl.querySelector(".level").textContent = `${vol.value}%`; } - const seek = el.querySelector(".seek input"); + const seek = musicEl.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)}`; + musicEl.querySelector(".time").textContent = `${clock(st.position)} / ${clock(st.length)}`; }, }; |
