##// END OF EJS Templates
Fixed bug
Jonathan Frederic -
Show More
@@ -1,581 +1,573
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2011 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // Keyboard management
10 //============================================================================
11
3
12 var IPython = (function (IPython) {
4 var IPython = (function (IPython) {
13 "use strict";
5 "use strict";
14
6
15 var browser = utils.browser[0];
7 var browser = utils.browser[0];
16 var platform = utils.platform;
8 var platform = utils.platform;
17
9
18 // Main keyboard manager for the notebook
10 // Main keyboard manager for the notebook
19 var keycodes = keyboard.keycodes;
11 var keycodes = keyboard.keycodes;
20
12
21 var KeyboardManager = function (options) {
13 var KeyboardManager = function (options) {
22 // Constructor
14 // Constructor
23 //
15 //
24 // Parameters:
16 // Parameters:
25 // options: dictionary
17 // options: dictionary
26 // Dictionary of keyword arguments.
18 // Dictionary of keyword arguments.
27 // events: $(Events) instance
19 // events: $(Events) instance
28 // pager: Pager instance
20 // pager: Pager instance
29 this.mode = 'command';
21 this.mode = 'command';
30 this.enabled = true;
22 this.enabled = true;
31 this.pager = options.pager;
23 this.pager = options.pager;
32 this.quick_help = undefined;
24 this.quick_help = undefined;
33 this.notebook = undefined;
25 this.notebook = undefined;
34 this.bind_events();
26 this.bind_events();
35 this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events);
27 this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events);
36 this.command_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
28 this.command_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
37 this.command_shortcuts.add_shortcuts(this.get_default_command_shortcuts());
29 this.command_shortcuts.add_shortcuts(this.get_default_command_shortcuts());
38 this.edit_shortcuts = new keyboard.ShortcutManager(undefined, options.events);
30 this.edit_shortcuts = new keyboard.ShortcutManager(undefined, options.events);
39 this.edit_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
31 this.edit_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
40 this.edit_shortcuts.add_shortcuts(this.get_default_edit_shortcuts());
32 this.edit_shortcuts.add_shortcuts(this.get_default_edit_shortcuts());
41 };
33 };
42
34
43 KeyboardManager.prototype.get_default_common_shortcuts = function() {
35 KeyboardManager.prototype.get_default_common_shortcuts = function() {
44 var that = this;
36 var that = this;
45 var shortcuts = {
37 var shortcuts = {
46 'shift' : {
38 'shift' : {
47 help : '',
39 help : '',
48 help_index : '',
40 help_index : '',
49 handler : function (event) {
41 handler : function (event) {
50 // ignore shift keydown
42 // ignore shift keydown
51 return true;
43 return true;
52 }
44 }
53 },
45 },
54 'shift-enter' : {
46 'shift-enter' : {
55 help : 'run cell, select below',
47 help : 'run cell, select below',
56 help_index : 'ba',
48 help_index : 'ba',
57 handler : function (event) {
49 handler : function (event) {
58 that.notebook.execute_cell_and_select_below();
50 that.notebook.execute_cell_and_select_below();
59 return false;
51 return false;
60 }
52 }
61 },
53 },
62 'ctrl-enter' : {
54 'ctrl-enter' : {
63 help : 'run cell',
55 help : 'run cell',
64 help_index : 'bb',
56 help_index : 'bb',
65 handler : function (event) {
57 handler : function (event) {
66 that.notebook.execute_cell();
58 that.notebook.execute_cell();
67 return false;
59 return false;
68 }
60 }
69 },
61 },
70 'alt-enter' : {
62 'alt-enter' : {
71 help : 'run cell, insert below',
63 help : 'run cell, insert below',
72 help_index : 'bc',
64 help_index : 'bc',
73 handler : function (event) {
65 handler : function (event) {
74 that.notebook.execute_cell_and_insert_below();
66 that.notebook.execute_cell_and_insert_below();
75 return false;
67 return false;
76 }
68 }
77 }
69 }
78 };
70 };
79
71
80 if (platform === 'MacOS') {
72 if (platform === 'MacOS') {
81 shortcuts['cmd-s'] =
73 shortcuts['cmd-s'] =
82 {
74 {
83 help : 'save notebook',
75 help : 'save notebook',
84 help_index : 'fb',
76 help_index : 'fb',
85 handler : function (event) {
77 handler : function (event) {
86 that.notebook.save_checkpoint();
78 that.notebook.save_checkpoint();
87 event.preventDefault();
79 event.preventDefault();
88 return false;
80 return false;
89 }
81 }
90 };
82 };
91 } else {
83 } else {
92 shortcuts['ctrl-s'] =
84 shortcuts['ctrl-s'] =
93 {
85 {
94 help : 'save notebook',
86 help : 'save notebook',
95 help_index : 'fb',
87 help_index : 'fb',
96 handler : function (event) {
88 handler : function (event) {
97 that.notebook.save_checkpoint();
89 that.notebook.save_checkpoint();
98 event.preventDefault();
90 event.preventDefault();
99 return false;
91 return false;
100 }
92 }
101 };
93 };
102 }
94 }
103 return shortcuts;
95 return shortcuts;
104 };
96 };
105
97
106 KeyboardManager.prototype.get_default_edit_shortcuts = function() {
98 KeyboardManager.prototype.get_default_edit_shortcuts = function() {
107 var that = this;
99 var that = this;
108 return {
100 return {
109 'esc' : {
101 'esc' : {
110 help : 'command mode',
102 help : 'command mode',
111 help_index : 'aa',
103 help_index : 'aa',
112 handler : function (event) {
104 handler : function (event) {
113 that.notebook.command_mode();
105 that.notebook.command_mode();
114 return false;
106 return false;
115 }
107 }
116 },
108 },
117 'ctrl-m' : {
109 'ctrl-m' : {
118 help : 'command mode',
110 help : 'command mode',
119 help_index : 'ab',
111 help_index : 'ab',
120 handler : function (event) {
112 handler : function (event) {
121 that.notebook.command_mode();
113 that.notebook.command_mode();
122 return false;
114 return false;
123 }
115 }
124 },
116 },
125 'up' : {
117 'up' : {
126 help : '',
118 help : '',
127 help_index : '',
119 help_index : '',
128 handler : function (event) {
120 handler : function (event) {
129 var index = that.notebook.get_selected_index();
121 var index = that.notebook.get_selected_index();
130 var cell = that.notebook.get_cell(index);
122 var cell = that.notebook.get_cell(index);
131 if (cell && cell.at_top() && index !== 0) {
123 if (cell && cell.at_top() && index !== 0) {
132 event.preventDefault();
124 event.preventDefault();
133 that.notebook.command_mode();
125 that.notebook.command_mode();
134 that.notebook.select_prev();
126 that.notebook.select_prev();
135 that.notebook.edit_mode();
127 that.notebook.edit_mode();
136 var cm = that.notebook.get_selected_cell().code_mirror;
128 var cm = that.notebook.get_selected_cell().code_mirror;
137 cm.setCursor(cm.lastLine(), 0);
129 cm.setCursor(cm.lastLine(), 0);
138 return false;
130 return false;
139 } else if (cell) {
131 } else if (cell) {
140 var cm = cell.code_mirror;
132 var cm = cell.code_mirror;
141 cm.execCommand('goLineUp');
133 cm.execCommand('goLineUp');
142 return false;
134 return false;
143 }
135 }
144 }
136 }
145 },
137 },
146 'down' : {
138 'down' : {
147 help : '',
139 help : '',
148 help_index : '',
140 help_index : '',
149 handler : function (event) {
141 handler : function (event) {
150 var index = that.notebook.get_selected_index();
142 var index = that.notebook.get_selected_index();
151 var cell = that.notebook.get_cell(index);
143 var cell = that.notebook.get_cell(index);
152 if (cell.at_bottom() && index !== (that.notebook.ncells()-1)) {
144 if (cell.at_bottom() && index !== (that.notebook.ncells()-1)) {
153 event.preventDefault();
145 event.preventDefault();
154 that.notebook.command_mode();
146 that.notebook.command_mode();
155 that.notebook.select_next();
147 that.notebook.select_next();
156 that.notebook.edit_mode();
148 that.notebook.edit_mode();
157 var cm = that.notebook.get_selected_cell().code_mirror;
149 var cm = that.notebook.get_selected_cell().code_mirror;
158 cm.setCursor(0, 0);
150 cm.setCursor(0, 0);
159 return false;
151 return false;
160 } else {
152 } else {
161 var cm = cell.code_mirror;
153 var cm = cell.code_mirror;
162 cm.execCommand('goLineDown');
154 cm.execCommand('goLineDown');
163 return false;
155 return false;
164 }
156 }
165 }
157 }
166 },
158 },
167 'ctrl-shift--' : {
159 'ctrl-shift--' : {
168 help : 'split cell',
160 help : 'split cell',
169 help_index : 'ea',
161 help_index : 'ea',
170 handler : function (event) {
162 handler : function (event) {
171 that.notebook.split_cell();
163 that.notebook.split_cell();
172 return false;
164 return false;
173 }
165 }
174 },
166 },
175 'ctrl-shift-subtract' : {
167 'ctrl-shift-subtract' : {
176 help : '',
168 help : '',
177 help_index : 'eb',
169 help_index : 'eb',
178 handler : function (event) {
170 handler : function (event) {
179 that.notebook.split_cell();
171 that.notebook.split_cell();
180 return false;
172 return false;
181 }
173 }
182 },
174 },
183 };
175 };
184 };
176 };
185
177
186 KeyboardManager.prototype.get_default_command_shortcuts = function() {
178 KeyboardManager.prototype.get_default_command_shortcuts = function() {
187 var that = this;
179 var that = this;
188 return {
180 return {
189 'space': {
181 'space': {
190 help: "Scroll down to next H1 cell",
182 help: "Scroll down to next H1 cell",
191 handler: function(event) {
183 handler: function(event) {
192 return that.notebook.scrollmanager.scroll(1);
184 return that.notebook.scrollmanager.scroll(1);
193 },
185 },
194 },
186 },
195 'shift-space': {
187 'shift-space': {
196 help: "Scroll up to previous H1 cell",
188 help: "Scroll up to previous H1 cell",
197 handler: function(event) {
189 handler: function(event) {
198 return that.notebook.scrollmanager.scroll(-1);
190 return that.notebook.scrollmanager.scroll(-1);
199 },
191 },
200 },
192 },
201 'enter' : {
193 'enter' : {
202 help : 'edit mode',
194 help : 'edit mode',
203 help_index : 'aa',
195 help_index : 'aa',
204 handler : function (event) {
196 handler : function (event) {
205 that.notebook.edit_mode();
197 that.notebook.edit_mode();
206 return false;
198 return false;
207 }
199 }
208 },
200 },
209 'up' : {
201 'up' : {
210 help : 'select previous cell',
202 help : 'select previous cell',
211 help_index : 'da',
203 help_index : 'da',
212 handler : function (event) {
204 handler : function (event) {
213 var index = that.notebook.get_selected_index();
205 var index = that.notebook.get_selected_index();
214 if (index !== 0 && index !== null) {
206 if (index !== 0 && index !== null) {
215 that.notebook.select_prev();
207 that.notebook.select_prev();
216 that.notebook.focus_cell();
208 that.notebook.focus_cell();
217 }
209 }
218 return false;
210 return false;
219 }
211 }
220 },
212 },
221 'down' : {
213 'down' : {
222 help : 'select next cell',
214 help : 'select next cell',
223 help_index : 'db',
215 help_index : 'db',
224 handler : function (event) {
216 handler : function (event) {
225 var index = that.notebook.get_selected_index();
217 var index = that.notebook.get_selected_index();
226 if (index !== (that.notebook.ncells()-1) && index !== null) {
218 if (index !== (that.notebook.ncells()-1) && index !== null) {
227 that.notebook.select_next();
219 that.notebook.select_next();
228 that.notebook.focus_cell();
220 that.notebook.focus_cell();
229 }
221 }
230 return false;
222 return false;
231 }
223 }
232 },
224 },
233 'k' : {
225 'k' : {
234 help : 'select previous cell',
226 help : 'select previous cell',
235 help_index : 'dc',
227 help_index : 'dc',
236 handler : function (event) {
228 handler : function (event) {
237 var index = that.notebook.get_selected_index();
229 var index = that.notebook.get_selected_index();
238 if (index !== 0 && index !== null) {
230 if (index !== 0 && index !== null) {
239 that.notebook.select_prev();
231 that.notebook.select_prev();
240 that.notebook.focus_cell();
232 that.notebook.focus_cell();
241 }
233 }
242 return false;
234 return false;
243 }
235 }
244 },
236 },
245 'j' : {
237 'j' : {
246 help : 'select next cell',
238 help : 'select next cell',
247 help_index : 'dd',
239 help_index : 'dd',
248 handler : function (event) {
240 handler : function (event) {
249 var index = that.notebook.get_selected_index();
241 var index = that.notebook.get_selected_index();
250 if (index !== (that.notebook.ncells()-1) && index !== null) {
242 if (index !== (that.notebook.ncells()-1) && index !== null) {
251 that.notebook.select_next();
243 that.notebook.select_next();
252 that.notebook.focus_cell();
244 that.notebook.focus_cell();
253 }
245 }
254 return false;
246 return false;
255 }
247 }
256 },
248 },
257 'x' : {
249 'x' : {
258 help : 'cut cell',
250 help : 'cut cell',
259 help_index : 'ee',
251 help_index : 'ee',
260 handler : function (event) {
252 handler : function (event) {
261 that.notebook.cut_cell();
253 that.notebook.cut_cell();
262 return false;
254 return false;
263 }
255 }
264 },
256 },
265 'c' : {
257 'c' : {
266 help : 'copy cell',
258 help : 'copy cell',
267 help_index : 'ef',
259 help_index : 'ef',
268 handler : function (event) {
260 handler : function (event) {
269 that.notebook.copy_cell();
261 that.notebook.copy_cell();
270 return false;
262 return false;
271 }
263 }
272 },
264 },
273 'shift-v' : {
265 'shift-v' : {
274 help : 'paste cell above',
266 help : 'paste cell above',
275 help_index : 'eg',
267 help_index : 'eg',
276 handler : function (event) {
268 handler : function (event) {
277 that.notebook.paste_cell_above();
269 that.notebook.paste_cell_above();
278 return false;
270 return false;
279 }
271 }
280 },
272 },
281 'v' : {
273 'v' : {
282 help : 'paste cell below',
274 help : 'paste cell below',
283 help_index : 'eh',
275 help_index : 'eh',
284 handler : function (event) {
276 handler : function (event) {
285 that.notebook.paste_cell_below();
277 that.notebook.paste_cell_below();
286 return false;
278 return false;
287 }
279 }
288 },
280 },
289 'd' : {
281 'd' : {
290 help : 'delete cell (press twice)',
282 help : 'delete cell (press twice)',
291 help_index : 'ej',
283 help_index : 'ej',
292 count: 2,
284 count: 2,
293 handler : function (event) {
285 handler : function (event) {
294 that.notebook.delete_cell();
286 that.notebook.delete_cell();
295 return false;
287 return false;
296 }
288 }
297 },
289 },
298 'a' : {
290 'a' : {
299 help : 'insert cell above',
291 help : 'insert cell above',
300 help_index : 'ec',
292 help_index : 'ec',
301 handler : function (event) {
293 handler : function (event) {
302 that.notebook.insert_cell_above();
294 that.notebook.insert_cell_above();
303 that.notebook.select_prev();
295 that.notebook.select_prev();
304 that.notebook.focus_cell();
296 that.notebook.focus_cell();
305 return false;
297 return false;
306 }
298 }
307 },
299 },
308 'b' : {
300 'b' : {
309 help : 'insert cell below',
301 help : 'insert cell below',
310 help_index : 'ed',
302 help_index : 'ed',
311 handler : function (event) {
303 handler : function (event) {
312 that.notebook.insert_cell_below();
304 that.notebook.insert_cell_below();
313 that.notebook.select_next();
305 that.notebook.select_next();
314 that.notebook.focus_cell();
306 that.notebook.focus_cell();
315 return false;
307 return false;
316 }
308 }
317 },
309 },
318 'y' : {
310 'y' : {
319 help : 'to code',
311 help : 'to code',
320 help_index : 'ca',
312 help_index : 'ca',
321 handler : function (event) {
313 handler : function (event) {
322 that.notebook.to_code();
314 that.notebook.to_code();
323 return false;
315 return false;
324 }
316 }
325 },
317 },
326 'm' : {
318 'm' : {
327 help : 'to markdown',
319 help : 'to markdown',
328 help_index : 'cb',
320 help_index : 'cb',
329 handler : function (event) {
321 handler : function (event) {
330 that.notebook.to_markdown();
322 that.notebook.to_markdown();
331 return false;
323 return false;
332 }
324 }
333 },
325 },
334 'r' : {
326 'r' : {
335 help : 'to raw',
327 help : 'to raw',
336 help_index : 'cc',
328 help_index : 'cc',
337 handler : function (event) {
329 handler : function (event) {
338 that.notebook.to_raw();
330 that.notebook.to_raw();
339 return false;
331 return false;
340 }
332 }
341 },
333 },
342 '1' : {
334 '1' : {
343 help : 'to heading 1',
335 help : 'to heading 1',
344 help_index : 'cd',
336 help_index : 'cd',
345 handler : function (event) {
337 handler : function (event) {
346 that.notebook.to_heading(undefined, 1);
338 that.notebook.to_heading(undefined, 1);
347 return false;
339 return false;
348 }
340 }
349 },
341 },
350 '2' : {
342 '2' : {
351 help : 'to heading 2',
343 help : 'to heading 2',
352 help_index : 'ce',
344 help_index : 'ce',
353 handler : function (event) {
345 handler : function (event) {
354 that.notebook.to_heading(undefined, 2);
346 that.notebook.to_heading(undefined, 2);
355 return false;
347 return false;
356 }
348 }
357 },
349 },
358 '3' : {
350 '3' : {
359 help : 'to heading 3',
351 help : 'to heading 3',
360 help_index : 'cf',
352 help_index : 'cf',
361 handler : function (event) {
353 handler : function (event) {
362 that.notebook.to_heading(undefined, 3);
354 that.notebook.to_heading(undefined, 3);
363 return false;
355 return false;
364 }
356 }
365 },
357 },
366 '4' : {
358 '4' : {
367 help : 'to heading 4',
359 help : 'to heading 4',
368 help_index : 'cg',
360 help_index : 'cg',
369 handler : function (event) {
361 handler : function (event) {
370 that.notebook.to_heading(undefined, 4);
362 that.notebook.to_heading(undefined, 4);
371 return false;
363 return false;
372 }
364 }
373 },
365 },
374 '5' : {
366 '5' : {
375 help : 'to heading 5',
367 help : 'to heading 5',
376 help_index : 'ch',
368 help_index : 'ch',
377 handler : function (event) {
369 handler : function (event) {
378 that.notebook.to_heading(undefined, 5);
370 that.notebook.to_heading(undefined, 5);
379 return false;
371 return false;
380 }
372 }
381 },
373 },
382 '6' : {
374 '6' : {
383 help : 'to heading 6',
375 help : 'to heading 6',
384 help_index : 'ci',
376 help_index : 'ci',
385 handler : function (event) {
377 handler : function (event) {
386 that.notebook.to_heading(undefined, 6);
378 that.notebook.to_heading(undefined, 6);
387 return false;
379 return false;
388 }
380 }
389 },
381 },
390 'o' : {
382 'o' : {
391 help : 'toggle output',
383 help : 'toggle output',
392 help_index : 'gb',
384 help_index : 'gb',
393 handler : function (event) {
385 handler : function (event) {
394 that.notebook.toggle_output();
386 that.notebook.toggle_output();
395 return false;
387 return false;
396 }
388 }
397 },
389 },
398 'shift-o' : {
390 'shift-o' : {
399 help : 'toggle output scrolling',
391 help : 'toggle output scrolling',
400 help_index : 'gc',
392 help_index : 'gc',
401 handler : function (event) {
393 handler : function (event) {
402 that.notebook.toggle_output_scroll();
394 that.notebook.toggle_output_scroll();
403 return false;
395 return false;
404 }
396 }
405 },
397 },
406 's' : {
398 's' : {
407 help : 'save notebook',
399 help : 'save notebook',
408 help_index : 'fa',
400 help_index : 'fa',
409 handler : function (event) {
401 handler : function (event) {
410 that.notebook.save_checkpoint();
402 that.notebook.save_checkpoint();
411 return false;
403 return false;
412 }
404 }
413 },
405 },
414 'ctrl-j' : {
406 'ctrl-j' : {
415 help : 'move cell down',
407 help : 'move cell down',
416 help_index : 'eb',
408 help_index : 'eb',
417 handler : function (event) {
409 handler : function (event) {
418 that.notebook.move_cell_down();
410 that.notebook.move_cell_down();
419 return false;
411 return false;
420 }
412 }
421 },
413 },
422 'ctrl-k' : {
414 'ctrl-k' : {
423 help : 'move cell up',
415 help : 'move cell up',
424 help_index : 'ea',
416 help_index : 'ea',
425 handler : function (event) {
417 handler : function (event) {
426 that.notebook.move_cell_up();
418 that.notebook.move_cell_up();
427 return false;
419 return false;
428 }
420 }
429 },
421 },
430 'l' : {
422 'l' : {
431 help : 'toggle line numbers',
423 help : 'toggle line numbers',
432 help_index : 'ga',
424 help_index : 'ga',
433 handler : function (event) {
425 handler : function (event) {
434 that.notebook.cell_toggle_line_numbers();
426 that.notebook.cell_toggle_line_numbers();
435 return false;
427 return false;
436 }
428 }
437 },
429 },
438 'i' : {
430 'i' : {
439 help : 'interrupt kernel (press twice)',
431 help : 'interrupt kernel (press twice)',
440 help_index : 'ha',
432 help_index : 'ha',
441 count: 2,
433 count: 2,
442 handler : function (event) {
434 handler : function (event) {
443 that.notebook.kernel.interrupt();
435 that.notebook.kernel.interrupt();
444 return false;
436 return false;
445 }
437 }
446 },
438 },
447 '0' : {
439 '0' : {
448 help : 'restart kernel (press twice)',
440 help : 'restart kernel (press twice)',
449 help_index : 'hb',
441 help_index : 'hb',
450 count: 2,
442 count: 2,
451 handler : function (event) {
443 handler : function (event) {
452 that.notebook.restart_kernel();
444 that.notebook.restart_kernel();
453 return false;
445 return false;
454 }
446 }
455 },
447 },
456 'h' : {
448 'h' : {
457 help : 'keyboard shortcuts',
449 help : 'keyboard shortcuts',
458 help_index : 'ge',
450 help_index : 'ge',
459 handler : function (event) {
451 handler : function (event) {
460 that.quick_help.show_keyboard_shortcuts();
452 that.quick_help.show_keyboard_shortcuts();
461 return false;
453 return false;
462 }
454 }
463 },
455 },
464 'z' : {
456 'z' : {
465 help : 'undo last delete',
457 help : 'undo last delete',
466 help_index : 'ei',
458 help_index : 'ei',
467 handler : function (event) {
459 handler : function (event) {
468 that.notebook.undelete_cell();
460 that.notebook.undelete_cell();
469 return false;
461 return false;
470 }
462 }
471 },
463 },
472 'shift-m' : {
464 'shift-m' : {
473 help : 'merge cell below',
465 help : 'merge cell below',
474 help_index : 'ek',
466 help_index : 'ek',
475 handler : function (event) {
467 handler : function (event) {
476 that.notebook.merge_cell_below();
468 that.notebook.merge_cell_below();
477 return false;
469 return false;
478 }
470 }
479 },
471 },
480 'q' : {
472 'q' : {
481 help : 'close pager',
473 help : 'close pager',
482 help_index : 'gd',
474 help_index : 'gd',
483 handler : function (event) {
475 handler : function (event) {
484 that.pager.collapse();
476 that.pager.collapse();
485 return false;
477 return false;
486 }
478 }
487 },
479 },
488 };
480 };
489 };
481 };
490
482
491 KeyboardManager.prototype.bind_events = function () {
483 KeyboardManager.prototype.bind_events = function () {
492 var that = this;
484 var that = this;
493 $(document).keydown(function (event) {
485 $(document).keydown(function (event) {
494 return that.handle_keydown(event);
486 return that.handle_keydown(event);
495 });
487 });
496 };
488 };
497
489
498 KeyboardManager.prototype.handle_keydown = function (event) {
490 KeyboardManager.prototype.handle_keydown = function (event) {
499 var notebook = this.notebook;
491 var notebook = this.notebook;
500
492
501 if (event.which === keycodes.esc) {
493 if (event.which === keycodes.esc) {
502 // Intercept escape at highest level to avoid closing
494 // Intercept escape at highest level to avoid closing
503 // websocket connection with firefox
495 // websocket connection with firefox
504 event.preventDefault();
496 event.preventDefault();
505 }
497 }
506
498
507 if (!this.enabled) {
499 if (!this.enabled) {
508 if (event.which === keycodes.esc) {
500 if (event.which === keycodes.esc) {
509 // ESC
501 // ESC
510 notebook.command_mode();
502 notebook.command_mode();
511 return false;
503 return false;
512 }
504 }
513 return true;
505 return true;
514 }
506 }
515
507
516 if (this.mode === 'edit') {
508 if (this.mode === 'edit') {
517 return this.edit_shortcuts.call_handler(event);
509 return this.edit_shortcuts.call_handler(event);
518 } else if (this.mode === 'command') {
510 } else if (this.mode === 'command') {
519 return this.command_shortcuts.call_handler(event);
511 return this.command_shortcuts.call_handler(event);
520 }
512 }
521 return true;
513 return true;
522 };
514 };
523
515
524 KeyboardManager.prototype.edit_mode = function () {
516 KeyboardManager.prototype.edit_mode = function () {
525 this.last_mode = this.mode;
517 this.last_mode = this.mode;
526 this.mode = 'edit';
518 this.mode = 'edit';
527 };
519 };
528
520
529 KeyboardManager.prototype.command_mode = function () {
521 KeyboardManager.prototype.command_mode = function () {
530 this.last_mode = this.mode;
522 this.last_mode = this.mode;
531 this.mode = 'command';
523 this.mode = 'command';
532 };
524 };
533
525
534 KeyboardManager.prototype.enable = function () {
526 KeyboardManager.prototype.enable = function () {
535 this.enabled = true;
527 this.enabled = true;
536 };
528 };
537
529
538 KeyboardManager.prototype.disable = function () {
530 KeyboardManager.prototype.disable = function () {
539 this.enabled = false;
531 this.enabled = false;
540 };
532 };
541
533
542 KeyboardManager.prototype.register_events = function (e) {
534 KeyboardManager.prototype.register_events = function (e) {
543 var that = this;
535 var that = this;
544 var handle_focus = function () {
536 var handle_focus = function () {
545 that.disable();
537 that.disable();
546 };
538 };
547 var handle_blur = function () {
539 var handle_blur = function () {
548 that.enable();
540 that.enable();
549 };
541 };
550 e.on('focusin', handle_focus);
542 e.on('focusin', handle_focus);
551 e.on('focusout', handle_blur);
543 e.on('focusout', handle_blur);
552 // TODO: Very strange. The focusout event does not seem fire for the
544 // TODO: Very strange. The focusout event does not seem fire for the
553 // bootstrap textboxes on FF25&26... This works around that by
545 // bootstrap textboxes on FF25&26... This works around that by
554 // registering focus and blur events recursively on all inputs within
546 // registering focus and blur events recursively on all inputs within
555 // registered element.
547 // registered element.
556 e.find('input').blur(handle_blur);
548 e.find('input').blur(handle_blur);
557 e.on('DOMNodeInserted', function (event) {
549 e.on('DOMNodeInserted', function (event) {
558 var target = $(event.target);
550 var target = $(event.target);
559 if (target.is('input')) {
551 if (target.is('input')) {
560 target.blur(handle_blur);
552 target.blur(handle_blur);
561 } else {
553 } else {
562 target.find('input').blur(handle_blur);
554 target.find('input').blur(handle_blur);
563 }
555 }
564 });
556 });
565 // There are times (raw_input) where we remove the element from the DOM before
557 // There are times (raw_input) where we remove the element from the DOM before
566 // focusout is called. In this case we bind to the remove event of jQueryUI,
558 // focusout is called. In this case we bind to the remove event of jQueryUI,
567 // which gets triggered upon removal, iff it is focused at the time.
559 // which gets triggered upon removal, iff it is focused at the time.
568 // is_focused must be used to check for the case where an element within
560 // is_focused must be used to check for the case where an element within
569 // the element being removed is focused.
561 // the element being removed is focused.
570 e.on('remove', function () {
562 e.on('remove', function () {
571 if (utils.is_focused(e[0])) {
563 if (utils.is_focused(e[0])) {
572 that.enable();
564 that.enable();
573 }
565 }
574 });
566 });
575 };
567 };
576
568
577 // For backwards compatability.
569 // For backwards compatability.
578 IPython.KeyboardManager = KeyboardManager;
570 IPython.KeyboardManager = KeyboardManager;
579
571
580 return {'KeyboardManager': KeyboardManager};
572 return {'KeyboardManager': KeyboardManager};
581 });
573 });
General Comments 0
You need to be logged in to leave comments. Login now