@@ -1,64 +1,65 | |||||
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 } from "./traits"; | |
3 |
|
3 | |||
4 | export abstract class BuildContextBase { |
|
4 | export abstract class BuildContextBase { | |
5 | _attrs = {}; |
|
5 | _attrs = {}; | |
6 |
|
6 | |||
7 | _children = new Array(); |
|
7 | _children = new Array(); | |
8 |
|
8 | |||
9 | _created: boolean = false; |
|
9 | _created: boolean = false; | |
10 |
|
10 | |||
11 | visitNext(v: any) { |
|
11 | visitNext(v: any) { | |
12 | if (isNull(v)) |
|
12 | if (isNull(v)) | |
13 | return; |
|
13 | return; | |
14 |
|
14 | |||
15 | if (isPlainObject(v)) { |
|
15 | if (isPlainObject(v)) { | |
16 |
|
16 | |||
17 | if (this._created) |
|
17 | if (this._created) | |
18 | this._setAttrs(v); |
|
18 | this._setAttrs(v); | |
19 | else |
|
19 | else | |
20 | mixin(this._attrs, v); |
|
20 | mixin(this._attrs, v); | |
21 |
|
21 | } else if (v instanceof Array) { | ||
|
22 | v.forEach(x => this._addChild(x)); | |||
22 | } else { |
|
23 | } else { | |
23 | if (this._created) |
|
24 | if (this._created) | |
24 | this._addChild(v); |
|
25 | this._addChild(v); | |
25 | else |
|
26 | else | |
26 | this._children.push(v); |
|
27 | this._children.push(v); | |
27 | } |
|
28 | } | |
28 | } |
|
29 | } | |
29 |
|
30 | |||
30 | getChildDom(v: any) { |
|
31 | getChildDom(v: any) { | |
31 | const tv = typeof v; |
|
32 | const tv = typeof v; | |
32 | if (tv === "string" || tv === "number" || tv === "boolean" || v instanceof RegExp || v instanceof Date) { |
|
33 | if (tv === "string" || tv === "number" || tv === "boolean" || v instanceof RegExp || v instanceof Date) { | |
33 | return document.createTextNode(v.toString()); |
|
34 | return document.createTextNode(v.toString()); | |
34 | } else if (isNode(v)) { |
|
35 | } else if (isNode(v)) { | |
35 | return v; |
|
36 | return v; | |
36 | } else if (isBuildContext(v)) { |
|
37 | } else if (isBuildContext(v)) { | |
37 | return v.getDomElement(); |
|
38 | return v.getDomElement(); | |
38 | } else { |
|
39 | } else { | |
39 | throw new Error("Invalid parameter"); |
|
40 | throw new Error("Invalid parameter"); | |
40 | } |
|
41 | } | |
41 | } |
|
42 | } | |
42 |
|
43 | |||
43 | abstract _getDomElement(): HTMLElement; |
|
44 | abstract _getDomElement(): HTMLElement; | |
44 |
|
45 | |||
45 | ensureCreated() { |
|
46 | ensureCreated() { | |
46 | if (!this._created) { |
|
47 | if (!this._created) { | |
47 | this._create(this._attrs, this._children); |
|
48 | this._create(this._attrs, this._children); | |
48 | this._children = []; |
|
49 | this._children = []; | |
49 | this._attrs = {}; |
|
50 | this._attrs = {}; | |
50 | this._created = true; |
|
51 | this._created = true; | |
51 | } |
|
52 | } | |
52 | } |
|
53 | } | |
53 |
|
54 | |||
54 | getDomElement() { |
|
55 | getDomElement() { | |
55 | this.ensureCreated(); |
|
56 | this.ensureCreated(); | |
56 | return this._getDomElement(); |
|
57 | return this._getDomElement(); | |
57 | } |
|
58 | } | |
58 |
|
59 | |||
59 | abstract _addChild(child: any): void; |
|
60 | abstract _addChild(child: any): void; | |
60 |
|
61 | |||
61 | abstract _setAttrs(attrs: object): void; |
|
62 | abstract _setAttrs(attrs: object): void; | |
62 |
|
63 | |||
63 | abstract _create(attrs: object, children: any[]): void; |
|
64 | abstract _create(attrs: object, children: any[]): void; | |
64 | } |
|
65 | } |
General Comments 0
You need to be logged in to leave comments.
Login now