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