##// END OF EJS Templates
Added tag 1.0.0-rc17 for changeset a1a1ef050ecc
Added tag 1.0.0-rc17 for changeset a1a1ef050ecc

File last commit:

r34:e8012fdf09ae 1.0.0-rc16 default
r37:2e8680269302 default
Show More
BuildContextBase.ts
68 lines | 1.8 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / tsx / BuildContextBase.ts
import { isNull, mixin } from "@implab/core-amd/safe";
import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits";
import dom = require("dojo/dom-construct");
export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> {
private _attrs = {};
private _children = new Array();
private _created: boolean = false;
visitNext(v: any) {
if (this._created)
throw new Error("The Element is already created");
if (isNull(v))
return;
if (isPlainObject(v)) {
mixin(this._attrs, v);
} else if (v instanceof Array) {
v.forEach(x => this.visitNext(x));
} 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.getDomNode();
} else {
throw new Error("Invalid parameter");
}
}
abstract _getDomNode(): TNode;
ensureCreated() {
if (!this._created) {
this._create(this._attrs, this._children);
this._children = [];
this._attrs = {};
this._created = true;
}
}
/** @deprecated use getDomNode() */
getDomElement() {
return this.getDomNode();
}
getDomNode() {
this.ensureCreated();
return this._getDomNode();
}
placeAt(refNode: string | Node, position?: DojoNodePosition) {
dom.place(this.getDomNode(), refNode, position);
}
abstract _create(attrs: object, children: any[]): void;
}