BuildContextBase.ts
71 lines
| 1.9 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r7 | import { isNull, mixin, argumentNotNull } 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"); | ||
export abstract class BuildContextBase implements BuildContext { | ||||
cin
|
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); | ||||
cin
|
r13 | } else if (v instanceof Array) { | ||
v.forEach(x => this._addChild(x)); | ||||
cin
|
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)) { | ||||
return v.getDomElement(); | ||||
} else { | ||||
throw new Error("Invalid parameter"); | ||||
} | ||||
} | ||||
abstract _getDomElement(): HTMLElement; | ||||
ensureCreated() { | ||||
if (!this._created) { | ||||
this._create(this._attrs, this._children); | ||||
this._children = []; | ||||
this._attrs = {}; | ||||
this._created = true; | ||||
} | ||||
} | ||||
getDomElement() { | ||||
this.ensureCreated(); | ||||
return this._getDomElement(); | ||||
} | ||||
cin
|
r14 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | ||
dom.place(this.getDomElement(), refNode, position); | ||||
} | ||||
cin
|
r7 | abstract _addChild(child: any): void; | ||
abstract _setAttrs(attrs: object): void; | ||||
abstract _create(attrs: object, children: any[]): void; | ||||
} | ||||