RenditionBase.ts
70 lines
| 2.3 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r102 | import { isPlainObject, DojoNodePosition, Rendition, isDocumentFragmentNode, placeAt, collectNodes, isMounted, startupWidgets } from "./traits"; | ||
|
|
r65 | |||
| export abstract class RenditionBase<TNode extends Node> implements Rendition<TNode> { | ||||
| private _attrs = {}; | ||||
|
|
r109 | private _children: unknown[] = []; | ||
|
|
r65 | |||
|
|
r109 | private _created = false; | ||
|
|
r65 | |||
|
|
r109 | visitNext(v: unknown) { | ||
|
|
r65 | if (this._created) | ||
| throw new Error("The Element is already created"); | ||||
|
|
r102 | if (v === null || v === undefined || typeof v === "boolean") | ||
|
|
r65 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | ||
| return; | ||||
| if (isPlainObject(v)) { | ||||
|
|
r102 | this._attrs = {... this._attrs, ...v}; | ||
|
|
r65 | } else if (v instanceof Array) { | ||
| v.forEach(x => this.visitNext(x)); | ||||
| } else { | ||||
| this._children.push(v); | ||||
| } | ||||
| } | ||||
|
|
r102 | ensureCreated() { | ||
|
|
r65 | if (!this._created) { | ||
|
|
r102 | this._create(this._attrs, this._children); | ||
|
|
r65 | this._children = []; | ||
| this._attrs = {}; | ||||
| this._created = true; | ||||
| } | ||||
| } | ||||
|
|
r93 | /** Is rendition was instantiated to the DOM node */ | ||
| isCreated() { | ||||
| return this._created; | ||||
|
|
r65 | } | ||
| /** Creates DOM node if not created. No additional actions are taken. */ | ||||
|
|
r102 | getDomNode() { | ||
| this.ensureCreated(); | ||||
|
|
r65 | return this._getDomNode(); | ||
| } | ||||
| /** Creates DOM node if not created, places it to the specified position | ||||
| * and calls startup() method for all widgets contained by this node. | ||||
| * | ||||
| * @param {string | Node} refNode The reference node where the created | ||||
| * DOM should be placed. | ||||
| * @param {DojoNodePosition} position Optional parameter, specifies the | ||||
| * position relative to refNode. Default is "last" (i.e. last child). | ||||
| */ | ||||
|
|
r96 | placeAt(refNode: string | Node, position: DojoNodePosition = "last") { | ||
|
|
r65 | const domNode = this.getDomNode(); | ||
|
|
r102 | const startupPending = isDocumentFragmentNode(domNode) ? collectNodes(domNode.childNodes) : [domNode]; | ||
|
|
r65 | |||
|
|
r96 | placeAt(domNode, refNode, position); | ||
|
|
r65 | |||
|
|
r102 | if (isMounted(startupPending[0])) | ||
| startupPending.forEach(n => startupWidgets(n)); | ||||
|
|
r67 | |||
|
|
r65 | } | ||
|
|
r102 | protected abstract _create(attrs: object, children: unknown[]): void; | ||
|
|
r65 | |||
| protected abstract _getDomNode(): TNode; | ||||
| } | ||||
