cyp/app/js/queue.js

49 lines
1,008 B
JavaScript
Raw Normal View History

2019-03-22 22:35:04 +08:00
import * as mpd from "./lib/mpd.js";
import * as html from "./lib/html.js";
import * as pubsub from "./lib/pubsub.js";
2019-03-26 17:09:26 +08:00
import * as ui from "./lib/ui.js";
2019-03-22 22:35:04 +08:00
let node;
let currentId;
function updateCurrent() {
let all = Array.from(node.querySelectorAll("[data-song-id]"));
all.forEach(node => {
node.classList.toggle("current", node.dataset.songId == currentId);
});
}
function buildSongs(songs) {
2019-03-25 22:49:23 +08:00
let ul = node.querySelector("ul");
html.clear(ul);
2019-03-22 22:35:04 +08:00
2019-03-26 17:09:26 +08:00
songs.map(song => ui.song(ui.SONG_QUEUE, song, ul));
2019-03-22 22:35:04 +08:00
updateCurrent();
}
function onSongChange(message, publisher, data) {
currentId = data["Id"];
updateCurrent();
}
2019-03-26 17:09:26 +08:00
function onQueueChange(message, publisher, data) {
syncQueue();
}
async function syncQueue() {
2019-03-22 22:35:04 +08:00
let songs = await mpd.listQueue();
buildSongs(songs);
2019-03-26 17:09:26 +08:00
// FIXME updateCount(songs.length)
}
export async function activate() {
syncQueue();
2019-03-22 22:35:04 +08:00
}
export function init(n) {
node = n;
pubsub.subscribe("song-change", onSongChange);
2019-03-26 17:09:26 +08:00
pubsub.subscribe("queue-change", onQueueChange);
2019-03-22 22:35:04 +08:00
}