cyp/app/js/status.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-03-20 03:45:23 +08:00
import * as mpd from "./mpd.js";
2019-03-20 23:20:17 +08:00
import * as art from "./art.js";
2019-03-20 03:45:23 +08:00
const DELAY = 2000;
2019-03-20 05:56:39 +08:00
const DOM = {};
2019-03-20 03:45:23 +08:00
2019-03-20 23:20:17 +08:00
let current = {};
2019-03-20 03:45:23 +08:00
async function tick() {
2019-03-20 23:20:17 +08:00
let data = await mpd.status();
update(data);
2019-03-20 05:56:39 +08:00
// console.log(data);
2019-03-20 03:45:23 +08:00
setTimeout(tick, DELAY);
}
2019-03-20 23:20:17 +08:00
function update(data) {
if (data["Title"] != current["Title"]) { DOM.title.textContent = data["Title"]; }
if (data["Artist"] != current["Artist"] || data["Album"] != current["Album"]) {
DOM.art.innerHTML = "";
art.get(data["Artist"], data["Album"], data["file"]).then(src => {
if (!src) { return; }
let image = document.createElement("img");
image.src = src;
DOM.art.appendChild(image);
});
}
current = data;
}
async function play() {
let data = await mpd.commandAndStatus("pause 0");
update(data);
}
async function pause() {
let data = await mpd.commandAndStatus("pause 1");
update(data);
}
2019-03-20 03:45:23 +08:00
export function init() {
2019-03-20 23:20:17 +08:00
let all = document.querySelectorAll("footer [class]");
Array.from(all).forEach(node => DOM[node.className] = node);
DOM.play.addEventListener("click", e => play());
DOM.pause.addEventListener("click", e => pause());
2019-03-20 05:56:39 +08:00
2019-03-20 03:45:23 +08:00
tick();
2019-03-20 23:20:17 +08:00
}