cyp/app/js/app.js

69 lines
1.4 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() {
Array.from(document.querySelectorAll("[data-icon]")).forEach(node => {
let icon = html.icon(node.dataset.icon);
node.insertBefore(icon, node.firstChild);
});
}
2020-03-09 05:11:46 +08:00
async function mpdExecutor(resolve, reject) {
2019-04-12 19:34:28 +08:00
try {
await mpd.init();
2020-03-09 05:11:46 +08:00
resolve(mpd);
2019-04-12 19:34:28 +08:00
} catch (e) {
2020-03-09 05:11:46 +08:00
resolve(mpdMock);
2019-04-12 19:34:28 +08:00
console.error(e);
2020-03-09 05:11:46 +08:00
reject(e);
2019-04-12 19:34:28 +08:00
}
2020-03-09 05:11:46 +08:00
}
class App extends HTMLElement {
constructor() {
super();
initIcons();
2019-03-26 19:35:47 +08:00
2020-03-09 05:11:46 +08:00
this._mpd = new Promise(mpdExecutor);
this._load();
2019-03-22 22:35:04 +08:00
}
2020-03-09 05:11:46 +08:00
get mpd() { return this._mpd; }
2019-03-20 03:45:23 +08:00
2020-03-09 05:11:46 +08:00
async _load() {
const promises = ["cyp-player"].map(name => customElements.whenDefined(name));
await Promise.all(promises);
2019-03-22 22:35:04 +08:00
2020-03-09 05:11:46 +08:00
const onHashChange = () => {
const hash = location.hash.substring(1);
this._activate(hash || "queue");
}
2020-03-09 01:06:54 +08:00
2020-03-09 05:11:46 +08:00
window.addEventListener("hashchange", onHashChange);
onHashChange();
}
_activate(what) {
location.hash = what;
this.setAttribute("component", what);
const component = this.querySelector(`cyp-${what}`);
// component.activate();
}
2020-03-09 01:06:54 +08:00
}
customElements.define("cyp-app", App);