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