-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.keybindings Kopie.js
More file actions
355 lines (323 loc) · 10.5 KB
/
jquery.keybindings Kopie.js
File metadata and controls
355 lines (323 loc) · 10.5 KB
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
(function (win, doc, $, _) {
if (!$) return;
if (!_) {
// If Underscore is available use the great Underscore library
// else create a shim for it from jquery functions
_ = {
each: function (list, iterator, 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
};
}
var slice = Array.prototype.slice,
fromCharCode = String.fromCharCode;
var Constants = {
"BACKSPACE": 8,
"TAB": 9,
"RETURN": 13,
"ESCAPE": 27,
"SPACE": 32,
"PAGE_UP": 33,
"PAGE_DOWN": 34,
"LEFT": 37,
"UP": 38,
"RIGHT": 39,
"DOWN": 40
},
getCharCodeFromConstant,
getConstantFromCharCode,
prefix, r_prefix,
isPrintableMap = {},
checkForPrintability = (function (testEl, textNode, body, r_whitespace) {
textNode.nodeValue = "";
testEl.style.display = "inline";
testEl.appendChild(textNode);
var measure = function () {
body.appendChild(testEl);
var res = {
width: testEl.offsetWidth,
height: testEl.offsetHeight
}
body.removeChild(testEl);
return res;
},
initMeasures = measure(),
newMeasures, isPrintable;
return function (str) {
if (r_whitespace.test(str)) {
return isPrintableMap[str] = false;
} else {
textNode.nodeValue = str || "";
newMeasures = measure(),
isPrintable = initMeasures.width !== newMeasures.width || initMeasures.height !== newMeasures.height
return isPrintableMap[str] = isPrintable;
}
}
})(doc.createElement("div"), doc.createTextNode(""), doc.body, /^[\s\t\r\n ]+$/),
isPrintableCharacter = function (charCode) {
var str = fromCharCode(charCode);
return (str in isPrintableMap ? isPrintableMap[str] : checkForPrintability(str));
};
if (win.KeyEvent) {
prefix = "DOM_VK_";
r_prefix = new RegExp("^" + prefix);
getCharCodeFromConstant = function (constant) {
return win.KeyEvent[prefix + constant];
};
getConstantFromCharCode = function (charCode) {
if (typeof charCode !== "number") return;
var name, obj = win.KeyEvent;
for (name in obj) {
if (obj[name] === charCode) {
return r_prefix.test(name) ? name.substr(prefix.length) : name;
}
}
}
} else {
getCharCodeFromConstant = function (constant) {
return Constants[constant];
};
getConstantFromCharCode = function (charCode) {
if (typeof charCode !== "number") return;
for (var name in Constants) {
if (Constants[name] === charCode) {
return name;
}
}
}
}
/*
* This block of functions is for translating charCodes in characters and the other way round
*/
var getCharCodesFromString = function (str) {
if (str.length > 1) {
return [getCharCodeFromConstant(str)];
}
return [str.toLowerCase().charCodeAt(0), str.toUpperCase().charCodeAt(0)];
}, getStringFromCharCode = function (charCode) {
return isPrintableCharacter(charCode) ? fromCharCode(charCode) : getConstantFromCharCode(charCode);
}
/*
* This block of functions is for registering and unregistering keybindings:
*/
var keybindings = {},
modifierNames = ["alt", "ctrl", "meta", "shift"],
modifierNamesObj = {
"alt": true,
"ctrl": true,
"meta": true,
"shift": true
},
modifierStringDelimiter = "-",
defaults = {
delimiter: "+"
},
globalOptions = _.extend({}, defaults);
var detectAllCharCodes = function (charCode) {
if (_.isString(charCode)) {
return getCharCodesFromString(charCode);
} else if (_.isNumber(charCode)) {
return [parseInt(charCode)];
}
}, registerListener = function (charCode, arrayOfModifiers, callback, context) {
var ret = false, stringOfModifiers, bindings, modifiedBindings;
if (charCode && arrayOfModifiers && _.isArray(arrayOfModifiers) && arrayOfModifiers.length && callback) {
stringOfModifiers = arrayOfModifiers.sort().join(modifierStringDelimiter);
charCode = detectAllCharCodes(charCode);
_.each(charCode, function (code) {
if (code) {
ret = true;
bindings = keybindings[code] || (keybindings[code] = {}),
modifiedBindings = bindings[stringOfModifiers] || (bindings[stringOfModifiers] = []);
modifiedBindings.push({
"callback": callback,
"context": context
})
}
})
}
return ret;
}, unregisterListener = function (charCode, arrayOfModifiers, callback) {
var ret = false, stringOfModifiers, bindings, modifiedBindings, i,l, listener;
if (charCode && arrayOfModifiers && _.isArray(arrayOfModifiers) && arrayOfModifiers.length) {
stringOfModifiers = arrayOfModifiers.sort().join(modifierStringDelimiter);
charCode = detectAllCharCodes(charCode);
if (!callback) {
_.each(charCode, function (code) {
if (code && (bindings = keybindings[code])) {
ret = true;
delete bindings[stringOfModifiers];
}
})
} else {
_.each(charCode, function (code) {
if (code && (bindings = keybindings[code]) && (modifiedBindings = bindings[stringOfModifiers])) {
for (i = 0, l = modifiedBindings.length; (listener = modifiedBindings[i]); i++) {
if (listener.callback === callback) {
ret = true;
modifiedBindings.splice(i, 1);
break;
}
};
}
})
}
}
return ret;
};
/*
* This block of function is for parsing and assembling shortcut-Strings:
*/
var parseShortcut = function (shortcut, specialDelimiter) {
var keys = shortcut.toLowerCase().split(specialDelimiter || globalOptions.delimiter || defaults.delimiter),
mainKey = [], modifiers = [];
_.each(keys, function (key) {
if (key = $.trim(key)) {
if (key in modifierNamesObj) {
modifiers.push(key);
} else {
if (!isNaN(parseInt(key))) {
mainKey.push(parseInt(key));
} else {
mainKey.push(key.toUpperCase());
}
}
}
});
return (mainKey.length === 1 && modifiers.length) ? [mainKey[0], modifiers] : null;
}, assembleShortcut = function (mainKey, modifiers, specialDelimiter) {
return modifiers.concat([mainKey.toUpperCase()]).join(specialDelimiter || globalOptions.delimiter || defaults.delimiter);
}, parseKeyevent = function (e) {
var mainKey = getStringFromCharCode(e.charCode),
modifiers = [];
_.each(modifierNames, function (name) {
if (e[name + "Key"]) {
modifiers.push(name);
}
});
return [mainKey, modifiers];
}, getKeybindingFromKeyevent = function(e, callback, context) {
var parsed = parseKeyevent(e);
if (parsed[0] && parsed[1].length) {
registerListener(parsed[0], parsed[1], callback, context);
}
}, getShortcutFromKeyevent = function (e) {
var parsed = parseKeyevent(e);
if (parsed[0] && parsed[1].length) {
return assembleShortcut(parsed[0], parsed[1]);
}
}, normalizeShortcut = function (shortcut, specialDelimiter) {
var parsed = parseShortcut(shortcut, specialDelimiter), mainKey, modifiers;
if (parsed) {
mainKey = parsed[0];
modifiers = _.map(parsed[1].sort(), function (v) {return v.toLowerCase()});
if (_.isString(mainKey)) {
mainKey = getCharCodesFromString(mainKey)[0];
} // mainKey is a number or undefined
if (mainKey && (mainKey = getStringFromCharCode(mainKey))) {
// mainKey is definitively a not-empty string now
return assembleShortcut(mainKey, modifiers, specialDelimiter);
}
}
}
/*
* This block of functions is for the time when the user is working with the web app:
*/
var invokeListeners = function (listenerArray, e) {
var args = slice.call(arguments, 1), shouldApply = args.length > 1;
_.each(listenerArray, function (listener) {
if (shouldApply) {
listener.callback.apply(listener.context, args);
} else {
listener.callback.call(listener.context, e);
}
})
}, checkForKeybinding = function (e) {
if (e.charCode in keybindings && currentlyPressedModifiers.length) {
var charCode = e.charCode, modifierStr = currentlyPressedModifiers.join(modifierStringDelimiter), bindings;
if (bindings = keybindings[charCode][modifierStr]) {
invokeListeners(bindings, e);
}
}
}, registerModifiers = function (e) {
var oneOrMoreModifiersArePressed = e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;
if (!currentlyPressedModifiers.length && oneOrMoreModifiersArePressed) {
_.each(modifierNames, function (name) {
if (e[name + "Key"]) {
currentlyPressedModifiers.push(name);
}
});
} else if (currentlyPressedModifiers.length && !oneOrMoreModifiersArePressed) {
currentlyPressedModifiers = [];
}
}
/*
* This block of functions is the public API:
*/
$.Keybinding = {
on: function (shortcut, specialDelimiter, callback, context) {
if (_.isFunction(specialDelimiter)) {
context = callback;
callback = specialDelimiter;
specialDelimiter = undefined;
}
var keys = parseShortcut(shortcut, specialDelimiter);
if (keys) {
return registerListener(keys[0], keys[1], callback, context);
}
return false;
},
off: function (shortcut, specialDelimiter, callback) {
if (_.isFunction(specialDelimiter)) {
callback = specialDelimiter;
specialDelimiter = undefined;
}
var keys = parseShortcut(shortcut, specialDelimiter);
if (keys) {
return unregisterListener(keys[0], keys[1], callback);
}
return false;
},
trigger: function (shortcut, specialDelimiter) {
var keys = parseShortcut(shortcut, specialDelimiter), bindings, modifiedBindings, args = slice.call(arguments, 1);
if (keys && (bindings = keybindings[keys[0]]) && (modifiedBindings = bindings[keys[1].sort().join(modifierStringDelimiter)])) {
invokeListeners.apply(null, args);
}
},
getKeybindingList: function () {
var list = [], alreadyInList = {}, str, mainKey, modifiers, shortcut;
_.each(keybindings, function (bindings, charCode) {
str = fromCharCode(charCode);
if (!(str in alreadyInList)) {
alreadyInList[str.toLowerCase()] = alreadyInList[str.toUpperCase()] = true;
mainKey = getStringFromCharCode(charCode);
_.each(bindings, function (modifiedBindings, modifierString) {
modifiers = modifierString.split(modifierStringDelimiter);
shortcut = assembleShortcut(mainKey, modifiers);
_.each(modifiedBindings, function (listener) {
list.push(_.extend({
shortcut: shortcut
}, listener));
})
})
}
})
return list;
},
getShortcutFromKeyevent: getShortcutFromKeyevent,
getKeybindingFromKeyevent: getKeybindingFromKeyevent,
normalizeShortcut: normalizeShortcut,
options: globalOptions
}
$(win).on("keydown", registerModifiers).on("keypress", checkForKeybinding);
})(window, window.document, window.jQuery, window._);