BuildContextBase.ts
80 lines
| 2.5 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r19 | import { isNull, mixin } from "@implab/core-amd/safe"; | ||
cin
|
r14 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits"; | ||
cin
|
r7 | |||
cin
|
r14 | import dom = require("dojo/dom-construct"); | ||
cin
|
r38 | import registry = require("dijit/registry"); | ||
cin
|
r14 | |||
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
|
r7 | if (isNull(v)) | ||
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 | } | ||
} | ||||
getChildDom(v: any) { | ||||
const tv = typeof v; | ||||
if (tv === "string" || tv === "number" || tv === "boolean" || v instanceof RegExp || v instanceof Date) { | ||||
return document.createTextNode(v.toString()); | ||||
} else if (isNode(v)) { | ||||
return v; | ||||
} else if (isBuildContext(v)) { | ||||
cin
|
r19 | return v.getDomNode(); | ||
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); | ||||
registry.findWidgets(domNode).forEach(w => w.startup()); | ||||
cin
|
r14 | } | ||
cin
|
r7 | abstract _create(attrs: object, children: any[]): void; | ||
cin
|
r38 | |||
abstract _getDomNode(): TNode; | ||||
cin
|
r7 | } | ||