@aurelia-mdc-web/base
Version:
Base classes for Aurelia Material Components Web
60 lines • 2.07 kB
JavaScript
export class MdcComponent {
constructor(root) {
this.root = root;
this.initialised = this.createInitiliasedPromise();
}
async createInitiliasedPromise() {
return new Promise(r => this.initialisedResolve = r);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async initialise() { }
// eslint-disable-next-line @typescript-eslint/no-empty-function
initialSyncWithDOM() { }
async attached() {
this.continueAttaching = true;
await this.initialise();
// detached might be called straight after attached starts
// do not continue attaching if that's the case
if (!this.continueAttaching) {
return;
}
this.foundation = this.getDefaultFoundation();
this.foundation.init();
this.initialisedResolve();
this.initialSyncWithDOM();
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
destroy() { }
detached() {
this.continueAttaching = false;
this.destroy();
this.foundation?.destroy();
this.foundation = undefined;
this.initialised = this.createInitiliasedPromise();
}
listen(evtType, handler, options) {
this.root.addEventListener(evtType, handler, options);
}
unlisten(evtType, handler, options) {
this.root.removeEventListener(evtType, handler, options);
}
/**
* @hidden
* Fires a cross-browser-compatible custom event from the component root of the given type, with the given data.
*/
emit(evtType, evtData, shouldBubble = false) {
let evt;
if (typeof CustomEvent === 'function') {
evt = new CustomEvent(evtType, {
bubbles: shouldBubble,
detail: evtData,
});
}
else {
evt = document.createEvent('CustomEvent');
evt.initCustomEvent(evtType, shouldBubble, false, evtData);
}
this.root.dispatchEvent(evt);
}
}
//# sourceMappingURL=mdc-component.js.map