forked from stepanvr/js-shortcuts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.shortcuts.js
302 lines (251 loc) · 9.31 KB
/
jquery.shortcuts.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
/**
* JavaScript Shortcuts Library (jQuery plugin) v0.7
* http://www.stepanreznikov.com/js-shortcuts/
* Copyright (c) 2010 Stepan Reznikov ([email protected])
* Date: 2010-08-08
*/
/*global jQuery */
(function($) {
/** Special keys */
var special = {
'backspace': 8,
'tab': 9,
'enter': 13,
'pause': 19,
'capslock': 20,
'esc': 27,
'space': 32,
'pageup': 33,
'pagedown': 34,
'end': 35,
'home': 36,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'insert': 45,
'delete': 46,
'f1': 112,
'f2': 113,
'f3': 114,
'f4': 115,
'f5': 116,
'f6': 117,
'f7': 118,
'f8': 119,
'f9': 120,
'f10': 121,
'f11': 122,
'f12': 123,
'?': 191, // Question mark
'minus': $.browser.opera ? [109, 45] : $.browser.mozilla ? 109 : [189, 109],
'plus': $.browser.opera ? [61, 43] : $.browser.mozilla ? [61, 107] : [187, 107]
};
/** Hash for shortcut lists */
var lists = {};
/** Active shortcut lists */
var active = {};
/** Hash for storing which keys are pressed at the moment. Key - ASCII key code (e.which), value - true/false. */
var pressed = {};
var isStarted = false;
var getKey = function(type, maskObj) {
var key = type;
if (maskObj.ctrl) { key += '_ctrl'; }
if (maskObj.alt) { key += '_alt'; }
if (maskObj.shift) { key += '_shift'; }
var keyMaker = function(key, which) {
if (which && which !== 16 && which !== 17 && which !== 18) { key += '_' + which; }
return key;
};
if ($.isArray(maskObj.which)) {
var keys = [];
$.each(maskObj.which, function(i, which) {
keys.push(keyMaker(key, which));
});
return keys;
} else {
return keyMaker(key, maskObj.which);
}
};
var getMaskObject = function(mask) {
var obj = {};
var items = mask.split('+');
$.each(items, function(i, item) {
if (item === 'ctrl' || item === 'alt' || item === 'shift') {
obj[item] = true;
} else {
obj.which = special[item] || item.toUpperCase().charCodeAt();
}
});
return obj;
};
var checkIsInput = function(target) {
var name = target.tagName.toLowerCase();
var type = target.type;
return (name === 'input' && $.inArray(type, ['text', 'password', 'file', 'search']) > -1) || name === 'textarea';
};
var run = function(type, e) {
var maskObj = {
ctrl: e.ctrlKey,
alt: e.altKey,
shift: e.shiftKey,
which: e.which
};
var key = getKey(type, maskObj);
var shortcuts;
$.each(active, function(list, list_activated) {
if (list_activated === true && lists[list] && lists[list][key]) {
shortcuts = lists[list][key];
}
});
if (!shortcuts) { return; }
var isInput = checkIsInput(e.target);
var isPrevented = false;
$.each(shortcuts, function(i, shortcut) {
// If not in input or this shortcut is enabled in inputs.
if (!isInput || shortcut.enableInInput) {
if (!isPrevented) {
e.preventDefault();
isPrevented = true;
}
shortcut.handler(shortcut, e); // Run the shortcut's handler.
}
});
};
$.Shortcuts = {};
/**
* Start reacting to shortcuts in the specified list.
* @param {String|Array} [list] List name or array of names
* @param {Boolean} [list] Should we replace the list of active lists?
*/
$.Shortcuts.start = function(list, replace) {
if (list === undefined)
list = 'default';
if (typeof(list) === 'string')
list = [list]
// Set the lists as active
$.each(active, function(k, v) {
if ($.inArray(k, list) !== -1)
active[k] = true;
else
active[k] = (replace === true ? false : active[k]);
});
if (isStarted) { return; } // We are going to attach event handlers only once, the first time this method is called.
$(document).bind(($.browser.opera ? 'keypress' : 'keydown') + '.shortcuts', function(e) {
// For a-z keydown and keyup the range is 65-90 and for keypress it's 97-122.
if (e.type === 'keypress' && e.which >= 97 && e.which <= 122) {
e.which = e.which - 32;
}
if (!pressed[e.which]) {
run('down', e);
}
pressed[e.which] = true;
run('hold', e);
});
$(document).bind('keyup.shortcuts', function(e) {
pressed[e.which] = false;
run('up', e);
});
isStarted = true;
return this;
};
/**
* Stop reacting to shortcuts (unbind event handlers).
*/
$.Shortcuts.stop = function(list) {
list = list || 'default';
active[list] = false;
var has_active = false;
$.each(active, function(list, list_activated) {
if (list_activated === true) {
has_active = true
}
});
if(has_active === false){
$(document).unbind('keypress.shortcuts keydown.shortcuts keyup.shortcuts');
isStarted = false;
}
return this;
};
/**
* Return the list of active lists
* @return {Array}
*/
$.Shortcuts.getActiveLists = function() {
var activies = [];
$.each(active, function(list, list_activated) {
if (list_activated === true) {
activies.push(list);
}
});
return activies;
};
/**
* Add a shortcut.
* @param {Object} params Shortcut parameters.
* @param {String} [params.type] The type of event to be used for running the shortcut's handler.
* Possible values:
* down – On key down (default value).
* up – On key up.
* hold – On pressing and holding down the key. The handler will be called immediately
* after pressing the key and then repeatedly while the key is held down.
*
* @param {String} params.mask A string specifying the key combination.
* Consists of key names separated by a plus sign. Case insensitive.
* Examples: 'Down', 'Esc', 'Shift+Up', 'ctrl+a'.
*
* @param {Function} params.handler A function to be called when the key combination is pressed. The event object will be passed to it.
* @param {String} [params.list] You can organize your shortcuts into lists and then switch between them.
* By default shortcuts are added to the 'default' list.
* @param {Boolean} [params.enableInInput] Whether to enable execution of the shortcut in input fields and textareas. Disabled by default.
*/
$.Shortcuts.add = function(params) {
if (!params.mask) { throw new Error("$.Shortcuts.add: required parameter 'params.mask' is undefined."); }
if (!params.handler) { throw new Error("$.Shortcuts.add: required parameter 'params.handler' is undefined."); }
var type = params.type || 'down';
var listNames = params.list ? params.list.replace(/\s+/g, '').split(',') : ['default'];
$.each(listNames, function(i, name) {
if (!lists[name]) {
lists[name] = {};
active[name] = false;
}
var list = lists[name];
var masks = params.mask.toLowerCase().replace(/\s+/g, '').split(',');
$.each(masks, function(i, mask) {
var maskObj = getMaskObject(mask);
var keys = getKey(type, maskObj);
if (!$.isArray(keys)) { keys = [keys]; }
$.each(keys, function(i, key) {
if (!list[key]) { list[key] = []; }
list[key].push(params);
});
});
});
return this;
};
/**
* Remove a shortcut.
* @param {Object} params Shortcut parameters.
* @param {String} [params.type] Event type (down|up|hold). Default: 'down'.
* @param {String} params.mask Key combination.
* @param {String} [params.list] A list from which to remove the shortcut. Default: 'default'.
*/
$.Shortcuts.remove = function(params) {
if (!params.mask) { throw new Error("$.Shortcuts.remove: required parameter 'params.mask' is undefined."); }
var type = params.type || 'down';
var listNames = params.list ? params.list.replace(/\s+/g, '').split(',') : ['default'];
$.each(listNames, function(i, name) {
if (!lists[name]) { return true; } // continue
var masks = params.mask.toLowerCase().replace(/\s+/g, '').split(',');
$.each(masks, function(i, mask) {
var maskObj = getMaskObject(mask);
var keys = getKey(type, maskObj);
if (!$.isArray(keys)) { keys = [keys]; }
$.each(keys, function(i, key) {
delete lists[name][key];
});
});
});
return this;
};
}(jQuery));