##// END OF EJS Templates
Ongoing work on cell splitting.
Brian E. Granger -
Show More
@@ -1,612 +1,619
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Keyboard management
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13 "use strict";
14 14
15 15 // Setup global keycodes and inverse keycodes.
16 16
17 17 // See http://unixpapa.com/js/key.html for a complete description. The short of
18 18 // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes"
19 19 // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
20 20 // but have minor differences.
21 21
22 22 // These apply to Firefox, (Webkit and IE)
23 23 var _keycodes = {
24 24 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
25 25 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82,
26 26 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
27 27 '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
28 28 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
29 29 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
30 30 '\\ |': 220, '\' "': 222,
31 31 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
32 32 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
33 33 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
34 34 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118,
35 35 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126,
36 36 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18,
37 37 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34,
38 38 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
39 39 'insert': 45, 'delete': 46, 'numlock': 144,
40 40 };
41 41
42 42 // These apply to Firefox and Opera
43 43 var _mozilla_keycodes = {
44 44 '; :': 59, '= +': 61, '- _': 109,
45 45 }
46 46
47 47 // This apply to Webkit and IE
48 48 var _ie_keycodes = {
49 49 '; :': 186, '= +': 187, '- _': 189,
50 50 }
51 51
52 52 var browser = IPython.utils.browser[0];
53 53
54 54 if (browser === 'Firefox' || browser === 'Opera') {
55 55 $.extend(_keycodes, _mozilla_keycodes);
56 56 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
57 57 $.extend(_keycodes, _ie_keycodes);
58 58 }
59 59
60 60 var keycodes = {};
61 61 var inv_keycodes = {};
62 62 for (var name in _keycodes) {
63 63 var names = name.split(' ');
64 64 if (names.length === 1) {
65 65 var n = names[0]
66 66 keycodes[n] = _keycodes[n]
67 67 inv_keycodes[_keycodes[n]] = n
68 68 } else {
69 69 var primary = names[0];
70 70 var secondary = names[1];
71 71 keycodes[primary] = _keycodes[name]
72 72 keycodes[secondary] = _keycodes[name]
73 73 inv_keycodes[_keycodes[name]] = primary
74 74 }
75 75 }
76 76
77 77
78 78 // Default keyboard shortcuts
79 79
80 80 var default_common_shortcuts = {
81 81 'meta+s' : {
82 82 help : 'save notebook',
83 83 handler : function (event) {
84 84 IPython.notebook.save_checkpoint();
85 85 event.preventDefault();
86 86 return false;
87 87 }
88 88 },
89 89 'ctrl+s' : {
90 90 help : 'save notebook',
91 91 handler : function (event) {
92 92 IPython.notebook.save_checkpoint();
93 93 event.preventDefault();
94 94 return false;
95 95 }
96 96 },
97 97 'shift' : {
98 98 help : '',
99 99 handler : function (event) {
100 100 // ignore shift keydown
101 101 return true;
102 102 }
103 103 },
104 104 'shift+enter' : {
105 105 help : 'run cell',
106 106 handler : function (event) {
107 107 IPython.notebook.execute_selected_cell('shift');
108 108 return false;
109 109 }
110 110 },
111 111 'alt+enter' : {
112 112 help : 'run cell, insert below',
113 113 handler : function (event) {
114 114 IPython.notebook.execute_selected_cell('alt');
115 115 return false;
116 116 }
117 117 },
118 118 'ctrl+enter' : {
119 119 help : 'run cell, select below',
120 120 handler : function (event) {
121 121 IPython.notebook.execute_selected_cell('ctrl');
122 122 return false;
123 123 }
124 124 }
125 125 }
126 126
127 127 // Edit mode defaults
128 128
129 129 var default_edit_shortcuts = {
130 130 'esc' : {
131 131 help : 'command mode',
132 132 handler : function (event) {
133 133 IPython.notebook.command_mode();
134 134 return false;
135 135 }
136 136 },
137 137 'ctrl+m' : {
138 138 help : 'command mode',
139 139 handler : function (event) {
140 140 IPython.notebook.command_mode();
141 141 return false;
142 142 }
143 143 },
144 144 'up' : {
145 145 help : 'select previous cell',
146 146 handler : function (event) {
147 147 var cell = IPython.notebook.get_selected_cell();
148 148 if (cell && cell.at_top()) {
149 149 event.preventDefault();
150 150 IPython.notebook.command_mode()
151 151 IPython.notebook.select_prev();
152 152 IPython.notebook.edit_mode();
153 153 return false;
154 154 };
155 155 }
156 156 },
157 157 'down' : {
158 158 help : 'select next cell',
159 159 handler : function (event) {
160 160 var cell = IPython.notebook.get_selected_cell();
161 161 if (cell && cell.at_bottom()) {
162 162 event.preventDefault();
163 163 IPython.notebook.command_mode()
164 164 IPython.notebook.select_next();
165 165 IPython.notebook.edit_mode();
166 166 return false;
167 167 };
168 168 }
169 169 },
170 'alt+-' : {
171 help : 'split cell',
172 handler : function (event) {
173 IPython.notebook.split_cell();
174 return false;
175 }
176 },
170 177 }
171 178
172 179 // Command mode defaults
173 180
174 181 var default_command_shortcuts = {
175 182 'enter' : {
176 183 help : 'edit mode',
177 184 handler : function (event) {
178 185 IPython.notebook.edit_mode();
179 186 return false;
180 187 }
181 188 },
182 189 'up' : {
183 190 help : 'select previous cell',
184 191 handler : function (event) {
185 192 var index = IPython.notebook.get_selected_index();
186 193 if (index !== 0 && index !== null) {
187 194 IPython.notebook.select_prev();
188 195 var cell = IPython.notebook.get_selected_cell();
189 196 cell.focus_cell();
190 197 };
191 198 return false;
192 199 }
193 200 },
194 201 'down' : {
195 202 help : 'select next cell',
196 203 handler : function (event) {
197 204 var index = IPython.notebook.get_selected_index();
198 205 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
199 206 IPython.notebook.select_next();
200 207 var cell = IPython.notebook.get_selected_cell();
201 208 cell.focus_cell();
202 209 };
203 210 return false;
204 211 }
205 212 },
206 213 'k' : {
207 214 help : 'select previous cell',
208 215 handler : function (event) {
209 216 var index = IPython.notebook.get_selected_index();
210 217 if (index !== 0 && index !== null) {
211 218 IPython.notebook.select_prev();
212 219 var cell = IPython.notebook.get_selected_cell();
213 220 cell.focus_cell();
214 221 };
215 222 return false;
216 223 }
217 224 },
218 225 'j' : {
219 226 help : 'select next cell',
220 227 handler : function (event) {
221 228 var index = IPython.notebook.get_selected_index();
222 229 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
223 230 IPython.notebook.select_next();
224 231 var cell = IPython.notebook.get_selected_cell();
225 232 cell.focus_cell();
226 233 };
227 234 return false;
228 235 }
229 236 },
230 237 'x' : {
231 238 help : 'cut cell',
232 239 handler : function (event) {
233 240 IPython.notebook.cut_cell();
234 241 return false;
235 242 }
236 243 },
237 244 'c' : {
238 245 help : 'copy cell',
239 246 handler : function (event) {
240 247 IPython.notebook.copy_cell();
241 248 return false;
242 249 }
243 250 },
244 251 'v' : {
245 252 help : 'paste cell below',
246 253 handler : function (event) {
247 254 IPython.notebook.paste_cell_below();
248 255 return false;
249 256 }
250 257 },
251 258 'd' : {
252 259 help : 'delete cell (press twice)',
253 260 handler : function (event) {
254 261 var dc = IPython.delete_count;
255 262 if (dc === undefined) {
256 263 IPython.delete_count = 1;
257 264 } else if (dc === 0) {
258 265 IPython.delete_count = 1;
259 266 setTimeout(function () {
260 267 IPython.delete_count = 0;
261 268 }, 800);
262 269 } else if (dc === 1) {
263 270 IPython.notebook.delete_cell();
264 271 IPython.delete_count = 0;
265 272 }
266 273 return false;
267 274 }
268 275 },
269 276 'a' : {
270 277 help : 'insert cell above',
271 278 handler : function (event) {
272 279 IPython.notebook.insert_cell_above('code');
273 280 IPython.notebook.select_prev();
274 281 return false;
275 282 }
276 283 },
277 284 'b' : {
278 285 help : 'insert cell below',
279 286 handler : function (event) {
280 287 IPython.notebook.insert_cell_below('code');
281 288 IPython.notebook.select_next();
282 289 return false;
283 290 }
284 291 },
285 292 'y' : {
286 293 help : 'to code',
287 294 handler : function (event) {
288 295 IPython.notebook.to_code();
289 296 return false;
290 297 }
291 298 },
292 299 'm' : {
293 300 help : 'to markdown',
294 301 handler : function (event) {
295 302 IPython.notebook.to_markdown();
296 303 return false;
297 304 }
298 305 },
299 306 't' : {
300 307 help : 'to raw',
301 308 handler : function (event) {
302 309 IPython.notebook.to_raw();
303 310 return false;
304 311 }
305 312 },
306 313 '1' : {
307 314 help : 'to heading 1',
308 315 handler : function (event) {
309 316 IPython.notebook.to_heading(undefined, 1);
310 317 return false;
311 318 }
312 319 },
313 320 '2' : {
314 321 help : 'to heading 2',
315 322 handler : function (event) {
316 323 IPython.notebook.to_heading(undefined, 2);
317 324 return false;
318 325 }
319 326 },
320 327 '3' : {
321 328 help : 'to heading 3',
322 329 handler : function (event) {
323 330 IPython.notebook.to_heading(undefined, 3);
324 331 return false;
325 332 }
326 333 },
327 334 '4' : {
328 335 help : 'to heading 4',
329 336 handler : function (event) {
330 337 IPython.notebook.to_heading(undefined, 4);
331 338 return false;
332 339 }
333 340 },
334 341 '5' : {
335 342 help : 'to heading 5',
336 343 handler : function (event) {
337 344 IPython.notebook.to_heading(undefined, 5);
338 345 return false;
339 346 }
340 347 },
341 348 '6' : {
342 349 help : 'to heading 6',
343 350 handler : function (event) {
344 351 IPython.notebook.to_heading(undefined, 6);
345 352 return false;
346 353 }
347 354 },
348 355 'o' : {
349 356 help : 'toggle output',
350 357 handler : function (event) {
351 358 IPython.notebook.toggle_output();
352 359 return false;
353 360 }
354 361 },
355 362 'shift+o' : {
356 363 help : 'toggle output',
357 364 handler : function (event) {
358 365 IPython.notebook.toggle_output_scroll();
359 366 return false;
360 367 }
361 368 },
362 369 's' : {
363 370 help : 'save notebook',
364 371 handler : function (event) {
365 372 IPython.notebook.save_checkpoint();
366 373 return false;
367 374 }
368 375 },
369 376 'ctrl+j' : {
370 377 help : 'move cell down',
371 378 handler : function (event) {
372 379 IPython.notebook.move_cell_down();
373 380 return false;
374 381 }
375 382 },
376 383 'ctrl+k' : {
377 384 help : 'move cell up',
378 385 handler : function (event) {
379 386 IPython.notebook.move_cell_up();
380 387 return false;
381 388 }
382 389 },
383 390 'l' : {
384 391 help : 'toggle line numbers',
385 392 handler : function (event) {
386 393 IPython.notebook.cell_toggle_line_numbers();
387 394 return false;
388 395 }
389 396 },
390 397 'i' : {
391 398 help : 'interrupt kernel',
392 399 handler : function (event) {
393 400 IPython.notebook.kernel.interrupt();
394 401 return false;
395 402 }
396 403 },
397 404 '.' : {
398 405 help : 'restart kernel',
399 406 handler : function (event) {
400 407 IPython.notebook.restart_kernel();
401 408 return false;
402 409 }
403 410 },
404 411 'h' : {
405 412 help : 'keyboard shortcuts',
406 413 handler : function (event) {
407 414 IPython.quick_help.show_keyboard_shortcuts();
408 415 return false;
409 416 }
410 417 },
411 418 'z' : {
412 419 help : 'undo last delete',
413 420 handler : function (event) {
414 421 IPython.notebook.undelete_cell();
415 422 return false;
416 423 }
417 424 },
418 425 '-' : {
419 426 help : 'split cell',
420 427 handler : function (event) {
421 428 IPython.notebook.split_cell();
422 429 return false;
423 430 }
424 431 },
425 432 'shift+=' : {
426 433 help : 'merge cell below',
427 434 handler : function (event) {
428 435 IPython.notebook.merge_cell_below();
429 436 return false;
430 437 }
431 438 },
432 439 }
433 440
434 441
435 442 // Shortcut manager class
436 443
437 444 var ShortcutManager = function () {
438 445 this._shortcuts = {}
439 446 }
440 447
441 448 ShortcutManager.prototype.help = function () {
442 449 var help = [];
443 450 for (var shortcut in this._shortcuts) {
444 451 help.push({shortcut: shortcut, help: this._shortcuts[shortcut]['help']});
445 452 }
446 453 return help;
447 454 }
448 455
449 456 ShortcutManager.prototype.canonicalize_key = function (key) {
450 457 return inv_keycodes[keycodes[key]];
451 458 }
452 459
453 460 ShortcutManager.prototype.canonicalize_shortcut = function (shortcut) {
454 461 // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift
455 462 var values = shortcut.split("+");
456 463 if (values.length === 1) {
457 464 return this.canonicalize_key(values[0])
458 465 } else {
459 466 var modifiers = values.slice(0,-1);
460 467 var key = this.canonicalize_key(values[values.length-1]);
461 468 modifiers.sort();
462 469 return modifiers.join('+') + '+' + key;
463 470 }
464 471 }
465 472
466 473 ShortcutManager.prototype.event_to_shortcut = function (event) {
467 474 // Convert a jQuery keyboard event to a strong based keyboard shortcut
468 475 var shortcut = '';
469 476 var key = inv_keycodes[event.which]
470 477 if (event.altKey && key !== 'alt') {shortcut += 'alt+';}
471 478 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';}
472 479 if (event.metaKey && key !== 'meta') {shortcut += 'meta+';}
473 480 if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';}
474 481 shortcut += key;
475 482 return shortcut
476 483 }
477 484
478 485 ShortcutManager.prototype.clear_shortcuts = function () {
479 486 this._shortcuts = {};
480 487 }
481 488
482 489 ShortcutManager.prototype.add_shortcut = function (shortcut, data) {
483 490 shortcut = this.canonicalize_shortcut(shortcut);
484 491 this._shortcuts[shortcut] = data;
485 492 }
486 493
487 494 ShortcutManager.prototype.add_shortcuts = function (data) {
488 495 for (var shortcut in data) {
489 496 this.add_shortcut(shortcut, data[shortcut]);
490 497 }
491 498 }
492 499
493 500 ShortcutManager.prototype.remove_shortcut = function (shortcut) {
494 501 shortcut = this.canonicalize_shortcut(shortcut);
495 502 delete this._shortcuts[shortcut];
496 503 }
497 504
498 505 ShortcutManager.prototype.call_handler = function (event) {
499 506 var shortcut = this.event_to_shortcut(event);
500 507 var data = this._shortcuts[shortcut];
501 508 if (data !== undefined) {
502 509 var handler = data['handler'];
503 510 if (handler !== undefined) {
504 511 return handler(event);
505 512 }
506 513 }
507 514 return true;
508 515 }
509 516
510 517
511 518
512 519 // Main keyboard manager for the notebook
513 520
514 521 var KeyboardManager = function () {
515 522 this.mode = 'command';
516 523 this.enabled = true;
517 524 this.delete_count = 0;
518 525 this.bind_events();
519 526 this.command_shortcuts = new ShortcutManager();
520 527 this.command_shortcuts.add_shortcuts(default_common_shortcuts);
521 528 this.command_shortcuts.add_shortcuts(default_command_shortcuts);
522 529 this.edit_shortcuts = new ShortcutManager();
523 530 this.edit_shortcuts.add_shortcuts(default_common_shortcuts);
524 531 this.edit_shortcuts.add_shortcuts(default_edit_shortcuts);
525 532 };
526 533
527 534 KeyboardManager.prototype.bind_events = function () {
528 535 var that = this;
529 536 $(document).keydown(function (event) {
530 537 return that.handle_keydown(event);
531 538 });
532 539 };
533 540
534 541 KeyboardManager.prototype.handle_keydown = function (event) {
535 542 var notebook = IPython.notebook;
536 543
537 544 console.log('keyboard_manager', this.mode, event.keyCode);
538 545
539 546 if (event.which === keycodes['esc']) {
540 547 // Intercept escape at highest level to avoid closing
541 548 // websocket connection with firefox
542 549 event.preventDefault();
543 550 }
544 551
545 552 if (!this.enabled) {
546 553 if (event.which === keycodes['esc']) {
547 554 // ESC
548 555 notebook.command_mode();
549 556 return false;
550 557 }
551 558 return true;
552 559 }
553 560
554 561 if (this.mode === 'edit') {
555 562 return this.edit_shortcuts.call_handler(event);
556 563 } else if (this.mode === 'command') {
557 564 return this.command_shortcuts.call_handler(event);
558 565 }
559 566 return true;
560 567 }
561 568
562 569 KeyboardManager.prototype.edit_mode = function () {
563 570 console.log('KeyboardManager', 'changing to edit mode');
564 571 this.last_mode = this.mode;
565 572 this.mode = 'edit';
566 573 }
567 574
568 575 KeyboardManager.prototype.command_mode = function () {
569 576 console.log('KeyboardManager', 'changing to command mode');
570 577 this.last_mode = this.mode;
571 578 this.mode = 'command';
572 579 }
573 580
574 581 KeyboardManager.prototype.enable = function () {
575 582 this.enabled = true;
576 583 }
577 584
578 585 KeyboardManager.prototype.disable = function () {
579 586 this.enabled = false;
580 587 }
581 588
582 589 KeyboardManager.prototype.register_events = function (e) {
583 590 var that = this;
584 591 e.on('focusin', function () {
585 592 that.command_mode();
586 593 that.disable();
587 594 });
588 595 e.on('focusout', function () {
589 596 that.command_mode();
590 597 that.enable();
591 598 });
592 599 // There are times (raw_input) where we remove the element from the DOM before
593 600 // focusout is called. In this case we bind to the remove event of jQueryUI,
594 601 // which gets triggered upon removal.
595 602 e.on('remove', function () {
596 603 that.command_mode();
597 604 that.enable();
598 605 });
599 606 }
600 607
601 608
602 609 IPython.keycodes = keycodes;
603 610 IPython.inv_keycodes = inv_keycodes;
604 611 IPython.default_common_shortcuts = default_common_shortcuts;
605 612 IPython.default_edit_shortcuts = default_edit_shortcuts;
606 613 IPython.default_command_shortcuts = default_command_shortcuts;
607 614 IPython.ShortcutManager = ShortcutManager;
608 615 IPython.KeyboardManager = KeyboardManager;
609 616
610 617 return IPython;
611 618
612 619 }(IPython));
@@ -1,2183 +1,2187
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Notebook
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13 "use strict";
14 14
15 15 var utils = IPython.utils;
16 16
17 17 /**
18 18 * A notebook contains and manages cells.
19 19 *
20 20 * @class Notebook
21 21 * @constructor
22 22 * @param {String} selector A jQuery selector for the notebook's DOM element
23 23 * @param {Object} [options] A config object
24 24 */
25 25 var Notebook = function (selector, options) {
26 26 var options = options || {};
27 27 this._baseProjectUrl = options.baseProjectUrl;
28 28 this.notebook_path = options.notebookPath;
29 29 this.notebook_name = options.notebookName;
30 30 this.element = $(selector);
31 31 this.element.scroll();
32 32 this.element.data("notebook", this);
33 33 this.next_prompt_number = 1;
34 34 this.session = null;
35 35 this.kernel = null;
36 36 this.clipboard = null;
37 37 this.undelete_backup = null;
38 38 this.undelete_index = null;
39 39 this.undelete_below = false;
40 40 this.paste_enabled = false;
41 41 // It is important to start out in command mode to match the intial mode
42 42 // of the KeyboardManager.
43 43 this.mode = 'command';
44 44 this.set_dirty(false);
45 45 this.metadata = {};
46 46 this._checkpoint_after_save = false;
47 47 this.last_checkpoint = null;
48 48 this.checkpoints = [];
49 49 this.autosave_interval = 0;
50 50 this.autosave_timer = null;
51 51 // autosave *at most* every two minutes
52 52 this.minimum_autosave_interval = 120000;
53 53 // single worksheet for now
54 54 this.worksheet_metadata = {};
55 55 this.notebook_name_blacklist_re = /[\/\\:]/;
56 56 this.nbformat = 3 // Increment this when changing the nbformat
57 57 this.nbformat_minor = 0 // Increment this when changing the nbformat
58 58 this.style();
59 59 this.create_elements();
60 60 this.bind_events();
61 61 };
62 62
63 63 /**
64 64 * Tweak the notebook's CSS style.
65 65 *
66 66 * @method style
67 67 */
68 68 Notebook.prototype.style = function () {
69 69 $('div#notebook').addClass('border-box-sizing');
70 70 };
71 71
72 72 /**
73 73 * Get the root URL of the notebook server.
74 74 *
75 75 * @method baseProjectUrl
76 76 * @return {String} The base project URL
77 77 */
78 78 Notebook.prototype.baseProjectUrl = function() {
79 79 return this._baseProjectUrl || $('body').data('baseProjectUrl');
80 80 };
81 81
82 82 Notebook.prototype.notebookName = function() {
83 83 return $('body').data('notebookName');
84 84 };
85 85
86 86 Notebook.prototype.notebookPath = function() {
87 87 return $('body').data('notebookPath');
88 88 };
89 89
90 90 /**
91 91 * Create an HTML and CSS representation of the notebook.
92 92 *
93 93 * @method create_elements
94 94 */
95 95 Notebook.prototype.create_elements = function () {
96 96 var that = this;
97 97 this.element.attr('tabindex','-1');
98 98 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
99 99 // We add this end_space div to the end of the notebook div to:
100 100 // i) provide a margin between the last cell and the end of the notebook
101 101 // ii) to prevent the div from scrolling up when the last cell is being
102 102 // edited, but is too low on the page, which browsers will do automatically.
103 103 var end_space = $('<div/>').addClass('end_space');
104 104 end_space.dblclick(function (e) {
105 105 var ncells = that.ncells();
106 106 that.insert_cell_below('code',ncells-1);
107 107 });
108 108 this.element.append(this.container);
109 109 this.container.append(end_space);
110 110 };
111 111
112 112 /**
113 113 * Bind JavaScript events: key presses and custom IPython events.
114 114 *
115 115 * @method bind_events
116 116 */
117 117 Notebook.prototype.bind_events = function () {
118 118 var that = this;
119 119
120 120 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
121 121 var index = that.find_cell_index(data.cell);
122 122 var new_cell = that.insert_cell_below('code',index);
123 123 new_cell.set_text(data.text);
124 124 that.dirty = true;
125 125 });
126 126
127 127 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
128 128 that.dirty = data.value;
129 129 });
130 130
131 131 $([IPython.events]).on('select.Cell', function (event, data) {
132 132 var index = that.find_cell_index(data.cell);
133 133 that.select(index);
134 134 });
135 135
136 136 $([IPython.events]).on('edit_mode.Cell', function (event, data) {
137 137 var index = that.find_cell_index(data.cell);
138 138 that.select(index);
139 139 that.edit_mode();
140 140 });
141 141
142 142 $([IPython.events]).on('command_mode.Cell', function (event, data) {
143 143 that.command_mode();
144 144 });
145 145
146 146 $([IPython.events]).on('status_autorestarting.Kernel', function () {
147 147 IPython.dialog.modal({
148 148 title: "Kernel Restarting",
149 149 body: "The kernel appears to have died. It will restart automatically.",
150 150 buttons: {
151 151 OK : {
152 152 class : "btn-primary"
153 153 }
154 154 }
155 155 });
156 156 });
157 157
158 158 var collapse_time = function (time) {
159 159 var app_height = $('#ipython-main-app').height(); // content height
160 160 var splitter_height = $('div#pager_splitter').outerHeight(true);
161 161 var new_height = app_height - splitter_height;
162 162 that.element.animate({height : new_height + 'px'}, time);
163 163 };
164 164
165 165 this.element.bind('collapse_pager', function (event, extrap) {
166 166 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
167 167 collapse_time(time);
168 168 });
169 169
170 170 var expand_time = function (time) {
171 171 var app_height = $('#ipython-main-app').height(); // content height
172 172 var splitter_height = $('div#pager_splitter').outerHeight(true);
173 173 var pager_height = $('div#pager').outerHeight(true);
174 174 var new_height = app_height - pager_height - splitter_height;
175 175 that.element.animate({height : new_height + 'px'}, time);
176 176 };
177 177
178 178 this.element.bind('expand_pager', function (event, extrap) {
179 179 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
180 180 expand_time(time);
181 181 });
182 182
183 183 // Firefox 22 broke $(window).on("beforeunload")
184 184 // I'm not sure why or how.
185 185 window.onbeforeunload = function (e) {
186 186 // TODO: Make killing the kernel configurable.
187 187 var kill_kernel = false;
188 188 if (kill_kernel) {
189 189 that.session.kill_kernel();
190 190 }
191 191 // if we are autosaving, trigger an autosave on nav-away.
192 192 // still warn, because if we don't the autosave may fail.
193 193 if (that.dirty) {
194 194 if ( that.autosave_interval ) {
195 195 // schedule autosave in a timeout
196 196 // this gives you a chance to forcefully discard changes
197 197 // by reloading the page if you *really* want to.
198 198 // the timer doesn't start until you *dismiss* the dialog.
199 199 setTimeout(function () {
200 200 if (that.dirty) {
201 201 that.save_notebook();
202 202 }
203 203 }, 1000);
204 204 return "Autosave in progress, latest changes may be lost.";
205 205 } else {
206 206 return "Unsaved changes will be lost.";
207 207 }
208 208 };
209 209 // Null is the *only* return value that will make the browser not
210 210 // pop up the "don't leave" dialog.
211 211 return null;
212 212 };
213 213 };
214 214
215 215 /**
216 216 * Set the dirty flag, and trigger the set_dirty.Notebook event
217 217 *
218 218 * @method set_dirty
219 219 */
220 220 Notebook.prototype.set_dirty = function (value) {
221 221 if (value === undefined) {
222 222 value = true;
223 223 }
224 224 if (this.dirty == value) {
225 225 return;
226 226 }
227 227 $([IPython.events]).trigger('set_dirty.Notebook', {value: value});
228 228 };
229 229
230 230 /**
231 231 * Scroll the top of the page to a given cell.
232 232 *
233 233 * @method scroll_to_cell
234 234 * @param {Number} cell_number An index of the cell to view
235 235 * @param {Number} time Animation time in milliseconds
236 236 * @return {Number} Pixel offset from the top of the container
237 237 */
238 238 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
239 239 var cells = this.get_cells();
240 240 var time = time || 0;
241 241 cell_number = Math.min(cells.length-1,cell_number);
242 242 cell_number = Math.max(0 ,cell_number);
243 243 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
244 244 this.element.animate({scrollTop:scroll_value}, time);
245 245 return scroll_value;
246 246 };
247 247
248 248 /**
249 249 * Scroll to the bottom of the page.
250 250 *
251 251 * @method scroll_to_bottom
252 252 */
253 253 Notebook.prototype.scroll_to_bottom = function () {
254 254 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
255 255 };
256 256
257 257 /**
258 258 * Scroll to the top of the page.
259 259 *
260 260 * @method scroll_to_top
261 261 */
262 262 Notebook.prototype.scroll_to_top = function () {
263 263 this.element.animate({scrollTop:0}, 0);
264 264 };
265 265
266 266 // Edit Notebook metadata
267 267
268 268 Notebook.prototype.edit_metadata = function () {
269 269 var that = this;
270 270 IPython.dialog.edit_metadata(this.metadata, function (md) {
271 271 that.metadata = md;
272 272 }, 'Notebook');
273 273 };
274 274
275 275 // Cell indexing, retrieval, etc.
276 276
277 277 /**
278 278 * Get all cell elements in the notebook.
279 279 *
280 280 * @method get_cell_elements
281 281 * @return {jQuery} A selector of all cell elements
282 282 */
283 283 Notebook.prototype.get_cell_elements = function () {
284 284 return this.container.children("div.cell");
285 285 };
286 286
287 287 /**
288 288 * Get a particular cell element.
289 289 *
290 290 * @method get_cell_element
291 291 * @param {Number} index An index of a cell to select
292 292 * @return {jQuery} A selector of the given cell.
293 293 */
294 294 Notebook.prototype.get_cell_element = function (index) {
295 295 var result = null;
296 296 var e = this.get_cell_elements().eq(index);
297 297 if (e.length !== 0) {
298 298 result = e;
299 299 }
300 300 return result;
301 301 };
302 302
303 303 /**
304 304 * Count the cells in this notebook.
305 305 *
306 306 * @method ncells
307 307 * @return {Number} The number of cells in this notebook
308 308 */
309 309 Notebook.prototype.ncells = function () {
310 310 return this.get_cell_elements().length;
311 311 };
312 312
313 313 /**
314 314 * Get all Cell objects in this notebook.
315 315 *
316 316 * @method get_cells
317 317 * @return {Array} This notebook's Cell objects
318 318 */
319 319 // TODO: we are often calling cells as cells()[i], which we should optimize
320 320 // to cells(i) or a new method.
321 321 Notebook.prototype.get_cells = function () {
322 322 return this.get_cell_elements().toArray().map(function (e) {
323 323 return $(e).data("cell");
324 324 });
325 325 };
326 326
327 327 /**
328 328 * Get a Cell object from this notebook.
329 329 *
330 330 * @method get_cell
331 331 * @param {Number} index An index of a cell to retrieve
332 332 * @return {Cell} A particular cell
333 333 */
334 334 Notebook.prototype.get_cell = function (index) {
335 335 var result = null;
336 336 var ce = this.get_cell_element(index);
337 337 if (ce !== null) {
338 338 result = ce.data('cell');
339 339 }
340 340 return result;
341 341 }
342 342
343 343 /**
344 344 * Get the cell below a given cell.
345 345 *
346 346 * @method get_next_cell
347 347 * @param {Cell} cell The provided cell
348 348 * @return {Cell} The next cell
349 349 */
350 350 Notebook.prototype.get_next_cell = function (cell) {
351 351 var result = null;
352 352 var index = this.find_cell_index(cell);
353 353 if (this.is_valid_cell_index(index+1)) {
354 354 result = this.get_cell(index+1);
355 355 }
356 356 return result;
357 357 }
358 358
359 359 /**
360 360 * Get the cell above a given cell.
361 361 *
362 362 * @method get_prev_cell
363 363 * @param {Cell} cell The provided cell
364 364 * @return {Cell} The previous cell
365 365 */
366 366 Notebook.prototype.get_prev_cell = function (cell) {
367 367 // TODO: off-by-one
368 368 // nb.get_prev_cell(nb.get_cell(1)) is null
369 369 var result = null;
370 370 var index = this.find_cell_index(cell);
371 371 if (index !== null && index > 1) {
372 372 result = this.get_cell(index-1);
373 373 }
374 374 return result;
375 375 }
376 376
377 377 /**
378 378 * Get the numeric index of a given cell.
379 379 *
380 380 * @method find_cell_index
381 381 * @param {Cell} cell The provided cell
382 382 * @return {Number} The cell's numeric index
383 383 */
384 384 Notebook.prototype.find_cell_index = function (cell) {
385 385 var result = null;
386 386 this.get_cell_elements().filter(function (index) {
387 387 if ($(this).data("cell") === cell) {
388 388 result = index;
389 389 };
390 390 });
391 391 return result;
392 392 };
393 393
394 394 /**
395 395 * Get a given index , or the selected index if none is provided.
396 396 *
397 397 * @method index_or_selected
398 398 * @param {Number} index A cell's index
399 399 * @return {Number} The given index, or selected index if none is provided.
400 400 */
401 401 Notebook.prototype.index_or_selected = function (index) {
402 402 var i;
403 403 if (index === undefined || index === null) {
404 404 i = this.get_selected_index();
405 405 if (i === null) {
406 406 i = 0;
407 407 }
408 408 } else {
409 409 i = index;
410 410 }
411 411 return i;
412 412 };
413 413
414 414 /**
415 415 * Get the currently selected cell.
416 416 * @method get_selected_cell
417 417 * @return {Cell} The selected cell
418 418 */
419 419 Notebook.prototype.get_selected_cell = function () {
420 420 var index = this.get_selected_index();
421 421 return this.get_cell(index);
422 422 };
423 423
424 424 /**
425 425 * Check whether a cell index is valid.
426 426 *
427 427 * @method is_valid_cell_index
428 428 * @param {Number} index A cell index
429 429 * @return True if the index is valid, false otherwise
430 430 */
431 431 Notebook.prototype.is_valid_cell_index = function (index) {
432 432 if (index !== null && index >= 0 && index < this.ncells()) {
433 433 return true;
434 434 } else {
435 435 return false;
436 436 };
437 437 }
438 438
439 439 /**
440 440 * Get the index of the currently selected cell.
441 441
442 442 * @method get_selected_index
443 443 * @return {Number} The selected cell's numeric index
444 444 */
445 445 Notebook.prototype.get_selected_index = function () {
446 446 var result = null;
447 447 this.get_cell_elements().filter(function (index) {
448 448 if ($(this).data("cell").selected === true) {
449 449 result = index;
450 450 };
451 451 });
452 452 return result;
453 453 };
454 454
455 455
456 456 // Cell selection.
457 457
458 458 /**
459 459 * Programmatically select a cell.
460 460 *
461 461 * @method select
462 462 * @param {Number} index A cell's index
463 463 * @return {Notebook} This notebook
464 464 */
465 465 Notebook.prototype.select = function (index) {
466 466 if (this.is_valid_cell_index(index)) {
467 467 var sindex = this.get_selected_index()
468 468 if (sindex !== null && index !== sindex) {
469 469 this.command_mode();
470 470 this.get_cell(sindex).unselect();
471 471 };
472 472 var cell = this.get_cell(index);
473 473 cell.select();
474 474 if (cell.cell_type === 'heading') {
475 475 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
476 476 {'cell_type':cell.cell_type,level:cell.level}
477 477 );
478 478 } else {
479 479 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
480 480 {'cell_type':cell.cell_type}
481 481 );
482 482 };
483 483 };
484 484 return this;
485 485 };
486 486
487 487 /**
488 488 * Programmatically select the next cell.
489 489 *
490 490 * @method select_next
491 491 * @return {Notebook} This notebook
492 492 */
493 493 Notebook.prototype.select_next = function () {
494 494 var index = this.get_selected_index();
495 495 this.select(index+1);
496 496 return this;
497 497 };
498 498
499 499 /**
500 500 * Programmatically select the previous cell.
501 501 *
502 502 * @method select_prev
503 503 * @return {Notebook} This notebook
504 504 */
505 505 Notebook.prototype.select_prev = function () {
506 506 var index = this.get_selected_index();
507 507 this.select(index-1);
508 508 return this;
509 509 };
510 510
511 511
512 512 // Edit/Command mode
513 513
514 514 Notebook.prototype.get_edit_index = function () {
515 515 var result = null;
516 516 this.get_cell_elements().filter(function (index) {
517 517 if ($(this).data("cell").mode === 'edit') {
518 518 result = index;
519 519 };
520 520 });
521 521 return result;
522 522 };
523 523
524 524 Notebook.prototype.command_mode = function () {
525 525 if (this.mode !== 'command') {
526 526 console.log('\nNotebook', 'changing to command mode');
527 527 var index = this.get_edit_index();
528 528 var cell = this.get_cell(index);
529 529 if (cell) {
530 530 cell.command_mode();
531 531 };
532 532 this.mode = 'command';
533 533 IPython.keyboard_manager.command_mode();
534 534 };
535 535 };
536 536
537 537 Notebook.prototype.edit_mode = function () {
538 538 if (this.mode !== 'edit') {
539 539 console.log('\nNotebook', 'changing to edit mode');
540 540 var cell = this.get_selected_cell();
541 541 if (cell === null) {return;} // No cell is selected
542 542 // We need to set the mode to edit to prevent reentering this method
543 543 // when cell.edit_mode() is called below.
544 544 this.mode = 'edit';
545 545 IPython.keyboard_manager.edit_mode();
546 546 cell.edit_mode();
547 547 };
548 548 };
549 549
550 550
551 551 // Cell movement
552 552
553 553 /**
554 554 * Move given (or selected) cell up and select it.
555 555 *
556 556 * @method move_cell_up
557 557 * @param [index] {integer} cell index
558 558 * @return {Notebook} This notebook
559 559 **/
560 560 Notebook.prototype.move_cell_up = function (index) {
561 561 var i = this.index_or_selected(index);
562 562 if (this.is_valid_cell_index(i) && i > 0) {
563 563 var pivot = this.get_cell_element(i-1);
564 564 var tomove = this.get_cell_element(i);
565 565 if (pivot !== null && tomove !== null) {
566 566 tomove.detach();
567 567 pivot.before(tomove);
568 568 this.select(i-1);
569 569 var cell = this.get_selected_cell();
570 570 cell.focus_cell();
571 571 };
572 572 this.set_dirty(true);
573 573 };
574 574 return this;
575 575 };
576 576
577 577
578 578 /**
579 579 * Move given (or selected) cell down and select it
580 580 *
581 581 * @method move_cell_down
582 582 * @param [index] {integer} cell index
583 583 * @return {Notebook} This notebook
584 584 **/
585 585 Notebook.prototype.move_cell_down = function (index) {
586 586 var i = this.index_or_selected(index);
587 587 if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
588 588 var pivot = this.get_cell_element(i+1);
589 589 var tomove = this.get_cell_element(i);
590 590 if (pivot !== null && tomove !== null) {
591 591 tomove.detach();
592 592 pivot.after(tomove);
593 593 this.select(i+1);
594 594 var cell = this.get_selected_cell();
595 595 cell.focus_cell();
596 596 };
597 597 };
598 598 this.set_dirty();
599 599 return this;
600 600 };
601 601
602 602
603 603 // Insertion, deletion.
604 604
605 605 /**
606 606 * Delete a cell from the notebook.
607 607 *
608 608 * @method delete_cell
609 609 * @param [index] A cell's numeric index
610 610 * @return {Notebook} This notebook
611 611 */
612 612 Notebook.prototype.delete_cell = function (index) {
613 613 var i = this.index_or_selected(index);
614 614 var cell = this.get_selected_cell();
615 615 this.undelete_backup = cell.toJSON();
616 616 $('#undelete_cell').removeClass('disabled');
617 617 if (this.is_valid_cell_index(i)) {
618 618 var old_ncells = this.ncells();
619 619 var ce = this.get_cell_element(i);
620 620 ce.remove();
621 621 if (i === 0) {
622 622 // Always make sure we have at least one cell.
623 623 if (old_ncells === 1) {
624 624 this.insert_cell_below('code');
625 625 }
626 626 this.select(0);
627 627 this.undelete_index = 0;
628 628 this.undelete_below = false;
629 629 } else if (i === old_ncells-1 && i !== 0) {
630 630 this.select(i-1);
631 631 this.undelete_index = i - 1;
632 632 this.undelete_below = true;
633 633 } else {
634 634 this.select(i);
635 635 this.undelete_index = i;
636 636 this.undelete_below = false;
637 637 };
638 638 $([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i});
639 639 this.set_dirty(true);
640 640 };
641 641 return this;
642 642 };
643 643
644 644 /**
645 645 * Restore the most recently deleted cell.
646 646 *
647 647 * @method undelete
648 648 */
649 649 Notebook.prototype.undelete_cell = function() {
650 650 if (this.undelete_backup !== null && this.undelete_index !== null) {
651 651 var current_index = this.get_selected_index();
652 652 if (this.undelete_index < current_index) {
653 653 current_index = current_index + 1;
654 654 }
655 655 if (this.undelete_index >= this.ncells()) {
656 656 this.select(this.ncells() - 1);
657 657 }
658 658 else {
659 659 this.select(this.undelete_index);
660 660 }
661 661 var cell_data = this.undelete_backup;
662 662 var new_cell = null;
663 663 if (this.undelete_below) {
664 664 new_cell = this.insert_cell_below(cell_data.cell_type);
665 665 } else {
666 666 new_cell = this.insert_cell_above(cell_data.cell_type);
667 667 }
668 668 new_cell.fromJSON(cell_data);
669 669 if (this.undelete_below) {
670 670 this.select(current_index+1);
671 671 } else {
672 672 this.select(current_index);
673 673 }
674 674 this.undelete_backup = null;
675 675 this.undelete_index = null;
676 676 }
677 677 $('#undelete_cell').addClass('disabled');
678 678 }
679 679
680 680 /**
681 681 * Insert a cell so that after insertion the cell is at given index.
682 682 *
683 683 * Similar to insert_above, but index parameter is mandatory
684 684 *
685 685 * Index will be brought back into the accissible range [0,n]
686 686 *
687 687 * @method insert_cell_at_index
688 688 * @param type {string} in ['code','markdown','heading']
689 689 * @param [index] {int} a valid index where to inser cell
690 690 *
691 691 * @return cell {cell|null} created cell or null
692 692 **/
693 693 Notebook.prototype.insert_cell_at_index = function(type, index){
694 694
695 695 var ncells = this.ncells();
696 696 var index = Math.min(index,ncells);
697 697 index = Math.max(index,0);
698 698 var cell = null;
699 699
700 700 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
701 701 if (type === 'code') {
702 702 cell = new IPython.CodeCell(this.kernel);
703 703 cell.set_input_prompt();
704 704 } else if (type === 'markdown') {
705 705 cell = new IPython.MarkdownCell();
706 706 } else if (type === 'raw') {
707 707 cell = new IPython.RawCell();
708 708 } else if (type === 'heading') {
709 709 cell = new IPython.HeadingCell();
710 710 }
711 711
712 712 if(this._insert_element_at_index(cell.element,index)) {
713 713 cell.render();
714 714 $([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index});
715 715 cell.refresh();
716 716 // We used to select the cell after we refresh it, but there
717 717 // are now cases were this method is called where select is
718 718 // not appropriate. The selection logic should be handled by the
719 719 // caller of the the top level insert_cell methods.
720 720 this.set_dirty(true);
721 721 }
722 722 }
723 723 return cell;
724 724
725 725 };
726 726
727 727 /**
728 728 * Insert an element at given cell index.
729 729 *
730 730 * @method _insert_element_at_index
731 731 * @param element {dom element} a cell element
732 732 * @param [index] {int} a valid index where to inser cell
733 733 * @private
734 734 *
735 735 * return true if everything whent fine.
736 736 **/
737 737 Notebook.prototype._insert_element_at_index = function(element, index){
738 738 if (element === undefined){
739 739 return false;
740 740 }
741 741
742 742 var ncells = this.ncells();
743 743
744 744 if (ncells === 0) {
745 745 // special case append if empty
746 746 this.element.find('div.end_space').before(element);
747 747 } else if ( ncells === index ) {
748 748 // special case append it the end, but not empty
749 749 this.get_cell_element(index-1).after(element);
750 750 } else if (this.is_valid_cell_index(index)) {
751 751 // otherwise always somewhere to append to
752 752 this.get_cell_element(index).before(element);
753 753 } else {
754 754 return false;
755 755 }
756 756
757 757 if (this.undelete_index !== null && index <= this.undelete_index) {
758 758 this.undelete_index = this.undelete_index + 1;
759 759 this.set_dirty(true);
760 760 }
761 761 return true;
762 762 };
763 763
764 764 /**
765 765 * Insert a cell of given type above given index, or at top
766 766 * of notebook if index smaller than 0.
767 767 *
768 768 * default index value is the one of currently selected cell
769 769 *
770 770 * @method insert_cell_above
771 771 * @param type {string} cell type
772 772 * @param [index] {integer}
773 773 *
774 774 * @return handle to created cell or null
775 775 **/
776 776 Notebook.prototype.insert_cell_above = function (type, index) {
777 777 index = this.index_or_selected(index);
778 778 return this.insert_cell_at_index(type, index);
779 779 };
780 780
781 781 /**
782 782 * Insert a cell of given type below given index, or at bottom
783 783 * of notebook if index greater thatn number of cell
784 784 *
785 785 * default index value is the one of currently selected cell
786 786 *
787 787 * @method insert_cell_below
788 788 * @param type {string} cell type
789 789 * @param [index] {integer}
790 790 *
791 791 * @return handle to created cell or null
792 792 *
793 793 **/
794 794 Notebook.prototype.insert_cell_below = function (type, index) {
795 795 index = this.index_or_selected(index);
796 796 return this.insert_cell_at_index(type, index+1);
797 797 };
798 798
799 799
800 800 /**
801 801 * Insert cell at end of notebook
802 802 *
803 803 * @method insert_cell_at_bottom
804 804 * @param {String} type cell type
805 805 *
806 806 * @return the added cell; or null
807 807 **/
808 808 Notebook.prototype.insert_cell_at_bottom = function (type){
809 809 var len = this.ncells();
810 810 return this.insert_cell_below(type,len-1);
811 811 };
812 812
813 813 /**
814 814 * Turn a cell into a code cell.
815 815 *
816 816 * @method to_code
817 817 * @param {Number} [index] A cell's index
818 818 */
819 819 Notebook.prototype.to_code = function (index) {
820 820 var i = this.index_or_selected(index);
821 821 if (this.is_valid_cell_index(i)) {
822 822 var source_element = this.get_cell_element(i);
823 823 var source_cell = source_element.data("cell");
824 824 if (!(source_cell instanceof IPython.CodeCell)) {
825 825 var target_cell = this.insert_cell_below('code',i);
826 826 var text = source_cell.get_text();
827 827 if (text === source_cell.placeholder) {
828 828 text = '';
829 829 }
830 830 target_cell.set_text(text);
831 831 // make this value the starting point, so that we can only undo
832 832 // to this state, instead of a blank cell
833 833 target_cell.code_mirror.clearHistory();
834 834 source_element.remove();
835 835 this.select(i);
836 836 this.edit_mode();
837 837 this.set_dirty(true);
838 838 };
839 839 };
840 840 };
841 841
842 842 /**
843 843 * Turn a cell into a Markdown cell.
844 844 *
845 845 * @method to_markdown
846 846 * @param {Number} [index] A cell's index
847 847 */
848 848 Notebook.prototype.to_markdown = function (index) {
849 849 var i = this.index_or_selected(index);
850 850 if (this.is_valid_cell_index(i)) {
851 851 var source_element = this.get_cell_element(i);
852 852 var source_cell = source_element.data("cell");
853 853 if (!(source_cell instanceof IPython.MarkdownCell)) {
854 854 var target_cell = this.insert_cell_below('markdown',i);
855 855 var text = source_cell.get_text();
856 856 if (text === source_cell.placeholder) {
857 857 text = '';
858 858 };
859 859 // We must show the editor before setting its contents
860 860 target_cell.unrender();
861 861 target_cell.set_text(text);
862 862 // make this value the starting point, so that we can only undo
863 863 // to this state, instead of a blank cell
864 864 target_cell.code_mirror.clearHistory();
865 865 source_element.remove();
866 866 this.select(i);
867 867 this.edit_mode();
868 868 this.set_dirty(true);
869 869 };
870 870 };
871 871 };
872 872
873 873 /**
874 874 * Turn a cell into a raw text cell.
875 875 *
876 876 * @method to_raw
877 877 * @param {Number} [index] A cell's index
878 878 */
879 879 Notebook.prototype.to_raw = function (index) {
880 880 var i = this.index_or_selected(index);
881 881 if (this.is_valid_cell_index(i)) {
882 882 var source_element = this.get_cell_element(i);
883 883 var source_cell = source_element.data("cell");
884 884 var target_cell = null;
885 885 if (!(source_cell instanceof IPython.RawCell)) {
886 886 target_cell = this.insert_cell_below('raw',i);
887 887 var text = source_cell.get_text();
888 888 if (text === source_cell.placeholder) {
889 889 text = '';
890 890 };
891 891 // We must show the editor before setting its contents
892 892 target_cell.unrender();
893 893 target_cell.set_text(text);
894 894 // make this value the starting point, so that we can only undo
895 895 // to this state, instead of a blank cell
896 896 target_cell.code_mirror.clearHistory();
897 897 source_element.remove();
898 898 this.select(i);
899 899 this.edit_mode();
900 900 this.set_dirty(true);
901 901 };
902 902 };
903 903 };
904 904
905 905 /**
906 906 * Turn a cell into a heading cell.
907 907 *
908 908 * @method to_heading
909 909 * @param {Number} [index] A cell's index
910 910 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
911 911 */
912 912 Notebook.prototype.to_heading = function (index, level) {
913 913 level = level || 1;
914 914 var i = this.index_or_selected(index);
915 915 if (this.is_valid_cell_index(i)) {
916 916 var source_element = this.get_cell_element(i);
917 917 var source_cell = source_element.data("cell");
918 918 var target_cell = null;
919 919 if (source_cell instanceof IPython.HeadingCell) {
920 920 source_cell.set_level(level);
921 921 } else {
922 922 target_cell = this.insert_cell_below('heading',i);
923 923 var text = source_cell.get_text();
924 924 if (text === source_cell.placeholder) {
925 925 text = '';
926 926 };
927 927 // We must show the editor before setting its contents
928 928 target_cell.set_level(level);
929 929 target_cell.unrender();
930 930 target_cell.set_text(text);
931 931 // make this value the starting point, so that we can only undo
932 932 // to this state, instead of a blank cell
933 933 target_cell.code_mirror.clearHistory();
934 934 source_element.remove();
935 935 this.select(i);
936 936 };
937 937 this.edit_mode();
938 938 this.set_dirty(true);
939 939 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
940 940 {'cell_type':'heading',level:level}
941 941 );
942 942 };
943 943 };
944 944
945 945
946 946 // Cut/Copy/Paste
947 947
948 948 /**
949 949 * Enable UI elements for pasting cells.
950 950 *
951 951 * @method enable_paste
952 952 */
953 953 Notebook.prototype.enable_paste = function () {
954 954 var that = this;
955 955 if (!this.paste_enabled) {
956 956 $('#paste_cell_replace').removeClass('disabled')
957 957 .on('click', function () {that.paste_cell_replace();});
958 958 $('#paste_cell_above').removeClass('disabled')
959 959 .on('click', function () {that.paste_cell_above();});
960 960 $('#paste_cell_below').removeClass('disabled')
961 961 .on('click', function () {that.paste_cell_below();});
962 962 this.paste_enabled = true;
963 963 };
964 964 };
965 965
966 966 /**
967 967 * Disable UI elements for pasting cells.
968 968 *
969 969 * @method disable_paste
970 970 */
971 971 Notebook.prototype.disable_paste = function () {
972 972 if (this.paste_enabled) {
973 973 $('#paste_cell_replace').addClass('disabled').off('click');
974 974 $('#paste_cell_above').addClass('disabled').off('click');
975 975 $('#paste_cell_below').addClass('disabled').off('click');
976 976 this.paste_enabled = false;
977 977 };
978 978 };
979 979
980 980 /**
981 981 * Cut a cell.
982 982 *
983 983 * @method cut_cell
984 984 */
985 985 Notebook.prototype.cut_cell = function () {
986 986 this.copy_cell();
987 987 this.delete_cell();
988 988 }
989 989
990 990 /**
991 991 * Copy a cell.
992 992 *
993 993 * @method copy_cell
994 994 */
995 995 Notebook.prototype.copy_cell = function () {
996 996 var cell = this.get_selected_cell();
997 997 this.clipboard = cell.toJSON();
998 998 this.enable_paste();
999 999 };
1000 1000
1001 1001 /**
1002 1002 * Replace the selected cell with a cell in the clipboard.
1003 1003 *
1004 1004 * @method paste_cell_replace
1005 1005 */
1006 1006 Notebook.prototype.paste_cell_replace = function () {
1007 1007 if (this.clipboard !== null && this.paste_enabled) {
1008 1008 var cell_data = this.clipboard;
1009 1009 var new_cell = this.insert_cell_above(cell_data.cell_type);
1010 1010 new_cell.fromJSON(cell_data);
1011 1011 var old_cell = this.get_next_cell(new_cell);
1012 1012 this.delete_cell(this.find_cell_index(old_cell));
1013 1013 this.select(this.find_cell_index(new_cell));
1014 1014 };
1015 1015 };
1016 1016
1017 1017 /**
1018 1018 * Paste a cell from the clipboard above the selected cell.
1019 1019 *
1020 1020 * @method paste_cell_above
1021 1021 */
1022 1022 Notebook.prototype.paste_cell_above = function () {
1023 1023 if (this.clipboard !== null && this.paste_enabled) {
1024 1024 var cell_data = this.clipboard;
1025 1025 var new_cell = this.insert_cell_above(cell_data.cell_type);
1026 1026 new_cell.fromJSON(cell_data);
1027 1027 };
1028 1028 };
1029 1029
1030 1030 /**
1031 1031 * Paste a cell from the clipboard below the selected cell.
1032 1032 *
1033 1033 * @method paste_cell_below
1034 1034 */
1035 1035 Notebook.prototype.paste_cell_below = function () {
1036 1036 if (this.clipboard !== null && this.paste_enabled) {
1037 1037 var cell_data = this.clipboard;
1038 1038 var new_cell = this.insert_cell_below(cell_data.cell_type);
1039 1039 new_cell.fromJSON(cell_data);
1040 1040 };
1041 1041 };
1042 1042
1043 1043 // Split/merge
1044 1044
1045 1045 /**
1046 1046 * Split the selected cell into two, at the cursor.
1047 1047 *
1048 1048 * @method split_cell
1049 1049 */
1050 1050 Notebook.prototype.split_cell = function () {
1051 1051 // Todo: implement spliting for other cell types.
1052 1052 var cell = this.get_selected_cell();
1053 1053 if (cell.is_splittable()) {
1054 1054 var texta = cell.get_pre_cursor();
1055 1055 var textb = cell.get_post_cursor();
1056 var mode = cell.mode;
1056 1057 if (cell instanceof IPython.CodeCell) {
1058 // In this case the operations keep the notebook in its existing mode
1059 // so we don't need to do any post-op mode changes.
1057 1060 cell.set_text(textb);
1058 1061 var new_cell = this.insert_cell_above('code');
1059 1062 new_cell.set_text(texta);
1060 this.select_next();
1061 } else if (cell instanceof IPython.MarkdownCell) {
1062 var render = cell.rendered;
1063 } else if (cell instanceof IPython.MarkdownCell && !cell.rendered) {
1063 1064 cell.set_text(textb);
1064 1065 cell.render();
1065 1066 var new_cell = this.insert_cell_above('markdown');
1066 new_cell.unrender(); // editor must be visible to call set_text
1067 // Editor must be visible to call set_text, so we unrender.
1068 // Note that this call will focus the CM editor, which selects
1069 // this cell and enters edit mode.
1070 new_cell.unrender();
1067 1071 new_cell.set_text(texta);
1068 1072 new_cell.render();
1069 this.select_next();
1070 if (!render) {
1071 1073 // The final rendered state of the split cells should
1072 1074 // match the original cell's state. The order matters
1073 1075 // here as we want the lower cell (cell) to be selected.
1076 // Each of these involves a CM focus and cell select.
1074 1077 new_cell.unrender();
1075 1078 cell.unrender();
1076 }
1079 console.log('setting edit mode...')
1080 this.edit_mode();
1077 1081 }
1078 1082 };
1079 1083 };
1080 1084
1081 1085 /**
1082 1086 * Combine the selected cell into the cell above it.
1083 1087 *
1084 1088 * @method merge_cell_above
1085 1089 */
1086 1090 Notebook.prototype.merge_cell_above = function () {
1087 1091 var index = this.get_selected_index();
1088 1092 var cell = this.get_cell(index);
1089 1093 var render = cell.rendered;
1090 1094 if (!cell.is_mergeable()) {
1091 1095 return;
1092 1096 }
1093 1097 if (index > 0) {
1094 1098 var upper_cell = this.get_cell(index-1);
1095 1099 if (!upper_cell.is_mergeable()) {
1096 1100 return;
1097 1101 }
1098 1102 var upper_text = upper_cell.get_text();
1099 1103 var text = cell.get_text();
1100 1104 if (cell instanceof IPython.CodeCell) {
1101 1105 cell.set_text(upper_text+'\n'+text);
1102 1106 } else if (cell instanceof IPython.MarkdownCell) {
1103 1107 cell.unrender(); // Must unrender before we set_text.
1104 1108 cell.set_text(upper_text+'\n\n'+text);
1105 1109 if (render) {
1106 1110 // The rendered state of the final cell should match
1107 1111 // that of the original selected cell;
1108 1112 cell.render();
1109 1113 }
1110 1114 };
1111 1115 this.delete_cell(index-1);
1112 1116 this.select(this.find_cell_index(cell));
1113 1117 };
1114 1118 };
1115 1119
1116 1120 /**
1117 1121 * Combine the selected cell into the cell below it.
1118 1122 *
1119 1123 * @method merge_cell_below
1120 1124 */
1121 1125 Notebook.prototype.merge_cell_below = function () {
1122 1126 var index = this.get_selected_index();
1123 1127 var cell = this.get_cell(index);
1124 1128 var render = cell.rendered;
1125 1129 if (!cell.is_mergeable()) {
1126 1130 return;
1127 1131 }
1128 1132 if (index < this.ncells()-1) {
1129 1133 var lower_cell = this.get_cell(index+1);
1130 1134 if (!lower_cell.is_mergeable()) {
1131 1135 return;
1132 1136 }
1133 1137 var lower_text = lower_cell.get_text();
1134 1138 var text = cell.get_text();
1135 1139 if (cell instanceof IPython.CodeCell) {
1136 1140 cell.set_text(text+'\n'+lower_text);
1137 1141 } else if (cell instanceof IPython.MarkdownCell) {
1138 1142 cell.unrender(); // Must unrender before we set_text.
1139 1143 cell.set_text(text+'\n\n'+lower_text);
1140 1144 if (render) {
1141 1145 // The rendered state of the final cell should match
1142 1146 // that of the original selected cell;
1143 1147 cell.render();
1144 1148 }
1145 1149 };
1146 1150 this.delete_cell(index+1);
1147 1151 this.select(this.find_cell_index(cell));
1148 1152 };
1149 1153 };
1150 1154
1151 1155
1152 1156 // Cell collapsing and output clearing
1153 1157
1154 1158 /**
1155 1159 * Hide a cell's output.
1156 1160 *
1157 1161 * @method collapse
1158 1162 * @param {Number} index A cell's numeric index
1159 1163 */
1160 1164 Notebook.prototype.collapse = function (index) {
1161 1165 var i = this.index_or_selected(index);
1162 1166 this.get_cell(i).collapse();
1163 1167 this.set_dirty(true);
1164 1168 };
1165 1169
1166 1170 /**
1167 1171 * Show a cell's output.
1168 1172 *
1169 1173 * @method expand
1170 1174 * @param {Number} index A cell's numeric index
1171 1175 */
1172 1176 Notebook.prototype.expand = function (index) {
1173 1177 var i = this.index_or_selected(index);
1174 1178 this.get_cell(i).expand();
1175 1179 this.set_dirty(true);
1176 1180 };
1177 1181
1178 1182 /** Toggle whether a cell's output is collapsed or expanded.
1179 1183 *
1180 1184 * @method toggle_output
1181 1185 * @param {Number} index A cell's numeric index
1182 1186 */
1183 1187 Notebook.prototype.toggle_output = function (index) {
1184 1188 var i = this.index_or_selected(index);
1185 1189 this.get_cell(i).toggle_output();
1186 1190 this.set_dirty(true);
1187 1191 };
1188 1192
1189 1193 /**
1190 1194 * Toggle a scrollbar for long cell outputs.
1191 1195 *
1192 1196 * @method toggle_output_scroll
1193 1197 * @param {Number} index A cell's numeric index
1194 1198 */
1195 1199 Notebook.prototype.toggle_output_scroll = function (index) {
1196 1200 var i = this.index_or_selected(index);
1197 1201 this.get_cell(i).toggle_output_scroll();
1198 1202 };
1199 1203
1200 1204 /**
1201 1205 * Hide each code cell's output area.
1202 1206 *
1203 1207 * @method collapse_all_output
1204 1208 */
1205 1209 Notebook.prototype.collapse_all_output = function () {
1206 1210 var ncells = this.ncells();
1207 1211 var cells = this.get_cells();
1208 1212 for (var i=0; i<ncells; i++) {
1209 1213 if (cells[i] instanceof IPython.CodeCell) {
1210 1214 cells[i].output_area.collapse();
1211 1215 }
1212 1216 };
1213 1217 // this should not be set if the `collapse` key is removed from nbformat
1214 1218 this.set_dirty(true);
1215 1219 };
1216 1220
1217 1221 /**
1218 1222 * Expand each code cell's output area, and add a scrollbar for long output.
1219 1223 *
1220 1224 * @method scroll_all_output
1221 1225 */
1222 1226 Notebook.prototype.scroll_all_output = function () {
1223 1227 var ncells = this.ncells();
1224 1228 var cells = this.get_cells();
1225 1229 for (var i=0; i<ncells; i++) {
1226 1230 if (cells[i] instanceof IPython.CodeCell) {
1227 1231 cells[i].output_area.expand();
1228 1232 cells[i].output_area.scroll_if_long();
1229 1233 }
1230 1234 };
1231 1235 // this should not be set if the `collapse` key is removed from nbformat
1232 1236 this.set_dirty(true);
1233 1237 };
1234 1238
1235 1239 /**
1236 1240 * Expand each code cell's output area, and remove scrollbars.
1237 1241 *
1238 1242 * @method expand_all_output
1239 1243 */
1240 1244 Notebook.prototype.expand_all_output = function () {
1241 1245 var ncells = this.ncells();
1242 1246 var cells = this.get_cells();
1243 1247 for (var i=0; i<ncells; i++) {
1244 1248 if (cells[i] instanceof IPython.CodeCell) {
1245 1249 cells[i].output_area.expand();
1246 1250 cells[i].output_area.unscroll_area();
1247 1251 }
1248 1252 };
1249 1253 // this should not be set if the `collapse` key is removed from nbformat
1250 1254 this.set_dirty(true);
1251 1255 };
1252 1256
1253 1257 /**
1254 1258 * Clear each code cell's output area.
1255 1259 *
1256 1260 * @method clear_all_output
1257 1261 */
1258 1262 Notebook.prototype.clear_all_output = function () {
1259 1263 var ncells = this.ncells();
1260 1264 var cells = this.get_cells();
1261 1265 for (var i=0; i<ncells; i++) {
1262 1266 if (cells[i] instanceof IPython.CodeCell) {
1263 1267 cells[i].clear_output();
1264 1268 // Make all In[] prompts blank, as well
1265 1269 // TODO: make this configurable (via checkbox?)
1266 1270 cells[i].set_input_prompt();
1267 1271 }
1268 1272 };
1269 1273 this.set_dirty(true);
1270 1274 };
1271 1275
1272 1276
1273 1277 // Other cell functions: line numbers, ...
1274 1278
1275 1279 /**
1276 1280 * Toggle line numbers in the selected cell's input area.
1277 1281 *
1278 1282 * @method cell_toggle_line_numbers
1279 1283 */
1280 1284 Notebook.prototype.cell_toggle_line_numbers = function() {
1281 1285 this.get_selected_cell().toggle_line_numbers();
1282 1286 };
1283 1287
1284 1288 // Session related things
1285 1289
1286 1290 /**
1287 1291 * Start a new session and set it on each code cell.
1288 1292 *
1289 1293 * @method start_session
1290 1294 */
1291 1295 Notebook.prototype.start_session = function () {
1292 1296 this.session = new IPython.Session(this.notebook_name, this.notebook_path, this);
1293 1297 this.session.start($.proxy(this._session_started, this));
1294 1298 };
1295 1299
1296 1300
1297 1301 /**
1298 1302 * Once a session is started, link the code cells to the kernel
1299 1303 *
1300 1304 */
1301 1305 Notebook.prototype._session_started = function(){
1302 1306 this.kernel = this.session.kernel;
1303 1307 var ncells = this.ncells();
1304 1308 for (var i=0; i<ncells; i++) {
1305 1309 var cell = this.get_cell(i);
1306 1310 if (cell instanceof IPython.CodeCell) {
1307 1311 cell.set_kernel(this.session.kernel);
1308 1312 };
1309 1313 };
1310 1314 };
1311 1315
1312 1316 /**
1313 1317 * Prompt the user to restart the IPython kernel.
1314 1318 *
1315 1319 * @method restart_kernel
1316 1320 */
1317 1321 Notebook.prototype.restart_kernel = function () {
1318 1322 var that = this;
1319 1323 IPython.dialog.modal({
1320 1324 title : "Restart kernel or continue running?",
1321 1325 body : $("<p/>").html(
1322 1326 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1323 1327 ),
1324 1328 buttons : {
1325 1329 "Continue running" : {},
1326 1330 "Restart" : {
1327 1331 "class" : "btn-danger",
1328 1332 "click" : function() {
1329 1333 that.session.restart_kernel();
1330 1334 }
1331 1335 }
1332 1336 }
1333 1337 });
1334 1338 };
1335 1339
1336 1340 /**
1337 1341 * Run the selected cell.
1338 1342 *
1339 1343 * Execute or render cell outputs.
1340 1344 *
1341 1345 * @method execute_selected_cell
1342 1346 * @param {Object} options Customize post-execution behavior
1343 1347 */
1344 1348 Notebook.prototype.execute_selected_cell = function (mode) {
1345 1349 // mode = shift, ctrl, alt
1346 1350 mode = mode || 'shift'
1347 1351 var cell = this.get_selected_cell();
1348 1352 var cell_index = this.find_cell_index(cell);
1349 1353
1350 1354 cell.execute();
1351 1355
1352 1356 // If we are at the end always insert a new cell and return
1353 1357 if (cell_index === (this.ncells()-1) && mode !== 'shift') {
1354 1358 this.insert_cell_below('code');
1355 1359 this.select(cell_index+1);
1356 1360 this.edit_mode();
1357 1361 this.scroll_to_bottom();
1358 1362 this.set_dirty(true);
1359 1363 return;
1360 1364 }
1361 1365
1362 1366 if (mode === 'shift') {
1363 1367 this.command_mode();
1364 1368 } else if (mode === 'ctrl') {
1365 1369 this.select(cell_index+1);
1366 1370 this.get_cell(cell_index+1).focus_cell();
1367 1371 } else if (mode === 'alt') {
1368 1372 // Only insert a new cell, if we ended up in an already populated cell
1369 1373 var next_text = this.get_cell(cell_index+1).get_text();
1370 1374 if (/\S/.test(next_text) === true) {
1371 1375 this.insert_cell_below('code');
1372 1376 }
1373 1377 this.select(cell_index+1);
1374 1378 this.edit_mode();
1375 1379 }
1376 1380 this.set_dirty(true);
1377 1381 };
1378 1382
1379 1383
1380 1384 /**
1381 1385 * Execute all cells below the selected cell.
1382 1386 *
1383 1387 * @method execute_cells_below
1384 1388 */
1385 1389 Notebook.prototype.execute_cells_below = function () {
1386 1390 this.execute_cell_range(this.get_selected_index(), this.ncells());
1387 1391 this.scroll_to_bottom();
1388 1392 };
1389 1393
1390 1394 /**
1391 1395 * Execute all cells above the selected cell.
1392 1396 *
1393 1397 * @method execute_cells_above
1394 1398 */
1395 1399 Notebook.prototype.execute_cells_above = function () {
1396 1400 this.execute_cell_range(0, this.get_selected_index());
1397 1401 };
1398 1402
1399 1403 /**
1400 1404 * Execute all cells.
1401 1405 *
1402 1406 * @method execute_all_cells
1403 1407 */
1404 1408 Notebook.prototype.execute_all_cells = function () {
1405 1409 this.execute_cell_range(0, this.ncells());
1406 1410 this.scroll_to_bottom();
1407 1411 };
1408 1412
1409 1413 /**
1410 1414 * Execute a contiguous range of cells.
1411 1415 *
1412 1416 * @method execute_cell_range
1413 1417 * @param {Number} start Index of the first cell to execute (inclusive)
1414 1418 * @param {Number} end Index of the last cell to execute (exclusive)
1415 1419 */
1416 1420 Notebook.prototype.execute_cell_range = function (start, end) {
1417 1421 for (var i=start; i<end; i++) {
1418 1422 this.select(i);
1419 1423 this.execute_selected_cell({add_new:false});
1420 1424 };
1421 1425 };
1422 1426
1423 1427 // Persistance and loading
1424 1428
1425 1429 /**
1426 1430 * Getter method for this notebook's name.
1427 1431 *
1428 1432 * @method get_notebook_name
1429 1433 * @return {String} This notebook's name
1430 1434 */
1431 1435 Notebook.prototype.get_notebook_name = function () {
1432 1436 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1433 1437 return nbname;
1434 1438 };
1435 1439
1436 1440 /**
1437 1441 * Setter method for this notebook's name.
1438 1442 *
1439 1443 * @method set_notebook_name
1440 1444 * @param {String} name A new name for this notebook
1441 1445 */
1442 1446 Notebook.prototype.set_notebook_name = function (name) {
1443 1447 this.notebook_name = name;
1444 1448 };
1445 1449
1446 1450 /**
1447 1451 * Check that a notebook's name is valid.
1448 1452 *
1449 1453 * @method test_notebook_name
1450 1454 * @param {String} nbname A name for this notebook
1451 1455 * @return {Boolean} True if the name is valid, false if invalid
1452 1456 */
1453 1457 Notebook.prototype.test_notebook_name = function (nbname) {
1454 1458 nbname = nbname || '';
1455 1459 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1456 1460 return true;
1457 1461 } else {
1458 1462 return false;
1459 1463 };
1460 1464 };
1461 1465
1462 1466 /**
1463 1467 * Load a notebook from JSON (.ipynb).
1464 1468 *
1465 1469 * This currently handles one worksheet: others are deleted.
1466 1470 *
1467 1471 * @method fromJSON
1468 1472 * @param {Object} data JSON representation of a notebook
1469 1473 */
1470 1474 Notebook.prototype.fromJSON = function (data) {
1471 1475 var content = data.content;
1472 1476 var ncells = this.ncells();
1473 1477 var i;
1474 1478 for (i=0; i<ncells; i++) {
1475 1479 // Always delete cell 0 as they get renumbered as they are deleted.
1476 1480 this.delete_cell(0);
1477 1481 };
1478 1482 // Save the metadata and name.
1479 1483 this.metadata = content.metadata;
1480 1484 this.notebook_name = data.name;
1481 1485 // Only handle 1 worksheet for now.
1482 1486 var worksheet = content.worksheets[0];
1483 1487 if (worksheet !== undefined) {
1484 1488 if (worksheet.metadata) {
1485 1489 this.worksheet_metadata = worksheet.metadata;
1486 1490 }
1487 1491 var new_cells = worksheet.cells;
1488 1492 ncells = new_cells.length;
1489 1493 var cell_data = null;
1490 1494 var new_cell = null;
1491 1495 for (i=0; i<ncells; i++) {
1492 1496 cell_data = new_cells[i];
1493 1497 // VERSIONHACK: plaintext -> raw
1494 1498 // handle never-released plaintext name for raw cells
1495 1499 if (cell_data.cell_type === 'plaintext'){
1496 1500 cell_data.cell_type = 'raw';
1497 1501 }
1498 1502
1499 1503 new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
1500 1504 new_cell.fromJSON(cell_data);
1501 1505 };
1502 1506 };
1503 1507 if (content.worksheets.length > 1) {
1504 1508 IPython.dialog.modal({
1505 1509 title : "Multiple worksheets",
1506 1510 body : "This notebook has " + data.worksheets.length + " worksheets, " +
1507 1511 "but this version of IPython can only handle the first. " +
1508 1512 "If you save this notebook, worksheets after the first will be lost.",
1509 1513 buttons : {
1510 1514 OK : {
1511 1515 class : "btn-danger"
1512 1516 }
1513 1517 }
1514 1518 });
1515 1519 }
1516 1520 };
1517 1521
1518 1522 /**
1519 1523 * Dump this notebook into a JSON-friendly object.
1520 1524 *
1521 1525 * @method toJSON
1522 1526 * @return {Object} A JSON-friendly representation of this notebook.
1523 1527 */
1524 1528 Notebook.prototype.toJSON = function () {
1525 1529 var cells = this.get_cells();
1526 1530 var ncells = cells.length;
1527 1531 var cell_array = new Array(ncells);
1528 1532 for (var i=0; i<ncells; i++) {
1529 1533 cell_array[i] = cells[i].toJSON();
1530 1534 };
1531 1535 var data = {
1532 1536 // Only handle 1 worksheet for now.
1533 1537 worksheets : [{
1534 1538 cells: cell_array,
1535 1539 metadata: this.worksheet_metadata
1536 1540 }],
1537 1541 metadata : this.metadata
1538 1542 };
1539 1543 return data;
1540 1544 };
1541 1545
1542 1546 /**
1543 1547 * Start an autosave timer, for periodically saving the notebook.
1544 1548 *
1545 1549 * @method set_autosave_interval
1546 1550 * @param {Integer} interval the autosave interval in milliseconds
1547 1551 */
1548 1552 Notebook.prototype.set_autosave_interval = function (interval) {
1549 1553 var that = this;
1550 1554 // clear previous interval, so we don't get simultaneous timers
1551 1555 if (this.autosave_timer) {
1552 1556 clearInterval(this.autosave_timer);
1553 1557 }
1554 1558
1555 1559 this.autosave_interval = this.minimum_autosave_interval = interval;
1556 1560 if (interval) {
1557 1561 this.autosave_timer = setInterval(function() {
1558 1562 if (that.dirty) {
1559 1563 that.save_notebook();
1560 1564 }
1561 1565 }, interval);
1562 1566 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1563 1567 } else {
1564 1568 this.autosave_timer = null;
1565 1569 $([IPython.events]).trigger("autosave_disabled.Notebook");
1566 1570 };
1567 1571 };
1568 1572
1569 1573 /**
1570 1574 * Save this notebook on the server.
1571 1575 *
1572 1576 * @method save_notebook
1573 1577 */
1574 1578 Notebook.prototype.save_notebook = function (extra_settings) {
1575 1579 // Create a JSON model to be sent to the server.
1576 1580 var model = {};
1577 1581 model.name = this.notebook_name;
1578 1582 model.path = this.notebook_path;
1579 1583 model.content = this.toJSON();
1580 1584 model.content.nbformat = this.nbformat;
1581 1585 model.content.nbformat_minor = this.nbformat_minor;
1582 1586 // time the ajax call for autosave tuning purposes.
1583 1587 var start = new Date().getTime();
1584 1588 // We do the call with settings so we can set cache to false.
1585 1589 var settings = {
1586 1590 processData : false,
1587 1591 cache : false,
1588 1592 type : "PUT",
1589 1593 data : JSON.stringify(model),
1590 1594 headers : {'Content-Type': 'application/json'},
1591 1595 success : $.proxy(this.save_notebook_success, this, start),
1592 1596 error : $.proxy(this.save_notebook_error, this)
1593 1597 };
1594 1598 if (extra_settings) {
1595 1599 for (var key in extra_settings) {
1596 1600 settings[key] = extra_settings[key];
1597 1601 }
1598 1602 }
1599 1603 $([IPython.events]).trigger('notebook_saving.Notebook');
1600 1604 var url = utils.url_join_encode(
1601 1605 this._baseProjectUrl,
1602 1606 'api/notebooks',
1603 1607 this.notebook_path,
1604 1608 this.notebook_name
1605 1609 );
1606 1610 $.ajax(url, settings);
1607 1611 };
1608 1612
1609 1613 /**
1610 1614 * Success callback for saving a notebook.
1611 1615 *
1612 1616 * @method save_notebook_success
1613 1617 * @param {Integer} start the time when the save request started
1614 1618 * @param {Object} data JSON representation of a notebook
1615 1619 * @param {String} status Description of response status
1616 1620 * @param {jqXHR} xhr jQuery Ajax object
1617 1621 */
1618 1622 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1619 1623 this.set_dirty(false);
1620 1624 $([IPython.events]).trigger('notebook_saved.Notebook');
1621 1625 this._update_autosave_interval(start);
1622 1626 if (this._checkpoint_after_save) {
1623 1627 this.create_checkpoint();
1624 1628 this._checkpoint_after_save = false;
1625 1629 };
1626 1630 };
1627 1631
1628 1632 /**
1629 1633 * update the autosave interval based on how long the last save took
1630 1634 *
1631 1635 * @method _update_autosave_interval
1632 1636 * @param {Integer} timestamp when the save request started
1633 1637 */
1634 1638 Notebook.prototype._update_autosave_interval = function (start) {
1635 1639 var duration = (new Date().getTime() - start);
1636 1640 if (this.autosave_interval) {
1637 1641 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1638 1642 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1639 1643 // round to 10 seconds, otherwise we will be setting a new interval too often
1640 1644 interval = 10000 * Math.round(interval / 10000);
1641 1645 // set new interval, if it's changed
1642 1646 if (interval != this.autosave_interval) {
1643 1647 this.set_autosave_interval(interval);
1644 1648 }
1645 1649 }
1646 1650 };
1647 1651
1648 1652 /**
1649 1653 * Failure callback for saving a notebook.
1650 1654 *
1651 1655 * @method save_notebook_error
1652 1656 * @param {jqXHR} xhr jQuery Ajax object
1653 1657 * @param {String} status Description of response status
1654 1658 * @param {String} error HTTP error message
1655 1659 */
1656 1660 Notebook.prototype.save_notebook_error = function (xhr, status, error) {
1657 1661 $([IPython.events]).trigger('notebook_save_failed.Notebook', [xhr, status, error]);
1658 1662 };
1659 1663
1660 1664 Notebook.prototype.new_notebook = function(){
1661 1665 var path = this.notebook_path;
1662 1666 var base_project_url = this._baseProjectUrl;
1663 1667 var settings = {
1664 1668 processData : false,
1665 1669 cache : false,
1666 1670 type : "POST",
1667 1671 dataType : "json",
1668 1672 async : false,
1669 1673 success : function (data, status, xhr){
1670 1674 var notebook_name = data.name;
1671 1675 window.open(
1672 1676 utils.url_join_encode(
1673 1677 base_project_url,
1674 1678 'notebooks',
1675 1679 path,
1676 1680 notebook_name
1677 1681 ),
1678 1682 '_blank'
1679 1683 );
1680 1684 }
1681 1685 };
1682 1686 var url = utils.url_join_encode(
1683 1687 base_project_url,
1684 1688 'api/notebooks',
1685 1689 path
1686 1690 );
1687 1691 $.ajax(url,settings);
1688 1692 };
1689 1693
1690 1694
1691 1695 Notebook.prototype.copy_notebook = function(){
1692 1696 var path = this.notebook_path;
1693 1697 var base_project_url = this._baseProjectUrl;
1694 1698 var settings = {
1695 1699 processData : false,
1696 1700 cache : false,
1697 1701 type : "POST",
1698 1702 dataType : "json",
1699 1703 data : JSON.stringify({copy_from : this.notebook_name}),
1700 1704 async : false,
1701 1705 success : function (data, status, xhr) {
1702 1706 window.open(utils.url_join_encode(
1703 1707 base_project_url,
1704 1708 'notebooks',
1705 1709 data.path,
1706 1710 data.name
1707 1711 ), '_blank');
1708 1712 }
1709 1713 };
1710 1714 var url = utils.url_join_encode(
1711 1715 base_project_url,
1712 1716 'api/notebooks',
1713 1717 path
1714 1718 );
1715 1719 $.ajax(url,settings);
1716 1720 };
1717 1721
1718 1722 Notebook.prototype.rename = function (nbname) {
1719 1723 var that = this;
1720 1724 var data = {name: nbname + '.ipynb'};
1721 1725 var settings = {
1722 1726 processData : false,
1723 1727 cache : false,
1724 1728 type : "PATCH",
1725 1729 data : JSON.stringify(data),
1726 1730 dataType: "json",
1727 1731 headers : {'Content-Type': 'application/json'},
1728 1732 success : $.proxy(that.rename_success, this),
1729 1733 error : $.proxy(that.rename_error, this)
1730 1734 };
1731 1735 $([IPython.events]).trigger('rename_notebook.Notebook', data);
1732 1736 var url = utils.url_join_encode(
1733 1737 this._baseProjectUrl,
1734 1738 'api/notebooks',
1735 1739 this.notebook_path,
1736 1740 this.notebook_name
1737 1741 );
1738 1742 $.ajax(url, settings);
1739 1743 };
1740 1744
1741 1745
1742 1746 Notebook.prototype.rename_success = function (json, status, xhr) {
1743 1747 this.notebook_name = json.name;
1744 1748 var name = this.notebook_name;
1745 1749 var path = json.path;
1746 1750 this.session.rename_notebook(name, path);
1747 1751 $([IPython.events]).trigger('notebook_renamed.Notebook', json);
1748 1752 }
1749 1753
1750 1754 Notebook.prototype.rename_error = function (xhr, status, error) {
1751 1755 var that = this;
1752 1756 var dialog = $('<div/>').append(
1753 1757 $("<p/>").addClass("rename-message")
1754 1758 .html('This notebook name already exists.')
1755 1759 )
1756 1760 $([IPython.events]).trigger('notebook_rename_failed.Notebook', [xhr, status, error]);
1757 1761 IPython.dialog.modal({
1758 1762 title: "Notebook Rename Error!",
1759 1763 body: dialog,
1760 1764 buttons : {
1761 1765 "Cancel": {},
1762 1766 "OK": {
1763 1767 class: "btn-primary",
1764 1768 click: function () {
1765 1769 IPython.save_widget.rename_notebook();
1766 1770 }}
1767 1771 },
1768 1772 open : function (event, ui) {
1769 1773 var that = $(this);
1770 1774 // Upon ENTER, click the OK button.
1771 1775 that.find('input[type="text"]').keydown(function (event, ui) {
1772 1776 if (event.which === utils.keycodes.ENTER) {
1773 1777 that.find('.btn-primary').first().click();
1774 1778 }
1775 1779 });
1776 1780 that.find('input[type="text"]').focus();
1777 1781 }
1778 1782 });
1779 1783 }
1780 1784
1781 1785 /**
1782 1786 * Request a notebook's data from the server.
1783 1787 *
1784 1788 * @method load_notebook
1785 1789 * @param {String} notebook_name and path A notebook to load
1786 1790 */
1787 1791 Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
1788 1792 var that = this;
1789 1793 this.notebook_name = notebook_name;
1790 1794 this.notebook_path = notebook_path;
1791 1795 // We do the call with settings so we can set cache to false.
1792 1796 var settings = {
1793 1797 processData : false,
1794 1798 cache : false,
1795 1799 type : "GET",
1796 1800 dataType : "json",
1797 1801 success : $.proxy(this.load_notebook_success,this),
1798 1802 error : $.proxy(this.load_notebook_error,this),
1799 1803 };
1800 1804 $([IPython.events]).trigger('notebook_loading.Notebook');
1801 1805 var url = utils.url_join_encode(
1802 1806 this._baseProjectUrl,
1803 1807 'api/notebooks',
1804 1808 this.notebook_path,
1805 1809 this.notebook_name
1806 1810 );
1807 1811 $.ajax(url, settings);
1808 1812 };
1809 1813
1810 1814 /**
1811 1815 * Success callback for loading a notebook from the server.
1812 1816 *
1813 1817 * Load notebook data from the JSON response.
1814 1818 *
1815 1819 * @method load_notebook_success
1816 1820 * @param {Object} data JSON representation of a notebook
1817 1821 * @param {String} status Description of response status
1818 1822 * @param {jqXHR} xhr jQuery Ajax object
1819 1823 */
1820 1824 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1821 1825 this.fromJSON(data);
1822 1826 if (this.ncells() === 0) {
1823 1827 this.insert_cell_below('code');
1824 1828 this.select(0);
1825 1829 this.edit_mode();
1826 1830 } else {
1827 1831 this.select(0);
1828 1832 this.command_mode();
1829 1833 };
1830 1834 this.set_dirty(false);
1831 1835 this.scroll_to_top();
1832 1836 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1833 1837 var msg = "This notebook has been converted from an older " +
1834 1838 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1835 1839 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1836 1840 "newer notebook format will be used and older versions of IPython " +
1837 1841 "may not be able to read it. To keep the older version, close the " +
1838 1842 "notebook without saving it.";
1839 1843 IPython.dialog.modal({
1840 1844 title : "Notebook converted",
1841 1845 body : msg,
1842 1846 buttons : {
1843 1847 OK : {
1844 1848 class : "btn-primary"
1845 1849 }
1846 1850 }
1847 1851 });
1848 1852 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1849 1853 var that = this;
1850 1854 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1851 1855 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1852 1856 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1853 1857 this_vs + ". You can still work with this notebook, but some features " +
1854 1858 "introduced in later notebook versions may not be available."
1855 1859
1856 1860 IPython.dialog.modal({
1857 1861 title : "Newer Notebook",
1858 1862 body : msg,
1859 1863 buttons : {
1860 1864 OK : {
1861 1865 class : "btn-danger"
1862 1866 }
1863 1867 }
1864 1868 });
1865 1869
1866 1870 }
1867 1871
1868 1872 // Create the session after the notebook is completely loaded to prevent
1869 1873 // code execution upon loading, which is a security risk.
1870 1874 if (this.session == null) {
1871 1875 this.start_session();
1872 1876 }
1873 1877 // load our checkpoint list
1874 1878 this.list_checkpoints();
1875 1879
1876 1880 // load toolbar state
1877 1881 if (this.metadata.celltoolbar) {
1878 1882 IPython.CellToolbar.global_show();
1879 1883 IPython.CellToolbar.activate_preset(this.metadata.celltoolbar);
1880 1884 }
1881 1885
1882 1886 $([IPython.events]).trigger('notebook_loaded.Notebook');
1883 1887 };
1884 1888
1885 1889 /**
1886 1890 * Failure callback for loading a notebook from the server.
1887 1891 *
1888 1892 * @method load_notebook_error
1889 1893 * @param {jqXHR} xhr jQuery Ajax object
1890 1894 * @param {String} status Description of response status
1891 1895 * @param {String} error HTTP error message
1892 1896 */
1893 1897 Notebook.prototype.load_notebook_error = function (xhr, status, error) {
1894 1898 $([IPython.events]).trigger('notebook_load_failed.Notebook', [xhr, status, error]);
1895 1899 if (xhr.status === 400) {
1896 1900 var msg = error;
1897 1901 } else if (xhr.status === 500) {
1898 1902 var msg = "An unknown error occurred while loading this notebook. " +
1899 1903 "This version can load notebook formats " +
1900 1904 "v" + this.nbformat + " or earlier.";
1901 1905 }
1902 1906 IPython.dialog.modal({
1903 1907 title: "Error loading notebook",
1904 1908 body : msg,
1905 1909 buttons : {
1906 1910 "OK": {}
1907 1911 }
1908 1912 });
1909 1913 }
1910 1914
1911 1915 /********************* checkpoint-related *********************/
1912 1916
1913 1917 /**
1914 1918 * Save the notebook then immediately create a checkpoint.
1915 1919 *
1916 1920 * @method save_checkpoint
1917 1921 */
1918 1922 Notebook.prototype.save_checkpoint = function () {
1919 1923 this._checkpoint_after_save = true;
1920 1924 this.save_notebook();
1921 1925 };
1922 1926
1923 1927 /**
1924 1928 * Add a checkpoint for this notebook.
1925 1929 * for use as a callback from checkpoint creation.
1926 1930 *
1927 1931 * @method add_checkpoint
1928 1932 */
1929 1933 Notebook.prototype.add_checkpoint = function (checkpoint) {
1930 1934 var found = false;
1931 1935 for (var i = 0; i < this.checkpoints.length; i++) {
1932 1936 var existing = this.checkpoints[i];
1933 1937 if (existing.id == checkpoint.id) {
1934 1938 found = true;
1935 1939 this.checkpoints[i] = checkpoint;
1936 1940 break;
1937 1941 }
1938 1942 }
1939 1943 if (!found) {
1940 1944 this.checkpoints.push(checkpoint);
1941 1945 }
1942 1946 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
1943 1947 };
1944 1948
1945 1949 /**
1946 1950 * List checkpoints for this notebook.
1947 1951 *
1948 1952 * @method list_checkpoints
1949 1953 */
1950 1954 Notebook.prototype.list_checkpoints = function () {
1951 1955 var url = utils.url_join_encode(
1952 1956 this._baseProjectUrl,
1953 1957 'api/notebooks',
1954 1958 this.notebook_path,
1955 1959 this.notebook_name,
1956 1960 'checkpoints'
1957 1961 );
1958 1962 $.get(url).done(
1959 1963 $.proxy(this.list_checkpoints_success, this)
1960 1964 ).fail(
1961 1965 $.proxy(this.list_checkpoints_error, this)
1962 1966 );
1963 1967 };
1964 1968
1965 1969 /**
1966 1970 * Success callback for listing checkpoints.
1967 1971 *
1968 1972 * @method list_checkpoint_success
1969 1973 * @param {Object} data JSON representation of a checkpoint
1970 1974 * @param {String} status Description of response status
1971 1975 * @param {jqXHR} xhr jQuery Ajax object
1972 1976 */
1973 1977 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
1974 1978 var data = $.parseJSON(data);
1975 1979 this.checkpoints = data;
1976 1980 if (data.length) {
1977 1981 this.last_checkpoint = data[data.length - 1];
1978 1982 } else {
1979 1983 this.last_checkpoint = null;
1980 1984 }
1981 1985 $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
1982 1986 };
1983 1987
1984 1988 /**
1985 1989 * Failure callback for listing a checkpoint.
1986 1990 *
1987 1991 * @method list_checkpoint_error
1988 1992 * @param {jqXHR} xhr jQuery Ajax object
1989 1993 * @param {String} status Description of response status
1990 1994 * @param {String} error_msg HTTP error message
1991 1995 */
1992 1996 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
1993 1997 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
1994 1998 };
1995 1999
1996 2000 /**
1997 2001 * Create a checkpoint of this notebook on the server from the most recent save.
1998 2002 *
1999 2003 * @method create_checkpoint
2000 2004 */
2001 2005 Notebook.prototype.create_checkpoint = function () {
2002 2006 var url = utils.url_join_encode(
2003 2007 this._baseProjectUrl,
2004 2008 'api/notebooks',
2005 2009 this.notebookPath(),
2006 2010 this.notebook_name,
2007 2011 'checkpoints'
2008 2012 );
2009 2013 $.post(url).done(
2010 2014 $.proxy(this.create_checkpoint_success, this)
2011 2015 ).fail(
2012 2016 $.proxy(this.create_checkpoint_error, this)
2013 2017 );
2014 2018 };
2015 2019
2016 2020 /**
2017 2021 * Success callback for creating a checkpoint.
2018 2022 *
2019 2023 * @method create_checkpoint_success
2020 2024 * @param {Object} data JSON representation of a checkpoint
2021 2025 * @param {String} status Description of response status
2022 2026 * @param {jqXHR} xhr jQuery Ajax object
2023 2027 */
2024 2028 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
2025 2029 var data = $.parseJSON(data);
2026 2030 this.add_checkpoint(data);
2027 2031 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
2028 2032 };
2029 2033
2030 2034 /**
2031 2035 * Failure callback for creating a checkpoint.
2032 2036 *
2033 2037 * @method create_checkpoint_error
2034 2038 * @param {jqXHR} xhr jQuery Ajax object
2035 2039 * @param {String} status Description of response status
2036 2040 * @param {String} error_msg HTTP error message
2037 2041 */
2038 2042 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
2039 2043 $([IPython.events]).trigger('checkpoint_failed.Notebook');
2040 2044 };
2041 2045
2042 2046 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2043 2047 var that = this;
2044 2048 var checkpoint = checkpoint || this.last_checkpoint;
2045 2049 if ( ! checkpoint ) {
2046 2050 console.log("restore dialog, but no checkpoint to restore to!");
2047 2051 return;
2048 2052 }
2049 2053 var body = $('<div/>').append(
2050 2054 $('<p/>').addClass("p-space").text(
2051 2055 "Are you sure you want to revert the notebook to " +
2052 2056 "the latest checkpoint?"
2053 2057 ).append(
2054 2058 $("<strong/>").text(
2055 2059 " This cannot be undone."
2056 2060 )
2057 2061 )
2058 2062 ).append(
2059 2063 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2060 2064 ).append(
2061 2065 $('<p/>').addClass("p-space").text(
2062 2066 Date(checkpoint.last_modified)
2063 2067 ).css("text-align", "center")
2064 2068 );
2065 2069
2066 2070 IPython.dialog.modal({
2067 2071 title : "Revert notebook to checkpoint",
2068 2072 body : body,
2069 2073 buttons : {
2070 2074 Revert : {
2071 2075 class : "btn-danger",
2072 2076 click : function () {
2073 2077 that.restore_checkpoint(checkpoint.id);
2074 2078 }
2075 2079 },
2076 2080 Cancel : {}
2077 2081 }
2078 2082 });
2079 2083 }
2080 2084
2081 2085 /**
2082 2086 * Restore the notebook to a checkpoint state.
2083 2087 *
2084 2088 * @method restore_checkpoint
2085 2089 * @param {String} checkpoint ID
2086 2090 */
2087 2091 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2088 2092 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2089 2093 var url = utils.url_join_encode(
2090 2094 this._baseProjectUrl,
2091 2095 'api/notebooks',
2092 2096 this.notebookPath(),
2093 2097 this.notebook_name,
2094 2098 'checkpoints',
2095 2099 checkpoint
2096 2100 );
2097 2101 $.post(url).done(
2098 2102 $.proxy(this.restore_checkpoint_success, this)
2099 2103 ).fail(
2100 2104 $.proxy(this.restore_checkpoint_error, this)
2101 2105 );
2102 2106 };
2103 2107
2104 2108 /**
2105 2109 * Success callback for restoring a notebook to a checkpoint.
2106 2110 *
2107 2111 * @method restore_checkpoint_success
2108 2112 * @param {Object} data (ignored, should be empty)
2109 2113 * @param {String} status Description of response status
2110 2114 * @param {jqXHR} xhr jQuery Ajax object
2111 2115 */
2112 2116 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
2113 2117 $([IPython.events]).trigger('checkpoint_restored.Notebook');
2114 2118 this.load_notebook(this.notebook_name, this.notebook_path);
2115 2119 };
2116 2120
2117 2121 /**
2118 2122 * Failure callback for restoring a notebook to a checkpoint.
2119 2123 *
2120 2124 * @method restore_checkpoint_error
2121 2125 * @param {jqXHR} xhr jQuery Ajax object
2122 2126 * @param {String} status Description of response status
2123 2127 * @param {String} error_msg HTTP error message
2124 2128 */
2125 2129 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
2126 2130 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
2127 2131 };
2128 2132
2129 2133 /**
2130 2134 * Delete a notebook checkpoint.
2131 2135 *
2132 2136 * @method delete_checkpoint
2133 2137 * @param {String} checkpoint ID
2134 2138 */
2135 2139 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2136 2140 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2137 2141 var url = utils.url_join_encode(
2138 2142 this._baseProjectUrl,
2139 2143 'api/notebooks',
2140 2144 this.notebookPath(),
2141 2145 this.notebook_name,
2142 2146 'checkpoints',
2143 2147 checkpoint
2144 2148 );
2145 2149 $.ajax(url, {
2146 2150 type: 'DELETE',
2147 2151 success: $.proxy(this.delete_checkpoint_success, this),
2148 2152 error: $.proxy(this.delete_notebook_error,this)
2149 2153 });
2150 2154 };
2151 2155
2152 2156 /**
2153 2157 * Success callback for deleting a notebook checkpoint
2154 2158 *
2155 2159 * @method delete_checkpoint_success
2156 2160 * @param {Object} data (ignored, should be empty)
2157 2161 * @param {String} status Description of response status
2158 2162 * @param {jqXHR} xhr jQuery Ajax object
2159 2163 */
2160 2164 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2161 2165 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2162 2166 this.load_notebook(this.notebook_name, this.notebook_path);
2163 2167 };
2164 2168
2165 2169 /**
2166 2170 * Failure callback for deleting a notebook checkpoint.
2167 2171 *
2168 2172 * @method delete_checkpoint_error
2169 2173 * @param {jqXHR} xhr jQuery Ajax object
2170 2174 * @param {String} status Description of response status
2171 2175 * @param {String} error_msg HTTP error message
2172 2176 */
2173 2177 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2174 2178 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2175 2179 };
2176 2180
2177 2181
2178 2182 IPython.Notebook = Notebook;
2179 2183
2180 2184
2181 2185 return IPython;
2182 2186
2183 2187 }(IPython));
@@ -1,605 +1,590
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2012 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // TextCell
10 10 //============================================================================
11 11
12 12
13 13
14 14 /**
15 15 A module that allow to create different type of Text Cell
16 16 @module IPython
17 17 @namespace IPython
18 18 */
19 19 var IPython = (function (IPython) {
20 20 "use strict";
21 21
22 22 // TextCell base class
23 23 var key = IPython.utils.keycodes;
24 24
25 25 /**
26 26 * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text'
27 27 * cell start as not redered.
28 28 *
29 29 * @class TextCell
30 30 * @constructor TextCell
31 31 * @extend IPython.Cell
32 32 * @param {object|undefined} [options]
33 33 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
34 34 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
35 35 */
36 36 var TextCell = function (options) {
37 37 // in all TextCell/Cell subclasses
38 38 // do not assign most of members here, just pass it down
39 39 // in the options dict potentially overwriting what you wish.
40 40 // they will be assigned in the base class.
41 41
42 42 // we cannot put this as a class key as it has handle to "this".
43 43 var cm_overwrite_options = {
44 44 onKeyEvent: $.proxy(this.handle_keyevent,this)
45 45 };
46 46
47 47 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
48 48
49 49 this.cell_type = this.cell_type || 'text';
50 50
51 51 IPython.Cell.apply(this, [options]);
52 52
53 53 this.rendered = false;
54 54 };
55 55
56 56 TextCell.prototype = new IPython.Cell();
57 57
58 58 TextCell.options_default = {
59 59 cm_config : {
60 60 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
61 61 mode: 'htmlmixed',
62 62 lineWrapping : true,
63 63 }
64 64 };
65 65
66 66
67 67 /**
68 68 * Create the DOM element of the TextCell
69 69 * @method create_element
70 70 * @private
71 71 */
72 72 TextCell.prototype.create_element = function () {
73 73 IPython.Cell.prototype.create_element.apply(this, arguments);
74 74
75 75 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
76 76 cell.attr('tabindex','2');
77 77
78 78 var prompt = $('<div/>').addClass('prompt input_prompt');
79 79 cell.append(prompt);
80 80 var inner_cell = $('<div/>').addClass('inner_cell');
81 81 this.celltoolbar = new IPython.CellToolbar(this);
82 82 inner_cell.append(this.celltoolbar.element);
83 83 var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
84 84 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
85 85 // The tabindex=-1 makes this div focusable.
86 86 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
87 87 addClass('rendered_html').attr('tabindex','-1');
88 88 inner_cell.append(input_area).append(render_area);
89 89 cell.append(inner_cell);
90 90 this.element = cell;
91 91 };
92 92
93 93
94 94 /**
95 95 * Bind the DOM evet to cell actions
96 96 * Need to be called after TextCell.create_element
97 97 * @private
98 98 * @method bind_event
99 99 */
100 100 TextCell.prototype.bind_events = function () {
101 101 IPython.Cell.prototype.bind_events.apply(this);
102 102 var that = this;
103 103
104 104 this.element.dblclick(function () {
105 105 if (that.selected === false) {
106 106 $([IPython.events]).trigger('select.Cell', {'cell':that});
107 107 };
108 108 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
109 109 });
110 110 };
111 111
112 112 TextCell.prototype.handle_keyevent = function (editor, event) {
113 113
114 114 console.log('CM', this.mode, event.which, event.type)
115 115
116 116 if (this.mode === 'command') {
117 117 return true;
118 118 } else if (this.mode === 'edit') {
119 119 return this.handle_codemirror_keyevent(editor, event);
120 120 }
121 121 };
122 122
123 123 /**
124 124 * This method gets called in CodeMirror's onKeyDown/onKeyPress
125 125 * handlers and is used to provide custom key handling.
126 126 *
127 127 * Subclass should override this method to have custom handeling
128 128 *
129 129 * @method handle_codemirror_keyevent
130 130 * @param {CodeMirror} editor - The codemirror instance bound to the cell
131 131 * @param {event} event -
132 132 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
133 133 */
134 134 TextCell.prototype.handle_codemirror_keyevent = function (editor, event) {
135 135 var that = this;
136 136
137 137 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey || event.altKey)) {
138 138 // Always ignore shift-enter in CodeMirror as we handle it.
139 139 return true;
140 140 } else if (event.which === key.UPARROW && event.type === 'keydown') {
141 141 // If we are not at the top, let CM handle the up arrow and
142 142 // prevent the global keydown handler from handling it.
143 143 if (!that.at_top()) {
144 144 event.stop();
145 145 return false;
146 146 } else {
147 147 return true;
148 148 };
149 149 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
150 150 // If we are not at the bottom, let CM handle the down arrow and
151 151 // prevent the global keydown handler from handling it.
152 152 if (!that.at_bottom()) {
153 153 event.stop();
154 154 return false;
155 155 } else {
156 156 return true;
157 157 };
158 158 }
159 159 return false;
160 160 };
161 161
162 162 // Cell level actions
163 163
164 164 TextCell.prototype.select = function () {
165 165 var cont = IPython.Cell.prototype.select.apply(this);
166 166 if (cont) {
167 167 if (this.mode === 'edit') {
168 168 this.code_mirror.refresh();
169 169 }
170 170 };
171 171 return cont;
172 172 };
173 173
174 174 TextCell.prototype.unrender = function () {
175 175 if (this.read_only) return;
176 176 var cont = IPython.Cell.prototype.unrender.apply(this);
177 177 if (cont) {
178 178 var text_cell = this.element;
179 179 var output = text_cell.find("div.text_cell_render");
180 180 output.hide();
181 181 text_cell.find('div.text_cell_input').show();
182 182 this.focus_editor();
183 183 if (this.get_text() === this.placeholder) {
184 184 this.set_text('');
185 185 this.refresh();
186 186 }
187 187
188 188 };
189 189 return cont;
190 190 };
191 191
192 192 TextCell.prototype.execute = function () {
193 193 this.render();
194 194 };
195 195
196 196 TextCell.prototype.command_mode = function () {
197 197 var cont = IPython.Cell.prototype.command_mode.apply(this);
198 198 if (cont) {
199 199 this.focus_cell();
200 200 };
201 201 return cont;
202 202 }
203 203
204 204 TextCell.prototype.edit_mode = function () {
205 205 var cont = IPython.Cell.prototype.edit_mode.apply(this);
206 206 if (cont) {
207 207 this.unrender();
208 208 this.focus_editor();
209 209 };
210 210 return cont;
211 211 }
212 212
213 213 /**
214 214 * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}}
215 215 * @method get_text
216 216 * @retrun {string} CodeMirror current text value
217 217 */
218 218 TextCell.prototype.get_text = function() {
219 219 return this.code_mirror.getValue();
220 220 };
221 221
222 222 /**
223 223 * @param {string} text - Codemiror text value
224 224 * @see TextCell#get_text
225 225 * @method set_text
226 226 * */
227 227 TextCell.prototype.set_text = function(text) {
228 228 this.code_mirror.setValue(text);
229 229 this.code_mirror.refresh();
230 230 };
231 231
232 232 /**
233 233 * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}}
234 234 * @method get_rendered
235 235 * @return {html} html of rendered element
236 236 * */
237 237 TextCell.prototype.get_rendered = function() {
238 238 return this.element.find('div.text_cell_render').html();
239 239 };
240 240
241 241 /**
242 242 * @method set_rendered
243 243 */
244 244 TextCell.prototype.set_rendered = function(text) {
245 245 this.element.find('div.text_cell_render').html(text);
246 246 };
247 247
248 248 /**
249 249 * @method at_top
250 250 * @return {Boolean}
251 251 */
252 252 TextCell.prototype.at_top = function () {
253 253 if (this.rendered) {
254 254 return true;
255 255 } else {
256 256 var cursor = this.code_mirror.getCursor();
257 257 if (cursor.line === 0 && cursor.ch === 0) {
258 258 return true;
259 259 } else {
260 260 return false;
261 261 };
262 262 };
263 263 };
264 264
265
266 /** @method at_bottom **/
267 TextCell.prototype.at_bottom = function () {
268 if (this.rendered) {
269 return true
270 } else {
271 var cursor = this.code_mirror.getCursor();
272 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
273 return true;
274 } else {
275 return false;
276 };
277 };
278 };
279
280 265 /**
281 266 * @method at_bottom
282 267 * @return {Boolean}
283 268 * */
284 269 TextCell.prototype.at_bottom = function () {
285 270 if (this.rendered) {
286 271 return true;
287 272 } else {
288 273 var cursor = this.code_mirror.getCursor();
289 274 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
290 275 return true;
291 276 } else {
292 277 return false;
293 278 };
294 279 };
295 280 };
296 281
297 282 /**
298 283 * Create Text cell from JSON
299 284 * @param {json} data - JSON serialized text-cell
300 285 * @method fromJSON
301 286 */
302 287 TextCell.prototype.fromJSON = function (data) {
303 288 IPython.Cell.prototype.fromJSON.apply(this, arguments);
304 289 if (data.cell_type === this.cell_type) {
305 290 if (data.source !== undefined) {
306 291 this.set_text(data.source);
307 292 // make this value the starting point, so that we can only undo
308 293 // to this state, instead of a blank cell
309 294 this.code_mirror.clearHistory();
310 295 this.set_rendered(data.rendered || '');
311 296 this.rendered = false;
312 297 this.render();
313 298 }
314 299 }
315 300 };
316 301
317 302 /** Generate JSON from cell
318 303 * @return {object} cell data serialised to json
319 304 */
320 305 TextCell.prototype.toJSON = function () {
321 306 var data = IPython.Cell.prototype.toJSON.apply(this);
322 307 data.source = this.get_text();
323 308 if (data.source == this.placeholder) {
324 309 data.source = "";
325 310 }
326 311 return data;
327 312 };
328 313
329 314
330 315 /**
331 316 * @class MarkdownCell
332 317 * @constructor MarkdownCell
333 318 * @extends IPython.HTMLCell
334 319 */
335 320 var MarkdownCell = function (options) {
336 321 options = this.mergeopt(MarkdownCell, options);
337 322
338 323 this.cell_type = 'markdown';
339 324 TextCell.apply(this, [options]);
340 325 };
341 326
342 327 MarkdownCell.options_default = {
343 328 cm_config: {
344 329 mode: 'gfm'
345 330 },
346 331 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
347 332 }
348 333
349 334 MarkdownCell.prototype = new TextCell();
350 335
351 336 /**
352 337 * @method render
353 338 */
354 339 MarkdownCell.prototype.render = function () {
355 340 var cont = IPython.TextCell.prototype.render.apply(this);
356 341 if (cont) {
357 342 var text = this.get_text();
358 343 var math = null;
359 344 if (text === "") { text = this.placeholder; }
360 345 var text_and_math = IPython.mathjaxutils.remove_math(text);
361 346 text = text_and_math[0];
362 347 math = text_and_math[1];
363 348 var html = marked.parser(marked.lexer(text));
364 349 html = $(IPython.mathjaxutils.replace_math(html, math));
365 350 // links in markdown cells should open in new tabs
366 351 html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
367 352 try {
368 353 this.set_rendered(html);
369 354 } catch (e) {
370 355 console.log("Error running Javascript in Markdown:");
371 356 console.log(e);
372 357 this.set_rendered($("<div/>").addClass("js-error").html(
373 358 "Error rendering Markdown!<br/>" + e.toString())
374 359 );
375 360 }
376 361 this.element.find('div.text_cell_input').hide();
377 362 this.element.find("div.text_cell_render").show();
378 363 this.typeset()
379 364 };
380 365 return cont;
381 366 };
382 367
383 368
384 369 // RawCell
385 370
386 371 /**
387 372 * @class RawCell
388 373 * @constructor RawCell
389 374 * @extends IPython.TextCell
390 375 */
391 376 var RawCell = function (options) {
392 377
393 378 options = this.mergeopt(RawCell,options)
394 379 TextCell.apply(this, [options]);
395 380 this.cell_type = 'raw';
396 381 // RawCell should always hide its rendered div
397 382 this.element.find('div.text_cell_render').hide();
398 383 };
399 384
400 385 RawCell.options_default = {
401 386 placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" +
402 387 "It will not be rendered in the notebook.\n" +
403 388 "When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
404 389 };
405 390
406 391 RawCell.prototype = new TextCell();
407 392
408 393 /** @method bind_events **/
409 394 RawCell.prototype.bind_events = function () {
410 395 TextCell.prototype.bind_events.apply(this);
411 396 var that = this
412 397 this.element.focusout(function() {
413 398 that.auto_highlight();
414 399 });
415 400 };
416 401
417 402 /**
418 403 * Trigger autodetection of highlight scheme for current cell
419 404 * @method auto_highlight
420 405 */
421 406 RawCell.prototype.auto_highlight = function () {
422 407 this._auto_highlight(IPython.config.raw_cell_highlight);
423 408 };
424 409
425 410 /** @method render **/
426 411 RawCell.prototype.render = function () {
427 412 // Make sure that this cell type can never be rendered
428 413 if (this.rendered) {
429 414 this.unrender();
430 415 }
431 416 var text = this.get_text();
432 417 if (text === "") { text = this.placeholder; }
433 418 this.set_text(text);
434 419 };
435 420
436 421
437 422 /** @method handle_codemirror_keyevent **/
438 423 RawCell.prototype.handle_codemirror_keyevent = function (editor, event) {
439 424
440 425 var that = this;
441 426 if (this.mode === 'command') {
442 427 return false
443 428 } else if (this.mode === 'edit') {
444 429 // TODO: review these handlers...
445 430 if (event.which === key.UPARROW && event.type === 'keydown') {
446 431 // If we are not at the top, let CM handle the up arrow and
447 432 // prevent the global keydown handler from handling it.
448 433 if (!that.at_top()) {
449 434 event.stop();
450 435 return false;
451 436 } else {
452 437 return true;
453 438 };
454 439 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
455 440 // If we are not at the bottom, let CM handle the down arrow and
456 441 // prevent the global keydown handler from handling it.
457 442 if (!that.at_bottom()) {
458 443 event.stop();
459 444 return false;
460 445 } else {
461 446 return true;
462 447 };
463 448 };
464 449 return false;
465 450 };
466 451 return false;
467 452 };
468 453
469 454
470 455 /**
471 456 * @class HeadingCell
472 457 * @extends IPython.TextCell
473 458 */
474 459
475 460 /**
476 461 * @constructor HeadingCell
477 462 * @extends IPython.TextCell
478 463 */
479 464 var HeadingCell = function (options) {
480 465 options = this.mergeopt(HeadingCell, options);
481 466
482 467 this.level = 1;
483 468 this.cell_type = 'heading';
484 469 TextCell.apply(this, [options]);
485 470
486 471 /**
487 472 * heading level of the cell, use getter and setter to access
488 473 * @property level
489 474 */
490 475 };
491 476
492 477 HeadingCell.options_default = {
493 478 placeholder: "Type Heading Here"
494 479 };
495 480
496 481 HeadingCell.prototype = new TextCell();
497 482
498 483 /** @method fromJSON */
499 484 HeadingCell.prototype.fromJSON = function (data) {
500 485 if (data.level != undefined){
501 486 this.level = data.level;
502 487 }
503 488 TextCell.prototype.fromJSON.apply(this, arguments);
504 489 };
505 490
506 491
507 492 /** @method toJSON */
508 493 HeadingCell.prototype.toJSON = function () {
509 494 var data = TextCell.prototype.toJSON.apply(this);
510 495 data.level = this.get_level();
511 496 return data;
512 497 };
513 498
514 499 /**
515 500 * can the cell be split into two cells
516 501 * @method is_splittable
517 502 **/
518 503 HeadingCell.prototype.is_splittable = function () {
519 504 return false;
520 505 };
521 506
522 507
523 508 /**
524 509 * can the cell be merged with other cells
525 510 * @method is_mergeable
526 511 **/
527 512 HeadingCell.prototype.is_mergeable = function () {
528 513 return false;
529 514 };
530 515
531 516 /**
532 517 * Change heading level of cell, and re-render
533 518 * @method set_level
534 519 */
535 520 HeadingCell.prototype.set_level = function (level) {
536 521 this.level = level;
537 522 if (this.rendered) {
538 523 this.rendered = false;
539 524 this.render();
540 525 };
541 526 };
542 527
543 528 /** The depth of header cell, based on html (h1 to h6)
544 529 * @method get_level
545 530 * @return {integer} level - for 1 to 6
546 531 */
547 532 HeadingCell.prototype.get_level = function () {
548 533 return this.level;
549 534 };
550 535
551 536
552 537 HeadingCell.prototype.set_rendered = function (html) {
553 538 this.element.find("div.text_cell_render").html(html);
554 539 };
555 540
556 541
557 542 HeadingCell.prototype.get_rendered = function () {
558 543 var r = this.element.find("div.text_cell_render");
559 544 return r.children().first().html();
560 545 };
561 546
562 547
563 548 HeadingCell.prototype.render = function () {
564 549 var cont = IPython.TextCell.prototype.render.apply(this);
565 550 if (cont) {
566 551 var text = this.get_text();
567 552 var math = null;
568 553 // Markdown headings must be a single line
569 554 text = text.replace(/\n/g, ' ');
570 555 if (text === "") { text = this.placeholder; }
571 556 text = Array(this.level + 1).join("#") + " " + text;
572 557 var text_and_math = IPython.mathjaxutils.remove_math(text);
573 558 text = text_and_math[0];
574 559 math = text_and_math[1];
575 560 var html = marked.parser(marked.lexer(text));
576 561 var h = $(IPython.mathjaxutils.replace_math(html, math));
577 562 // add id and linkback anchor
578 563 var hash = h.text().replace(/ /g, '-');
579 564 h.attr('id', hash);
580 565 h.append(
581 566 $('<a/>')
582 567 .addClass('anchor-link')
583 568 .attr('href', '#' + hash)
584 569 .text('¶')
585 570 );
586 571
587 572 this.set_rendered(h);
588 573 this.typeset();
589 574 this.element.find('div.text_cell_input').hide();
590 575 this.element.find("div.text_cell_render").show();
591 576
592 577 };
593 578 return cont;
594 579 };
595 580
596 581 IPython.TextCell = TextCell;
597 582 IPython.MarkdownCell = MarkdownCell;
598 583 IPython.RawCell = RawCell;
599 584 IPython.HeadingCell = HeadingCell;
600 585
601 586
602 587 return IPython;
603 588
604 589 }(IPython));
605 590
General Comments 0
You need to be logged in to leave comments. Login now