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