cyp/app/js/app.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-03-09 05:11:46 +08:00
import "./lib/range.js";
import "./menu.js";
import "./player.js";
import "./queue.js";
2019-03-22 22:35:04 +08:00
import * as mpd from "./lib/mpd.js";
2020-03-09 05:11:46 +08:00
import * as mpdMock from "./lib/mpd-mock.js";
2019-03-29 05:52:57 +08:00
import * as html from "./lib/html.js";
2019-03-22 22:35:04 +08:00
2019-03-25 22:49:23 +08:00
import * as library from "./library.js";
import * as fs from "./fs.js";
2019-03-28 22:23:28 +08:00
import * as playlists from "./playlists.js";
2019-03-29 04:43:18 +08:00
import * as yt from "./yt.js";
2019-04-03 01:52:53 +08:00
import * as settings from "./settings.js";
2019-03-22 22:35:04 +08:00
2019-03-29 05:52:57 +08:00
function initIcons() {
2020-03-09 16:26:10 +08:00
Array.from(document.querySelectorAll("[data-icon]")).forEach(/** @param {HTMLElement} node */ node => {
2019-03-29 05:52:57 +08:00
let icon = html.icon(node.dataset.icon);
node.insertBefore(icon, node.firstChild);
});
}
2020-03-09 05:11:46 +08:00
class App extends HTMLElement {
2020-03-09 16:26:10 +08:00
static get observedAttributes() { return ["component"]; }
2020-03-09 05:11:46 +08:00
constructor() {
super();
2019-03-26 19:35:47 +08:00
2020-03-09 16:26:10 +08:00
initIcons();
2020-03-09 05:11:46 +08:00
this._load();
2019-03-22 22:35:04 +08:00
}
2020-03-09 16:26:10 +08:00
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case "component":
const e = new CustomEvent("component-change");
this.dispatchEvent(e);
break;
}
}
2019-03-20 03:45:23 +08:00
2020-03-09 05:11:46 +08:00
async _load() {
2020-03-09 16:26:10 +08:00
try {
await mpd.init();
this.mpd = mpd;
} catch (e) {
console.error(e);
this.mpd = mpdMock;
}
2020-03-09 05:11:46 +08:00
const promises = ["cyp-player"].map(name => customElements.whenDefined(name));
await Promise.all(promises);
2019-03-22 22:35:04 +08:00
2020-03-09 16:26:10 +08:00
this.dispatchEvent(new CustomEvent("load"));
2020-03-09 05:11:46 +08:00
const onHashChange = () => {
const hash = location.hash.substring(1);
this._activate(hash || "queue");
}
window.addEventListener("hashchange", onHashChange);
onHashChange();
}
_activate(what) {
location.hash = what;
this.setAttribute("component", what);
}
2020-03-09 01:06:54 +08:00
}
customElements.define("cyp-app", App);