Repository navigation
Expand file tree
/
Copy patheventShim.js
More file actions
190 lines (149 loc) · 7.03 KB
/
Copy patheventShim.js
File metadata and controls
190 lines (149 loc) · 7.03 KB
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
/* Copyright (c) 2012 Jeremy McPeak http://www.wdonline.com
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function() {
function init() {
var loadedForBrowser = false;
if (Object.defineProperty) {
// create an MS event object and get prototype
var proto = document.createEventObject().constructor.prototype;
/**
* Indicates whether an event propagates up from the target.
* @returns Boolean
*/
Object.defineProperty(proto, "bubbles", {
get: function() {
// not a complete list of DOM3 events; some of these IE8 doesn't support
var bubbleEvents = ["select", "scroll", "click", "dblclick",
"mousedown", "mousemove", "mouseout", "mouseover", "mouseup", "wheel", "textinput",
"keydown", "keypress", "keyup"],
type = this.type;
for (var i = 0, l = bubbleEvents.length; i < l; i++) {
if (type === bubbleEvents[i]) {
return true;
}
}
return false;
}
});
/**
* Indicates whether or not preventDefault() was called on the event.
* @returns Boolean
*/
Object.defineProperty(proto, "defaultPrevented", {
get: function() {
// if preventDefault() was never called, or returnValue not given a value
// then returnValue is undefined
var returnValue = this.returnValue,
undef;
return !(returnValue === undef || returnValue);
}
});
/**
* Gets the secondary targets of mouseover and mouseout events (toElement and fromElement)
* @returns EventTarget or {null}
*/
Object.defineProperty(proto, "relatedTarget", {
get: function() {
var type = this.type;
if (type === "mouseover" || type === "mouseout") {
return (type === "mouseover") ? this.fromElement : this.toElement;
}
return null;
}
});
/**
* Gets the target of the event (srcElement)
* @returns EventTarget
*/
Object.defineProperty(proto, "target", {
get: function() { return this.srcElement; }
});
loadedForBrowser = true;
}
if (!proto.preventDefault) {
/**
* Cancels the event if it is cancelable. (returnValue)
* @returns {undefined}
*/
proto.preventDefault = function() {
this.returnValue = false;
};
loadedForBrowser = true;
}
if (!proto.stopPropagation) {
/**
* Prevents further propagation of the current event. (cancelBubble())
* @returns {undefined}
*/
proto.stopPropagation = function() {
this.cancelBubble = true;
};
loadedForBrowser = true;
}
/***************************************
*
* Event Listener Setup
* Nothing complex here
*
***************************************/
if (!Element.prototype.addEventListener) {
/**
* Determines if the provided object implements EventListener
* @returns boolean
*/
var implementsEventListener = function(obj) {
return (typeof obj !== "function" && typeof obj["handleEvent"] === "function");
};
var customELKey = "__eventShim__";
/**
* Adds an event listener to the DOM object
* @returns {undefined}
*/
var addEventListenerFunc = function(type, handler, useCapture) {
// useCapture isn't used; it's IE!
var fn = handler;
if (implementsEventListener(handler)) {
if (typeof handler[customELKey] !== "function") {
handler[customELKey] = function(e) {
handler["handleEvent"](e);
};
}
fn = handler[customELKey];
}
this.attachEvent("on" + type, fn.bind(this));
};
/**
* Removes an event listener to the DOM object
* @returns {undefined}
*/
var removeEventListenerFunc = function(type, handler, useCapture) {
// useCapture isn't used; it's IE!
var fn = handler;
if (implementsEventListener(handler)) {
fn = handler[customELKey];
}
this.detachEvent("on" + type, fn.bind(this));
};
// setup the DOM and window objects
HTMLDocument.prototype.addEventListener = addEventListenerFunc;
HTMLDocument.prototype.removeEventListener = removeEventListenerFunc;
Element.prototype.addEventListener = addEventListenerFunc;
Element.prototype.removeEventListener = removeEventListenerFunc;
window.addEventListener = addEventListenerFunc;
window.removeEventListener = removeEventListenerFunc;
loadedForBrowser = true;
}
return {
loadedForBrowser : loadedForBrowser
};
}
// check for AMD support
if (typeof define === "function" && define["amd"]) {
define(init);
} else {
return init();
}
}());