mvdom
Version:
deprecated - Moved to dom-native package
68 lines • 2.26 kB
JavaScript
import { bindOnEvents, off } from './event';
import { bindHubEvents, unbindHubEvents } from './hub';
import { bindOnEventsDecorators, unbindOnEventsDecorators } from './ts-decorator-on-event';
import { bindOnHubDecorators, unbindOnHubDecorators } from './ts-decorator-on-hub';
let c_seq = 0;
export class BaseHTMLElement extends HTMLElement {
constructor() {
super();
this._init = false;
this.uid = 'c_uid_' + c_seq++;
this._nsObj = { ns: this.uid };
}
get initialized() { return this._init; }
init() { }
connectedCallback() {
if (!this._init) {
bindComponentEvents.call(this);
bindOnEventsDecorators.call(this);
bindOnHubDecorators.call(this);
this.init();
this._init = true;
}
if (this.preDisplay) {
requestAnimationFrame(() => { this.preDisplay(); });
}
if (this.postDisplay) {
requestAnimationFrame(() => { requestAnimationFrame(() => { this.postDisplay(); }); });
}
}
disconnectedCallback() {
if (this.docEvents) {
off(document, this._nsObj);
}
if (this.winEvents) {
off(window, this._nsObj);
}
if (this.hubEvents) {
unbindHubEvents(this.hubEvents, this._nsObj);
}
unbindOnEventsDecorators.call(this);
unbindOnHubDecorators.call(this);
}
attributeChangedCallback(attrName, oldVal, newVal) { }
}
export function addDOMEvents(target, source) {
return Object.assign(target || {}, source);
}
export function addHubEvents(target, source) {
const t = (target == null) ? [] : (target instanceof Array) ? target : [target];
(source instanceof Array) ? t.push(...source) : t.push(source);
return t;
}
function bindComponentEvents() {
const opts = { ns: this._nsObj.ns, ctx: this };
if (this.events) {
bindOnEvents(this, this.events, opts);
}
if (this.docEvents) {
bindOnEvents(document, this.docEvents, opts);
}
if (this.winEvents) {
bindOnEvents(window, this.winEvents, opts);
}
if (this.hubEvents) {
bindHubEvents(this.hubEvents, opts);
}
}
//# sourceMappingURL=c-base.js.map