import djDom = require("dojo/dom-construct"); import djAttr = require("dojo/dom-attr"); import { argumentNotEmptyString } from "@implab/core-amd/safe"; import { RenditionBase } from "./RenditionBase"; import { placeAt } from "./traits"; import { IScope } from "./Scope"; import { getItemDom, renderHook } from "./render"; export class HtmlRendition extends RenditionBase { elementType: string; _element: Element | undefined; constructor(elementType: string) { argumentNotEmptyString(elementType, "elementType"); super(); this.elementType = elementType; } _addChild(child: unknown, scope: IScope): void { if (!this._element) throw new Error("The HTML element isn't created"); placeAt(getItemDom(child), this._element); } _create({ xmlns, ref, ...attrs }: { xmlns?: string, ref?: JSX.Ref }, children: unknown[], scope: IScope) { if (xmlns) { this._element = document.createElementNS(xmlns, this.elementType); djAttr.set(this._element, attrs); } else { this._element = djDom.create(this.elementType, attrs); } children.forEach(v => this._addChild(v, scope)); const element = this._element; if (ref) renderHook(() => ref(element)); } _getDomNode() { if (!this._element) throw new Error("The HTML element isn't created"); return this._element; } }