-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview-util.js
124 lines (104 loc) · 3.52 KB
/
view-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const platformNames = require("tns-core-modules/platform").platformNames
const ContentView = require("tns-core-modules/ui/content-view").ContentView
const LayoutBase = require("tns-core-modules/ui/layouts/layout-base").LayoutBase
const elementRegistry = require("./element-registry")
const ELEMENT_NODE_TYPE = 1;
class CommentNode {
constructor(){
this.templateParent = null
this.meta = { skipAddToDom: true }
}
}
class ViewUtil {
constructor(device) {
this.isIos = device.os === platformNames.ios;
this.isAndroid = device.os === platformNames.android;
}
isView(view) {
return view instanceof View
}
isLayout(view) {
return view instanceof LayoutBase
}
isContentView(view) {
return view instanceof ContentView
}
isCommentNode(view) {
return view instanceof CommentNode
}
insertChild(parent, child, atIndex) {
if (atIndex == undefined)
atIndex = -1;
if (child instanceof CommentNode) {
child.templateParent = parent;
return;
}
if (!parent || elementRegistry.isDetachedElement(child)) {
return;
}
if (parent.meta && parent.meta.insertChild) {
parent.meta.insertChild(parent, child, atIndex);
} else if (this.isLayout(parent)) {
if (child.parent === parent) {
const index = (parent).getChildIndex(child);
if (index !== -1) {
parent.removeChild(child);
}
}
if (atIndex !== -1) {
parent.insertChild(child, atIndex);
} else {
parent.addChild(child);
}
} else if (this.isContentView(parent)) {
parent.content = child;
} else if (parent && parent._addChildFromBuilder) {
parent._addChildFromBuilder(child.nodeName, child);
} else {
// throw new Error("Parent can"t contain children: " + parent.nodeName + ", " + parent);
}
}
removeChild(parent, child) {
if (!parent ||
child instanceof CommentNode ||
elementRegistry.isDetachedElement(child)) {
return;
}
if (parent.meta && parent.meta.removeChild) {
parent.meta.removeChild(parent, child);
} else if (this.isLayout(parent)) {
parent.removeChild(child);
} else if (this.isContentView(parent)) {
if (parent.content === child) {
parent.content = null;
}
} else if (this.isView(parent)) {
parent._removeView(child);
} else {
// throw new Error("Unknown parent type: " + parent);
}
}
createComment() {
return new CommentNode();
}
createText() {
return new CommentNode();
}
createView(name) {
if (!elementRegistry.isKnownView(name)) {
name = "proxyViewContainer";
}
const viewClass = elementRegistry.getViewClass(name);
let view = new viewClass();
view.nodeName = name;
view.meta = elementRegistry.getViewMeta(name);
// we're setting the node type of the view
// to 'element' because of checks done in the
// dom animation engine:
// tslint:disable-next-line:max-line-length
// https://github.com/angular/angular/blob/master/packages/animations/browser/src/render/dom_animation_engine.ts#L70-L81
view.nodeType = ELEMENT_NODE_TYPE;
return view;
}
}
module.exports = ViewUtil