-
Notifications
You must be signed in to change notification settings - Fork 9
/
polyfill.js
66 lines (59 loc) · 2 KB
/
polyfill.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
// Polyfill for creating CustomEvents on IE9/10/11
// code pulled from:
// https://github.com/d4tocchini/customevent-polyfill
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Polyfill
(function() {
if (typeof window === 'undefined') {
return;
}
try {
var ce = new window.CustomEvent('test', { cancelable: true });
ce.preventDefault();
if (ce.defaultPrevented !== true) {
// IE has problems with .preventDefault() on custom events
// http://stackoverflow.com/questions/23349191
throw new Error('Could not prevent default');
}
} catch (e) {
var CustomEvent = function(event, params) {
var evt, origPrevent;
// We use here some version of `Object.assign` implementation, to create a shallow copy of `params`.
// Based on https://github.com/christiansany/object-assign-polyfill/blob/213cc63df14515fb543117059d1576204bfaa8a7/index.js
var newParams = {};
// Skip over if undefined or null
if (params != null) {
for (var nextKey in params) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(params, nextKey)) {
newParams[nextKey] = params[nextKey];
}
}
}
newParams.bubbles = !!newParams.bubbles;
newParams.cancelable = !!newParams.cancelable;
evt = document.createEvent('CustomEvent');
evt.initCustomEvent(
event,
newParams.bubbles,
newParams.cancelable,
newParams.detail
);
origPrevent = evt.preventDefault;
evt.preventDefault = function() {
origPrevent.call(this);
try {
Object.defineProperty(this, 'defaultPrevented', {
get: function() {
return true;
}
});
} catch (e) {
this.defaultPrevented = true;
}
};
return evt;
};
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent; // expose definition to window
}
})();