redom
Version:
Tiny turboboosted JavaScript library for creating user interfaces.
76 lines (62 loc) • 1.74 kB
JavaScript
/* global Node */
import { text } from "./text.js";
import { mount } from "./mount.js";
import { unmount } from "./unmount.js";
import { getEl } from "./util.js";
export function place(View, initData) {
return new Place(View, initData);
}
export class Place {
constructor(View, initData) {
this.el = text("");
this.visible = false;
this.view = null;
this._placeholder = this.el;
if (View instanceof Node) {
this._el = View;
} else if (View.el instanceof Node) {
this._el = View;
this.view = View;
} else {
this._View = View;
}
this._initData = initData;
}
update(visible, data) {
const placeholder = this._placeholder;
const parentNode = this.el.parentNode;
if (visible) {
if (!this.visible) {
if (this._el) {
mount(parentNode, this._el, placeholder);
unmount(parentNode, placeholder);
this.el = getEl(this._el);
this.visible = visible;
} else {
const View = this._View;
const view = new View(this._initData);
this.el = getEl(view);
this.view = view;
mount(parentNode, view, placeholder);
unmount(parentNode, placeholder);
}
}
this.view?.update?.(data);
} else {
if (this.visible) {
if (this._el) {
mount(parentNode, placeholder, this._el);
unmount(parentNode, this._el);
this.el = placeholder;
this.visible = visible;
return;
}
mount(parentNode, placeholder, this.view);
unmount(parentNode, this.view);
this.el = placeholder;
this.view = null;
}
}
this.visible = visible;
}
}