HtmlRendition.ts
50 lines
| 1.4 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"; | ||
cin
|
r102 | import { getItemDom, refHook } 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
|
r102 | _addChild(child: unknown): 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
|
r102 | _create({ xmlns, ref, ...attrs }: { xmlns?: string, ref?: JSX.Ref<Element> }, children: unknown[]) { | ||
cin
|
r101 | |||
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
|
r102 | children.forEach(v => this._addChild(v)); | ||
cin
|
r101 | |||
const element = this._element; | ||||
if (ref) | ||||
cin
|
r102 | refHook(element, ref); | ||
cin
|
r65 | } | ||
_getDomNode() { | ||||
if (!this._element) | ||||
throw new Error("The HTML element isn't created"); | ||||
return this._element; | ||||
} | ||||
} | ||||