/
vorlon.ngInspector.client.ts
172 lines (140 loc) · 6.67 KB
/
vorlon.ngInspector.client.ts
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
module VORLON {
declare var angular: any;
export class NgInspectorClient extends ClientPlugin {
private _rootScopes: Scope[] = [];
private _currentShownScopeId: number = null;
constructor() {
super("ngInspector");
this._ready = false;
}
public getID(): string {
return "NGINSPECTOR";
}
public refresh(): void {
this._packageAndSendScopes();
}
public startClientSide(): void {
document.addEventListener("DOMContentLoaded",() => {
this._ready = true;
this.refresh();
});
}
public onRealtimeMessageReceivedFromDashboardSide(receivedObject: any): void {
if (typeof angular == 'undefined')
return;
if (receivedObject.type === MessageType.ReloadWithDebugInfo) {
angular.reloadWithDebugInfo();
}
}
private _timeoutId: NodeJS.Timer;
private _markForRefresh() {
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
this._timeoutId = setTimeout(() => {
this.refresh();
}, 2000);
}
private _packageAndSendScopes(): void {
this._rootScopes = [];
this._findRootScopes(document.body);
this.sendToDashboard({ scopes: this._rootScopes });
}
private _findRootScopes(element: Node) {
if (typeof angular == 'undefined')
return;
var rootScope = angular.element(element).scope();
if (!!rootScope) {
var cleanedRootScope = this._cleanScope(rootScope);
cleanedRootScope.$type = ScopeType.RootScope;
cleanedRootScope.$name = "$rootScope";
this._rootScopes.push(cleanedRootScope);
this._findChildrenScopes(element, cleanedRootScope);
this._listenScopeChanges(rootScope);
} else {
for (var i = 0; i < element.childNodes.length; i++) {
this._findRootScopes(element.childNodes[i]);
}
}
}
private _findChildrenScopes(element: Node, parentScope: Scope) {
if (typeof angular == 'undefined')
return;
for (var i = 0; i < element.childNodes.length; i++) {
var childNode = element.childNodes[i];
var childScope = angular.element(childNode).scope();
if (!!childScope
&& childNode instanceof Element
&& childScope.$id !== parentScope.$id
&& parentScope.$children.indexOf(childScope) === -1) {
var cleanedChildScope = this._cleanScope(childScope);
if (childNode.attributes &&
(childNode.attributes["ng-repeat"] ||
childNode.attributes["data-ng-repeat"] ||
childNode.attributes["x-ng-repeat"] ||
childNode.attributes["ng_repeat"] ||
childNode.attributes["ng:repeat"])) {
cleanedChildScope.$type = ScopeType.NgRepeat;
cleanedChildScope.$name = "ng-repeat";
}
else if (angular.element(childNode).data("$ngControllerController")) {
var constructor: string = angular.element(childNode).data("$ngControllerController").constructor;
// Workaround for IE, name property of constructor return undefined :/
// Get the name from the constructor function as string
var match = constructor.toString().match(/function (\w+)\(/);
var name: string = "";
if (!!match && match.length > 1) {
name = match[1];
}
else {
var ngControllerAttribute = childNode.attributes["ng-controller"] ||
childNode.attributes["data-ng-controller"] ||
childNode.attributes["x-ng-controller"] ||
childNode.attributes["ng_controller"] ||
childNode.attributes["ng:controller"];
if (ngControllerAttribute) {
name = ngControllerAttribute.name;
}
}
cleanedChildScope.$type = ScopeType.Controller;
cleanedChildScope.$name = name;
}
parentScope.$children.push(cleanedChildScope);
this._findChildrenScopes(childNode, cleanedChildScope);
} else {
this._findChildrenScopes(childNode, parentScope);
}
}
}
private _listenScopeChanges(scope: any) {
scope.$watch((newValue, oldValue) => {
this._markForRefresh();
});
}
private _scopePropertiesNamesToExclude: string[] = ["$$applyAsyncQueue", "$$asyncQueue", "$$childHead", "$$ChildScope", "$$childTail", "$$destroyed", "$$isolateBindings", "$$listenerCount", "$$listeners", "$$nextSibling", "$$phase", "$$postDigest", "$$postDigestQueue", "$$prevSibling", "$$transcluded", "$$watchers", "$$watchersCount", "$apply", "$applyAsync", "$broadcast", "$childTail", "$destroy", "$digest", "$emit", "$eval", "$evalAsync", "$even", "$first", "$index", "$last", "$middle", "$new", "$odd", "$on", "$parent", "$root", "$watch", "$watchCollection", "$watchGroup"];
private _cleanScope(scope: any): Scope {
var scopePackaged: Scope = {
$id: null,
$parentId: null,
$children: [],
$functions: [],
$type: ScopeType.Controller,
$name: ""
};
var keys = Object.keys(scope);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (this._scopePropertiesNamesToExclude.indexOf(key) === -1) {
scopePackaged[key] = scope[key];
}
}
if (scope.$parent !== null) {
scopePackaged.$parentId = scope.$parent.$id;
}
return scopePackaged;
}
}
// Register
Core.RegisterClientPlugin(new NgInspectorClient());
}