@dooboostore/dom-render
Version:
html view template engine
150 lines (149 loc) • 6.68 kB
JavaScript
import { DomRenderProxy } from "./DomRenderProxy.js";
import { PathRouter } from "@dooboostore/core-web";
import { HashRouter } from "@dooboostore/core-web";
import { DomRenderFinalProxy } from "./types/Types.js";
import { RawSet } from "./rawsets/RawSet.js";
import { DefaultMessenger } from "./messenger/DefaultMessenger.js";
import { drComponent } from "./components/index.js";
import { EventManager } from "./events/EventManager.js";
import { RandomUtils } from "@dooboostore/core";
let eventManager = null;
class DomRender {
constructor(parameter, executeConfig) {
const s = DomRender.runSet(parameter, executeConfig);
this._config = s.config;
this._target = s.target;
this._rootObject = s.rootObject;
this._router = s.config.router;
this._eventManager = s.config.eventManager;
}
get rootObject() {
return this._rootObject;
}
get config() {
return this._config;
}
get router() {
return this._router;
}
get eventManager() {
return this._eventManager;
}
get target() {
return this._target;
}
static run(data) {
return DomRender.runSet(data).rootObject;
}
static runSet({
rootObject,
target,
config
}, executeConfig) {
rootObject = typeof rootObject === "function" ? rootObject() : rootObject;
let targetObject = rootObject;
if ("_DomRender_isProxy" in rootObject) {
if (target) {
rootObject._DomRender_proxy.initRender(target);
}
targetObject = rootObject;
return { rootObject: targetObject, target, config: rootObject._domRender_config };
}
const targetConfig = Object.assign({}, config);
eventManager ?? (eventManager = new EventManager(targetConfig.window));
targetConfig.uuid = RandomUtils.uuid4();
targetConfig.messenger = DomRenderFinalProxy.final(targetConfig.messenger ?? new DefaultMessenger(targetConfig));
targetConfig.proxyExcludeTyps = targetConfig.proxyExcludeTyps ?? [];
targetConfig.targetElements ?? (targetConfig.targetElements = []);
if (typeof Window !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === Window)) {
targetConfig.proxyExcludeTyps.push(Window);
}
if (typeof Map !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === Map)) {
targetConfig.proxyExcludeTyps.push(Map);
}
if (typeof Set !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === Set)) {
targetConfig.proxyExcludeTyps.push(Set);
}
if (typeof Promise !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === Promise)) {
targetConfig.proxyExcludeTyps.push(Promise);
}
if (typeof ResizeObserver !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === ResizeObserver)) {
targetConfig.proxyExcludeTyps.push(ResizeObserver);
}
if (typeof ImageBitmap !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === ImageBitmap)) {
targetConfig.proxyExcludeTyps.push(ImageBitmap);
}
if (typeof CanvasRenderingContext2D !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === CanvasRenderingContext2D)) {
targetConfig.proxyExcludeTyps.push(CanvasRenderingContext2D);
}
if (typeof CSSStyleDeclaration !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === CSSStyleDeclaration)) {
targetConfig.proxyExcludeTyps.push(CSSStyleDeclaration);
}
if (typeof Date !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === Date)) {
targetConfig.proxyExcludeTyps.push(Date);
}
if (typeof MutationObserver !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === IntersectionObserver)) {
targetConfig.proxyExcludeTyps.push(IntersectionObserver);
}
if (typeof IntersectionObserver !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === IntersectionObserver)) {
targetConfig.proxyExcludeTyps.push(IntersectionObserver);
}
if (typeof HTMLElement !== "undefined" && !targetConfig.proxyExcludeTyps.some((it) => it === HTMLElement)) {
targetConfig.proxyExcludeTyps.push(HTMLElement);
}
if (!targetConfig.proxyExcludeTyps.some((it) => it === RawSet)) {
targetConfig.proxyExcludeTyps.push(RawSet);
}
if (!targetConfig.proxyExcludeTyps.some((it) => it === DomRender)) {
targetConfig.proxyExcludeTyps.push(DomRender);
}
targetConfig.rootElement = target;
targetConfig.eventManager = eventManager;
const domRender = new DomRenderProxy(rootObject, target, targetConfig);
const dest = new Proxy(rootObject, domRender);
targetObject = dest;
targetConfig.root = targetObject;
if (!targetConfig.router) {
let targetRouter;
if ("routerType" in config && config?.routerType === "hash") {
targetRouter = new HashRouter({ rootObject: targetObject, window: targetConfig.window, firstUrl: executeConfig?.firstUrl });
} else if ("routerType" in config && config?.routerType === "path") {
targetRouter = new PathRouter({ rootObject: targetObject, window: targetConfig.window, firstUrl: executeConfig?.firstUrl });
} else if ("routerType" in config && typeof config?.routerType === "function") {
targetRouter = config.routerType(targetObject, targetConfig);
} else if ("routerType" in config && typeof config?.routerType === "object") {
targetRouter = config.routerType;
}
targetConfig.router = targetRouter;
}
for (const value of Object.values(drComponent)) {
const a = value(targetConfig, DomRender);
if (!targetConfig.targetElements.some((it) => it.name === a.name)) {
targetConfig.targetElements.push(a);
}
}
domRender.run(targetObject);
return { rootObject: targetObject, config: targetConfig, target };
}
static createComponent(param) {
const component = RawSet.createComponentTargetElement({
name: param.tagName ?? (typeof param.type === "function" ? param.type.name : param.type.constructor.name),
objFactory: (e, o, r2, counstructorParam) => {
return typeof param.type === "function" ? new param.type(...counstructorParam) : param.type;
},
noStrip: param.noStrip,
template: param.template ?? "",
styles: Array.isArray(param.styles) ? param.styles : param.styles ? [param.styles] : void 0
});
return component;
}
// ?? 언제 쓰는거지?? 훔..
static createAttribute(attrName, getThisObj, factory) {
const targetAttribute = RawSet.createComponentTargetAttribute(attrName, getThisObj, factory);
return targetAttribute;
}
}
export {
DomRender
};
//# sourceMappingURL=DomRender.js.map