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