RenditionBase.ts
70 lines
| 2.3 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r102 | import { isPlainObject, DojoNodePosition, Rendition, isDocumentFragmentNode, placeAt, collectNodes, isMounted, startupWidgets } from "./traits"; | ||
cin
|
r65 | |||
export abstract class RenditionBase<TNode extends Node> implements Rendition<TNode> { | ||||
private _attrs = {}; | ||||
cin
|
r109 | private _children: unknown[] = []; | ||
cin
|
r65 | |||
cin
|
r109 | private _created = false; | ||
cin
|
r65 | |||
cin
|
r109 | visitNext(v: unknown) { | ||
cin
|
r65 | if (this._created) | ||
throw new Error("The Element is already created"); | ||||
cin
|
r102 | if (v === null || v === undefined || typeof v === "boolean") | ||
cin
|
r65 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | ||
return; | ||||
if (isPlainObject(v)) { | ||||
cin
|
r102 | this._attrs = {... this._attrs, ...v}; | ||
cin
|
r65 | } else if (v instanceof Array) { | ||
v.forEach(x => this.visitNext(x)); | ||||
} else { | ||||
this._children.push(v); | ||||
} | ||||
} | ||||
cin
|
r102 | ensureCreated() { | ||
cin
|
r65 | if (!this._created) { | ||
cin
|
r102 | this._create(this._attrs, this._children); | ||
cin
|
r65 | this._children = []; | ||
this._attrs = {}; | ||||
this._created = true; | ||||
} | ||||
} | ||||
cin
|
r93 | /** Is rendition was instantiated to the DOM node */ | ||
isCreated() { | ||||
return this._created; | ||||
cin
|
r65 | } | ||
/** Creates DOM node if not created. No additional actions are taken. */ | ||||
cin
|
r102 | getDomNode() { | ||
this.ensureCreated(); | ||||
cin
|
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). | ||||
*/ | ||||
cin
|
r96 | placeAt(refNode: string | Node, position: DojoNodePosition = "last") { | ||
cin
|
r65 | const domNode = this.getDomNode(); | ||
cin
|
r102 | const startupPending = isDocumentFragmentNode(domNode) ? collectNodes(domNode.childNodes) : [domNode]; | ||
cin
|
r65 | |||
cin
|
r96 | placeAt(domNode, refNode, position); | ||
cin
|
r65 | |||
cin
|
r112 | if (startupPending.length && isMounted(startupPending[0])) | ||
cin
|
r102 | startupPending.forEach(n => startupWidgets(n)); | ||
cin
|
r67 | |||
cin
|
r65 | } | ||
cin
|
r102 | protected abstract _create(attrs: object, children: unknown[]): void; | ||
cin
|
r65 | |||
protected abstract _getDomNode(): TNode; | ||||
} | ||||