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