Files
mixly3/common/modules/mixly-modules/common/component.js

110 lines
1.9 KiB
JavaScript

goog.loadJs('common', () => {
goog.require('Mixly.Events');
goog.require('Mixly.IdGenerator');
goog.provide('Mixly.Component');
const {
Events,
IdGenerator
} = Mixly;
class Component {
#$content_ = null;
#mounted_ = false;
#disposed_ = false;
#events_ = new Events(['destroyed']);
#id_ = IdGenerator.generate();
constructor() {}
mountOn($container) {
if (this.#mounted_) {
this.unmount();
}
$container.append(this.#$content_);
this.onMounted();
}
unmount() {
this.#$content_ && this.#$content_.detach();
this.onUnmounted();
}
onMounted() {
this.#mounted_ = true;
}
onUnmounted() {
this.#mounted_ = false;
}
isMounted() {
return this.#mounted_;
}
isDisposed() {
return this.#disposed_;
}
setContent($elem) {
if (this.#$content_) {
this.#$content_.replaceWith($elem);
}
this.#$content_ = $elem;
$elem.attr('page-id', this.#id_);
}
getContent() {
return this.#$content_;
}
resize() {}
getId() {
return this.#id_;
}
setId(id) {
this.#id_ = id;
}
dispose() {
this.#$content_.remove();
this.#$content_ = null;
this.runEvent('destroyed');
this.resetEvent();
this.#events_ = null;
this.#disposed_ = true;
this.#mounted_ = false;
}
bind(type, func) {
return this.#events_.bind(type, func);
}
unbind(id) {
this.#events_.unbind(id);
}
addEventsType(eventsType) {
this.#events_.addType(eventsType);
}
runEvent(eventsType, ...args) {
return this.#events_.run(eventsType, ...args);
}
offEvent(eventsType) {
this.#events_.off(eventsType);
}
resetEvent() {
this.#events_.reset();
}
}
Mixly.Component = Component;
});