This repository has been archived by the owner on Mar 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
EventListenerOptions.polyfill.js
105 lines (98 loc) · 3.82 KB
/
EventListenerOptions.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
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
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name EventListenerOptions.shim.min.js
// @language ECMASCRIPT5
// ==/ClosureCompiler==
(function() {
var supportsPassive = false;
document.createElement("div").addEventListener("test", function() {}, {
get passive() {
supportsPassive = true;
return false;
}
});
if (!supportsPassive) {
var super_add_event_listener = EventTarget.prototype.addEventListener;
var super_remove_event_listener = EventTarget.prototype.removeEventListener;
var super_prevent_default = Event.prototype.preventDefault;
function parseOptions(type, listener, options, action) {
var needsWrapping = false;
var useCapture = false;
var passive = false;
var fieldId;
if (options) {
if (typeof(options) === 'object') {
passive = options.passive ? true : false;
useCapture = options.useCapture ? true : false;
} else {
useCapture = options;
}
}
if (passive)
needsWrapping = true;
if (needsWrapping) {
fieldId = useCapture.toString();
fieldId += passive.toString();
}
action(needsWrapping, fieldId, useCapture, passive);
}
Event.prototype.preventDefault = function() {
if (this.__passive) {
console.warn("Ignored attempt to preventDefault an event from a passive listener");
return;
}
super_prevent_default.apply(this);
}
EventTarget.prototype.addEventListener = function(type, listener, options) {
var super_this = this;
parseOptions(type, listener, options,
function(needsWrapping, fieldId, useCapture, passive) {
if (needsWrapping) {
var fieldId = useCapture.toString();
fieldId += passive.toString();
if (!this.__event_listeners_options)
this.__event_listeners_options = {};
if (!this.__event_listeners_options[type])
this.__event_listeners_options[type] = {};
if (!this.__event_listeners_options[type][listener])
this.__event_listeners_options[type][listener] = [];
if (this.__event_listeners_options[type][listener][fieldId])
return;
var wrapped = {
handleEvent: function (e) {
e.__passive = passive;
if (typeof(listener) === 'function') {
listener(e);
} else {
listener.handleEvent(e);
}
e.__passive = false;
}
};
this.__event_listeners_options[type][listener][fieldId] = wrapped;
super_add_event_listener.call(super_this, type, wrapped, useCapture);
} else {
super_add_event_listener.call(super_this, type, listener, useCapture);
}
});
}
EventTarget.prototype.removeEventListener = function(type, listener, options) {
var super_this = this;
parseOptions(type, listener, options,
function(needsWrapping, fieldId, useCapture, passive) {
if (needsWrapping &&
this.__event_listeners_options &&
this.__event_listeners_options[type] &&
this.__event_listeners_options[type][listener] &&
this.__event_listeners_options[type][listener][fieldId]) {
super_remove_event_listener.call(super_this, type, this.__event_listeners_options[type][listener][fieldId], false);
delete this.__event_listeners_options[type][listener][fieldId];
if (this.__event_listeners_options[type][listener].length == 0)
delete this.__event_listeners_options[type][listener];
} else {
super_remove_event_listener.call(super_this, type, listener, useCapture);
}
});
}
}
})();