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