BuildContextBase.ts
76 lines
| 2.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r19 | import { isNull, mixin } from "@implab/core-amd/safe"; | ||
|
|
r14 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits"; | ||
|
|
r7 | |||
|
|
r14 | import dom = require("dojo/dom-construct"); | ||
|
|
r22 | export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> { | ||
|
|
r7 | _attrs = {}; | ||
| _children = new Array(); | ||||
| _created: boolean = false; | ||||
| visitNext(v: any) { | ||||
| if (isNull(v)) | ||||
| return; | ||||
| if (isPlainObject(v)) { | ||||
| if (this._created) | ||||
| this._setAttrs(v); | ||||
| else | ||||
| mixin(this._attrs, v); | ||||
|
|
r13 | } else if (v instanceof Array) { | ||
| v.forEach(x => this._addChild(x)); | ||||
|
|
r7 | } else { | ||
| if (this._created) | ||||
| this._addChild(v); | ||||
| else | ||||
| this._children.push(v); | ||||
| } | ||||
| } | ||||
| 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)) { | ||||
|
|
r19 | return v.getDomNode(); | ||
|
|
r7 | } else { | ||
| throw new Error("Invalid parameter"); | ||||
| } | ||||
| } | ||||
|
|
r22 | abstract _getDomNode(): TNode; | ||
|
|
r7 | |||
| ensureCreated() { | ||||
| if (!this._created) { | ||||
| this._create(this._attrs, this._children); | ||||
| this._children = []; | ||||
| this._attrs = {}; | ||||
| this._created = true; | ||||
| } | ||||
| } | ||||
|
|
r19 | /** @deprecated use getDomNode() */ | ||
|
|
r7 | getDomElement() { | ||
|
|
r19 | return this.getDomNode(); | ||
| } | ||||
| getDomNode() { | ||||
|
|
r7 | this.ensureCreated(); | ||
|
|
r19 | return this._getDomNode(); | ||
|
|
r7 | } | ||
|
|
r14 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | ||
|
|
r19 | dom.place(this.getDomNode(), refNode, position); | ||
|
|
r14 | } | ||
|
|
r7 | abstract _addChild(child: any): void; | ||
| abstract _setAttrs(attrs: object): void; | ||||
| abstract _create(attrs: object, children: any[]): void; | ||||
| } | ||||
