forked from angular/zone.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
196 lines (170 loc) · 6.2 KB
/
utils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as keys from './keys';
export function bindArguments(args) {
for (var i = args.length - 1; i >= 0; i--) {
if (typeof args[i] === 'function') {
args[i] = global.zone.bind(args[i]);
}
}
return args;
};
export function patchPrototype(obj, fnNames) {
fnNames.forEach(function (name) {
var delegate = obj[name];
if (delegate) {
obj[name] = function () {
return delegate.apply(this, bindArguments(arguments));
};
}
});
};
export function isWebWorker() {
return (typeof document === "undefined");
}
export function patchProperty(obj, prop) {
var desc = Object.getOwnPropertyDescriptor(obj, prop) || {
enumerable: true,
configurable: true
};
// A property descriptor cannot have getter/setter and be writable
// deleting the writable and value properties avoids this error:
//
// TypeError: property descriptors must not specify a value or be writable when a
// getter or setter has been specified
delete desc.writable;
delete desc.value;
// substr(2) cuz 'onclick' -> 'click', etc
var eventName = prop.substr(2);
var _prop = '_' + prop;
desc.set = function (fn) {
if (this[_prop]) {
this.removeEventListener(eventName, this[_prop]);
}
if (typeof fn === 'function') {
function wrapFn(event) {
var result;
result = fn.apply(this, arguments);
if (result != undefined && !result)
event.preventDefault();
};
this[_prop] = wrapFn;
this.addEventListener(eventName, wrapFn, false);
} else {
this[_prop] = null;
}
};
desc.get = function () {
return this[_prop];
};
Object.defineProperty(obj, prop, desc);
};
export function patchProperties(obj, properties?) {
(properties || (function () {
var props = [];
for (var prop in obj) {
props.push(prop);
}
return props;
}()).
filter(function (propertyName) {
return propertyName.substr(0,2) === 'on';
})).
forEach(function (eventName) {
patchProperty(obj, eventName);
});
};
var originalFnKey = keys.create('originalFn');
var boundFnsKey = keys.create('boundFns');
export function patchEventTargetMethods(obj) {
// This is required for the addEventListener hook on the root zone.
obj[keys.common.addEventListener] = obj.addEventListener;
obj.addEventListener = function (eventName, handler, useCapturing) {
//Ignore special listeners of IE11 & Edge dev tools, see https://github.com/angular/zone.js/issues/150
if (handler && handler.toString() !== "[object FunctionWrapper]") {
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
var fn;
if (handler.handleEvent) {
// Have to pass in 'handler' reference as an argument here, otherwise it gets clobbered in
// IE9 by the arguments[1] assignment at end of this function.
fn = (function(handler) {
return function() {
handler.handleEvent.apply(handler, arguments);
};
})(handler);
} else {
fn = handler;
}
handler[originalFnKey] = fn;
handler[boundFnsKey] = handler[boundFnsKey] || {};
handler[boundFnsKey][eventType] = handler[boundFnsKey][eventType] || global.zone.bind(fn);
arguments[1] = handler[boundFnsKey][eventType];
}
// - Inside a Web Worker, `this` is undefined, the context is `global` (= `self`)
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
// see https://github.com/angular/zone.js/issues/190
var target = this || global;
return global.zone.addEventListener.apply(target, arguments);
};
// This is required for the removeEventListener hook on the root zone.
obj[keys.common.removeEventListener] = obj.removeEventListener;
obj.removeEventListener = function (eventName, handler, useCapturing) {
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
if (handler && handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
var _bound = handler[boundFnsKey];
arguments[1] = _bound[eventType];
delete _bound[eventType];
global.zone.dequeueTask(handler[originalFnKey]);
}
// - Inside a Web Worker, `this` is undefined, the context is `global`
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
// see https://github.com/angular/zone.js/issues/190
var target = this || global;
var result = global.zone.removeEventListener.apply(target, arguments);
return result;
};
};
var originalInstanceKey = keys.create('originalInstance');
// wrap some native API on `window`
export function patchClass(className) {
var OriginalClass = global[className];
if (!OriginalClass) return;
global[className] = function () {
var a = bindArguments(arguments);
switch (a.length) {
case 0: this[originalInstanceKey] = new OriginalClass(); break;
case 1: this[originalInstanceKey] = new OriginalClass(a[0]); break;
case 2: this[originalInstanceKey] = new OriginalClass(a[0], a[1]); break;
case 3: this[originalInstanceKey] = new OriginalClass(a[0], a[1], a[2]); break;
case 4: this[originalInstanceKey] = new OriginalClass(a[0], a[1], a[2], a[3]); break;
default: throw new Error('what are you even doing?');
}
};
var instance = new OriginalClass();
var prop;
for (prop in instance) {
(function (prop) {
if (typeof instance[prop] === 'function') {
global[className].prototype[prop] = function () {
return this[originalInstanceKey][prop].apply(this[originalInstanceKey], arguments);
};
} else {
Object.defineProperty(global[className].prototype, prop, {
set: function (fn) {
if (typeof fn === 'function') {
this[originalInstanceKey][prop] = global.zone.bind(fn);
} else {
this[originalInstanceKey][prop] = fn;
}
},
get: function () {
return this[originalInstanceKey][prop];
}
});
}
}(prop));
}
for (prop in OriginalClass) {
if (prop !== 'prototype' && OriginalClass.hasOwnProperty(prop)) {
global[className][prop] = OriginalClass[prop];
}
}
};