This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathscopes.js
309 lines (260 loc) · 7.39 KB
/
scopes.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict';
var summarize = require('../lib/summarize-model');
var debounceOn = require('debounce-on');
var hint = angular.hint;
hint.emit = hint.emit || function () {};
module.exports = angular.module('ngHintScopes', []).config(['$provide', function ($provide) {
$provide.decorator('$rootScope', ['$delegate', '$parse', decorateRootScope]);
$provide.decorator('$compile', ['$delegate', decorateDollaCompile]);
}]);
function decorateRootScope($delegate, $parse) {
var perf = window.performance || { now: function () { return 0; } };
var scopes = {},
watching = {};
var debouncedEmitModelChange = debounceOn(emitModelChange, 10, byScopeId);
hint.watch = function (scopeId, path) {
path = typeof path === 'string' ? path.split('.') : path;
if (!watching[scopeId]) {
watching[scopeId] = {};
}
for (var i = 1, ii = path.length; i <= ii; i += 1) {
var partialPath = path.slice(0, i).join('.');
if (watching[scopeId][partialPath]) {
continue;
}
var get = gettterer(scopeId, partialPath);
var value = summarize(get());
watching[scopeId][partialPath] = {
get: get,
value: value
};
hint.emit('model:change', {
id: scopeId,
path: partialPath,
value: value
});
}
};
hint.assign = function (scopeId, path, value) {
var scope;
if (scope = scopes[scopeId]) {
scope.$apply(function () {
return $parse(path).assign(scope, value);
});
}
};
hint.inspectScope = function (scopeId) {
var scope;
if (scope = scopes[scopeId]) {
window.$scope = scope;
}
};
hint.unwatch = function (scopeId, unwatchPath) {
Object.keys(watching[scopeId]).
forEach(function (path) {
if (path.indexOf(unwatchPath) === 0) {
delete watching[scopeId][path];
}
});
};
var debouncedEmit = debounceOn(hint.emit, 10, function (params) {
return params.id + params.path;
});
var scopePrototype = ('getPrototypeOf' in Object) ?
Object.getPrototypeOf($delegate) : $delegate.__proto__;
// var _watch = scopePrototype.$watch;
// scopePrototype.$watch = function (watchExpression, reactionFunction) {
// var watchStr = humanReadableWatchExpression(watchExpression);
// var scopeId = this.$id;
// if (typeof watchExpression === 'function') {
// arguments[0] = function () {
// var start = perf.now();
// var ret = watchExpression.apply(this, arguments);
// var end = perf.now();
// hint.emit('scope:watch', {
// id: scopeId,
// watch: watchStr,
// time: end - start
// });
// return ret;
// };
// } else {
// var thatScope = this;
// arguments[0] = function () {
// var start = perf.now();
// var ret = thatScope.$eval(watchExpression);
// var end = perf.now();
// hint.emit('scope:watch', {
// id: scopeId,
// watch: watchStr,
// time: end - start
// });
// return ret;
// };
// }
// if (typeof reactionFunction === 'function') {
// var applyStr = reactionFunction.toString();
// arguments[1] = function () {
// var start = perf.now();
// var ret = reactionFunction.apply(this, arguments);
// var end = perf.now();
// hint.emit('scope:reaction', {
// id: this.$id,
// watch: watchStr,
// time: end - start
// });
// return ret;
// };
// }
// return _watch.apply(this, arguments);
// };
var _destroy = scopePrototype.$destroy;
scopePrototype.$destroy = function () {
var id = this.id;
hint.emit('scope:destroy', { id: id });
delete scopes[id];
delete watching[id];
return _destroy.apply(this, arguments);
};
var _new = scopePrototype.$new;
scopePrototype.$new = function () {
var child = _new.apply(this, arguments);
scopes[child.$id] = child;
watching[child.$id] = {};
hint.emit('scope:new', { parent: this.$id, child: child.$id });
setTimeout(function () {
emitScopeElt(child);
}, 0);
return child;
};
function emitScopeElt (scope) {
var scopeId = scope.$id;
var elt = findElt(scopeId);
var descriptor = scopeDescriptor(elt, scope);
hint.emit('scope:link', {
id: scopeId,
descriptor: descriptor
});
}
function findElt (scopeId) {
var elts = document.querySelectorAll('.ng-scope');
var elt, scope;
for (var i = 0; i < elts.length; i++) {
elt = angular.element(elts[i]);
scope = elt.scope();
if (scope.$id === scopeId) {
return elt;
}
}
}
// var _digest = scopePrototype.$digest;
// scopePrototype.$digest = function (fn) {
// var start = perf.now();
// var ret = _digest.apply(this, arguments);
// var end = perf.now();
// hint.emit('scope:digest', { id: this.$id, time: end - start });
// return ret;
// };
var _apply = scopePrototype.$apply;
scopePrototype.$apply = function (fn) {
// var start = perf.now();
var ret = _apply.apply(this, arguments);
// var end = perf.now();
// hint.emit('scope:apply', { id: this.$id, time: end - start });
debouncedEmitModelChange(this);
return ret;
};
function gettterer (scopeId, path) {
if (path === '') {
return function () {
return scopes[scopeId];
};
}
var getter = $parse(path);
return function () {
return getter(scopes[scopeId]);
};
}
function emitModelChange (scope) {
var scopeId = scope.$id;
if (watching[scopeId]) {
Object.keys(watching[scopeId]).forEach(function (path) {
var model = watching[scopeId][path];
var value = summarize(model.get());
if (value !== model.value) {
hint.emit('model:change', {
id: scope.$id,
path: path,
oldValue: model.value,
value: value
});
model.value = value;
}
});
}
}
hint.emit('scope:new', {
parent: null,
child: $delegate.$id
});
scopes[$delegate.$id] = $delegate;
watching[$delegate.$id] = {};
return $delegate;
}
function decorateDollaCompile ($delegate) {
var newCompile = function () {
var link = $delegate.apply(this, arguments);
return function (scope) {
var elt = link.apply(this, arguments);
var descriptor = scopeDescriptor(elt, scope);
hint.emit('scope:link', {
id: scope.$id,
descriptor: descriptor
});
return elt;
}
};
// TODO: test this
// copy private helpers like $$addScopeInfo
for (var prop in $delegate) {
if ($delegate.hasOwnProperty(prop)) {
newCompile[prop] = $delegate[prop];
}
}
return newCompile;
}
var TYPES = [
'ng-app',
'ng-controller',
'ng-repeat',
'ng-include'
];
function scopeDescriptor (elt, scope) {
var val,
theseTypes = [],
type;
if (elt) {
for (var i = 0, ii = TYPES.length; i < ii; i++) {
type = TYPES[i];
if (val = elt.attr(type)) {
theseTypes.push(type + '="' + val + '"');
}
}
}
if (theseTypes.length === 0) {
return 'scope.$id=' + scope.$id;
} else {
return theseTypes.join(' ');
}
}
function byScopeId (scope) {
return scope.$id;
}
function humanReadableWatchExpression (fn) {
if (fn.exp) {
fn = fn.exp;
} else if (fn.name) {
fn = fn.name;
}
return fn.toString();
}