/
util-events.js
235 lines (172 loc) · 5.16 KB
/
util-events.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// PhotoSwipe - http://www.photoswipe.com/
// Copyright (c) %%year%% by Code Computerlove (http://www.codecomputerlove.com)
// Licensed under the MIT license
// version: %%version%%
(function (window, Util) {
Util.extend(Util, {
Events: {
/*
* Function: add
* Add an event handler
*/
add: function(obj, type, handler){
this._checkHandlersProperty(obj);
if (typeof obj.__eventHandlers[type] === 'undefined'){
obj.__eventHandlers[type] = [];
}
obj.__eventHandlers[type].push(handler);
// DOM element
if (this._isBrowserObject(obj)){
obj.addEventListener(type, handler, false);
}
},
/*
* Function: remove
* Removes a handler or all handlers associated with a type
*/
remove: function(obj, type, handler){
this._checkHandlersProperty(obj);
if (obj.__eventHandlers[type] instanceof Array){
var
i,
handlers = obj.__eventHandlers[type];
// Removing all handlers for a type
if (Util.isNothing(handler)){
if (this._isBrowserObject(obj)){
for (var i=0; i<handlers.length; i++){
obj.removeEventListener(type, handlers[i], false);
}
}
obj.__eventHandlers[type] = [];
return;
}
// Removing a specific handler
for (var i=0, len=handlers.length; i < len; i++){
if (handlers[i] === handler){
handlers.splice(i, 1);
break;
}
}
// DOM element
if (this._isBrowserObject(obj)){
obj.removeEventListener(type, handler, false);
return;
}
}
},
/*
* Function: fire
* Fire an event
*/
fire: function(obj, type){
var
event,
listeners,
args = Array.prototype.slice.call(arguments).splice(2);
// DOM element
if (this._isBrowserObject(obj)){
if (typeof type !== "string"){
throw 'type must be a string for DOM elements';
}
var isNative = this._NATIVE_EVENTS[type]
event = document.createEvent(isNative ? "HTMLEvents" : "UIEvents");
event[isNative ? 'initEvent' : 'initUIEvent'](type, true, true, window, 1);
// Fire an event on an element that has no extra arguments
if (args.length < 1){
obj.dispatchEvent(event);
return;
}
}
this._checkHandlersProperty(obj);
if (typeof type == "string"){
event = { type: type };
}
else{
event = type;
}
if (!event.target){
event.target = obj;
}
if (!event.type){
throw new Error("Event object missing 'type' property.");
}
if (obj.__eventHandlers[event.type] instanceof Array){
listeners = obj.__eventHandlers[event.type];
args.unshift(event);
for (var i=0, len=listeners.length; i < len; i++){
listeners[i].apply(obj, args);
}
}
},
/*
* Function: getMousePosition
*/
getMousePosition: function(event){
var retval = {
x: 0,
y: 0
};
if (event.pageX) {
retval.x = event.pageX;
}
else if (event.clientX) {
retval.x = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}
if (event.pageY) {
retval.y = event.pageY;
}
else if (event.clientY) {
retval.y = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
return retval;
},
/*
* Function: getTouchEvent
*/
getTouchEvent: function(event){
return event;
},
_checkHandlersProperty: function(obj){
if (Util.isNothing(obj.__eventHandlers)){
Util.extend(obj, {
__eventHandlers: { }
});
}
},
_isBrowserObject: function(obj){
if (obj === window || obj === window.document){
return true;
}
return this._isElement(obj) || this._isNode(obj);
},
_isElement: function(obj){
return (
typeof HTMLElement === "object" ? obj instanceof HTMLElement : //DOM2
typeof obj === "object" && obj.nodeType === 1 && typeof obj.nodeName==="string"
);
},
_isNode: function(obj){
return (
typeof Node === "object" ? obj instanceof Node :
typeof obj === "object" && typeof obj.nodeType === "number" && typeof obj.nodeName==="string"
);
},
_NATIVE_EVENTS: {
click: 1, dblclick: 1, mouseup: 1, mousedown: 1, contextmenu: 1, //mouse buttons
mousewheel: 1, DOMMouseScroll: 1, //mouse wheel
mouseover: 1, mouseout: 1, mousemove: 1, selectstart: 1, selectend: 1, //mouse movement
keydown: 1, keypress: 1, keyup: 1, //keyboard
orientationchange: 1, // mobile
touchstart: 1, touchmove: 1, touchend: 1, touchcancel: 1, // touch
gesturestart: 1, gesturechange: 1, gestureend: 1, // gesture
focus: 1, blur: 1, change: 1, reset: 1, select: 1, submit: 1, //form elements
load: 1, unload: 1, beforeunload: 1, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window
error: 1, abort: 1, scroll: 1
}
}
});
})
(
window,
Code.PhotoSwipe.Util
);