-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathender.js
2740 lines (2426 loc) · 93.3 KB
/
ender.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
/*!
* =============================================================
* Ender: open module JavaScript framework (https://ender.no.de)
* Build: ender build jwerty bean bowser domready hashchange jeesh
* =============================================================
*/
/*!
* Ender: open module JavaScript framework (client-lib)
* copyright Dustin Diaz & Jacob Thornton 2011 (@ded @fat)
* http://ender.no.de
* License MIT
*/
!function (context) {
// a global object for node.js module compatiblity
// ============================================
context['global'] = context
// Implements simple module system
// losely based on CommonJS Modules spec v1.1.1
// ============================================
var modules = {}
, old = context.$
function require (identifier) {
// modules can be required from ender's build system, or found on the window
var module = modules[identifier] || window[identifier]
if (!module) throw new Error("Requested module '" + identifier + "' has not been defined.")
return module
}
function provide (name, what) {
return (modules[name] = what)
}
context['provide'] = provide
context['require'] = require
function aug(o, o2) {
for (var k in o2) k != 'noConflict' && k != '_VERSION' && (o[k] = o2[k])
return o
}
function boosh(s, r, els) {
// string || node || nodelist || window
if (typeof s == 'string' || s.nodeName || (s.length && 'item' in s) || s == window) {
els = ender._select(s, r)
els.selector = s
} else els = isFinite(s.length) ? s : [s]
return aug(els, boosh)
}
function ender(s, r) {
return boosh(s, r)
}
aug(ender, {
_VERSION: '0.3.6'
, fn: boosh // for easy compat to jQuery plugins
, ender: function (o, chain) {
aug(chain ? boosh : ender, o)
}
, _select: function (s, r) {
return (r || document).querySelectorAll(s)
}
})
aug(boosh, {
forEach: function (fn, scope, i) {
// opt out of native forEach so we can intentionally call our own scope
// defaulting to the current item and be able to return self
for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(scope || this[i], this[i], i, this)
// return self for chaining
return this
},
$: ender // handy reference to self
})
ender.noConflict = function () {
context.$ = old
return this
}
if (typeof module !== 'undefined' && module.exports) module.exports = ender
// use subscript notation as extern for Closure compilation
context['ender'] = context['$'] = context['ender'] || ender
}(this);
!function () {
var module = { exports: {} }, exports = module.exports;
/*
* jwerty - Awesome handling of keyboard events
*
* jwerty is a JS lib which allows you to bind, fire and assert key combination strings against
* elements and events. It normalises the poor std api into something easy to use and clear.
*
* This code is licensed under the MIT
* For more details, see http://www.opensource.org/licenses/mit-license.php
* For more information, see http://github.com/keithcirkel/jwerty
*
* @author Keith Cirkel ('keithamus') <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
* @copyright Copyright © 2011, Keith Cirkel
*
* 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 (global, exports) {
// Helper methods & vars:
var $d = global.document
, $ = (global.jQuery || global.Zepto || global.ender || $d)
, $$
, $b
, ke = 'keydown';
function realTypeOf(v, s) {
return (v === null) ? s === 'null'
: (v === undefined) ? s === 'undefined'
: (v.is && v instanceof $) ? s === 'element'
: Object.prototype.toString.call(v).toLowerCase().indexOf(s) > 7;
}
if ($ === $d) {
$$ = function (selector, context) {
return selector ? $.querySelector(selector, context || $) : $;
};
$b = function (e, fn) { e.addEventListener(ke, fn, false); };
$f = function (e, jwertyEv) {
var ret = document.createEvent('Event')
, i;
ret.initEvent(ke, true, true);
for (i in jwertyEv) ret[i] = jwertyEv[i];
return (e || $).dispatchEvent(ret);
}
} else {
$$ = function (selector, context, fn) { return $(selector || $d, context); };
$b = function (e, fn) { $(e).bind(ke + '.jwerty', fn); };
$f = function (e, ob) { $(e || $d).trigger($.Event(ke, ob)); };
}
// Private
var _modProps = { 16: 'shiftKey', 17: 'ctrlKey', 18: 'altKey', 91: 'metaKey' };
// Generate key mappings for common keys that are not printable.
var _keys = {
// MOD aka toggleable keys
mods: {
// Shift key, ⇧
'⇧': 16, shift: 16,
// CTRL key, on Mac: ⌃
'⌃': 17, ctrl: 17,
// ALT key, on Mac: ⌥ (Alt)
'⌥': 18, alt: 18, option: 18,
// META, on Mac: ⌘ (CMD), on Windows (Win), on Linux (Super)
'⌘': 91, meta: 91, cmd: 91, 'super': 91, win: 91
},
// Normal keys
keys: {
// Backspace key, on Mac: ⌫ (Backspace)
'⌫': 8, backspace: 8,
// Tab Key, on Mac: ⇥ (Tab), on Windows ⇥⇥
'⇥': 9, '⇆': 9, tab: 9,
// Return key, ↩
'↩': 13, 'return': 13, enter: 13, '⌅': 13,
// Pause/Break key
'pause': 19, 'pause-break': 19,
// Caps Lock key, ⇪
'⇪': 20, caps: 20, 'caps-lock': 20,
// Escape key, on Mac: ⎋, on Windows: Esc
'⎋': 27, escape: 27, esc: 27,
// Space key
space: 32,
// Page-Up key, or pgup, on Mac: ↖
'↖': 33, pgup: 33, 'page-up': 33,
// Page-Down key, or pgdown, on Mac: ↘
'↘': 34, pgdown: 34, 'page-down': 34,
// END key, on Mac: ⇟
'⇟': 35, end: 35,
// HOME key, on Mac: ⇞
'⇞': 36, home: 36,
// Insert key, or ins
ins: 45, insert: 45,
// Delete key, on Mac: ⌫ (Delete)
del: 45, 'delete': 45,
// Left Arrow Key, or ←
'←': 37, left: 37, 'arrow-left': 37,
// Up Arrow Key, or ↑
'↑': 38, up: 38, 'arrow-up': 38,
// Right Arrow Key, or →
'→': 39, right: 39, 'arrow-right': 39,
// Up Arrow Key, or ↓
'↓': 40, down: 40, 'arrow-down': 40,
// odities, printing characters that come out wrong:
// Num-Multiply, or *
'*': 106, star: 106, asterisk: 106, multiply: 106,
// Num-Plus or +
'+': 107, 'plus': 107,
// Num-Subtract, or -
'-': 109, subtract: 109,
//';': 186, //???
// = or equals
'=': 187, 'equals': 187,
// Comma, or ,
',': 188, comma: 188,
//'-': 189, //???
// Period, or ., or full-stop
'.': 190, period: 190, 'full-stop': 190,
// Slash, or /, or forward-slash
'/': 191, slash: 191, 'forward-slash': 191,
// Tick, or `, or back-quote
'`': 192, tick: 192, 'back-quote': 192,
// Open bracket, or [
'[': 219, 'open-bracket': 219,
// Back slash, or \
'\\': 220, 'back-slash': 220,
// Close backet, or ]
']': 221, 'close-bracket': 221,
// Apostraphe, or Quote, or '
'\'': 222, quote: 222, apostraphe: 222
}
};
// To minimise code bloat, add all of the NUMPAD 0-9 keys in a loop
i = 95, n = 0;
while(++i < 106) {
_keys.keys['num-' + n] = i;
++n;
}
// To minimise code bloat, add all of the top row 0-9 keys in a loop
i = 47, n = 0;
while(++i < 58) {
_keys.keys[n] = i;
++n;
}
// To minimise code bloat, add all of the F1-F25 keys in a loop
i = 111, n = 1;
while(++i < 136) {
_keys.keys['f' + n] = i;
++n;
}
// To minimise code bloat, add all of the letters of the alphabet in a loop
var i = 64;
while(++i < 91) {
_keys.keys[String.fromCharCode(i).toLowerCase()] = i;
}
function JwertyCode(jwertyCode) {
var i
, c
, n
, z
, keyCombo
, optionals
, jwertyCodeFragment
, rangeMatches
, rangeI;
// In-case we get called with an instance of ourselves, just return that.
if (jwertyCode instanceof JwertyCode) return jwertyCode;
// If jwertyCode isn't an array, cast it as a string and split into array.
if (!realTypeOf(jwertyCode, 'array')) {
jwertyCode = (String(jwertyCode)).replace(/\s/g, '').toLowerCase().
match(/(?:\+,|[^,])+/g);
}
// Loop through each key sequence in jwertyCode
for (i = 0, c = jwertyCode.length; i < c; ++i) {
// If the key combo at this part of the sequence isn't an array,
// cast as a string and split into an array.
if (!realTypeOf(jwertyCode[i], 'array')) {
jwertyCode[i] = String(jwertyCode[i])
.match(/(?:\+\/|[^\/])+/g);
}
// Parse the key optionals in this sequence
optionals = [], n = jwertyCode[i].length;
while (n--) {
// Begin creating the object for this key combo
var jwertyCodeFragment = jwertyCode[i][n];
keyCombo = {
jwertyCombo: String(jwertyCodeFragment),
shiftKey: false,
ctrlKey: false,
altKey: false,
metaKey: false
}
// If jwertyCodeFragment isn't an array then cast as a string
// and split it into one.
if (!realTypeOf(jwertyCodeFragment, 'array')) {
jwertyCodeFragment = String(jwertyCodeFragment).toLowerCase()
.match(/(?:(?:[^\+])+|\+\+|^\+$)/g);
}
z = jwertyCodeFragment.length;
while (z--) {
// Normalise matching errors
if (jwertyCodeFragment[z] === '++') jwertyCodeFragment[z] = '+';
// Inject either keyCode or ctrl/meta/shift/altKey into keyCombo
if (jwertyCodeFragment[z] in _keys.mods) {
keyCombo[_modProps[_keys.mods[jwertyCodeFragment[z]]]] = true;
} else if(jwertyCodeFragment[z] in _keys.keys) {
keyCombo.keyCode = _keys.keys[jwertyCodeFragment[z]];
} else {
rangeMatches = jwertyCodeFragment[z].match(/^\[([^-]+\-?[^-]*)-([^-]+\-?[^-]*)\]$/);
}
}
if (realTypeOf(keyCombo.keyCode, 'undefined')) {
// If we picked up a range match earlier...
if (rangeMatches && (rangeMatches[1] in _keys.keys) && (rangeMatches[2] in _keys.keys)) {
rangeMatches[2] = _keys.keys[rangeMatches[2]];
rangeMatches[1] = _keys.keys[rangeMatches[1]];
// Go from match 1 and capture all key-comobs up to match 2
for (rangeI = rangeMatches[1]; rangeI < rangeMatches[2]; ++rangeI) {
optionals.push({
altKey: keyCombo.altKey,
shiftKey: keyCombo.shiftKey,
metaKey: keyCombo.metaKey,
ctrlKey: keyCombo.ctrlKey,
keyCode: rangeI,
jwertyCombo: String(jwertyCodeFragment)
});
}
keyCombo.keyCode = rangeI;
// Inject either keyCode or ctrl/meta/shift/altKey into keyCombo
} else {
keyCombo.keyCode = 0;
}
}
optionals.push(keyCombo);
}
this[i] = optionals;
}
this.length = i;
return this;
}
var jwerty = exports.jwerty = {
/**
* jwerty.event
*
* `jwerty.event` will return a function, which expects the first
* argument to be a key event. When the key event matches `jwertyCode`,
* `callbackFunction` is fired. `jwerty.event` is used by `jwerty.key`
* to bind the function it returns. `jwerty.event` is useful for
* attaching to your own event listeners. It can be used as a decorator
* method to encapsulate functionality that you only want to fire after
* a specific key combo. If `callbackContext` is specified then it will
* be supplied as `callbackFunction`'s context - in other words, the
* keyword `this` will be set to `callbackContext` inside the
* `callbackFunction` function.
*
* @param {Mixed} jwertyCode can be an array, or string of key
* combinations, which includes optinals and or sequences
* @param {Function} callbackFucntion is a function (or boolean) which
* is fired when jwertyCode is matched. Return false to
* preventDefault()
* @param {Object} callbackContext (Optional) The context to call
* `callback` with (i.e this)
*
*/
event: function (jwertyCode, callbackFunction, callbackContext /*? this */) {
// Construct a function out of callbackFunction, if it is a boolean.
if (realTypeOf(callbackFunction, 'boolean')) {
var bool = callbackFunction;
callbackFunction = function () { return bool; }
}
jwertyCode = new JwertyCode(jwertyCode);
// Initialise in-scope vars.
var i = 0
, c = jwertyCode.length - 1
, returnValue
, jwertyCodeIs;
// This is the event listener function that gets returned...
return function (event) {
// if jwertyCodeIs returns truthy (string)...
if ((jwertyCodeIs = jwerty.is(jwertyCode, event, i))) {
// ... and this isn't the last key in the sequence,
// incriment the key in sequence to check.
if (i < c) {
++i;
return;
// ... and this is the last in the sequence (or the only
// one in sequence), then fire the callback
} else {
returnValue = callbackFunction.call(
callbackContext || this, event, jwertyCodeIs);
// If the callback returned false, then we should run
// preventDefault();
if (returnValue === false) event.preventDefault();
// Reset i for the next sequence to fire.
i = 0;
return;
}
}
// If the event didn't hit this time, we should reset i to 0,
// that is, unless this combo was the first in the sequence,
// in which case we should reset i to 1.
i = jwerty.is(jwertyCode, event) ? 1 : 0;
}
},
/**
* jwerty.is
*
* `jwerty.is` will return a boolean value, based on if `event` matches
* `jwertyCode`. `jwerty.is` is called by `jwerty.event` to check
* whether or not to fire the callback. `event` can be a DOM event, or
* a jQuery/Zepto/Ender manufactured event. The properties of
* `jwertyCode` (speficially ctrlKey, altKey, metaKey, shiftKey and
* keyCode) should match `jwertyCode`'s properties - if they do, then
* `jwerty.is` will return `true`. If they don't, `jwerty.is` will
* return `false`.
*
* @param {Mixed} jwertyCode can be an array, or string of key
* combinations, which includes optinals and or sequences
* @param {KeyboardEvent} event is the KeyboardEvent to assert against
* @param {Integer} i (Optional) checks the `i` key in jwertyCode
* sequence
*
*/
is: function (jwertyCode, event, i /*? 0*/) {
jwertyCode = new JwertyCode(jwertyCode);
// Default `i` to 0
i = i || 0;
// We are only interesting in `i` of jwertyCode;
jwertyCode = jwertyCode[i];
// jQuery stores the *real* event in `originalEvent`, which we use
// because it does annoything stuff to `metaKey`
event = event.originalEvent || event;
// We'll look at each optional in this jwertyCode sequence...
var key
, n = jwertyCode.length
, returnValue = false;
// Loop through each fragment of jwertyCode
while (n--) {
returnValue = jwertyCode[n].jwertyCombo;
// For each property in the jwertyCode object, compare to `event`
for (var p in jwertyCode[n]) {
// ...except for jwertyCode.jwertyCombo...
if (p !== 'jwertyCombo' && event[p] !== jwertyCode[n][p]) returnValue = false;
}
// If this jwertyCode optional wasn't falsey, then we can return early.
if (returnValue !== false) return returnValue;
}
return returnValue;
},
/**
* jwerty.key
*
* `jwerty.key` will attach an event listener and fire
* `callbackFunction` when `jwertyCode` matches. The event listener is
* attached to `document`, meaning it will listen for any key events
* on the page (a global shortcut listener). If `callbackContext` is
* specified then it will be supplied as `callbackFunction`'s context
* - in other words, the keyword `this` will be set to
* `callbackContext` inside the `callbackFunction` function.
*
* @param {Mixed} jwertyCode can be an array, or string of key
* combinations, which includes optinals and or sequences
* @param {Function} callbackFunction is a function (or boolean) which
* is fired when jwertyCode is matched. Return false to
* preventDefault()
* @param {Object} callbackContext (Optional) The context to call
* `callback` with (i.e this)
* @param {Mixed} selector can be a string, jQuery/Zepto/Ender object,
* or an HTML*Element on which to bind the eventListener
* @param {Mixed} selectorContext can be a string, jQuery/Zepto/Ender
* object, or an HTML*Element on which to scope the selector
*
*/
key: function (jwertyCode, callbackFunction, callbackContext /*? this */, selector /*? document */, selectorContext /*? body */) {
// Because callbackContext is optional, we should check if the
// `callbackContext` is a string or element, and if it is, then the
// function was called without a context, and `callbackContext` is
// actually `selector`
var realSelector = realTypeOf(callbackContext, 'element') || realTypeOf(callbackContext, 'string') ? callbackContext : selector
// If `callbackContext` is undefined, or if we skipped it (and
// therefore it is `realSelector`), set context to `global`.
, realcallbackContext = realSelector === callbackContext ? global : callbackContext
// Finally if we did skip `callbackContext`, then shift
// `selectorContext` to the left (take it from `selector`)
, realSelectorContext = realSelector === callbackContext ? selector : selectorContext;
// If `realSelector` is already a jQuery/Zepto/Ender/DOM element,
// then just use it neat, otherwise find it in DOM using $$()
$b(realTypeOf(realSelector, 'element') ?
realSelector : $$(realSelector, realSelectorContext)
, jwerty.event(jwertyCode, callbackFunction, realcallbackContext));
},
/**
* jwerty.fire
*
* `jwerty.fire` will construct a keyup event to fire, based on
* `jwertyCode`. The event will be fired against `selector`.
* `selectorContext` is used to search for `selector` within
* `selectorContext`, similar to jQuery's
* `$('selector', 'context')`.
*
* @param {Mixed} jwertyCode can be an array, or string of key
* combinations, which includes optinals and or sequences
* @param {Mixed} selector can be a string, jQuery/Zepto/Ender object,
* or an HTML*Element on which to bind the eventListener
* @param {Mixed} selectorContext can be a string, jQuery/Zepto/Ender
* object, or an HTML*Element on which to scope the selector
*
*/
fire: function (jwertyCode, selector /*? document */, selectorContext /*? body */, i) {
jwertyCode = new JwertyCode(jwertyCode);
var realI = realTypeOf(selectorContext, 'number') ? selectorContext : i;
// If `realSelector` is already a jQuery/Zepto/Ender/DOM element,
// then just use it neat, otherwise find it in DOM using $$()
$f(realTypeOf(selector, 'element') ?
selector : $$(selector, selectorContext)
, jwertyCode[realI || 0][0]);
},
KEYS: _keys
};
}(this, (typeof module !== 'undefined' && module.exports ? module.exports : this)));
provide("jwerty", module.exports);
/*
* jwerty - Awesome handling of keyboard events
*
* enderBridge.js
*
* jwerty is a JS lib which allows you to bind, fire and assert key combination strings against
* elements and events. It normalises the poor std api into something easy to use and clear.
*
* This code is licensed under the MIT
* For more details, see http://www.opensource.org/licenses/mit-license.php
* For more information, see http://github.com/keithcirkel/jwerty
*
* @author Keith Cirkel ('keithamus') <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
* @copyright Copyright © 2011, Keith Cirkel
*
* 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 ($, r) {
var kk = r('jwerty').jwerty;
$.ender({
key: kk.key,
keyEvent: kk.event,
isKey: kk.is,
fireKey: kk.fire
});
}(ender, require));
}();
!function () {
var module = { exports: {} }, exports = module.exports;
/*!
* bean.js - copyright Jacob Thornton 2011
* https://github.com/fat/bean
* MIT License
* special thanks to:
* dean edwards: http://dean.edwards.name/
* dperini: https://github.com/dperini/nwevents
* the entire mootools team: github.com/mootools/mootools-core
*/
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition();
else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
else this[name] = definition();
}('bean', function () {
var win = window,
__uid = 1,
registry = {},
collected = {},
overOut = /over|out/,
namespace = /[^\.]*(?=\..*)\.|.*/,
stripName = /\..*/,
addEvent = 'addEventListener',
attachEvent = 'attachEvent',
removeEvent = 'removeEventListener',
detachEvent = 'detachEvent',
doc = document || {},
root = doc.documentElement || {},
W3C_MODEL = root[addEvent],
eventSupport = W3C_MODEL ? addEvent : attachEvent,
isDescendant = function (parent, child) {
var node = child.parentNode;
while (node !== null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
},
retrieveUid = function (obj, uid) {
return (obj.__uid = uid && (uid + '::' + __uid++) || obj.__uid || __uid++);
},
retrieveEvents = function (element) {
var uid = retrieveUid(element);
return (registry[uid] = registry[uid] || {});
},
listener = W3C_MODEL ? function (element, type, fn, add) {
element[add ? addEvent : removeEvent](type, fn, false);
} : function (element, type, fn, add, custom) {
if (custom && add && element['_on' + custom] === null) {
element['_on' + custom] = 0;
}
element[add ? attachEvent : detachEvent]('on' + type, fn);
},
nativeHandler = function (element, fn, args) {
return function (event) {
event = fixEvent(event || ((this.ownerDocument || this.document || this).parentWindow || win).event);
return fn.apply(element, [event].concat(args));
};
},
customHandler = function (element, fn, type, condition, args) {
return function (event) {
if (condition ? condition.apply(this, arguments) : W3C_MODEL ? true : event && event.propertyName == '_on' + type || !event) {
event = event ? fixEvent(event || ((this.ownerDocument || this.document || this).parentWindow || win).event) : null;
fn.apply(element, Array.prototype.slice.call(arguments, event ? 0 : 1).concat(args));
}
};
},
addListener = function (element, orgType, fn, args) {
var type = orgType.replace(stripName, ''),
events = retrieveEvents(element),
handlers = events[type] || (events[type] = {}),
originalFn = fn,
uid = retrieveUid(fn, orgType.replace(namespace, ''));
if (handlers[uid]) {
return element;
}
var custom = customEvents[type];
if (custom) {
fn = custom.condition ? customHandler(element, fn, type, custom.condition) : fn;
type = custom.base || type;
}
var isNative = nativeEvents[type];
fn = isNative ? nativeHandler(element, fn, args) : customHandler(element, fn, type, false, args);
isNative = W3C_MODEL || isNative;
if (type == 'unload') {
var org = fn;
fn = function () {
removeListener(element, type, fn) && org();
};
}
element[eventSupport] && listener(element, isNative ? type : 'propertychange', fn, true, !isNative && type);
handlers[uid] = fn;
fn.__uid = uid;
fn.__originalFn = originalFn;
return type == 'unload' ? element : (collected[retrieveUid(element)] = element);
},
removeListener = function (element, orgType, handler) {
var uid, names, uids, i, events = retrieveEvents(element), type = orgType.replace(stripName, '');
if (!events || !events[type]) {
return element;
}
names = orgType.replace(namespace, '');
uids = names ? names.split('.') : [handler.__uid];
function destroyHandler(uid) {
handler = events[type][uid];
if (!handler) {
return;
}
delete events[type][uid];
if (element[eventSupport]) {
type = customEvents[type] ? customEvents[type].base : type;
var isNative = W3C_MODEL || nativeEvents[type];
listener(element, isNative ? type : 'propertychange', handler, false, !isNative && type);
}
}
destroyHandler(names); //get combos
for (i = uids.length; i--; destroyHandler(uids[i])) {} //get singles
return element;
},
del = function (selector, fn, $) {
return function (e) {
var array = typeof selector == 'string' ? $(selector, this) : selector;
for (var target = e.target; target && target != this; target = target.parentNode) {
for (var i = array.length; i--;) {
if (array[i] == target) {
return fn.apply(target, arguments);
}
}
}
};
},
add = function (element, events, fn, delfn, $) {
if (typeof events == 'object' && !fn) {
for (var type in events) {
events.hasOwnProperty(type) && add(element, type, events[type]);
}
} else {
var isDel = typeof fn == 'string', types = (isDel ? fn : events).split(' ');
fn = isDel ? del(events, delfn, $) : fn;
for (var i = types.length; i--;) {
addListener(element, types[i], fn, Array.prototype.slice.call(arguments, isDel ? 4 : 3));
}
}
return element;
},
remove = function (element, orgEvents, fn) {
var k, m, type, events, i,
isString = typeof(orgEvents) == 'string',
names = isString && orgEvents.replace(namespace, ''),
rm = removeListener,
attached = retrieveEvents(element);
names = names && names.split('.');
if (isString && /\s/.test(orgEvents)) {
orgEvents = orgEvents.split(' ');
i = orgEvents.length - 1;
while (remove(element, orgEvents[i]) && i--) {}
return element;
}
events = isString ? orgEvents.replace(stripName, '') : orgEvents;
if (!attached || names || (isString && !attached[events])) {
for (k in attached) {
if (attached.hasOwnProperty(k)) {
for (i in attached[k]) {
for (m = names.length; m--;) {
attached[k].hasOwnProperty(i) && new RegExp('^' + names[m] + '::\\d*(\\..*)?$').test(i) && rm(element, [k, i].join('.'));
}
}
}
}
return element;
}
if (typeof fn == 'function') {
rm(element, events, fn);
} else if (names) {
rm(element, orgEvents);
} else {
rm = events ? rm : remove;
type = isString && events;
events = events ? (fn || attached[events] || events) : attached;
for (k in events) {
if (events.hasOwnProperty(k)) {
rm(element, type || k, events[k]);
delete events[k]; // remove unused leaf keys
}
}
}
return element;
},
fire = function (element, type, args) {
var evt, k, i, m, types = type.split(' ');
for (i = types.length; i--;) {
type = types[i].replace(stripName, '');
var isNative = nativeEvents[type],
isNamespace = types[i].replace(namespace, ''),
handlers = retrieveEvents(element)[type];
if (isNamespace) {
isNamespace = isNamespace.split('.');
for (k = isNamespace.length; k--;) {
for (m in handlers) {
handlers.hasOwnProperty(m) && new RegExp('^' + isNamespace[k] + '::\\d*(\\..*)?$').test(m) && handlers[m].apply(element, [false].concat(args));
}
}
} else if (!args && element[eventSupport]) {
fireListener(isNative, type, element);
} else {
for (k in handlers) {
handlers.hasOwnProperty(k) && handlers[k].apply(element, [false].concat(args));
}
}
}
return element;
},
fireListener = W3C_MODEL ? function (isNative, type, element) {
evt = document.createEvent(isNative ? "HTMLEvents" : "UIEvents");
evt[isNative ? 'initEvent' : 'initUIEvent'](type, true, true, win, 1);
element.dispatchEvent(evt);
} : function (isNative, type, element) {
isNative ? element.fireEvent('on' + type, document.createEventObject()) : element['_on' + type]++;
},
clone = function (element, from, type) {
var events = retrieveEvents(from), obj, k;
var uid = retrieveUid(element);
obj = type ? events[type] : events;
for (k in obj) {
obj.hasOwnProperty(k) && (type ? add : clone)(element, type || from, type ? obj[k].__originalFn : k);
}
return element;
},
fixEvent = function (e) {
var result = {};
if (!e) {
return result;
}
var type = e.type, target = e.target || e.srcElement;
result.preventDefault = fixEvent.preventDefault(e);
result.stopPropagation = fixEvent.stopPropagation(e);
result.target = target && target.nodeType == 3 ? target.parentNode : target;
if (~type.indexOf('key')) {
result.keyCode = e.which || e.keyCode;
} else if ((/click|mouse|menu/i).test(type)) {
result.rightClick = e.which == 3 || e.button == 2;
result.pos = { x: 0, y: 0 };
if (e.pageX || e.pageY) {
result.clientX = e.pageX;
result.clientY = e.pageY;
} else if (e.clientX || e.clientY) {
result.clientX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
result.clientY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
overOut.test(type) && (result.relatedTarget = e.relatedTarget || e[(type == 'mouseover' ? 'from' : 'to') + 'Element']);
}
for (var k in e) {
if (!(k in result)) {
result[k] = e[k];
}
}
return result;
};
fixEvent.preventDefault = function (e) {
return function () {
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
};
};
fixEvent.stopPropagation = function (e) {
return function () {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
};
};
var nativeEvents = { 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 }; //misc
function check(event) {
var related = event.relatedTarget;
if (!related) {
return related === null;
}
return (related != this && related.prefix != 'xul' && !/document/.test(this.toString()) && !isDescendant(this, related));
}
var customEvents = {
mouseenter: { base: 'mouseover', condition: check },
mouseleave: { base: 'mouseout', condition: check },
mousewheel: { base: /Firefox/.test(navigator.userAgent) ? 'DOMMouseScroll' : 'mousewheel' }
};
var bean = { add: add, remove: remove, clone: clone, fire: fire };
var clean = function (el) {
var uid = remove(el).__uid;
if (uid) {
delete collected[uid];
delete registry[uid];
}
};
if (win[attachEvent]) {
add(win, 'unload', function () {
for (var k in collected) {
collected.hasOwnProperty(k) && clean(collected[k]);
}
win.CollectGarbage && CollectGarbage();
});
}
bean.noConflict = function () {
context.bean = old;
return this;
};
return bean;
});
provide("bean", module.exports);
!function ($) {
var b = require('bean'),
integrate = function (method, type, method2) {
var _args = type ? [type] : [];
return function () {
for (var args, i = 0, l = this.length; i < l; i++) {
args = [this[i]].concat(_args, Array.prototype.slice.call(arguments, 0));
args.length == 4 && args.push($);
!arguments.length && method == 'add' && type && (method = 'fire');
b[method].apply(this, args);
}
return this;
};
};
var add = integrate('add'),