@@ -1,65 +1,71 | |||
|
1 | 1 | import { isNull, mixin, argumentNotNull } from "@implab/core-amd/safe"; |
|
2 | import { isPlainObject, isNode, isBuildContext } from "./traits"; | |
|
2 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits"; | |
|
3 | 3 | |
|
4 | export abstract class BuildContextBase { | |
|
4 | import dom = require("dojo/dom-construct"); | |
|
5 | ||
|
6 | export abstract class BuildContextBase implements BuildContext { | |
|
5 | 7 | _attrs = {}; |
|
6 | 8 | |
|
7 | 9 | _children = new Array(); |
|
8 | 10 | |
|
9 | 11 | _created: boolean = false; |
|
10 | 12 | |
|
11 | 13 | visitNext(v: any) { |
|
12 | 14 | if (isNull(v)) |
|
13 | 15 | return; |
|
14 | 16 | |
|
15 | 17 | if (isPlainObject(v)) { |
|
16 | 18 | |
|
17 | 19 | if (this._created) |
|
18 | 20 | this._setAttrs(v); |
|
19 | 21 | else |
|
20 | 22 | mixin(this._attrs, v); |
|
21 | 23 | } else if (v instanceof Array) { |
|
22 | 24 | v.forEach(x => this._addChild(x)); |
|
23 | 25 | } else { |
|
24 | 26 | if (this._created) |
|
25 | 27 | this._addChild(v); |
|
26 | 28 | else |
|
27 | 29 | this._children.push(v); |
|
28 | 30 | } |
|
29 | 31 | } |
|
30 | 32 | |
|
31 | 33 | getChildDom(v: any) { |
|
32 | 34 | const tv = typeof v; |
|
33 | 35 | if (tv === "string" || tv === "number" || tv === "boolean" || v instanceof RegExp || v instanceof Date) { |
|
34 | 36 | return document.createTextNode(v.toString()); |
|
35 | 37 | } else if (isNode(v)) { |
|
36 | 38 | return v; |
|
37 | 39 | } else if (isBuildContext(v)) { |
|
38 | 40 | return v.getDomElement(); |
|
39 | 41 | } else { |
|
40 | 42 | throw new Error("Invalid parameter"); |
|
41 | 43 | } |
|
42 | 44 | } |
|
43 | 45 | |
|
44 | 46 | abstract _getDomElement(): HTMLElement; |
|
45 | 47 | |
|
46 | 48 | ensureCreated() { |
|
47 | 49 | if (!this._created) { |
|
48 | 50 | this._create(this._attrs, this._children); |
|
49 | 51 | this._children = []; |
|
50 | 52 | this._attrs = {}; |
|
51 | 53 | this._created = true; |
|
52 | 54 | } |
|
53 | 55 | } |
|
54 | 56 | |
|
55 | 57 | getDomElement() { |
|
56 | 58 | this.ensureCreated(); |
|
57 | 59 | return this._getDomElement(); |
|
58 | 60 | } |
|
59 | 61 | |
|
62 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
|
63 | dom.place(this.getDomElement(), refNode, position); | |
|
64 | } | |
|
65 | ||
|
60 | 66 | abstract _addChild(child: any): void; |
|
61 | 67 | |
|
62 | 68 | abstract _setAttrs(attrs: object): void; |
|
63 | 69 | |
|
64 | 70 | abstract _create(attrs: object, children: any[]): void; |
|
65 | 71 | } |
@@ -1,32 +1,35 | |||
|
1 | 1 | import _WidgetBase = require("dijit/_WidgetBase"); |
|
2 | 2 | |
|
3 | 3 | type _WidgetBaseConstructor = typeof _WidgetBase; |
|
4 | 4 | |
|
5 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; | |
|
5 | 6 | |
|
6 | 7 | export interface BuildContext { |
|
7 | 8 | getDomElement(): HTMLElement; |
|
9 | ||
|
10 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; | |
|
8 | 11 | } |
|
9 | 12 | |
|
10 | 13 | export function isNode(el: any): el is HTMLElement { |
|
11 | 14 | return el && el.nodeName && el.nodeType; |
|
12 | 15 | } |
|
13 | 16 | |
|
14 | 17 | export function isWidget(v: any): v is _WidgetBase { |
|
15 | 18 | return v && "domNode" in v; |
|
16 | 19 | } |
|
17 | 20 | |
|
18 | 21 | export function isBuildContext(v: any): v is BuildContext { |
|
19 | 22 | return typeof v === "object" && typeof v.getDomElement === "function"; |
|
20 | 23 | } |
|
21 | 24 | |
|
22 | 25 | export function isPlainObject(v: object) { |
|
23 | 26 | if (typeof v !== "object") |
|
24 | 27 | return false; |
|
25 | 28 | |
|
26 | 29 | const vp = Object.getPrototypeOf(v); |
|
27 | 30 | return !vp || vp === Object.prototype; |
|
28 | 31 | } |
|
29 | 32 | |
|
30 | 33 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { |
|
31 | 34 | return typeof v === "function" && v.prototype && "domNode" in v.prototype; |
|
32 | 35 | } |
@@ -1,18 +1,18 | |||
|
1 | 1 | { |
|
2 | 2 | "extends": "../tsconfig", |
|
3 | 3 | "compilerOptions": { |
|
4 | 4 | "baseUrl": ".", |
|
5 | "rootDir": "ts", | |
|
5 | //"rootDir": "ts", | |
|
6 | 6 | "rootDirs": [ |
|
7 | 7 | "ts", |
|
8 | 8 | "typings", |
|
9 | 9 | "../main/ts", |
|
10 | 10 | "../main/typings" |
|
11 | 11 | ], |
|
12 | 12 | "paths": { |
|
13 | 13 | "@implab/djx" : ["../main/ts", "../main/typings"], |
|
14 | 14 | "@implab/djx/*" : ["../main/ts/*", "../main/typings/*" ] |
|
15 | 15 | }, |
|
16 | 16 | "types": ["requirejs", "../main/typings"] |
|
17 | 17 | } |
|
18 | 18 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now