cyp/app/js/component.js

33 lines
888 B
JavaScript
Raw Normal View History

2020-03-10 05:24:31 +08:00
import Selection from "./selection.js";
2020-03-09 05:11:46 +08:00
2020-03-09 16:26:10 +08:00
export class HasApp extends HTMLElement {
get _app() { return this.closest("cyp-app"); }
get _mpd() { return this._app.mpd; }
}
2020-03-10 05:24:31 +08:00
export class Item extends HasApp {
constructor() {
super();
this.addEventListener("click", _ => this.parentNode.selection.toggle(this));
}
}
2020-03-09 16:26:10 +08:00
export default class Component extends HasApp {
2020-03-10 17:42:13 +08:00
constructor(options = {}) {
2020-03-09 05:11:46 +08:00
super();
2020-03-10 17:07:30 +08:00
if (options.selection) { this.selection = new Selection(this, options.selection); }
2020-03-09 21:26:39 +08:00
}
2020-03-09 05:11:46 +08:00
2020-03-09 21:26:39 +08:00
connectedCallback() {
2020-03-09 16:26:10 +08:00
this._app.addEventListener("load", _ => this._onAppLoad());
this._app.addEventListener("component-change", _ => {
const component = this._app.getAttribute("component");
const isThis = (this.nodeName.toLowerCase() == `cyp-${component}`);
this._onComponentChange(component, isThis);
2020-03-09 05:11:46 +08:00
});
}
2020-03-09 16:26:10 +08:00
_onAppLoad() {}
2020-03-10 05:24:31 +08:00
_onComponentChange(_component, _isThis) {}
2020-03-09 01:06:54 +08:00
}