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