cyp/app/js/queue.js

77 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-03-22 22:35:04 +08:00
import * as html from "./lib/html.js";
2019-03-26 17:09:26 +08:00
import * as ui from "./lib/ui.js";
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
import Component from "./component.js";
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
class Queue extends Component {
constructor() {
super();
this._currentId = null;
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
this.querySelector(".clear").addEventListener("click", async _ => {
const mpd = await this._mpd;
await mpd.command("clear");
this._sync();
});
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
this.querySelector(".save").addEventListener("click", async _ => {
let name = prompt("Save current queue as a playlist?", "name");
if (name === null) { return; }
const mpd = await this._mpd;
mpd.command(`save "${mpd.escape(name)}"`);
});
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
this._app.then(app => {
app.addEventListener("song-change", this);
app.addEventListener("queue-change", this);
})
this._sync();
}
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
handleEvent(e) {
switch (e.type) {
case "song-change":
this._currentId = e.detail["Id"];
this._updateCurrent();
break;
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
case "queue-change":
this._sync();
break;
}
}
2019-03-26 17:09:26 +08:00
2020-03-09 05:11:46 +08:00
_onComponentChange(c, isThis) {
this.hidden = !isThis;
2019-03-26 17:09:26 +08:00
2020-03-09 05:11:46 +08:00
isThis && this._sync();
}
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
async _sync() {
const mpd = await this._mpd;
let songs = await mpd.listQueue();
this._buildSongs(songs);
// FIXME pubsub?
document.querySelector("#queue-length").textContent = `(${songs.length})`;
}
_updateCurrent() {
let all = Array.from(this.querySelectorAll("[data-song-id]"));
all.forEach(node => {
node.classList.toggle("current", node.dataset.songId == this._currentId);
});
}
_buildSongs(songs) {
let ul = this.querySelector("ul");
html.clear(ul);
songs.map(song => ui.song(ui.CTX_QUEUE, song, ul));
this._updateCurrent();
}
2019-03-22 22:35:04 +08:00
}
2020-03-09 05:11:46 +08:00
customElements.define("cyp-queue", Queue);