RenditionBase.ts
73 lines
| 2.4 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r65 | import { isNull, mixin } from "@implab/core-amd/safe"; | ||
cin
|
r96 | import { isPlainObject, DojoNodePosition, Rendition, isDocumentFragmentNode, placeAt, collectNodes, autostartWidgets } from "./traits"; | ||
cin
|
r65 | |||
cin
|
r96 | import { IScope } from "./Scope"; | ||
import { getScope } from "./Renderer"; | ||||
cin
|
r65 | |||
export abstract class RenditionBase<TNode extends Node> implements Rendition<TNode> { | ||||
private _attrs = {}; | ||||
private _children = new Array(); | ||||
private _created: boolean = false; | ||||
visitNext(v: any) { | ||||
if (this._created) | ||||
throw new Error("The Element is already created"); | ||||
if (isNull(v) || typeof v === "boolean") | ||||
// skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | ||||
return; | ||||
if (isPlainObject(v)) { | ||||
mixin(this._attrs, v); | ||||
} else if (v instanceof Array) { | ||||
v.forEach(x => this.visitNext(x)); | ||||
} else { | ||||
this._children.push(v); | ||||
} | ||||
} | ||||
cin
|
r96 | ensureCreated(scope: IScope) { | ||
cin
|
r65 | if (!this._created) { | ||
cin
|
r96 | this._create(this._attrs, this._children, scope); | ||
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
|
r96 | getDomNode(scope?: IScope) { | ||
this.ensureCreated(scope || getScope()); | ||||
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
|
r96 | const startupPending = isDocumentFragmentNode(domNode) ? collectNodes(domNode.children) : [domNode]; | ||
cin
|
r65 | |||
cin
|
r96 | placeAt(domNode, refNode, position); | ||
cin
|
r65 | |||
cin
|
r96 | startupPending.forEach(autostartWidgets); | ||
cin
|
r67 | |||
cin
|
r65 | } | ||
cin
|
r96 | protected abstract _create(attrs: object, children: unknown[], scope: IScope): void; | ||
cin
|
r65 | |||
protected abstract _getDomNode(): TNode; | ||||
} | ||||