BuildContextBase.ts
93 lines
| 3.0 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r19 | import { isNull, mixin } from "@implab/core-amd/safe"; | ||
cin
|
r59 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext, isInPage, isWidget } from "./traits"; | ||
cin
|
r7 | |||
cin
|
r14 | import dom = require("dojo/dom-construct"); | ||
cin
|
r38 | import registry = require("dijit/registry"); | ||
cin
|
r14 | |||
cin
|
r48 | |||
cin
|
r22 | export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> { | ||
cin
|
r34 | private _attrs = {}; | ||
cin
|
r7 | |||
cin
|
r34 | private _children = new Array(); | ||
cin
|
r7 | |||
cin
|
r34 | private _created: boolean = false; | ||
cin
|
r7 | |||
visitNext(v: any) { | ||||
cin
|
r34 | if (this._created) | ||
throw new Error("The Element is already created"); | ||||
cin
|
r40 | if (isNull(v) || typeof v === "boolean") | ||
// skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | ||||
cin
|
r7 | return; | ||
if (isPlainObject(v)) { | ||||
cin
|
r34 | mixin(this._attrs, v); | ||
cin
|
r13 | } else if (v instanceof Array) { | ||
cin
|
r34 | v.forEach(x => this.visitNext(x)); | ||
cin
|
r7 | } else { | ||
cin
|
r34 | this._children.push(v); | ||
cin
|
r7 | } | ||
} | ||||
cin
|
r59 | protected getItemDom(v: any) { | ||
cin
|
r7 | const tv = typeof v; | ||
cin
|
r40 | if (tv === "string" || tv === "number" || v instanceof RegExp || v instanceof Date) { | ||
cin
|
r7 | return document.createTextNode(v.toString()); | ||
} else if (isNode(v)) { | ||||
return v; | ||||
} else if (isBuildContext(v)) { | ||||
cin
|
r19 | return v.getDomNode(); | ||
cin
|
r59 | } else if(isWidget(v)) { | ||
return v.domNode; | ||||
} else if(tv === "boolean" || v === null || v === undefined) { | ||||
return document.createComment(`[${tv} ${String(v)}]`); | ||||
cin
|
r7 | } else { | ||
throw new Error("Invalid parameter"); | ||||
} | ||||
} | ||||
ensureCreated() { | ||||
if (!this._created) { | ||||
this._create(this._attrs, this._children); | ||||
this._children = []; | ||||
this._attrs = {}; | ||||
this._created = true; | ||||
} | ||||
} | ||||
cin
|
r38 | /** @deprecated will be removed in 1.0.0, use getDomNode() */ | ||
cin
|
r7 | getDomElement() { | ||
cin
|
r19 | return this.getDomNode(); | ||
} | ||||
cin
|
r38 | /** Creates DOM node if not created. No additional actions are taken. */ | ||
cin
|
r19 | getDomNode() { | ||
cin
|
r7 | this.ensureCreated(); | ||
cin
|
r19 | return this._getDomNode(); | ||
cin
|
r7 | } | ||
cin
|
r38 | /** 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
|
r14 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | ||
cin
|
r38 | const domNode = this.getDomNode(); | ||
dom.place(domNode, refNode, position); | ||||
cin
|
r48 | const parentWidget = domNode.parentNode ? registry.getEnclosingWidget(domNode.parentNode) : null; | ||
if ((parentWidget && parentWidget._started) || isInPage(domNode)) | ||||
this._startup(); | ||||
} | ||||
_startup () { | ||||
registry.findWidgets(this._getDomNode()).forEach(w => w.startup()); | ||||
cin
|
r14 | } | ||
cin
|
r7 | abstract _create(attrs: object, children: any[]): void; | ||
cin
|
r38 | |||
abstract _getDomNode(): TNode; | ||||
cin
|
r7 | } | ||