##// END OF EJS Templates
Backport PR #8131: handle undefined when sorting quick help...
Min RK -
Show More
@@ -1,454 +1,454 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3 /**
4 4 *
5 5 *
6 6 * @module keyboard
7 7 * @namespace keyboard
8 8 * @class ShortcutManager
9 9 */
10 10
11 11 define([
12 12 'base/js/namespace',
13 13 'jquery',
14 14 'base/js/utils',
15 15 'underscore',
16 16 ], function(IPython, $, utils, _) {
17 17 "use strict";
18 18
19 19
20 20 /**
21 21 * Setup global keycodes and inverse keycodes.
22 22 *
23 23 * See http://unixpapa.com/js/key.html for a complete description. The short of
24 24 * it is that there are different keycode sets. Firefox uses the "Mozilla keycodes"
25 25 * and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
26 26 * but have minor differences.
27 27 **/
28 28
29 29 // These apply to Firefox, (Webkit and IE)
30 30 // This does work **only** on US keyboard.
31 31 var _keycodes = {
32 32 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
33 33 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82,
34 34 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
35 35 '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
36 36 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
37 37 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
38 38 '\\ |': 220, '\' "': 222,
39 39 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
40 40 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
41 41 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
42 42 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118,
43 43 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126,
44 44 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18,
45 45 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34,
46 46 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
47 47 'insert': 45, 'delete': 46, 'numlock': 144,
48 48 };
49 49
50 50 // These apply to Firefox and Opera
51 51 var _mozilla_keycodes = {
52 52 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
53 53 };
54 54
55 55 // This apply to Webkit and IE
56 56 var _ie_keycodes = {
57 57 '; :': 186, '= +': 187, '- _': 189
58 58 };
59 59
60 60 var browser = utils.browser[0];
61 61 var platform = utils.platform;
62 62
63 63 if (browser === 'Firefox' || browser === 'Opera' || browser === 'Netscape') {
64 64 $.extend(_keycodes, _mozilla_keycodes);
65 65 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
66 66 $.extend(_keycodes, _ie_keycodes);
67 67 }
68 68
69 69 var keycodes = {};
70 70 var inv_keycodes = {};
71 71 for (var name in _keycodes) {
72 72 var names = name.split(' ');
73 73 if (names.length === 1) {
74 74 var n = names[0];
75 75 keycodes[n] = _keycodes[n];
76 76 inv_keycodes[_keycodes[n]] = n;
77 77 } else {
78 78 var primary = names[0];
79 79 var secondary = names[1];
80 80 keycodes[primary] = _keycodes[name];
81 81 keycodes[secondary] = _keycodes[name];
82 82 inv_keycodes[_keycodes[name]] = primary;
83 83 }
84 84 }
85 85
86 86 var normalize_key = function (key) {
87 87 return inv_keycodes[keycodes[key]];
88 88 };
89 89
90 90 var normalize_shortcut = function (shortcut) {
91 91 /**
92 92 * @function _normalize_shortcut
93 93 * @private
94 94 * return a dict containing the normalized shortcut and the number of time it should be pressed:
95 95 *
96 96 * Put a shortcut into normalized form:
97 97 * 1. Make lowercase
98 98 * 2. Replace cmd by meta
99 99 * 3. Sort '-' separated modifiers into the order alt-ctrl-meta-shift
100 100 * 4. Normalize keys
101 101 **/
102 102 if (platform === 'MacOS') {
103 103 shortcut = shortcut.toLowerCase().replace('cmdtrl-', 'cmd-');
104 104 } else {
105 105 shortcut = shortcut.toLowerCase().replace('cmdtrl-', 'ctrl-');
106 106 }
107 107
108 108 shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
109 109 shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
110 110 shortcut = shortcut.replace(/,$/, 'comma'); // catch shortcuts using '-' key
111 111 if(shortcut.indexOf(',') !== -1){
112 112 var sht = shortcut.split(',');
113 113 sht = _.map(sht, normalize_shortcut);
114 114 return shortcut;
115 115 }
116 116 shortcut = shortcut.replace(/comma/g, ','); // catch shortcuts using '-' key
117 117 var values = shortcut.split("-");
118 118 if (values.length === 1) {
119 119 return normalize_key(values[0]);
120 120 } else {
121 121 var modifiers = values.slice(0,-1);
122 122 var key = normalize_key(values[values.length-1]);
123 123 modifiers.sort();
124 124 return modifiers.join('-') + '-' + key;
125 125 }
126 126 };
127 127
128 128 var shortcut_to_event = function (shortcut, type) {
129 129 /**
130 130 * Convert a shortcut (shift-r) to a jQuery Event object
131 131 **/
132 132 type = type || 'keydown';
133 133 shortcut = normalize_shortcut(shortcut);
134 134 shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
135 135 var values = shortcut.split("-");
136 136 var modifiers = values.slice(0,-1);
137 137 var key = values[values.length-1];
138 138 var opts = {which: keycodes[key]};
139 139 if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
140 140 if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
141 141 if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
142 142 if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
143 143 return $.Event(type, opts);
144 144 };
145 145
146 146 var only_modifier_event = function(event){
147 147 /**
148 148 * Return `true` if the event only contains modifiers keys.
149 149 * false otherwise
150 150 **/
151 151 var key = inv_keycodes[event.which];
152 152 return ((event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) &&
153 153 (key === 'alt'|| key === 'ctrl'|| key === 'meta'|| key === 'shift'));
154 154
155 155 };
156 156
157 157 var event_to_shortcut = function (event) {
158 158 /**
159 159 * Convert a jQuery Event object to a normalized shortcut string (shift-r)
160 160 **/
161 161 var shortcut = '';
162 162 var key = inv_keycodes[event.which];
163 163 if (event.altKey && key !== 'alt') {shortcut += 'alt-';}
164 164 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl-';}
165 165 if (event.metaKey && key !== 'meta') {shortcut += 'meta-';}
166 166 if (event.shiftKey && key !== 'shift') {shortcut += 'shift-';}
167 167 shortcut += key;
168 168 return shortcut;
169 169 };
170 170
171 171 // Shortcut manager class
172 172
173 173 var ShortcutManager = function (delay, events, actions, env) {
174 174 /**
175 175 * A class to deal with keyboard event and shortcut
176 176 *
177 177 * @class ShortcutManager
178 178 * @constructor
179 179 */
180 180 this._shortcuts = {};
181 181 this.delay = delay || 800; // delay in milliseconds
182 182 this.events = events;
183 183 this.actions = actions;
184 184 this.actions.extend_env(env);
185 185 this._queue = [];
186 186 this._cleartimeout = null;
187 187 Object.seal(this);
188 188 };
189 189
190 190 ShortcutManager.prototype.clearsoon = function(){
191 191 /**
192 192 * Clear the pending shortcut soon, and cancel previous clearing
193 193 * that might be registered.
194 194 **/
195 195 var that = this;
196 196 clearTimeout(this._cleartimeout);
197 197 this._cleartimeout = setTimeout(function(){that.clearqueue();}, this.delay);
198 198 };
199 199
200 200
201 201 ShortcutManager.prototype.clearqueue = function(){
202 202 /**
203 203 * clear the pending shortcut sequence now.
204 204 **/
205 205 this._queue = [];
206 206 clearTimeout(this._cleartimeout);
207 207 };
208 208
209 209
210 210 var flatten_shorttree = function(tree){
211 211 /**
212 212 * Flatten a tree of shortcut sequences.
213 213 * use full to iterate over all the key/values of available shortcuts.
214 214 **/
215 215 var dct = {};
216 216 for(var key in tree){
217 217 var value = tree[key];
218 218 if(typeof(value) === 'string'){
219 219 dct[key] = value;
220 220 } else {
221 221 var ftree=flatten_shorttree(value);
222 222 for(var subkey in ftree){
223 223 dct[key+','+subkey] = ftree[subkey];
224 224 }
225 225 }
226 226 }
227 227 return dct;
228 228 };
229 229
230 230 ShortcutManager.prototype.help = function () {
231 231 var help = [];
232 232 var ftree = flatten_shorttree(this._shortcuts);
233 233 for (var shortcut in ftree) {
234 234 var action = this.actions.get(ftree[shortcut]);
235 235 var help_string = action.help||'== no help ==';
236 236 var help_index = action.help_index;
237 237 if (help_string) {
238 238 var shortstring = (action.shortstring||shortcut);
239 239 help.push({
240 240 shortcut: shortstring,
241 241 help: help_string,
242 242 help_index: help_index}
243 243 );
244 244 }
245 245 }
246 246 help.sort(function (a, b) {
247 if (a.help_index > b.help_index){
247 if (a.help_index === b.help_index) {
248 return 0;
249 }
250 if (a.help_index === undefined || a.help_index > b.help_index){
248 251 return 1;
249 252 }
250 if (a.help_index < b.help_index){
251 253 return -1;
252 }
253 return 0;
254 254 });
255 255 return help;
256 256 };
257 257
258 258 ShortcutManager.prototype.clear_shortcuts = function () {
259 259 this._shortcuts = {};
260 260 };
261 261
262 262 ShortcutManager.prototype.get_shortcut = function (shortcut){
263 263 /**
264 264 * return a node of the shortcut tree which an action name (string) if leaf,
265 265 * and an object with `object.subtree===true`
266 266 **/
267 267 if(typeof(shortcut) === 'string'){
268 268 shortcut = shortcut.split(',');
269 269 }
270 270
271 271 return this._get_leaf(shortcut, this._shortcuts);
272 272 };
273 273
274 274
275 275 ShortcutManager.prototype._get_leaf = function(shortcut_array, tree){
276 276 /**
277 277 * @private
278 278 * find a leaf/node in a subtree of the keyboard shortcut
279 279 *
280 280 **/
281 281 if(shortcut_array.length === 1){
282 282 return tree[shortcut_array[0]];
283 283 } else if( typeof(tree[shortcut_array[0]]) !== 'string'){
284 284 return this._get_leaf(shortcut_array.slice(1), tree[shortcut_array[0]]);
285 285 }
286 286 return null;
287 287 };
288 288
289 289 ShortcutManager.prototype.set_shortcut = function( shortcut, action_name){
290 290 if( typeof(action_name) !== 'string'){ throw('action is not a string', action_name);}
291 291 if( typeof(shortcut) === 'string'){
292 292 shortcut = shortcut.split(',');
293 293 }
294 294 return this._set_leaf(shortcut, action_name, this._shortcuts);
295 295 };
296 296
297 297 ShortcutManager.prototype._is_leaf = function(shortcut_array, tree){
298 298 if(shortcut_array.length === 1){
299 299 return(typeof(tree[shortcut_array[0]]) === 'string');
300 300 } else {
301 301 var subtree = tree[shortcut_array[0]];
302 302 return this._is_leaf(shortcut_array.slice(1), subtree );
303 303 }
304 304 };
305 305
306 306 ShortcutManager.prototype._remove_leaf = function(shortcut_array, tree, allow_node){
307 307 if(shortcut_array.length === 1){
308 308 var current_node = tree[shortcut_array[0]];
309 309 if(typeof(current_node) === 'string'){
310 310 delete tree[shortcut_array[0]];
311 311 } else {
312 312 throw('try to delete non-leaf');
313 313 }
314 314 } else {
315 315 this._remove_leaf(shortcut_array.slice(1), tree[shortcut_array[0]], allow_node);
316 316 if(_.keys(tree[shortcut_array[0]]).length === 0){
317 317 delete tree[shortcut_array[0]];
318 318 }
319 319 }
320 320 };
321 321
322 322 ShortcutManager.prototype._set_leaf = function(shortcut_array, action_name, tree){
323 323 var current_node = tree[shortcut_array[0]];
324 324 if(shortcut_array.length === 1){
325 325 if(current_node !== undefined && typeof(current_node) !== 'string'){
326 326 console.warn('[warning], you are overriting a long shortcut with a shorter one');
327 327 }
328 328 tree[shortcut_array[0]] = action_name;
329 329 return true;
330 330 } else {
331 331 if(typeof(current_node) === 'string'){
332 332 console.warn('you are trying to set a shortcut that will be shadowed'+
333 333 'by a more specific one. Aborting for :', action_name, 'the follwing '+
334 334 'will take precedence', current_node);
335 335 return false;
336 336 } else {
337 337 tree[shortcut_array[0]] = tree[shortcut_array[0]]||{};
338 338 }
339 339 this._set_leaf(shortcut_array.slice(1), action_name, tree[shortcut_array[0]]);
340 340 return true;
341 341 }
342 342 };
343 343
344 344 ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
345 345 /**
346 346 * Add a action to be handled by shortcut manager.
347 347 *
348 348 * - `shortcut` should be a `Shortcut Sequence` of the for `Ctrl-Alt-C,Meta-X`...
349 349 * - `data` could be an `action name`, an `action` or a `function`.
350 350 * if a `function` is passed it will be converted to an anonymous `action`.
351 351 *
352 352 **/
353 353 var action_name = this.actions.get_name(data);
354 354 if (! action_name){
355 355 throw('does nto know how to deal with ', data);
356 356 }
357 357
358 358 shortcut = normalize_shortcut(shortcut);
359 359 this.set_shortcut(shortcut, action_name);
360 360
361 361 if (!suppress_help_update) {
362 362 // update the keyboard shortcuts notebook help
363 363 this.events.trigger('rebuild.QuickHelp');
364 364 }
365 365 };
366 366
367 367 ShortcutManager.prototype.add_shortcuts = function (data) {
368 368 /**
369 369 * Convenient methods to call `add_shortcut(key, value)` on several items
370 370 *
371 371 * data : Dict of the form {key:value, ...}
372 372 **/
373 373 for (var shortcut in data) {
374 374 this.add_shortcut(shortcut, data[shortcut], true);
375 375 }
376 376 // update the keyboard shortcuts notebook help
377 377 this.events.trigger('rebuild.QuickHelp');
378 378 };
379 379
380 380 ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
381 381 /**
382 382 * Remove the binding of shortcut `sortcut` with its action.
383 383 * throw an error if trying to remove a non-exiting shortcut
384 384 **/
385 385 shortcut = normalize_shortcut(shortcut);
386 386 if( typeof(shortcut) === 'string'){
387 387 shortcut = shortcut.split(',');
388 388 }
389 389 this._remove_leaf(shortcut, this._shortcuts);
390 390 if (!suppress_help_update) {
391 391 // update the keyboard shortcuts notebook help
392 392 this.events.trigger('rebuild.QuickHelp');
393 393 }
394 394 };
395 395
396 396
397 397
398 398 ShortcutManager.prototype.call_handler = function (event) {
399 399 /**
400 400 * Call the corresponding shortcut handler for a keyboard event
401 401 * @method call_handler
402 402 * @return {Boolean} `true|false`, `false` if no handler was found, otherwise the value return by the handler.
403 403 * @param event {event}
404 404 *
405 405 * given an event, call the corresponding shortcut.
406 406 * return false is event wan handled, true otherwise
407 407 * in any case returning false stop event propagation
408 408 **/
409 409
410 410
411 411 this.clearsoon();
412 412 if(only_modifier_event(event)){
413 413 return true;
414 414 }
415 415 var shortcut = event_to_shortcut(event);
416 416 this._queue.push(shortcut);
417 417 var action_name = this.get_shortcut(this._queue);
418 418
419 419 if (typeof(action_name) === 'undefined'|| action_name === null){
420 420 this.clearqueue();
421 421 return true;
422 422 }
423 423
424 424 if (this.actions.exists(action_name)) {
425 425 event.preventDefault();
426 426 this.clearqueue();
427 427 return this.actions.call(action_name, event);
428 428 }
429 429
430 430 return false;
431 431 };
432 432
433 433
434 434 ShortcutManager.prototype.handles = function (event) {
435 435 var shortcut = event_to_shortcut(event);
436 436 var action_name = this.get_shortcut(this._queue.concat(shortcut));
437 437 return (typeof(action_name) !== 'undefined');
438 438 };
439 439
440 440 var keyboard = {
441 441 keycodes : keycodes,
442 442 inv_keycodes : inv_keycodes,
443 443 ShortcutManager : ShortcutManager,
444 444 normalize_key : normalize_key,
445 445 normalize_shortcut : normalize_shortcut,
446 446 shortcut_to_event : shortcut_to_event,
447 447 event_to_shortcut : event_to_shortcut,
448 448 };
449 449
450 450 // For backwards compatibility.
451 451 IPython.keyboard = keyboard;
452 452
453 453 return keyboard;
454 454 });
General Comments 0
You need to be logged in to leave comments. Login now