forked from alexyoung/turing.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turing.events.js
333 lines (287 loc) · 8.61 KB
/
turing.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*!
* Turing Events
* Copyright (C) 2010-2011 Alex R. Young
* MIT Licensed
*/
/**
* The Turing Events module.
*
*/
(function() {
var events = {}, cache = [], onReadyBound = false, isReady = false, DOMContentLoaded, readyCallbacks = [];
function isValidElement(element) {
return element.nodeType !== 3 && element.nodeType !== 8;
}
function stop(event) {
event.preventDefault(event);
event.stopPropagation(event);
}
function fix(event, element) {
if (!event) var event = window.event;
event.stop = function() { stop(event); };
if (typeof event.target === 'undefined')
event.target = event.srcElement || element;
if (!event.preventDefault)
event.preventDefault = function() { event.returnValue = false; };
if (!event.stopPropagation)
event.stopPropagation = function() { event.cancelBubble = true; };
if (event.target && event.target.nodeType === 3)
event.target = event.target.parentNode;
if (event.pageX == null && event.clientX != null) {
var doc = document.documentElement, body = document.body;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}
return event;
}
function createResponder(element, handler) {
return function(event) {
fix(event, element);
return handler(event);
};
}
function removeCachedResponder(element, type, handler) {
var i = 0, responder, j = 0;
for (j = 0; j < cache.length; j++) {
if (cache[j].element !== element
&& cache[j].type !== type
&& cache[j].handler !== handler) {
cache[i++] = cache[j];
} else {
responder = cache[j].responder;
}
}
cache.length = i;
return responder;
}
function ready() {
if (!isReady) {
// Make sure body exists
if (!document.body) {
return setTimeout(ready, 13);
}
isReady = true;
for (var i in readyCallbacks) {
readyCallbacks[i]();
}
readyCallbacks = null;
// TODO:
// When custom events work properly in IE:
// events.fire(document, 'dom:ready');
}
}
// This checks if the DOM is ready recursively
function DOMReadyScrollCheck() {
if (isReady) {
return;
}
try {
document.documentElement.doScroll('left');
} catch(e) {
setTimeout(DOMReadyScrollCheck, 1);
return;
}
ready();
}
// DOMContentLoaded cleans up listeners
if (document.addEventListener) {
DOMContentLoaded = function() {
document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);
ready();
};
} else if ( document.attachEvent ) {
DOMContentLoaded = function() {
if (document.readyState === 'complete') {
document.detachEvent('onreadystatechange', DOMContentLoaded);
ready();
}
};
}
function bindOnReady() {
if (onReadyBound) return;
onReadyBound = true;
if (document.readyState === 'complete') {
ready();
} else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', DOMContentLoaded, false );
window.addEventListener('load', ready, false);
} else if (document.attachEvent) {
document.attachEvent('onreadystatechange', DOMContentLoaded);
window.attachEvent('onload', ready);
// Check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if (document.documentElement.doScroll && toplevel) {
DOMReadyScrollCheck();
}
}
}
function IEType(type) {
if (type.match(/:/)) {
return type;
}
return 'on' + type;
}
/**
* Bind an event to an element.
*
* turing.events.add(element, 'click', function() {
* console.log('Clicked');
* });
*
* @param {Object} element A DOM element
* @param {String} type The event name
* @param {Function} handler The event handler
*/
events.add = function(element, type, handler) {
if (!isValidElement(element)) return;
var responder = createResponder(element, handler);
cache.push({ element: element, type: type, handler: handler, responder: responder });
if (type.match(/:/) && element.attachEvent) {
element.attachEvent('ondataavailable', responder);
} else {
if (element.addEventListener) {
element.addEventListener(type, responder, false);
} else if (element.attachEvent) {
element.attachEvent(IEType(type), responder);
}
}
};
/**
* Remove an event from an element.
*
* turing.events.add(element, 'click', callback);
*
* @param {Object} element A DOM element
* @param {String} type The event name
* @param {Function} handler The event handler
*/
events.remove = function(element, type, handler) {
if (!isValidElement(element)) return;
var responder = removeCachedResponder(element, type, handler);
if (document.removeEventListener) {
element.removeEventListener(type, responder, false);
} else {
element.detachEvent(IEType(type), responder);
}
};
/**
* Fires an event.
*
* turing.events.fire(element, 'click');
*
* @param {Object} element A DOM element
* @param {String} type The event name
* @returns {Object} The browser's `fireEvent` or `dispatchEvent` result
*/
events.fire = function(element, type) {
var event;
if (document.createEventObject) {
event = document.createEventObject();
fix(event, element);
// This isn't quite ready
if (type.match(/:/)) {
event.eventName = type;
event.eventType = 'ondataavailable';
return element.fireEvent(event.eventType, event)
} else {
return element.fireEvent(IEType(type), event)
}
} else {
event = document.createEvent('HTMLEvents');
fix(event, element);
event.eventName = type;
event.initEvent(type, true, true);
return !element.dispatchEvent(event);
}
};
/**
* Add a 'DOM ready' callback.
*
* turing.events.ready(function() {
* // The DOM is ready
* });
*
* @param {Function} callback A callback to run
*/
events.ready = function(callback) {
bindOnReady();
readyCallbacks.push(callback);
};
if (turing.dom !== 'undefined') {
events.delegate = function(element, selector, type, handler) {
return events.add(element, type, function(event) {
var matches = turing.dom.findElement(event.target, selector, event.currentTarget);
if (matches) {
handler(event);
}
});
};
}
/**
* Events can be chained with DOM calls:
*
* turing('p').bind('click', function(e) {
* alert('ouch');
* });
*
* The event will be bound to each matching element.
*
*/
events.addDOMethods = function() {
if (typeof turing.domChain === 'undefined') return;
turing.domChain.bind = function(type, handler) {
var element;
for (var i = 0; i < this.length; i++) {
element = this[i];
if (handler) {
turing.events.add(element, type, handler);
} else {
turing.events.fire(element, type);
}
}
return this;
};
var chainedAliases = ('click dblclick mouseover mouseout mousemove ' +
'mousedowe mouseup blur focus change keydown ' +
'keypress keyup resize scroll').split(' ');
for (var i = 0; i < chainedAliases.length; i++) {
(function(name) {
turing.domChain[name] = function(handler) {
return this.bind(name, handler);
};
})(chainedAliases[i]);
}
};
events.addDOMethods();
/**
* DOM ready event handlers can also be set with:
*
* turing.ready(function() { });
*
* Or just by passing a function to `turing()`:
*
* turing(function() {} );
*
*/
turing.ready = events.ready;
turing.events = events;
turing.init(function(arg) {
if (arguments.length === 1
&& typeof arguments[0] === 'function') {
turing.events.ready(arguments[0]);
}
});
if (typeof window !== 'undefined' && window.attachEvent && !window.addEventListener) {
window.attachEvent('onunload', function() {
for (var i = 0; i < cache.length; i++) {
try {
events.remove(cache[i].element, cache[i].type);
cache[i] = null;
} catch(e) {}
}
});
}
})();