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