BuildContextBase.ts
68 lines
| 1.8 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
|
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"); | ||||
} | ||||
} | ||||
cin
|
r22 | abstract _getDomNode(): TNode; | ||
cin
|
r7 | |||
ensureCreated() { | ||||
if (!this._created) { | ||||
this._create(this._attrs, this._children); | ||||
this._children = []; | ||||
this._attrs = {}; | ||||
this._created = true; | ||||
} | ||||
} | ||||
cin
|
r19 | /** @deprecated use getDomNode() */ | ||
cin
|
r7 | getDomElement() { | ||
cin
|
r19 | return this.getDomNode(); | ||
} | ||||
getDomNode() { | ||||
cin
|
r7 | this.ensureCreated(); | ||
cin
|
r19 | return this._getDomNode(); | ||
cin
|
r7 | } | ||
cin
|
r14 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | ||
cin
|
r19 | dom.place(this.getDomNode(), refNode, position); | ||
cin
|
r14 | } | ||
cin
|
r7 | abstract _create(attrs: object, children: any[]): void; | ||
} | ||||