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