This repository has been archived by the owner on Feb 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ACT_event.js
499 lines (459 loc) · 16.5 KB
/
ACT_event.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright 2016, Yahoo Inc.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/* global ACT */
ACT.define('Event', [/*@<*/'Debug', /*>@*/ 'Lang'], function (ACT) {
'use strict';
var lang = ACT.Lang;
/*@<*/
var debug = ACT.Debug;
/*>@*/
/**
* Pair of events and HTML tags used for testing event support for current browser.
* @readonly
* @type {{select: string, change: string, submit: string, reset: string, error: string, load: string, abort: string}}
*/
var TAGNAMES = {
select: 'input',
change: 'input',
submit: 'form',
reset: 'form',
error: 'img',
load: 'img',
abort: 'img'
};
/**
* Object containing all events being listened
* @private
* @type {{}}
*/
var listeners = {};
/**
* Simple integer to keep generate a 'random index' for added events.
* @private
* @type {Number}
*/
var eventIndex = 0;
/**
* Event Utilities / Helpers
* @class Event
* @module ACT
* @requires lang
*/
function Event() {
/* istanbul ignore if */
if (Event.prototype.singleton) {
return Event.prototype.singleton;
}
Event.prototype.singleton = this;
}
/**
* @attribute ATTRS
* @type {{NAME: string, version: string}}
* @initOnly
*/
Event.ATTRS = {
NAME: 'event'
};
/**
* Checks event string against events supported by current browser.
* Used to decide if it's custom event or not.
* @param eventName event string
* @param element element to test
* @returns {boolean} is a native browser event
* @private
*/
function isEventSupported(eventName, element) {
var isSupported;
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
isSupported = (eventName in element);
if (!isSupported) {
/* istanbul ignore else */
if (!element.setAttribute) {
element = document.createElement('div');
}
/* istanbul ignore else */
if (element.setAttribute && element.removeAttribute) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] === 'function';
/* istanbul ignore else */
if (typeof element[eventName] !== 'undefined') {
element[eventName] = undefined;
}
element.removeAttribute(eventName);
}
}
element = null;
return isSupported;
}
/**
* Wrapper to add event listeners to objects.
* @param {String} event Event to trigger on
* @param {Function} fn Function to call when the event is fired
* @param {Object} element Element to attach the event to
* @param {Object} scope Scope Object to trigger the function with
* @method addListener
* @private
* @static
*/
function addListener(evnt, fn, element, scope) {
var bound = fn;
if (scope) {
bound = lang.bind(scope, [], fn);
}
if (element && isEventSupported(evnt, element)) {
if (window.addEventListener) {
element.addEventListener(evnt, bound, 0);
} else if (window.attachEvent) {
element.attachEvent('on' + evnt, bound);
}
} else {
element = null;
}
return {
element: element,
fn: fn,
scope: scope,
event: evnt,
bound: bound
};
}
Event.prototype = {
/*@<*/
removable: [],
/*>@*/
// exposing for debug and test
/*@<*/
debugListeners: listeners,
debugEventIndex: function () {
return eventIndex;
},
debugIsEventSupported: isEventSupported,
debugAddListener: addListener,
debugGetListeners: function () {
return listeners;
},
debugSetListeners: function (key, value) {
listeners[key] = value;
return listeners;
},
/*>@*/
/**
* Subscribe to listen to event
* @method on
* @param {String} event event string
* @param {Object} fn callback method
* @param {Object} element DOM element listening for event (optional)
* @param {Object} scope event scope - default to this
* @return {Object} obj.remove A simple wrapper to remove this event listener.
* @public
*
* @example
* var Event = ACT.Event;
* var Dom = ACT.Dom;
* // custom event
* Event.on( "customEventString", function( eventData ) { ...do something based on this event...}, null, this);
* // 'vanilla' event
* Event.on("click", function( eventData ) { Event.preventDefault(eventData); ... do something ...}, Dom.byId("some_node_id"), this);
*
*/
on: function (evnt, fn, element, scope) {
var root = this;
var list;
var index = eventIndex;
var returnSet;
eventIndex++;
/*@<*/
debug.log('[ ACT_event.js ] Attach ON for: ', evnt);
/*>@*/
// Create the topic's object if not yet created
if (!lang.objHasKey(listeners, evnt)) {
listeners[evnt] = {};
}
list = addListener(evnt, fn, element, scope);
listeners[evnt][index] = list;
// Provide handle back for removal of topic
returnSet = {
eventType: evnt,
index: index,
remove: function () {
/* if the event is removed already then ignore it */
if (!listeners[evnt][index]) {
return;
}
if (listeners[evnt][index].element) {
/* NOTE: The call to removeListener deletes the listeners[event][index] automagically. Hence
the delete is in the else. This is done in case someone calls removeListener directly.
*/
root.removeListener(evnt, fn, element);
} else {
delete listeners[evnt][index];
}
}
};
/*@<*/
this.removable.push(returnSet);
/*>@*/
return returnSet;
},
/**
* Wrapper to add event listeners to CSS.
* @param {String} event Event to trigger on
* @param {Function} fn Function to call when the event is fired
* @param {Object} element Element to attach the event to
* @param {Object} scope Scope Object to trigger the function with
* @method addCSSListener
* @public
* @static
*/
addCSSListener: function (evnt, fn, element, scope) {
this.on(evnt.toLowerCase(), fn, element, scope);
this.on('webkit' + evnt, fn, element, scope);
this.on('moz' + evnt, fn, element, scope);
this.on('ms' + evnt, fn, element, scope);
this.on('o' + evnt, fn, element, scope);
},
/**
* Helper Function to remove event listeners from objects.
* @param {String} event Event to remove
* @param {Function} fn Function to remove from the object
* @param {Object} element Element to remove the event from
* @method removeListenerHelper
* @private
* @static
*/
removeListenerHelper: function (evnt, fn, element) {
if (window.removeEventListener) {
element.removeEventListener(evnt, fn, 0);
/* istanbul ignore else */
} else if (window.detachEvent) {
element.detachEvent('on' + evnt, fn);
}
},
/**
* Wrapper to remove event listeners from objects.
* @param {String} event Event to remove
* @param {Function} fn Function to remove from the object
* @param {Object} element Element to remove the event from
* @method removeListener
* @public
* @static
*/
removeListener: function (evnt, fn, element) {
var list = listeners[evnt] || {};
var key;
var el;
for (key in list) {
if (list.hasOwnProperty(key)) {
el = list[key];
if (el && el.element === element && (el.fn === fn || el.bound === fn)) {
this.removeListenerHelper(evnt, el.bound, element);
delete listeners[evnt][key];
}
}
}
},
/**
* The 'ready' function is a modified version of the JQuery 'ready' functionality found at http://api.jquery.com/ready/
* The code below is modified according to the JQuery MIT License https://github.com/jquery/jquery
*/
ready: (function () {
var document = window.document;
var readyBound = false;
var callbackQueue = [];
var ready;
var registerOrRunCallback = function (callback) {
/* istanbul ignore else */
if (typeof callback === 'function') {
callbackQueue.push(callback);
}
};
var domReadyCallback = function () {
while (callbackQueue.length) {
(callbackQueue.shift())();
}
registerOrRunCallback = function (callback) {
callback();
};
};
var domReady = function () {
/* istanbul ignore else */
if (!ready.isReady) {
/* istanbul ignore else */
if (!document.body) {
return setTimeout(domReady, 1);
}
ready.isReady = true;
domReadyCallback();
}
return 0;
};
var DOMContentLoaded = function () {
if (document.addEventListener) {
document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);
} else {
document.detachEvent('onreadystatechange', DOMContentLoaded);
}
domReady();
};
var doScrollCheck = function () {
/* istanbul ignore else */
if (ready.isReady) {
return;
}
try {
document.documentElement.doScroll('left');
} catch (error) {
setTimeout(doScrollCheck, 1);
return;
}
domReady();
};
var bindReady = function () {
var toplevel = false;
/* istanbul ignore else */
if (readyBound) {
return;
}
readyBound = true;
/* istanbul ignore else */
if (document.readyState === 'complete') {
domReady();
}
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', DOMContentLoaded, false);
window.addEventListener('load', DOMContentLoaded, false);
/* istanbul ignore else */
} else if (document.attachEvent) {
document.attachEvent('onreadystatechange', DOMContentLoaded);
window.attachEvent('onload', DOMContentLoaded);
try {
toplevel = window.frameElement === null;
} catch (e) {
/*@<*/
debug.log('[ ACT_event.js ] Error in try catch for window.frameElement', e);
/*>@*/
}
/* istanbul ignore else */
if (document.documentElement.doScroll && toplevel) {
doScrollCheck();
}
}
};
ready = function (callback) {
registerOrRunCallback(callback);
bindReady();
};
ready.isReady = false;
return ready;
}()),
/**
* Removes all listeners from the given element.
* @param {Object} object to have purged
* @method purgeListeners
* @public
* @static
*/
purgeListeners: function (element) {
var evnt;
var key;
var el;
for (evnt in listeners) {
if (listeners.hasOwnProperty(evnt) && listeners[evnt] && lang.isObject(listeners[evnt])) {
for (key in listeners[evnt]) {
if (listeners[evnt].hasOwnProperty(key)) {
el = listeners[evnt][key];
if (el.element === element) {
this.removeListenerHelper(evnt, el.bound, element);
delete listeners[evnt][key];
}
}
}
}
}
},
/**
* Publish event for listeners to execute
* @method fire
* @param {String} event Name of the event that is being fired
* @param {Object} info Meta data that is being fired with the event
* @public
*
* @example
* ```
* // Now anyone can subscribe to the custom event.
* var Event = ACT.Event;
* var Dom = ACT.Dom;
* // Example of listener
* function listenForCustomEvent( event ) {
* if( event.hasOwnProperty("node")){
* if(event.node.id === "node1"){
* // got this event from node1
* } else if( event.node.id === "node2" ){
* // got this event from node2
* }
* } else if ( event.hasOwnProperty("meta") ){
* // heard first event.
* }
* }
* Event.on( "customEventString", listenForCustomEvent, null, this);
*
* Event.fire( "customEventString", { meta:"data" });
* // Event.fire example with the same event fired for two different elements.
* Event.fire( "customEventString", { "node": Dom.byId("node1"); });
* Event.fire( "customEventString", { "node": Dom.byId("node2"); });
* ```
*/
fire: function (evnt, info) {
var params = info || {};
var scope;
var index;
var item;
// If the topic doesn't exist, or there's no listeners in queue, just leave
/* istanbul ignore else */
if (!lang.objHasKey(listeners, evnt)) {
return;
}
// Cycle through topics queue, fire!
for (index in listeners[evnt]) {
/* istanbul ignore else */
if (listeners[evnt].hasOwnProperty(index)) {
item = listeners[evnt][index];
// if it doesn't have a specific scope, just run through all events of the same type.
scope = params.scope ? params.scope : item.scope;
/* istanbul ignore else */
if (scope === item.scope && lang.isFunction(item.fn)) {
if (scope) {
item.fn.call(scope, info);
} else {
item.fn(info);
}
}
}
}
},
/**
* Wrapper to prevent default behaviour on an event
* @param {Object} event Event that was triggered
* @method preventDefault
* @public
* @static
*/
preventDefault: function (evnt) {
if (evnt.preventDefault) {
evnt.preventDefault();
} else {
evnt.returnValue = false;
}
},
/**
* constructor
* @public
*/
constructor: Event
};
return new Event();
});