HtmlRendition.ts
51 lines
| 1.5 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r101 | import djDom = require("dojo/dom-construct"); | ||
| import djAttr = require("dojo/dom-attr"); | ||||
|
|
r65 | import { argumentNotEmptyString } from "@implab/core-amd/safe"; | ||
| import { RenditionBase } from "./RenditionBase"; | ||||
|
|
r96 | import { placeAt } from "./traits"; | ||
| import { IScope } from "./Scope"; | ||||
|
|
r101 | import { getItemDom, renderHook } from "./render"; | ||
|
|
r65 | |||
|
|
r101 | export class HtmlRendition extends RenditionBase<Element> { | ||
|
|
r65 | elementType: string; | ||
|
|
r101 | _element: Element | undefined; | ||
|
|
r65 | |||
| constructor(elementType: string) { | ||||
| argumentNotEmptyString(elementType, "elementType"); | ||||
| super(); | ||||
| this.elementType = elementType; | ||||
| } | ||||
|
|
r96 | _addChild(child: unknown, scope: IScope): void { | ||
|
|
r65 | if (!this._element) | ||
| throw new Error("The HTML element isn't created"); | ||||
|
|
r101 | placeAt(getItemDom(child), this._element); | ||
|
|
r65 | } | ||
|
|
r101 | _create({ xmlns, ref, ...attrs }: { xmlns?: string, ref?: JSX.Ref<Element> }, 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); | ||||
| } | ||||
|
|
r65 | |||
|
|
r96 | children.forEach(v => this._addChild(v, scope)); | ||
|
|
r101 | |||
| const element = this._element; | ||||
| if (ref) | ||||
| renderHook(() => ref(element)); | ||||
|
|
r65 | } | ||
| _getDomNode() { | ||||
| if (!this._element) | ||||
| throw new Error("The HTML element isn't created"); | ||||
| return this._element; | ||||
| } | ||||
| } | ||||
