aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/music.js
blob: de9df7f423cf570a8b97bab129a5d8c6dd4ff661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
async function post(action) {
  try {
    await fetch(`/api/music/${action}`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: "{}",
    });
  } catch {
    // the next poll shows the real state
  }
}

function button(label, action) {
  const b = document.createElement("button");
  b.textContent = label;
  b.onclick = () => post(action);
  return b;
}

export default {
  title: "Music",
  interval: 2,

  mount(el) {
    const box = document.createElement("div");
    box.className = "music";
    box.innerHTML = `
      <div class="now"><b class="title">—</b><span class="artist muted"></span></div>
      <div class="row controls"></div>
      <div class="row vol"></div>`;
    box.querySelector(".controls").append(
      button("⏮", "prev"), button("⏯", "play"), button("⏭", "next"));
    box.querySelector(".vol").append(
      button("Vol−", "voldown"), button("Mute", "mute"), button("Vol+", "volup"));
    el.append(box);
  },

  update(st, el) {
    const box = el.querySelector(".music");
    box.querySelector(".title").textContent = st.title || "Nothing playing";
    box.querySelector(".artist").textContent = st.artist || "";
    box.querySelector(".controls button:nth-child(2)").textContent =
      st.status === "Playing" ? "⏸" : "▶";
    const mute = box.querySelector(".vol button:nth-child(2)");
    mute.classList.toggle("on", Boolean(st.volume?.muted));
    mute.textContent = st.volume?.muted ? "Unmute" : "Mute";
  },
};