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