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