import { DomBaseModule } from "./DomBaseModule"; import { DomLibraries } from "./DomLibraries"; // import { DomModel, DomModelInstance } from "./DomModel"; import { DomModel } from "./model/DomModel"; import { DomChildType, DomParamsType } from "./types"; // type ValueOf = Obj[keyof Obj]; // type OneOnly = { [key in Exclude]: null } & Pick; // type OneOfByKey = { [key in keyof Obj]: OneOnly }; // export type OneOf = ValueOf>; export class DomCore { private static _uidDate = 0; private static _uidCnt = 0; private static libraries = new DomLibraries(); static get uid(): string { const d = Date.now(); if (d === this._uidDate) { this._uidCnt++; } else { this._uidCnt = 0; this._uidDate = d; } return this._uidDate.toString(36) + "-" + this._uidCnt; } static html( tagName: string, params?: Record, children?: Array, namespace?: string ): T { const element = namespace ? document.createElementNS(namespace, tagName) : document.createElement(tagName); if (params) { this.parseProps(element, params); } if (children) { element.append( ...(children.map(v => (v instanceof DomModel ? v.dom : v)) as Array) ); } return element as T; } static parseProps(target: Record, data: Record): any { Object.entries(data).map(([key, value]) => { if (typeof value === "object") { this.parseProps(target[key], value); } else { target[key] = value; } }); } static render( module: DomBaseModule | null, tagName: string | DomModel, params?: DomParamsType, children: Array = [], namespace?: string ): T { // console.log('XXX Render '+tagName+' = ',module?.hasModel2(tagName+''),params,children); if (module?.hasModel(tagName + "")) { const m2 = module.getModel(tagName + "") as typeof DomModel; const instance = new m2(params, children); //@ts-ignore assign private instance._module = module; return instance.dom as T; } // else if(tagName instanceof DomModel){} return DomCore.html(tagName + "", params, children, namespace); } static objName2tagName(tagName: string): string { return tagName.replace(/([A-Z])/g, (a, b) => "-" + b.toLowerCase()); } static tagName2objName(tagName: string): string { return tagName.replace(/\-([a-z])/g, (a, b) => b.toUpperCase()); } }