@dooboostore/dom-render
Version:
html view template engine
210 lines • 8.54 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { RawSet } from '../rawsets/RawSet';
import { DomRenderNoProxy } from '../decorators/DomRenderNoProxy';
import { ConvertUtils } from '@dooboostore/core/convert/ConvertUtils';
import * as Render from '../rawsets/Render';
export class ComponentBase {
// private attributeData?: FieldUnionType<T, null>;
get render() {
return this._render;
}
get rawSet() {
return this._rawSet;
}
getAttribute(name, attribute = this._attribute) {
const target = attribute;
if (!target) {
return null;
}
const nameString = String(name);
let p = nameString;
if (p in target) {
return target[p];
}
p = ConvertUtils.snakeToCamelCase(nameString);
if (p in target) {
return target[p];
}
p = ConvertUtils.camelToSnakeCase(nameString);
if (p in target) {
return target[p];
}
p = nameString.toUpperCase();
if (p in target) {
return target[p];
}
p = nameString.toLowerCase();
if (p in target) {
return target[p];
}
return null;
}
// get attribute(): T | undefined {
// const handler = {
// get(target: any, prop: string) {
// let p = prop;
// if (p in target) {
// return target[p];
// }
// p = ConvertUtils.snakeToCamelCase(prop);
// if (p in target) {
// return target[p];
// }
// p = ConvertUtils.camelToSnakeCase(prop);
// if (p in target) {
// return target[p];
// }
// p = prop.toUpperCase()
// if (p in target) {
// return target[p];
// }
// p = prop.toLowerCase()
// if (p in target) {
// return target[p];
// }
// return null;
// }
// };
// const obj = new Proxy(this._attribute, handler);
// return obj;
// }
hasAttribute(name) {
return this.getAttribute(name) !== null;
}
equalsAttributeName(n, name) {
return n === name ||
ConvertUtils.snakeToCamelCase(n) === name ||
ConvertUtils.camelToSnakeCase(n) === name ||
n.toUpperCase() === name.toUpperCase() ||
n.toLowerCase() === name.toLowerCase();
}
// attribute(name: keyof T, {attribute = this._attribute}: {equals?: (data: T[keyof T]) => void, notEquals?: () => void,attribute?: T} = {}) {
// const data = this.getAttribute(name, attribute)
// return n === name ||
// ConvertUtils.snakeToCamelCase(n) === name ||
// ConvertUtils.camelToSnakeCase(n) === name ||
// n.toUpperCase() === (name as string).toUpperCase() ||
// n.toLowerCase() === (name as string).toLowerCase()
// ;
// }
get uuid() {
var _a;
return (_a = this._rawSet) === null || _a === void 0 ? void 0 : _a.uuid;
}
get children() {
return this._children;
}
getElement() {
var _a, _b, _c, _d, _e;
return (_e = (_d = ((_a = this.render) !== null && _a !== void 0 ? _a : (_c = (_b = this.rawSet) === null || _b === void 0 ? void 0 : _b.dataSet) === null || _c === void 0 ? void 0 : _c.render)) === null || _d === void 0 ? void 0 : _d.getElement) === null || _e === void 0 ? void 0 : _e.call(_d);
}
getFirstChild(type) {
return this._children.find(it => it instanceof type);
}
getParentThis() {
var _a;
return (_a = this.render) === null || _a === void 0 ? void 0 : _a.parentThis;
}
// 구현 시그니처 (오버로드 시그니처보다 더 일반적이어야 함)
getChildren(typeOrTypes) {
const targets = Array.isArray(typeOrTypes) ? typeOrTypes : [typeOrTypes];
// filter의 결과는 항상 배열이므로 `?? []`는 필요 없습니다.
// filter가 아무것도 찾지 못하면 빈 배열을 반환합니다.
return this._children.filter(child => targets.some(targetConstructor => child instanceof targetConstructor));
}
// getChildren<T extends ConstructorType<any>[] | ConstructorType<any>>(
// type: T
// ): T extends (infer U)[] ? InstanceType<U>[] : T extends ConstructorType<infer V> ? V[] : never {
// const target = Array.isArray(type) ? type : [type];
// return this._children.filter(it => target.some(t => it instanceof t)) ?? [];
// }
// getChildren<T>(type: ConstructorType<T> | ConstructorType<T>[]): T[] {
// const target = Array.isArray(type) ? type : [type];
// return this._children.filter(it => target.some(t => it instanceof t)) ?? [];
// }
/*
getChildren<T>(type: ConstructorType<T>): T[] {
return this._children.filter(it => it instanceof type) ?? [];
}
*/
constructor(config) {
this.config = config;
this._children = [];
}
setAttribute(attribute) {
this._attribute = attribute;
// this.attributeData = this._attribute;
}
onCreatedThisChild(child, data) {
this._children.push(child);
// this._rawSet = data.render?.rawSet;
// this._attribute = data.render?.attribute as T;
}
onCreateRenderData(data) {
var _a, _b, _c, _d, _e, _f, _g;
this._rawSet = (_a = data.render) === null || _a === void 0 ? void 0 : _a.rawSet;
this.setAttribute((_b = data.render) === null || _b === void 0 ? void 0 : _b.attribute);
this._render = data.render;
if ((_c = this.config) === null || _c === void 0 ? void 0 : _c.onlyParentType) {
const isOk = Array.isArray((_d = this.config) === null || _d === void 0 ? void 0 : _d.onlyParentType) ? (_e = this.config) === null || _e === void 0 ? void 0 : _e.onlyParentType : [(_f = this.config) === null || _f === void 0 ? void 0 : _f.onlyParentType].some(it => { var _a; return ((_a = data.render) === null || _a === void 0 ? void 0 : _a.parentThis) instanceof it; });
if (!isOk) {
throw new Error('only my parentThis ' + ((_g = this.config) === null || _g === void 0 ? void 0 : _g.onlyParentType));
}
}
}
onChangeAttrRender(name, value, other) {
var _a, _b;
if (other.rawSet) {
this._rawSet = other.rawSet;
if (this._rawSet) {
this.setAttribute((_b = (_a = this._rawSet.dataSet) === null || _a === void 0 ? void 0 : _a.render) === null || _b === void 0 ? void 0 : _b.attribute);
}
}
// this.changeAttribute?.(this._attribute);
}
onInitRender(param, rawSet) {
var _a, _b;
if (rawSet) {
this._rawSet = rawSet;
if (this._rawSet) {
this.setAttribute((_b = (_a = rawSet.dataSet) === null || _a === void 0 ? void 0 : _a.render) === null || _b === void 0 ? void 0 : _b.attribute);
}
const e = this.getElement();
if (e) {
e.component = this;
}
}
}
onDrThisBind() {
}
onDrThisUnBind() {
}
onDestroyRender(data) {
// console.log('ComponentBase---onDestroyRender----')
}
}
__decorate([
DomRenderNoProxy,
__metadata("design:type", RawSet)
], ComponentBase.prototype, "_rawSet", void 0);
__decorate([
DomRenderNoProxy,
__metadata("design:type", Array)
], ComponentBase.prototype, "_children", void 0);
__decorate([
DomRenderNoProxy,
__metadata("design:type", Object)
], ComponentBase.prototype, "_render", void 0);
__decorate([
DomRenderNoProxy,
__metadata("design:type", Object)
], ComponentBase.prototype, "_attribute", void 0);
//# sourceMappingURL=ComponentBase.js.map