-
Notifications
You must be signed in to change notification settings - Fork 0
/
jAwn.js
1584 lines (1277 loc) · 51.4 KB
/
jAwn.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (jAwn, window, document, undefined) {
// Private Variables
var rnotwhite = (/\S+/g);
var rmsPrefix = /^-ms-/;
var rdashAlpha = /-([\da-z])/gi;
var fcamelCase = function (all, letter) {
return letter.toUpperCase();
};
// Private Methods
function camelCase (string) {
return string.replace(rmsPrefix, 'ms-').replace(rdashAlpha, fcamelCase);
};
function acceptData (owner) {
// Accepts only:
// - Node
// - Node.ELEMENT_NODE
// - Node.DOCUMENT_NODE
// - Object
// - Any
return owner && (owner.nodeType === 1 || owner.nodeType === 9 || !(+owner.nodeType));
};
// Data Cache Definition
function Data () {
this.cacheKey = 'jAwnCache3.0' + Data.guid++;
};
Data.guid = 1;
Data.accepts = acceptData;
Data.prototype = {
register: function (owner) {
var value = {};
// If it is a node unlikely to be stringify-ed or looped over use plain assignment
if (owner.nodeType) {
owner[this.cacheKey] = value;
}
else {
// Otherwise secure it in a non-enumerable, non-writable property
// configurability must be true to allow the property to be
// deleted with the delete operator
Object.defineProperty(owner, this.cacheKey, {
value: value,
writable: true,
configurable: true
});
}
return owner[this.cacheKey];
},
cache: function (owner) {
// We can accept data for non-element nodes in modern browsers, but we should not, see #8335.
// Always return an empty object.
if (!Data.accepts(owner)) {
return {};
}
// Check if the owner object already has a cache
var cache = owner[this.cacheKey];
// If so, return it
if (cache) {
return cache;
}
// If not, register one
return this.register(owner);
},
set: function (owner, data, value) {
var prop, cache = this.cache(owner);
if (typeof data === 'string') {
cache[camelCase(data)] = value;
}
else {
// Copy the properties one-by-one to the cache object
for (prop in data) {
cache[camelCase(prop)] = data[prop];
}
}
return cache;
},
get: function (owner, key) {
var cache = this.cache(owner);
return key === undefined ? cache : cache[camelCase(key)];
},
access: function (owner, key, value) {
// In cases where either:
//
// 1. No key was specified
// 2. A string key was specified, but no value provided
//
// Take the 'read' path and allow the get method to determine
// which value to return, respectively either:
//
// 1. The entire cache object
// 2. The data stored at the key
//
if (key === undefined || ((key && typeof key === 'string') && value === undefined)) {
return this.get(owner, key);
}
// [*]When the key is not a string, or both a key and value
// are specified, set or extend (existing objects) with either:
//
// 1. An object of properties
// 2. A key and value
//
this.set(owner, key, value);
// Since the 'set' path can have two possible entry points
// return the expected data based on which path was taken[*]
return value !== undefined ? value : key;
},
remove: function (owner, key) {
var index, cache = owner[this.cacheKey];
if (cache === undefined) {
return;
}
if (key !== undefined) {
// Support array or space separated string of keys
if (Array.isArray(key)) {
key = key.map(camelCase);
}
else {
key = camelCase(key);
// If a key with the spaces exists, use it.
// Otherwise, create an array by matching non-whitespace
key = key in cache ? [key] : (key.match(rnotwhite) || []);
}
index = key.length;
while (index--) {
delete cache[key[index]];
}
}
// Remove the cache key if there's no more data
if (key === undefined || isEmptyObject(cache)) {
delete owner[ this.cacheKey ];
}
},
hasData: function (owner) {
var cache = owner[this.cacheKey];
return cache !== undefined && !isEmptyObject(cache);
}
};
// Create new cache
var dataCache = new Data();
// Public Methods
jAwn.cache = {};
jAwn.cache.get = function (element, key) {
return dataCache.get(element, key);
};
jAwn.cache.set = function (element, key, value) {
return dataCache.set(element, key, value);
};
jAwn.cache.access = function (element, key, value) {
return dataCache.access(element, key, value);
};
jAwn.cache.hasData = function (element) {
return dataCache.hasData(element);
};
jAwn.cache.remove = function (element, key) {
return dataCache.remove(element, key);
};
// TODO: Test performance
// Called from jAwn.RemoveElement to cleanup data and events on elements prior to removal from DOM
function cleanElementData (elements) {
// Convert elements to an array, if necessary.
if (!elements.length) {
elements = [elements];
}
var data, element, type, index = 0;
var eventInternal = getInternal();
var special = eventInternal.special;
// For each element destroy widgets, remove events and delete any remaining data
for ( ; (element = elements[index]) !== undefined; index++) {
if (acceptData(element) && (data = element[dataCache.cacheKey])) {
if (data.events) {
// Destroy widgets on elements
try {
// Only trigger remove when necessary to save time
if (data.events.remove) {
jAwn.triggerHandler(element, 'remove');
}
} catch (e) {}
// Remove events
for (type in data.events) {
if (special[type]) {
eventInternal.remove(element, type);
}
else {
// Shortcut to avoid jAwn.event.remove's overhead
if (element.removeEventListener) {
element.removeEventListener(type, data.handle);
}
}
}
}
// Remove data
delete element[dataCache.cacheKey];
}
}
};
// Private Variables
var eventGUID = 0;
var commonGUID = 'jAwn' + ('3.0' + Math.random()).replace(/\D/g, '');
var expr = {
attrHandle: {},
match: {
bool: /^(?:checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$/i,
needsContext: /^[\x20\t\r\n\f]*[>+~]/
}
};
var focusinBubbles = 'onfocusin' in window;
var plainObject = {};
var hasOwn = plainObject.hasOwnProperty;
var toString = plainObject.toString;
var plainArray = [];
var slice = plainArray.slice;
var concat = plainArray.concat;
var push = plainArray.push;
var indexOf = plainArray.indexOf;
var rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|pointer|contextmenu|drag|drop)|click/,
rfocusMorph = /^(?:focusinfocus|focusoutblur)$/,
rtypenamespace = /^([^.]*)(?:\.(.+)|)/,
rnotwhite = (/\S+/g);
var eventInternal = {
global: {},
add: function (elem, types, handler, data, selector) {
var handleObjIn, eventHandle, tmp, events, t, handleObj, special, handlers, type, namespaces, origType,
elemData = jAwn.cache.get(elem) || {};
// Caller can pass in an object of custom data in lieu of the handler
if (handler.handler) {
handleObjIn = handler;
var context = {};
handler = handleObjIn.handler;
selector = handleObjIn.selector;
}
// If the selector is invalid, throw any exceptions at attach time
if (selector) {
find(selector, elem);
}
// Make sure that the handler has a unique ID, used to find/remove it later
if (!handler.guid) {
handler.guid = 'jAwnGUID' + eventGUID++;
}
// Init the element's event structure and main handler, if this is the first
if (!(events = elemData.events)) {
events = elemData.events = {};
}
if (!(eventHandle = elemData.handle)) {
eventHandle = elemData.handle = function (e) {
// Discard the second event of a eventInternal.trigger() and
// when an event is called after a page has unloaded
return typeof jAwn !== 'undefined' && eventInternal.triggered !== e.type ? eventInternal.dispatch.apply(elem, arguments) : undefined;
};
}
// Handle multiple events separated by a space
types = (types || '').match(rnotwhite) || [''];
t = types.length;
while (t--) {
tmp = rtypenamespace.exec(types[t]) || [];
type = origType = tmp[1];
namespaces = (tmp[2] || '').split('.').sort();
// There *must* be a type, no attaching namespace-only handlers
if (!type) {
continue;
}
// If event changes its type, use the special event handlers for the changed type
special = eventInternal.special[type] || {};
// If selector defined, determine special event api type, otherwise given type
type = (selector ? special.delegateType : special.bindType) || type;
// Update special based on newly reset type
special = eventInternal.special[type] || {};
// handleObj is passed to all event handlers
handleObj = mergeObjects({
type: type,
origType: origType,
data: data,
handler: handler,
guid: handler.guid,
selector: selector,
needsContext: selector && expr.match.needsContext.test(selector),
namespace: namespaces.join('.')
}, handleObjIn);
// Init the event handler queue if we're the first
if (!(handlers = events[type])) {
handlers = events[type] = [];
handlers.delegateCount = 0;
// Only use addEventListener if the special events handler returns false
if (!special.setup || special.setup.call(elem, data, namespaces, eventHandle) === false) {
if (elem.addEventListener) {
elem.addEventListener(type, eventHandle, false);
}
}
}
if (special.add) {
special.add.call(elem, handleObj);
if (!handleObj.handler.guid) {
handleObj.handler.guid = handler.guid;
}
}
// Add to the element's handler list, delegates in front
if (selector) {
handlers.splice(handlers.delegateCount++, 0, handleObj);
}
else {
handlers.push(handleObj);
}
// Keep track of which events have ever been used, for event optimization
eventInternal.global[type] = true;
}
},
// Detach an event or set of events from an element
remove: function (elem, types, handler, selector, mappedTypes) {
var j, origCount, tmp, events, t, handleObj, special, handlers, type, namespaces, origType,
elemData = jAwn.cache.hasData(elem) && jAwn.cache.get(elem);
if (!elemData || !(events = elemData.events)) {
return;
}
// Once for each type.namespace in types; type may be omitted
types = (types || '').match(rnotwhite) || [''];
t = types.length;
while (t--) {
tmp = rtypenamespace.exec(types[t]) || [];
type = origType = tmp[1];
namespaces = (tmp[2] || '').split('.').sort();
// Unbind all events (on this namespace, if provided) for the element
if (!type) {
for (type in events) {
eventInternal.remove(elem, type + types[t], handler, selector, true);
}
continue;
}
special = eventInternal.special[type] || {};
type = (selector ? special.delegateType : special.bindType) || type;
handlers = events[type] || [];
tmp = tmp[2] && new RegExp('(^|\\.)' + namespaces.join('\\.(?:.*\\.|)') + '(\\.|$)');
// Remove matching events
origCount = j = handlers.length;
while (j--) {
handleObj = handlers[j];
if ((mappedTypes || origType === handleObj.origType) &&
(!handler || handler.guid === handleObj.guid) &&
(!tmp || tmp.test(handleObj.namespace)) &&
(!selector || selector === handleObj.selector || selector === '**' && handleObj.selector)) {
handlers.splice(j, 1);
if (handleObj.selector) {
handlers.delegateCount--;
}
if (special.remove) {
special.remove.call(elem, handleObj);
}
}
}
// Remove generic event handler if we removed something and no more handlers exist
// (avoids potential for endless recursion during removal of special event handlers)
if (origCount && !handlers.length) {
if (!special.teardown || special.teardown.call(elem, namespaces, elemData.handle) === false) {
removeEvent(elem, type, elemData.handle);
}
delete events[type];
}
}
// Remove the commonGUID if it's no longer used
if (isEmptyObject(events)) {
delete elemData.handle;
jAwn.cache.remove(elem, 'events');
}
},
trigger: function (event, data, elem, onlyHandlers) {
var i, cur, tmp, bubbleType, ontype, handle, special, eventPath = [elem || document],
type = hasOwn.call(event, 'type') ? event.type : event,
namespaces = hasOwn.call(event, 'namespace') ? event.namespace.split('.') : [];
cur = tmp = elem = elem || document;
// Don't do events on text and comment nodes
if (elem.nodeType === 3 || elem.nodeType === 8) {
return;
}
// focus/blur morphs to focusin/out; ensure we're not firing them right now
if (rfocusMorph.test(type + eventInternal.triggered)) {
return;
}
if (type.indexOf('.') >= 0) {
// Namespaced trigger; create a regexp to match event type in handle()
namespaces = type.split('.');
type = namespaces.shift();
namespaces.sort();
}
ontype = type.indexOf(':') < 0 && 'on' + type;
// Caller can pass in a jAwn.event object, Object, or just an event type string
event = event[commonGUID] ? event : new jAwn.event(type, isObject(event) && event);
// Trigger bitmask: & 1 for native handlers; & 2 for custom (always true)
event.isTrigger = onlyHandlers ? 2 : 3;
event.namespace = namespaces.join('.');
event.namespace_re = event.namespace ? new RegExp('(^|\\.)' + namespaces.join('\\.(?:.*\\.|)') + '(\\.|$)') : null;
// Clean up the event in case it is being reused
event.result = undefined;
if (!event.target) {
event.target = elem;
}
// Clone any incoming data and prepend the event, creating the handler arg list
data = data == null ? [event] : makeArray(data, [event]);
// Allow special events to draw outside the lines
special = eventInternal.special[type] || {};
if (!onlyHandlers && special.trigger && special.trigger.apply(elem, data) === false) {
return
}
// Determine event propagation path in advance, per W3C events spec (#9951)
// Bubble up to document, then to window; watch for a global ownerDocument var (#9724)
if (!onlyHandlers && !special.noBubble && !isWindow(elem)) {
bubbleType = special.delegateType || type;
if (!rfocusMorph.test(bubbleType + type)) {
cur = cur.parentNode;
}
for ( ; cur; cur = cur.parentNode) {
eventPath.push(cur);
tmp = cur;
}
// Only add window if we got to document (e.g., not plain obj or detached DOM)
if (tmp === (elem.ownerDocument || document)) {
eventPath.push(tmp.defaultView || tmp.parentWindow || window);
}
}
// Fire handlers on the event path
i = 0;
while ((cur = eventPath[i++]) && !event.isPropagationStopped()) {
event.type = i > 1 ? bubbleType : special.bindType || type;
// Custom handler
handle = (jAwn.cache.get(cur, 'events') || {})[event.type] && jAwn.cache.get(cur, 'handle');
if (handle) {
handle.apply(cur, data);
}
// Native handler
handle = ontype && cur[ontype];
if (handle && handle.apply && acceptData(cur)) {
event.result = handle.apply(cur, data);
if (event.result === false) {
event.preventDefault();
}
}
}
event.type = type;
// If nobody prevented the default action, do it now
if (!onlyHandlers && !event.isDefaultPrevented()) {
if ((!special._default || special._default.apply(eventPath.pop(), data) === false) && acceptData(elem)) {
// Call a native DOM method on the target with the same name name as the event.
// Don't do default actions on window, that's where global variables be (#6170)
if (ontype && isFunction(elem[type]) && !isWindow(elem)) {
// Don't re-trigger an onFOO event when we call its FOO() method
tmp = elem[ontype];
if (tmp) {
elem[ontype] = null;
}
// Prevent re-triggering of the same event, since we already bubbled it above
eventInternal.triggered = type;
elem[type]();
eventInternal.triggered = undefined;
if (tmp) {
elem[ontype] = tmp;
}
}
}
}
return event.result;
},
dispatch: function (nativeEvent) {
// Make a writable jAwn.event from the native event object
var event = eventInternal.fix(nativeEvent);
var i, j, ret, matched, handleObj, handlerQueue,
args = new Array(arguments.length),
handlers = (jAwn.cache.get(this, 'events') || {})[event.type] || [],
special = eventInternal.special[event.type] || {};
// Use the fix-ed jAwn.event rather than the (read-only) native event
args[0] = event;
for (i = 1; i < arguments.length; i++) {
args[i] = arguments[i];
}
event.delegateTarget = this;
// Call the preDispatch hook for the mapped type, and let it bail if desired
if (special.preDispatch && special.preDispatch.call(this, event) === false) {
return;
}
// Determine handlers
handlerQueue = eventInternal.handlers.call(this, event, handlers);
// Run delegates first; they may want to stop propagation beneath us
i = 0;
while ((matched = handlerQueue[i++]) && !event.isPropagationStopped()) {
event.currentTarget = matched.elem;
j = 0;
while ((handleObj = matched.handlers[j++]) && !event.isImmediatePropagationStopped()) {
// Triggered event must either 1) have no namespace, or
// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
if (!event.namespace_re || event.namespace_re.test(handleObj.namespace)) {
event.handleObj = handleObj;
event.data = handleObj.data;
ret = ((eventInternal.special[handleObj.origType] || {}).handle || handleObj.handler).apply(matched.elem, args);
if (ret !== undefined) {
if ((event.result = ret) === false) {
event.preventDefault();
event.stopPropagation();
}
}
}
}
}
// Call the postDispatch hook for the mapped type
if (special.postDispatch) {
special.postDispatch.call(this, event);
}
return event.result;
},
handlers: function (event, handlers) {
var i, matches, sel, handleObj,
handlerQueue = [],
delegateCount = handlers.delegateCount,
cur = event.target;
// Find delegate handlers
// Black-hole SVG <use> instance trees (#13180)
// Avoid non-left-click bubbling in Firefox (#3861)
if (delegateCount && cur.nodeType && (!event.button || event.type !== 'click')) {
for ( ; cur !== this; cur = cur.parentNode || this) {
// Don't check non-elements (#13208)
// Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
if (cur.nodeType === 1 && (cur.disabled !== true || event.type !== 'click')) {
matches = [];
for ( i = 0; i < delegateCount; i++) {
handleObj = handlers[i];
// Don't conflict with Object.prototype properties (#13203)
sel = handleObj.selector + ' ';
if (matches[sel] === undefined) {
matches[sel] = handleObj.needsContext ? getIndex(queryAll(sel, this), cur) >= 0 : find(sel, this, null, [cur]).length;
}
if (matches[sel]) {
matches.push(handleObj);
}
}
if (matches.length) {
handlerQueue.push({ elem: cur, handlers: matches });
}
}
}
}
// Add the remaining (directly-bound) handlers
if (delegateCount < handlers.length) {
handlerQueue.push({ elem: this, handlers: handlers.slice(delegateCount) });
}
return handlerQueue;
},
addProp: function (name, hook) {
Object.defineProperty(jAwn.event.prototype, name, {
enumerable: true,
configurable: true,
get: isFunction(hook) ?
function () {
if (this.originalEvent) {
return hook(this.originalEvent);
}
} :
function () {
if (this.originalEvent) {
return this.originalEvent[name];
}
},
set: function (value) {
Object.defineProperty(this, name, {
enumerable: true,
configurable: true,
writable: true,
value: value
});
}
});
},
fix: function (originalEvent) {
return originalEvent[commonGUID] ? originalEvent : new jAwn.event(originalEvent);
},
special: {
load: {
// Prevent triggered image.load events from bubbling to window.load
noBubble: true
},
focus: {
// Fire native event if possible so blur/focus sequence is correct
trigger: function () {
if (this !== safeActiveElement() && this.focus) {
this.focus();
return false;
}
},
delegateType: 'focusin'
},
blur: {
trigger: function () {
if (this === safeActiveElement() && this.blur) {
this.blur();
return false;
}
},
delegateType: 'focusout'
},
click: {
// For checkbox, fire native event so checked state will be right
trigger: function () {
if (this.type === 'checkbox' && this.click && checkNodeType(this, 'input')) {
this.click();
return false;
}
},
// For cross-browser consistency, don't fire native .click() on links
_default: function (event) {
return checkNodeType(event.target, 'a');
}
},
beforeunload: {
postDispatch: function (event) {
// Support: Firefox 20+
// Firefox doesn't alert if the returnValue field is not set.
if (event.result !== undefined && event.originalEvent) {
event.originalEvent.returnValue = event.result;
}
}
}
},
simulate: function (type, elem, event, bubble) {
// Piggyback on a donor event to simulate a different one
// Used only for `focus(in | out)` events
var e = mergeObjects(
new jAwn.event(),
event,
{
type: type,
isSimulated: true
}
);
eventInternal.trigger(e, null, elem);
}
};
// jAwn.event is based on DOM3 Events as specified by the ECMAScript Language Binding
// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
jAwn.event = function (src, props) {
// Allow instantiation without the 'new' keyword
if (!(this instanceof jAwn.event)) {
return new jAwn.event(src, props);
}
// Event object
if (src && src.type) {
this.originalEvent = src;
this.type = src.type;
// Events bubbling up the document may have been marked as prevented
// by a handler lower down the tree; reflect the correct value.
// Support: Android < 4.0
this.isDefaultPrevented = src.defaultPrevented || src.defaultPrevented === undefined && src.returnValue === false ? returnTrue : returnFalse;
// Create target properties
// Support: Safari <=6 - 7 only
// Target should not be a text node (#504, #13143)
this.target = (src.target && src.target.nodeType === 3) ? src.target.parentNode : src.target;
this.currentTarget = src.currentTarget;
this.relatedTarget = src.relatedTarget;
// Event type
}
else {
this.type = src;
}
// Put explicitly provided properties onto the event object
if (props) {
mergeObjects(this, props);
}
// Create a timestamp if incoming event doesn't have one
this.timeStamp = src && src.timeStamp || Date.now();
// Mark it as fixed
this[commonGUID] = true;
};
jAwn.event.prototype = {
constructor: jAwn.event,
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse,
isSimulated: false,
preventDefault: function () {
var e = this.originalEvent;
this.isDefaultPrevented = returnTrue;
if (e && !this.isSimulated) {
e.preventDefault();
}
},
stopPropagation: function () {
var e = this.originalEvent;
this.isPropagationStopped = returnTrue;
if (e && !this.isSimulated) {
e.stopPropagation();
}
},
stopImmediatePropagation: function () {
var e = this.originalEvent;
this.isImmediatePropagationStopped = returnTrue;
if (e && !this.isSimulated) {
e.stopImmediatePropagation();
}
this.stopPropagation();
}
};
function getInternal (property) {
if (isDefined(property) && isNotEmptyString(property)) {
return eventInternal[property];
}
else {
return eventInternal;
}
};
function removeEvent (elem, type, handle) {
if (elem.removeEventListener) {
elem.removeEventListener(type, handle, false);
}
};
// Public Methods
jAwn.on = function (elements, types, selector, data, fn, one) {
var origFn, type;
// NodeList?
if (isNodeList(elements)) {
elements = nodeListToArray(elements);
}
// Types can be a map of types/handlers
if (isObject(types)) {
// (types-Object, selector, data)
if (isString(selector)) {
// (types-Object, data)
data = data || selector;
selector = undefined;
}
for (type in types) {
jAwn.on(elements, type, selector, data, types[type], one);
}
return elements;
}
if (data == null && fn == null) {
// (types, fn)
fn = selector;
data = selector = undefined;
}
else if (fn == null) {
if (isString(selector)) {
// (types, selector, fn)
fn = data;
data = undefined;
}
else {
// (types, data, fn)
fn = data;
data = selector;
selector = undefined;
}
}
if (fn === false) {
fn = returnFalse;
}
else if (!fn) {
return elements;
}
if (one === 1) {
origFn = fn;
fn = function (event) {
// Can use an empty set, since event contains the info
jAwn.off(event);
return origFn.apply(this, arguments);
};
// Use same guid so caller can remove using origFn
fn.guid = origFn.guid || (origFn.guid = 'jAwnGUID' + eventGUID++);
}
if (isArray(elements)) {
var index = 0, length = elements.length;
for ( ; index < length; index++) {
eventInternal.add(elements[index], types, fn, data, selector);
}
}
else {
eventInternal.add(elements, types, fn, data, selector);
}
};
jAwn.one = function (elements, types, selector, data, fn) {
// NodeList?
if (isNodeList(elements)) {
elements = nodeListToArray(elements);
}
return jAwn.on(elements, types, selector, data, fn, 1);
};
jAwn.off = function (elements, types, selector, fn) {
// NodeList?
if (isNodeList(elements)) {
elements = nodeListToArray(elements);
}
var handleObj, type;
if (elements && elements.preventDefault && elements.handleObj) {
// (event) dispatched jAwn.event
handleObj = elements.handleObj;
jAwn.off(elements.delegateTarget,
handleObj.namespace ? handleObj.origType + '.' + handleObj.namespace : handleObj.origType,
handleObj.selector,
handleObj.handler
);
return elements;
}
if (isObject(types)) {
// (types-object [, selector])
for (type in types) {
jAwn.off(elements, type, selector, types[type]);
}
return elements;
}
if (selector === false || isFunction(selector)) {
// (types [, fn])
fn = selector;
selector = undefined;
}
if (fn === false) {
fn = returnFalse;
}
if (isArray(elements)) {
var index = 0, length = elements.length;
for ( ; index < length; index++) {
eventInternal.remove(elements[index], types, fn, selector);
}
}
else {
eventInternal.remove(elements, types, fn, selector);
}
};
jAwn.trigger = function (elements, type, data) {
// NodeList?
if (isNodeList(elements)) {
elements = nodeListToArray(elements);