// A small cassette player on the desk: a cassette window with two reels that turn while the music plays, // the track title on the cassette label, and five vintage keys (previous, play, pause, stop, next). import { t } from "../i18n/i18n.js"; import { $ } from "./desk.js"; const KEYS = [ ["prev", ``], ["play", ``], ["pause", ``], ["stop", ``], ["next", ``], ]; const reel = (side) => ` `; export function createWalkman(root, { music, onPress = () => {} }) { root.innerHTML = `
VALDOR${t("walkman.model")}
${reel("left")}
${reel("right")}
${KEYS.map(([act, icon]) => ` `).join("")}
`; const title = $(root, ".walkman__title"); const label = $(root, ".walkman__label"); root.addEventListener("click", (e) => { const b = e.target.closest("button[data-act]"); if (!b || b.disabled) return; onPress(); music[b.dataset.act](); }); function update() { const m = music.info(); const playing = m.state === "playing"; root.classList.toggle("is-playing", playing); root.classList.toggle("is-empty", !m.available); root.title = m.available ? "" : t("walkman.hint"); let text = t("walkman.empty"); if (m.available) text = m.waitingForGesture ? t("walkman.tap") : m.state === "stopped" ? t("walkman.stopped") : m.title; if (title.textContent !== text) { title.textContent = text; title.classList.remove("is-scroll"); // a long title scrolls across the label while the tape runs requestAnimationFrame(() => title.classList.toggle("is-scroll", title.scrollWidth > label.clientWidth + 2)); } for (const b of root.querySelectorAll("button[data-act]")) { b.disabled = !m.available; if (b.dataset.act === "play") b.setAttribute("aria-pressed", String(playing)); if (b.dataset.act === "pause") b.setAttribute("aria-pressed", String(m.state === "paused")); if (b.dataset.act === "stop") b.setAttribute("aria-pressed", String(m.state === "stopped")); } } music.setListener(update); return { update }; }