forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_node.dart
More file actions
193 lines (169 loc) · 5.34 KB
/
debug_node.dart
File metadata and controls
193 lines (169 loc) · 5.34 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
library angular2.src.core.debug.debug_node;
import "package:angular2/src/facade/lang.dart" show isPresent, Type;
import "package:angular2/src/facade/collection.dart"
show Predicate, ListWrapper, MapWrapper;
import "package:angular2/src/core/di.dart" show Injector;
import "package:angular2/src/core/render/api.dart" show RenderDebugInfo;
class EventListener {
String name;
Function callback;
EventListener(this.name, this.callback) {}
}
class DebugNode {
RenderDebugInfo _debugInfo;
dynamic nativeNode;
List<EventListener> listeners;
DebugElement parent;
DebugNode(dynamic nativeNode, DebugNode parent, this._debugInfo) {
this.nativeNode = nativeNode;
if (isPresent(parent) && parent is DebugElement) {
parent.addChild(this);
} else {
this.parent = null;
}
this.listeners = [];
}
Injector get injector {
return isPresent(this._debugInfo) ? this._debugInfo.injector : null;
}
dynamic get componentInstance {
return isPresent(this._debugInfo) ? this._debugInfo.component : null;
}
dynamic get context {
return isPresent(this._debugInfo) ? this._debugInfo.context : null;
}
Map<String, dynamic> get references {
return isPresent(this._debugInfo) ? this._debugInfo.references : null;
}
List<dynamic> get providerTokens {
return isPresent(this._debugInfo) ? this._debugInfo.providerTokens : null;
}
String get source {
return isPresent(this._debugInfo) ? this._debugInfo.source : null;
}
/**
* Use injector.get(token) instead.
*
*
*/
dynamic inject(dynamic token) {
return this.injector.get(token);
}
}
class DebugElement extends DebugNode {
String name;
Map<String, String> properties;
Map<String, String> attributes;
List<DebugNode> childNodes;
dynamic nativeElement;
DebugElement(dynamic nativeNode, dynamic parent, RenderDebugInfo _debugInfo)
: super(nativeNode, parent, _debugInfo) {
/* super call moved to initializer */;
this.properties = {};
this.attributes = {};
this.childNodes = [];
this.nativeElement = nativeNode;
}
addChild(DebugNode child) {
if (isPresent(child)) {
this.childNodes.add(child);
child.parent = this;
}
}
removeChild(DebugNode child) {
var childIndex = this.childNodes.indexOf(child);
if (!identical(childIndex, -1)) {
child.parent = null;
ListWrapper.splice(this.childNodes, childIndex, 1);
}
}
insertChildrenAfter(DebugNode child, List<DebugNode> newChildren) {
var siblingIndex = this.childNodes.indexOf(child);
if (!identical(siblingIndex, -1)) {
var previousChildren =
ListWrapper.slice(this.childNodes, 0, siblingIndex + 1);
var nextChildren = ListWrapper.slice(this.childNodes, siblingIndex + 1);
this.childNodes = ListWrapper.concat(
ListWrapper.concat(previousChildren, newChildren), nextChildren);
for (var i = 0; i < newChildren.length; ++i) {
var newChild = newChildren[i];
if (isPresent(newChild.parent)) {
newChild.parent.removeChild(newChild);
}
newChild.parent = this;
}
}
}
DebugElement query(Predicate<DebugElement> predicate) {
var results = this.queryAll(predicate);
return results.length > 0 ? results[0] : null;
}
List<DebugElement> queryAll(Predicate<DebugElement> predicate) {
List<DebugElement> matches = [];
_queryElementChildren(this, predicate, matches);
return matches;
}
List<DebugNode> queryAllNodes(Predicate<DebugNode> predicate) {
List<DebugNode> matches = [];
_queryNodeChildren(this, predicate, matches);
return matches;
}
List<DebugElement> get children {
List<DebugElement> children = [];
this.childNodes.forEach((node) {
if (node is DebugElement) {
children.add(node);
}
});
return children;
}
triggerEventHandler(String eventName, dynamic eventObj) {
this.listeners.forEach((listener) {
if (listener.name == eventName) {
listener.callback(eventObj);
}
});
}
}
dynamic asNativeElements(List<DebugElement> debugEls) {
return debugEls.map((el) => el.nativeElement).toList();
}
_queryElementChildren(DebugElement element, Predicate<DebugElement> predicate,
List<DebugElement> matches) {
element.childNodes.forEach((node) {
if (node is DebugElement) {
if (predicate(node)) {
matches.add(node);
}
_queryElementChildren(node, predicate, matches);
}
});
}
_queryNodeChildren(DebugNode parentNode, Predicate<DebugNode> predicate,
List<DebugNode> matches) {
if (parentNode is DebugElement) {
parentNode.childNodes.forEach((node) {
if (predicate(node)) {
matches.add(node);
}
if (node is DebugElement) {
_queryNodeChildren(node, predicate, matches);
}
});
}
}
// Need to keep the nodes in a global Map so that multiple angular apps are supported.
var _nativeNodeToDebugNode = new Map<dynamic, DebugNode>();
DebugNode getDebugNode(dynamic nativeNode) {
return _nativeNodeToDebugNode[nativeNode];
}
List<DebugNode> getAllDebugNodes() {
return MapWrapper.values(_nativeNodeToDebugNode);
}
indexDebugNode(DebugNode node) {
_nativeNodeToDebugNode[node.nativeNode] = node;
}
removeDebugNodeFromIndex(DebugNode node) {
(_nativeNodeToDebugNode.containsKey(node.nativeNode) &&
(_nativeNodeToDebugNode.remove(node.nativeNode) != null || true));
}