-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.Shortcut.js
620 lines (601 loc) · 19.5 KB
/
jquery.Shortcut.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
(function (win, doc, $, _) {
if (!$) return;
/*
* This block of function contains utility-functions:
*/
if (!(_ && _.each && _.map && _.isArray && _.invert)) {
// If Underscore is available use the great Underscore library
// otherwise create a shim for it from jquery functions
_ = {
each: function (list, iterator, context) {
// We can't just use the jQuery-function, because the
// order of arguments passed to the iterator-function is
// different. And jQuery's each doesn't bind a context.
$.each(list, function (i, val) {
iterator.call(context, val, i, list);
})
},
map: $.map,
isArray: $.isArray,
isString: function (str) {
return typeof str === "string";
},
isFunction: $.isFunction,
isNumber: $.isNumeric,
extend: $.extend,
invert: function (obj) {
var inversion = {};
$.each(obj, function (key, val) {
inversion[val] = key;
});
return inversion;
},
indexOf: function (list, value) {
return $.inArray(value, list);
},
uniq: function (array) {
if (!array || array.length <= 1) {
return array;
}
var uniqArr = [];
$.each(array, function (i, val) {
if ($.inArray(val, uniqArr) === -1) {
uniqArr.push(val);
}
});
return uniqArr;
}
};
}
var core_slice = Array.prototype.slice,
toArray = function (arr, index) {
return core_slice.call(arr, index);
},
trim = $.trim,
body = doc.body,
hasFocus = function (elem) {
return elem === doc.activeElement && (!doc.hasFocus || doc.hasFocus());
},
isAnyNodeInFocus = function () {
return !hasFocus(body);
},
assembleListFromDOMTree = function (child, parent) {
// Creates an array with all elements from child to parent or to body
if (child === body) {
return [child];
} else if (!child.nodeName || !child.nodeType) {
return [];
}
var list = [child], current = child.parentNode;
parent && parent.nodeName || (parent = body);
while (current && current != parent && current != body) {
list.push(current);
current = current.parentNode;
}
list.timestamp = +new Date;
return list;
}
/*
* This block of code is pure data:
*/
// Cross-browser keyCodes according to http://unixpapa.com/js/key.html
var safeKeyCodes = {
"A":65,
"B":66,
"C":67,
"D":68,
"E":69,
"F":70,
"G":71,
"H":72,
"I":73,
"J":74,
"K":75,
"L":76,
"M":77,
"N":78,
"O":79,
"P":80,
"Q":81,
"R":82,
"S":83,
"T":84,
"U":85,
"V":86,
"W":87,
"X":88,
"Y":89,
"Z":90,
"SPACE":32,
"RETURN":13,
"TAB":9,
"ESCAPE":27,
"BACK_SPACE":8,
// "SHIFT":16,
// "CONTROL":17,
// "ALT":18,
"CAPS_LOCK":20,
"NUM_LOCK":144,
"1":49,
"2":50,
"3":51,
"4":52,
"5":53,
"6":54,
"7":55,
"8":56,
"9":57,
"0":48,
"LEFT":37,
"UP":38,
"RIGHT":39,
"DOWN":40,
"INSERT":45,
"DELETE":46,
"PAGE_UP":33,
"PAGE_DOWN":34,
"END":35,
"HOME":36
},
safeKeyCodes_inversion = _.invert(safeKeyCodes),
modifierNames = ["alt", "ctrl", "meta", "shift"],
noModifierFlag = "nomodifier",
modifierStringDelimiter = "-",
defaults = {
delimiter: "+",
active: true
},
globalOptions = _.extend({}, defaults),
keybindingDefaults = {
"disableWhenTargetHasFocus": false,
"disableWhenAnyElementHasFocus": false,
"enableOnlyWhenDocumentHasFocus": true,
"preventDefault": false,
"isActive": true
},
r_modifierNames = new RegExp("^(" + modifierNames.join("|") + ")$", "i"),
r_normalizedShortcut = new RegExp("^(" + modifierNames.join("\\" + defaults.delimiter + ")?(") + "\\" + defaults.delimiter + ")?([A-Z1-9]+)$", "g"),
listOfBoundShortcuts = {
// The structure of this list is essential to understand the
// further code. This object has numeric Ids which represent
// keyCodes. Each registered key has an object in which
// modifierStrings are the keys. The modifierStrings have
// Arrays as values. The arrays hold the Keybinding-instances
// 13: {
// "meta-shift": [...]
// },
// 37: {
// "meta": [...]
// }
},
parsedShortcuts = {};
var breaker = {},
iterateKeybindings = function (iteratorFn, context) {
if (iteratorFn) {
var key, keyName, bindingsByKey, modifierString, modifiers, bindingsByKeyAndModifiers, i, l, keybinding, res = [];
for (key in listOfBoundShortcuts) {
keyName = safeKeyCodes_inversion[key];
if (bindingsByKey = listOfBoundShortcuts[key]) {
for (modifierString in bindingsByKey) {
modifiers = modifierString === noModifierFlag ? [] : modifierString.split(defaults.delimiter);
if (bindingsByKeyAndModifiers = bindingsByKey[modifierString]) {
for (i = 0, l = bindingsByKeyAndModifiers.length; i < l, (keybinding = bindingsByKeyAndModifiers[i]); i++) {
if (iteratorFn.call(context, keybinding, keyName, modifiers) === breaker) {
return;
}
}
}
}
}
}
}
},
findKeybinding = function (iteratorFn, context) {
var res;
if (iteratorFn) {
iterateKeybindings(function (keybinding, keyName, modifiers) {
if (iteratorFn(keybinding, keyName, modifiers) === true) {
res = keybinding;
return breaker;
}
}, context);
}
return res;
},
filterKeybindings = function (iteratorFn, context) {
var res = [];
if (iteratorFn) {
iterateKeybindings(function (keybinding, keyName, modifiers) {
if (iteratorFn(keybinding, keyName, modifiers) === true) {
res.push(keybinding);
}
}, context);
}
return res;
};
/*
* This block of function is for parsing and assembling shortcut-Strings:
*/
var parseShortcut = function (shortcut) {
if (!shortcut) {
return;
}
if (shortcut in parsedShortcuts) {
// Was already parsed once
return parsedShortcuts[shortcut];
}
// Wasn't parsed yet. Try the faster algorithm for normalized shortcuts
// If it doesn't return a result, use the algorithm for unnormalized shortcuts
// Cache whatever result is returned
return parsedShortcuts[shortcut] = parseNormalizedShortcut(shortcut) || parseUnnormalizedShortcut(shortcut);
},
parseUnnormalizedShortcut = function (shortcut) {
var keys = shortcut.toLowerCase().split(globalOptions.delimiter || defaults.delimiter),
mainKey = [], modifiers = [], installedModifiers = {}, i = 0, l = keys.length, key;
for (; i < l, (key = trim(keys[i])); i++) {
if (r_modifierNames.test(key)) {
if (!installedModifiers[key]){
// The object installedModifiers is for ensuring that each modifier is only once in the list
modifiers.push(key);
installedModifiers[key] = true;
}
} else if ((key = key.toUpperCase()) in safeKeyCodes) {
mainKey.push(key);
}
}
if (mainKey.length === 1) {
return [mainKey[0], modifiers.sort()];
}
},
parseNormalizedShortcut = function (shortcut) {
var parts = r_normalizedShortcut.exec(shortcut),
modifiers = [], modifiersTemp, modKey, mainKey, i, l, delimiter = defaults.delimiter;
if (parts) {
mainKey = parts.pop();
i = 1;
l = parts.length;
while (i < l) {
if (modKey = parts[i]) {
modifiers.push(modKey.substr(0, modKey.length - 1));
}
++i;
}
if (mainKey in safeKeyCodes) {
return [mainKey, modifiers]
}
}
},
normalizeShortcut = function (mainKey, modifiers) {
// parseShortcut has ensured that mainKey and modifiers are valid
return (modifiers.length ? modifiers.sort().join(defaults.delimiter).toLowerCase() + defaults.delimiter : "") + mainKey.toUpperCase();
};
var findKeybindingByCallback = function (callback) {
// Returns the first Keybinding-instance that has this callback. Undefined if no keybinding could be found.
return findKeybinding(function (keybinding) {
return keybinding.callback === callback;
})
},
getKeybindingsByShortcut = function (shortcut) {
// Returns an array of Keybinding-instances associated with this shortcut. The array is empty if no Keybinding-instances could be found.
var keys, modifierString, bindingsByKey, bindingsByKeyAndModifiers;
if (shortcut && (keys = parseShortcut(shortcut))) {
modifierString = keys[1].join(modifierStringDelimiter) || noModifierFlag,
bindingsByKey = listOfBoundShortcuts[safeKeyCodes[keys[0]]] || {}, // Fallback to empty object to prevent a reference error
bindingsByKeyAndModifiers = bindingsByKey[modifierString];
if (bindingsByKeyAndModifiers) {
return toArray(bindingsByKeyAndModifiers);
}
}
return [];
},
isKeybindingAttached = function (keybinding, keys) {
// Returns true if this Keybinding-instance is registered, false otherwise. Its activation-status doesn't matter.
if (!keybinding || !(keybinding instanceof Keybinding)) {
return false;
}
var keybindings = getKeybindingsByShortcut(keybinding.shortcut);
return keybindings ? _.indexOf(bindingsByKeyAndModifiers, keybinding) > -1 : false;
},
getKeybindingList = function () {
// Returns a duplicate-free list of all registered Keybindings
return _.uniq(filterKeybindings(function () {return true}));
};
var registerListener = function (mainKey, modifiers, keybinding) {
// Stores the Keybinding-instance in the listOfBoundShortcuts
// parseShortcut has ensured that mainKey and modifiers are valid
var modifierString = modifiers.join(modifierStringDelimiter) || noModifierFlag,
bindingsByKey = listOfBoundShortcuts[safeKeyCodes[mainKey]] || (listOfBoundShortcuts[safeKeyCodes[mainKey]] = {}),
bindingsByKeyAndModifiers = bindingsByKey[modifierString] || (bindingsByKey[modifierString] = []);
bindingsByKeyAndModifiers.push(keybinding);
return true;
},
unregisterListener = function (mainKey, modifiers, keybinding) {
// Removes all registered Keybinding-instances of the passed mainKey
// and modifiers. Removes only a single Keybinding-instance if keybinding
// is passed. Returns false if something went wrong.
// parseShortcut has ensured that mainKey and modifiers are valid
var modifierString = modifiers.join(modifierStringDelimiter) || noModifierFlag,
bindingsByKey = listOfBoundShortcuts[safeKeyCodes[mainKey]],
bindingsByKeyAndModifiers, i, l, callback;
if (bindingsByKey) {
if (keybinding) {
if (_.isFunction(keybinding)) {
keybinding = findKeybindingByCallback(keybinding);
}
if (!(keybinding instanceof Keybinding)) {
return false;
}
bindingsByKeyAndModifiers = bindingsByKey[modifierString],
i = 0, l = bindingsByKeyAndModifiers.length,
callback = keybinding.callback;
if (bindingsByKeyAndModifiers && callback) {
for (; i < l; i++) {
if (bindingsByKeyAndModifiers[i].callback === callback) {
bindingsByKeyAndModifiers.splice(i, 1);
if (!bindingsByKeyAndModifiers.length) {
// No bound Shortcuts left, you can delete this object
delete bindingsByKey[modifierString];
}
return true;
}
}
}
} else {
delete bindingsByKey[modifierString];
return true;
}
}
return false;
},
isKeybindingTriggerable = function (keybinding, e) {
// Checks whether a keybinding-callback should be invoked or
// not. Returns true if the keybinding is triggerable, false
// otherwise.
var options = keybinding.options;
if (!options.isActive) {
return false;
}
if (options.enableOnlyWhenDocumentHasFocus && doc.hasFocus && !doc.hasFocus()) {
// This document doesn't have focus, so shortcuts shouldn't get triggered here
return false;
}
if (options.disableWhenTargetHasFocus && e && e.target && hasFocus(e.target)) {
return false;
}
if (options.disableWhenAnyElementHasFocus && isAnyNodeInFocus()) {
return false;
}
return true;
},
triggerListener = function (mainKey, modifiers, e) {
// Triggers all bound keybinding-callbacks which are associated with the mainKey-modifiers-combination
var modifierString = modifiers.join(modifierStringDelimiter) || noModifierFlag,
bindingsByKey = listOfBoundShortcuts[safeKeyCodes[mainKey]] || {}, // Fallback to empty object to prevent a reference error
bindingsByKeyAndModifiers = bindingsByKey[modifierString],
args, argsLength, useCall, i, l, keybinding, callback, res = true;
if (bindingsByKeyAndModifiers) {
args = toArray(arguments, 2), argsLength = args.length,
useCall = argsLength <= 1, // if there's only one (or less) arguments, use call to improve performance
prevShortcutProp = e.shortcut; // We trigger the event with a "shortcut"-property. To restore the event's original form we save the potentially existing property.
e.shortcut = normalizeShortcut(mainKey, modifiers);
for (i = 0, l = bindingsByKeyAndModifiers.length; i < l, (keybinding = bindingsByKeyAndModifiers[i]); i++) {
if (!isKeybindingTriggerable(keybinding, e)) {
continue;
}
if (_.isFunction(callback = keybinding.callback)) {
res &= useCall ? callback.call(keybinding.context || this, e) : callback.apply(keybinding.context || this, e.args);
}
if (keybinding.options.preventDefault) {
e.preventDefault();
}
}
e.shortcut = prevShortcutProp;
}
return !!res;
},
checkForKeybinding = function (e) {
// Keydown Event-handler which checks if there are any
// keybindings associated with the currently pressed keys
if (globalOptions.active && e && e.keyCode && e.keyCode in listOfBoundShortcuts) {
var mainKey = safeKeyCodes_inversion[e.keyCode],
modifiers = [];
_.each(modifierNames, function (name) {
if (e[name + "Key"]) {
modifiers.push(name);
}
});
return triggerListener(mainKey, modifiers, e);
}
}
function Keybinding (normalizedShortcut, callback, context, options) {
this.shortcut = normalizedShortcut;
this.callback = callback;
this.context = context;
this.options = _.extend({}, keybindingDefaults, options);
}
Keybinding.prototype = {
setShortcut: function (shortcut) {
var args = arguments,
keys = ( args.length === 2 && _.isString(args[0]) ) ?
_.isArray(args[1]) ? args : [args[0], []] :
parseShortcut (shortcut),
prevKeys, normalizedShortcut
if (keys) {
normalizedShortcut = normalizeShortcut(keys[0], keys[1]);
prevKeys = parseShortcut(this.shortcut);
if (!prevKeys || this.shortcut !== normalizedShortcut) {
// The new shortcut indeed differs from the previous one
unregisterListener(prevKeys[0], prevKeys[1], this);
this.shortcut = normalizedShortcut;
return registerListener(keys[0], keys[1], this);
}
}
return false;
},
remove: function () {
var keys = parseShortcut(this.shortcut);
if (keys) {
unregisterListener(keys[0], keys[1], this);
}
this.callback = null;
},
isBound: function () {
return isKeybindingAttached(this);
},
bind: function () {
var keys = parseShortcut(this.shortcut);
if (isKeybindingAttached(this, keys)) {
return true;
}
return keys ? registerListener(keys[0], keys[1], this) : false;
},
unbind: function () {
var keys = parseShortcut(this.shortcut);
return keys ? unregisterListener(keys[0], keys[1], this) : false;
},
setCallback: function (callback) {
if (_.isFunction(callback)) {
this.callback = callback;
return true;
}
return false;
},
setContext: function (context) {
if (this.context !== context) {
this.context = context;
return true;
}
return false;
},
setOption: function (name, value) {
var type = $.type(name);
if (type === "string") {
this.options[name] = value;
} else if (type === "object") {
return _.each(name, function (val, key) {
this.setOption(key, val);
}, this);
}
},
deactivate: function () {
this.options.isActive = false;
},
activate: function () {
this.options.isActive = true;
}
}
var generate = function (keys, callback, context, options) {
var normalizedShortcut = normalizeShortcut(keys[0], keys[1]);
if ($.isPlainObject(callback)) {
options = options ? _.extend(options, callback) : callback;
callback = undefined;
}
return new Keybinding(normalizedShortcut, callback, context, options);
},
onoffListener = function (on, register, shortcut, callback, context, options) {
// This parses the passed arguments and calls registerListener or unregisterListener with it
// Set register to false if you just want to generate the Keybinding without registering / unregistering it
// Set on to true to register the keybinding, set it to false to unregister it. The register argument must be true
// shortcut can be an object with the keys being the shortcuts and its values the callbacks
// or an array with its values being several different Shortcuts for which one callback is registered
// or a string being the shortcut.
var keys, keybinding;
if (typeof shortcut === "string") {
// First the typeof check, as this is the fastest
keys = parseShortcut(shortcut);
if (keys) {
if (!register) {
return generate(keys, callback, context, options);
} else if (on) {
keybinding = generate(keys, callback, context, options);
return registerListener(keys[0], keys[1], keybinding) ? keybinding : false;
} else if (off) {
return unregisterListener(keys[0], keys[1], callback);
}
}
} else if (shortcut instanceof Keybinding) {
// Then the instanceof check
keybinding = shortcut;
shortcut = keybinding.shortcut;
keys = parseShortcut(shortcut);
if (register && keys) {
if (on) {
return registerListener(keys[0], keys[1], keybinding) ? keybinding : false;
} else {
return unregisterListener(keys[0], keys[1], callback);
}
}
} else if (typeof shortcut === "object") {
// Then the other checks
return _.isArray(shortcut) ?
_.map(shortcut, function (s) {
return onoffListener(on, register, s, callback, context, options);
}) :
_.map(shortcut, function (c, s) {
// In case shortcut is an object, the callback-argument
// is meaningless. Instead the context is in place of the
// callback. And so the options are in place of the context-
// argument:
// callback = context
// context = options
return onoffListener(on, register, s, c, callback, context);
})
}
return false;
}
var prev$Shortcut = $.Shortcut,
this$Shortcut = $.Shortcut = {
noConflict: function () {
$.Shortcut = prev$Shortcut;
return this$Shortcut; // is safer than returning this` as this` can be changed by the caller
},
setGlobal: function () {
$.Shortcut = this$Shortcut; // is safer than setting this` as this` can be changed by the caller
},
on: function (shortcut, callback, context, options) {
return onoffListener(true, true, shortcut, callback, context, options);
},
off: function (shortcut, callback, context) {
return onoffListener(false, true, shortcut, callback, context);
},
generate: function (shortcut, callback, context, options) {
return onoffListener(true, false, shortcut, callback, context, options);
},
trigger: function (shortcut) {
if (_.isArray(shortcut)) {
return _.each(shortcut, $.Shortcut.trigger, $.Shortcut);
}
var keys = parseShortcut(shortcut), args = toArray(arguments, 1);
if (keys) {
args.length ? triggerListener.apply(this, keys.concat(args)) : triggerListener(keys[0], keys[1]);
}
},
getKeybindings: function (shortcuts) {
if (!shortcuts) {
return getKeybindingList();
}
if (!_.isArray(shortcuts)) {
shortcuts = [shortcuts];
}
var res = [];
_.each(shortcuts, function (shortcut) {
res = res.concat(getKeybindingsByShortcut(shortcut));
});
return _.uniq(res);
},
activate: function () {
this.setOption("active", true);
},
deactivate: function () {
this.setOption("active", false);
},
setOption: function (attr, val) {
if (typeof attr === "object") {
_.extend(globalOptions, attr);
} else {
globalOptions[attr] = val;
}
}
}
$(win).on("keydown", checkForKeybinding);
})(window, window.document, window.jQuery, window._);