-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventFunctions.js
134 lines (126 loc) · 3.73 KB
/
eventFunctions.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
/*@eventFunctions.js
Injects polyfills and helper methods for dealing with events.
*/
(function (J) {
"use strict";
/* Polyfil for custom events*/
window.CustomEvent = function( eventName, defaultInitDict ) {
function CustomEvent(type, eventInitDict) {
var event;
if ('createEvent' in document){
event = document.createEvent(eventName);
if (type !== null) {
initCustomEvent.call(
event,
type,
(eventInitDict || (eventInitDict = defaultInitDict)).bubbles,
eventInitDict.cancelable,
eventInitDict.detail
);
} else {
event.initCustomEvent = initCustomEvent;
}
} else {
event = document.createEventObject();
event.type = type;
}
return event;
}
function initCustomEvent(type, bubbles, cancelable, detail) {
this['init' + eventName](type, bubbles, cancelable, detail);
if (!('detail' in this)){
this.detail = detail;
}
}
return CustomEvent;
}(
'CustomEvent' in window ? 'CustomEvent' : 'Event',
{
bubbles: false,
cancelable: false,
detail: null
}
);
// Polyfil addEventListener for IE8.
if (!Event.prototype.preventDefault) {
Event.prototype.preventDefault = function() {
this.returnValue=false;
};
}
if (!Event.prototype.stopPropagation) {
Event.prototype.stopPropagation=function() {
this.cancelBubble=true;
};
}
if (!Element.prototype.addEventListener) {
(function () {
var eventListeners=[];
var addEventListener=function(type,listener /*, useCapture (will be ignored) */) {
var self=this;
var wrapper=function(e) {
e.target=e.srcElement;
e.currentTarget=self;
if (listener.handleEvent) {
listener.handleEvent(e);
} else {
listener.call(self,e);
}
};
if (type=="DOMContentLoaded") {
var wrapper2=function(e) {
if (document.readyState=="complete") {
wrapper(e);
}
};
document.attachEvent("onreadystatechange",wrapper2);
eventListeners.push({object:this,type:type,listener:listener,wrapper:wrapper2});
if (document.readyState=="complete") {
var e=new Event();
e.srcElement=window;
wrapper2(e);
}
} else {
this.attachEvent("on"+type,wrapper);
eventListeners.push({object:this,type:type,listener:listener,wrapper:wrapper});
}
};
var removeEventListener=function(type,listener /*, useCapture (will be ignored) */) {
var counter=0;
while (counter<eventListeners.length) {
var eventListener=eventListeners[counter];
if (eventListener.object==this && eventListener.type==type && eventListener.listener==listener) {
if (type=="DOMContentLoaded") {
this.detachEvent("onreadystatechange",eventListener.wrapper);
} else {
this.detachEvent("on"+type,eventListener.wrapper);
}
eventListeners.splice(counter, 1);
break;
}
++counter;
}
};
Element.prototype.addEventListener=addEventListener;
Element.prototype.removeEventListener=removeEventListener;
if (HTMLDocument) {
HTMLDocument.prototype.addEventListener=addEventListener;
HTMLDocument.prototype.removeEventListener=removeEventListener;
}
if (Window) {
Window.prototype.addEventListener=addEventListener;
Window.prototype.removeEventListener=removeEventListener;
}
})();
}
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true;
}
});
window.addEventListener("testPassive", null, opts);
window.removeEventListener("testPassive", null, opts);
} catch (e) { }
window.J.set('supportsPassiveEvents', supportsPassive);
}(window.J));