##// END OF EJS Templates
fix one more == to ===
Matthias BUSSONNIER -
Show More
@@ -1,1406 +1,1406
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 !== null && 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 /**
487 487 * Move given (or selected) cell up and select it
488 488 * @method move_cell_up
489 489 * @param [index] {integer} cell index
490 490 **/
491 491 Notebook.prototype.move_cell_up = function (index) {
492 492 var i = this.index_or_selected(index);
493 493 if (this.is_valid_cell_index(i) && i > 0) {
494 494 var pivot = this.get_cell_element(i-1);
495 495 var tomove = this.get_cell_element(i);
496 496 if (pivot !== null && tomove !== null) {
497 497 tomove.detach();
498 498 pivot.before(tomove);
499 499 this.select(i-1);
500 500 };
501 501 this.dirty = true;
502 502 };
503 503 return this;
504 504 };
505 505
506 506
507 507 /**
508 508 * Move given (or selected) cell down and select it
509 509 * @method move_cell_down
510 510 * @param [index] {integer} cell index
511 511 **/
512 512 Notebook.prototype.move_cell_down = function (index) {
513 513 var i = this.index_or_selected(index);
514 514 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
515 515 var pivot = this.get_cell_element(i+1);
516 516 var tomove = this.get_cell_element(i);
517 517 if (pivot !== null && tomove !== null) {
518 518 tomove.detach();
519 519 pivot.after(tomove);
520 520 this.select(i+1);
521 521 };
522 522 };
523 523 this.dirty = true;
524 524 return this;
525 525 };
526 526
527 527
528 528 // Insertion, deletion.
529 529
530 530 Notebook.prototype.delete_cell = function (index) {
531 531 var i = this.index_or_selected(index);
532 532 var cell = this.get_selected_cell();
533 533 this.undelete_backup = cell.toJSON();
534 534 if (this.is_valid_cell_index(i)) {
535 535 var ce = this.get_cell_element(i);
536 536 ce.remove();
537 537 if (i === (this.ncells())) {
538 538 this.select(i-1);
539 539 this.undelete_index = i - 1;
540 540 this.undelete_below = true;
541 541 } else {
542 542 this.select(i);
543 543 this.undelete_index = i;
544 544 this.undelete_below = false;
545 545 };
546 546 this.dirty = true;
547 547 };
548 548 return this;
549 549 };
550 550
551 551
552 552
553 553
554 554 /**
555 555 * Insert a cell so that after insertion the cell is at given index.
556 556 *
557 557 * Similar to insert_above, but index parameter is mandatory
558 558 *
559 559 * Index will be brought back into the accissible range [0,n]
560 560 *
561 561 * @param type {string} in ['code','html','markdown','heading']
562 562 * @param [index] {int} a valid index where to inser cell
563 563 *
564 564 * @return cell {cell|null} created cell or null
565 565 **/
566 566 Notebook.prototype.insert_cell_at_index = function(type, index){
567 567
568 568 var ncells = this.ncells();
569 569 var index = Math.min(index,ncells);
570 570 index = Math.max(index,0);
571 571 var cell = null;
572 572
573 if (ncells === 0 || this.is_valid_cell_index(index) || index== ncells) {
573 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
574 574 if (type === 'code') {
575 575 cell = new IPython.CodeCell(this.kernel);
576 576 cell.set_input_prompt();
577 577 } else if (type === 'markdown') {
578 578 cell = new IPython.MarkdownCell();
579 579 } else if (type === 'html') {
580 580 cell = new IPython.HTMLCell();
581 581 } else if (type === 'raw') {
582 582 cell = new IPython.RawCell();
583 583 } else if (type === 'heading') {
584 584 cell = new IPython.HeadingCell();
585 585 }
586 586
587 587 if(this._insert_element_at_index(cell.element,index)){
588 588 cell.render();
589 589 this.select(this.find_cell_index(cell));
590 590 this.dirty = true;
591 591 }
592 592 }
593 593 return cell;
594 594
595 595 };
596 596
597 597 /**
598 598 * Insert an element at given cell index.
599 599 *
600 600 * @param element {dom element} a cell element
601 601 * @param [index] {int} a valid index where to inser cell
602 602 * @private
603 603 *
604 604 * return true if everything whent fine.
605 605 **/
606 606 Notebook.prototype._insert_element_at_index = function(element, index){
607 607 if (element === undefined){
608 608 return false;
609 609 }
610 610
611 611 var ncells = this.ncells();
612 612
613 613 if (ncells === 0) {
614 614 // special case append if empty
615 615 this.element.find('div.end_space').before(element);
616 616 } else if ( ncells === index ) {
617 617 // special case append it the end, but not empty
618 618 this.get_cell_element(index-1).after(element);
619 619 } else if (this.is_valid_cell_index(index)) {
620 620 // otherwise always somewhere to append to
621 621 this.get_cell_element(index).before(element);
622 622 } else {
623 623 return false;
624 624 }
625 625
626 626 if (this.undelete_index !== null && index <= this.undelete_index) {
627 627 this.undelete_index = this.undelete_index + 1;
628 628 this.dirty = true;
629 629 }
630 630 return true;
631 631 };
632 632
633 633 /**
634 634 * Insert a cell of given type above given index, or at top
635 635 * of notebook if index smaller than 0.
636 636 *
637 637 * default index value is the one of currently selected cell
638 638 *
639 639 * @param type {string} cell type
640 640 * @param [index] {integer}
641 641 *
642 642 * @return handle to created cell or null
643 643 **/
644 644 Notebook.prototype.insert_cell_above = function (type, index) {
645 645 index = this.index_or_selected(index);
646 646 return this.insert_cell_at_index(type, index);
647 647 };
648 648
649 649 /**
650 650 * Insert a cell of given type below given index, or at bottom
651 651 * of notebook if index greater thatn number of cell
652 652 *
653 653 * default index value is the one of currently selected cell
654 654 *
655 655 * @method insert_cell_below
656 656 * @param type {string} cell type
657 657 * @param [index] {integer}
658 658 *
659 659 * @return handle to created cell or null
660 660 *
661 661 **/
662 662 Notebook.prototype.insert_cell_below = function (type, index) {
663 663 index = this.index_or_selected(index);
664 664 return this.insert_cell_at_index(type, index+1);
665 665 };
666 666
667 667
668 668 /**
669 669 * Insert cell at end of notebook
670 670 *
671 671 * @method insert_cell_at_bottom
672 672 * @param type {String} cell type
673 673 *
674 674 * @return the added cell; or null
675 675 **/
676 676 Notebook.prototype.insert_cell_at_bottom = function (type){
677 677 var len = this.ncells();
678 678 return this.insert_cell_below(type,len-1);
679 679 };
680 680
681 681
682 682
683 683 Notebook.prototype.to_code = function (index) {
684 684 var i = this.index_or_selected(index);
685 685 if (this.is_valid_cell_index(i)) {
686 686 var source_element = this.get_cell_element(i);
687 687 var source_cell = source_element.data("cell");
688 688 if (!(source_cell instanceof IPython.CodeCell)) {
689 689 var target_cell = this.insert_cell_below('code',i);
690 690 var text = source_cell.get_text();
691 691 if (text === source_cell.placeholder) {
692 692 text = '';
693 693 }
694 694 target_cell.set_text(text);
695 695 // make this value the starting point, so that we can only undo
696 696 // to this state, instead of a blank cell
697 697 target_cell.code_mirror.clearHistory();
698 698 source_element.remove();
699 699 this.dirty = true;
700 700 };
701 701 };
702 702 };
703 703
704 704
705 705 Notebook.prototype.to_markdown = function (index) {
706 706 var i = this.index_or_selected(index);
707 707 if (this.is_valid_cell_index(i)) {
708 708 var source_element = this.get_cell_element(i);
709 709 var source_cell = source_element.data("cell");
710 710 if (!(source_cell instanceof IPython.MarkdownCell)) {
711 711 var target_cell = this.insert_cell_below('markdown',i);
712 712 var text = source_cell.get_text();
713 713 if (text === source_cell.placeholder) {
714 714 text = '';
715 715 };
716 716 // The edit must come before the set_text.
717 717 target_cell.edit();
718 718 target_cell.set_text(text);
719 719 // make this value the starting point, so that we can only undo
720 720 // to this state, instead of a blank cell
721 721 target_cell.code_mirror.clearHistory();
722 722 source_element.remove();
723 723 this.dirty = true;
724 724 };
725 725 };
726 726 };
727 727
728 728
729 729 Notebook.prototype.to_html = function (index) {
730 730 var i = this.index_or_selected(index);
731 731 if (this.is_valid_cell_index(i)) {
732 732 var source_element = this.get_cell_element(i);
733 733 var source_cell = source_element.data("cell");
734 734 var target_cell = null;
735 735 if (!(source_cell instanceof IPython.HTMLCell)) {
736 736 target_cell = this.insert_cell_below('html',i);
737 737 var text = source_cell.get_text();
738 738 if (text === source_cell.placeholder) {
739 739 text = '';
740 740 };
741 741 // The edit must come before the set_text.
742 742 target_cell.edit();
743 743 target_cell.set_text(text);
744 744 // make this value the starting point, so that we can only undo
745 745 // to this state, instead of a blank cell
746 746 target_cell.code_mirror.clearHistory();
747 747 source_element.remove();
748 748 this.dirty = true;
749 749 };
750 750 };
751 751 };
752 752
753 753
754 754 Notebook.prototype.to_raw = function (index) {
755 755 var i = this.index_or_selected(index);
756 756 if (this.is_valid_cell_index(i)) {
757 757 var source_element = this.get_cell_element(i);
758 758 var source_cell = source_element.data("cell");
759 759 var target_cell = null;
760 760 if (!(source_cell instanceof IPython.RawCell)) {
761 761 target_cell = this.insert_cell_below('raw',i);
762 762 var text = source_cell.get_text();
763 763 if (text === source_cell.placeholder) {
764 764 text = '';
765 765 };
766 766 // The edit must come before the set_text.
767 767 target_cell.edit();
768 768 target_cell.set_text(text);
769 769 // make this value the starting point, so that we can only undo
770 770 // to this state, instead of a blank cell
771 771 target_cell.code_mirror.clearHistory();
772 772 source_element.remove();
773 773 this.dirty = true;
774 774 };
775 775 };
776 776 };
777 777
778 778
779 779 Notebook.prototype.to_heading = function (index, level) {
780 780 level = level || 1;
781 781 var i = this.index_or_selected(index);
782 782 if (this.is_valid_cell_index(i)) {
783 783 var source_element = this.get_cell_element(i);
784 784 var source_cell = source_element.data("cell");
785 785 var target_cell = null;
786 786 if (source_cell instanceof IPython.HeadingCell) {
787 787 source_cell.set_level(level);
788 788 } else {
789 789 target_cell = this.insert_cell_below('heading',i);
790 790 var text = source_cell.get_text();
791 791 if (text === source_cell.placeholder) {
792 792 text = '';
793 793 };
794 794 // The edit must come before the set_text.
795 795 target_cell.set_level(level);
796 796 target_cell.edit();
797 797 target_cell.set_text(text);
798 798 // make this value the starting point, so that we can only undo
799 799 // to this state, instead of a blank cell
800 800 target_cell.code_mirror.clearHistory();
801 801 source_element.remove();
802 802 this.dirty = true;
803 803 };
804 804 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
805 805 {'cell_type':'heading',level:level}
806 806 );
807 807 };
808 808 };
809 809
810 810
811 811 // Cut/Copy/Paste
812 812
813 813 Notebook.prototype.enable_paste = function () {
814 814 var that = this;
815 815 if (!this.paste_enabled) {
816 816 $('#paste_cell_replace').removeClass('ui-state-disabled')
817 817 .on('click', function () {that.paste_cell_replace();});
818 818 $('#paste_cell_above').removeClass('ui-state-disabled')
819 819 .on('click', function () {that.paste_cell_above();});
820 820 $('#paste_cell_below').removeClass('ui-state-disabled')
821 821 .on('click', function () {that.paste_cell_below();});
822 822 this.paste_enabled = true;
823 823 };
824 824 };
825 825
826 826
827 827 Notebook.prototype.disable_paste = function () {
828 828 if (this.paste_enabled) {
829 829 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
830 830 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
831 831 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
832 832 this.paste_enabled = false;
833 833 };
834 834 };
835 835
836 836
837 837 Notebook.prototype.cut_cell = function () {
838 838 this.copy_cell();
839 839 this.delete_cell();
840 840 }
841 841
842 842 Notebook.prototype.copy_cell = function () {
843 843 var cell = this.get_selected_cell();
844 844 this.clipboard = cell.toJSON();
845 845 this.enable_paste();
846 846 };
847 847
848 848
849 849 Notebook.prototype.paste_cell_replace = function () {
850 850 if (this.clipboard !== null && this.paste_enabled) {
851 851 var cell_data = this.clipboard;
852 852 var new_cell = this.insert_cell_above(cell_data.cell_type);
853 853 new_cell.fromJSON(cell_data);
854 854 var old_cell = this.get_next_cell(new_cell);
855 855 this.delete_cell(this.find_cell_index(old_cell));
856 856 this.select(this.find_cell_index(new_cell));
857 857 };
858 858 };
859 859
860 860
861 861 Notebook.prototype.paste_cell_above = function () {
862 862 if (this.clipboard !== null && this.paste_enabled) {
863 863 var cell_data = this.clipboard;
864 864 var new_cell = this.insert_cell_above(cell_data.cell_type);
865 865 new_cell.fromJSON(cell_data);
866 866 };
867 867 };
868 868
869 869
870 870 Notebook.prototype.paste_cell_below = function () {
871 871 if (this.clipboard !== null && this.paste_enabled) {
872 872 var cell_data = this.clipboard;
873 873 var new_cell = this.insert_cell_below(cell_data.cell_type);
874 874 new_cell.fromJSON(cell_data);
875 875 };
876 876 };
877 877
878 878 // Cell undelete
879 879
880 880 Notebook.prototype.undelete = function() {
881 881 if (this.undelete_backup !== null && this.undelete_index !== null) {
882 882 var current_index = this.get_selected_index();
883 883 if (this.undelete_index < current_index) {
884 884 current_index = current_index + 1;
885 885 }
886 886 if (this.undelete_index >= this.ncells()) {
887 887 this.select(this.ncells() - 1);
888 888 }
889 889 else {
890 890 this.select(this.undelete_index);
891 891 }
892 892 var cell_data = this.undelete_backup;
893 893 var new_cell = null;
894 894 if (this.undelete_below) {
895 895 new_cell = this.insert_cell_below(cell_data.cell_type);
896 896 } else {
897 897 new_cell = this.insert_cell_above(cell_data.cell_type);
898 898 }
899 899 new_cell.fromJSON(cell_data);
900 900 this.select(current_index);
901 901 this.undelete_backup = null;
902 902 this.undelete_index = null;
903 903 }
904 904 }
905 905
906 906 // Split/merge
907 907
908 908 Notebook.prototype.split_cell = function () {
909 909 // Todo: implement spliting for other cell types.
910 910 var cell = this.get_selected_cell();
911 911 if (cell.is_splittable()) {
912 912 var texta = cell.get_pre_cursor();
913 913 var textb = cell.get_post_cursor();
914 914 if (cell instanceof IPython.CodeCell) {
915 915 cell.set_text(texta);
916 916 var new_cell = this.insert_cell_below('code');
917 917 new_cell.set_text(textb);
918 918 } else if (cell instanceof IPython.MarkdownCell) {
919 919 cell.set_text(texta);
920 920 cell.render();
921 921 var new_cell = this.insert_cell_below('markdown');
922 922 new_cell.edit(); // editor must be visible to call set_text
923 923 new_cell.set_text(textb);
924 924 new_cell.render();
925 925 } else if (cell instanceof IPython.HTMLCell) {
926 926 cell.set_text(texta);
927 927 cell.render();
928 928 var new_cell = this.insert_cell_below('html');
929 929 new_cell.edit(); // editor must be visible to call set_text
930 930 new_cell.set_text(textb);
931 931 new_cell.render();
932 932 };
933 933 };
934 934 };
935 935
936 936
937 937 Notebook.prototype.merge_cell_above = function () {
938 938 var index = this.get_selected_index();
939 939 var cell = this.get_cell(index);
940 940 if (index > 0) {
941 941 var upper_cell = this.get_cell(index-1);
942 942 var upper_text = upper_cell.get_text();
943 943 var text = cell.get_text();
944 944 if (cell instanceof IPython.CodeCell) {
945 945 cell.set_text(upper_text+'\n'+text);
946 946 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
947 947 cell.edit();
948 948 cell.set_text(upper_text+'\n'+text);
949 949 cell.render();
950 950 };
951 951 this.delete_cell(index-1);
952 952 this.select(this.find_cell_index(cell));
953 953 };
954 954 };
955 955
956 956
957 957 Notebook.prototype.merge_cell_below = function () {
958 958 var index = this.get_selected_index();
959 959 var cell = this.get_cell(index);
960 960 if (index < this.ncells()-1) {
961 961 var lower_cell = this.get_cell(index+1);
962 962 var lower_text = lower_cell.get_text();
963 963 var text = cell.get_text();
964 964 if (cell instanceof IPython.CodeCell) {
965 965 cell.set_text(text+'\n'+lower_text);
966 966 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
967 967 cell.edit();
968 968 cell.set_text(text+'\n'+lower_text);
969 969 cell.render();
970 970 };
971 971 this.delete_cell(index+1);
972 972 this.select(this.find_cell_index(cell));
973 973 };
974 974 };
975 975
976 976
977 977 // Cell collapsing and output clearing
978 978
979 979 Notebook.prototype.collapse = function (index) {
980 980 var i = this.index_or_selected(index);
981 981 this.get_cell(i).collapse();
982 982 this.dirty = true;
983 983 };
984 984
985 985
986 986 Notebook.prototype.expand = function (index) {
987 987 var i = this.index_or_selected(index);
988 988 this.get_cell(i).expand();
989 989 this.dirty = true;
990 990 };
991 991
992 992
993 993 Notebook.prototype.toggle_output = function (index) {
994 994 var i = this.index_or_selected(index);
995 995 this.get_cell(i).toggle_output();
996 996 this.dirty = true;
997 997 };
998 998
999 999
1000 1000 Notebook.prototype.toggle_output_scroll = function (index) {
1001 1001 var i = this.index_or_selected(index);
1002 1002 this.get_cell(i).toggle_output_scroll();
1003 1003 };
1004 1004
1005 1005
1006 1006 Notebook.prototype.collapse_all_output = function () {
1007 1007 var ncells = this.ncells();
1008 1008 var cells = this.get_cells();
1009 1009 for (var i=0; i<ncells; i++) {
1010 1010 if (cells[i] instanceof IPython.CodeCell) {
1011 1011 cells[i].output_area.collapse();
1012 1012 }
1013 1013 };
1014 1014 // this should not be set if the `collapse` key is removed from nbformat
1015 1015 this.dirty = true;
1016 1016 };
1017 1017
1018 1018
1019 1019 Notebook.prototype.scroll_all_output = function () {
1020 1020 var ncells = this.ncells();
1021 1021 var cells = this.get_cells();
1022 1022 for (var i=0; i<ncells; i++) {
1023 1023 if (cells[i] instanceof IPython.CodeCell) {
1024 1024 cells[i].output_area.expand();
1025 1025 cells[i].output_area.scroll_if_long(20);
1026 1026 }
1027 1027 };
1028 1028 // this should not be set if the `collapse` key is removed from nbformat
1029 1029 this.dirty = true;
1030 1030 };
1031 1031
1032 1032
1033 1033 Notebook.prototype.expand_all_output = function () {
1034 1034 var ncells = this.ncells();
1035 1035 var cells = this.get_cells();
1036 1036 for (var i=0; i<ncells; i++) {
1037 1037 if (cells[i] instanceof IPython.CodeCell) {
1038 1038 cells[i].output_area.expand();
1039 1039 cells[i].output_area.unscroll_area();
1040 1040 }
1041 1041 };
1042 1042 // this should not be set if the `collapse` key is removed from nbformat
1043 1043 this.dirty = true;
1044 1044 };
1045 1045
1046 1046
1047 1047 Notebook.prototype.clear_all_output = function () {
1048 1048 var ncells = this.ncells();
1049 1049 var cells = this.get_cells();
1050 1050 for (var i=0; i<ncells; i++) {
1051 1051 if (cells[i] instanceof IPython.CodeCell) {
1052 1052 cells[i].clear_output(true,true,true);
1053 1053 // Make all In[] prompts blank, as well
1054 1054 // TODO: make this configurable (via checkbox?)
1055 1055 cells[i].set_input_prompt();
1056 1056 }
1057 1057 };
1058 1058 this.dirty = true;
1059 1059 };
1060 1060
1061 1061
1062 1062 // Other cell functions: line numbers, ...
1063 1063
1064 1064 Notebook.prototype.cell_toggle_line_numbers = function() {
1065 1065 this.get_selected_cell().toggle_line_numbers();
1066 1066 };
1067 1067
1068 1068 // Kernel related things
1069 1069
1070 1070 Notebook.prototype.start_kernel = function () {
1071 1071 var base_url = $('body').data('baseKernelUrl') + "kernels";
1072 1072 this.kernel = new IPython.Kernel(base_url);
1073 1073 this.kernel.start(this.notebook_id);
1074 1074 // Now that the kernel has been created, tell the CodeCells about it.
1075 1075 var ncells = this.ncells();
1076 1076 for (var i=0; i<ncells; i++) {
1077 1077 var cell = this.get_cell(i);
1078 1078 if (cell instanceof IPython.CodeCell) {
1079 1079 cell.set_kernel(this.kernel)
1080 1080 };
1081 1081 };
1082 1082 };
1083 1083
1084 1084
1085 1085 Notebook.prototype.restart_kernel = function () {
1086 1086 var that = this;
1087 1087 var dialog = $('<div/>');
1088 1088 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
1089 1089 $(document).append(dialog);
1090 1090 dialog.dialog({
1091 1091 resizable: false,
1092 1092 modal: true,
1093 1093 title: "Restart kernel or continue running?",
1094 1094 closeText: '',
1095 1095 buttons : {
1096 1096 "Restart": function () {
1097 1097 that.kernel.restart();
1098 1098 $(this).dialog('close');
1099 1099 },
1100 1100 "Continue running": function () {
1101 1101 $(this).dialog('close');
1102 1102 }
1103 1103 }
1104 1104 });
1105 1105 };
1106 1106
1107 1107
1108 1108 Notebook.prototype.execute_selected_cell = function (options) {
1109 1109 // add_new: should a new cell be added if we are at the end of the nb
1110 1110 // terminal: execute in terminal mode, which stays in the current cell
1111 1111 var default_options = {terminal: false, add_new: true};
1112 1112 $.extend(default_options, options);
1113 1113 var that = this;
1114 1114 var cell = that.get_selected_cell();
1115 1115 var cell_index = that.find_cell_index(cell);
1116 1116 if (cell instanceof IPython.CodeCell) {
1117 1117 cell.execute();
1118 1118 } else if (cell instanceof IPython.HTMLCell) {
1119 1119 cell.render();
1120 1120 }
1121 1121 if (default_options.terminal) {
1122 1122 cell.select_all();
1123 1123 } else {
1124 1124 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1125 1125 that.insert_cell_below('code');
1126 1126 // If we are adding a new cell at the end, scroll down to show it.
1127 1127 that.scroll_to_bottom();
1128 1128 } else {
1129 1129 that.select(cell_index+1);
1130 1130 };
1131 1131 };
1132 1132 this.dirty = true;
1133 1133 };
1134 1134
1135 1135
1136 1136 Notebook.prototype.execute_cells_below = function () {
1137 1137 this.execute_cell_range(this.get_selected_index(), this.ncells());
1138 1138 this.scroll_to_bottom();
1139 1139 };
1140 1140
1141 1141 Notebook.prototype.execute_cells_above = function () {
1142 1142 this.execute_cell_range(0, this.get_selected_index());
1143 1143 };
1144 1144
1145 1145 Notebook.prototype.execute_all_cells = function () {
1146 1146 this.execute_cell_range(0, this.ncells());
1147 1147 that.scroll_to_bottom();
1148 1148 };
1149 1149
1150 1150 Notebook.prototype.execute_cell_range = function (start, end) {
1151 1151 for (var i=start; i<end; i++) {
1152 1152 this.select(i);
1153 1153 this.execute_selected_cell({add_new:false});
1154 1154 };
1155 1155 };
1156 1156
1157 1157 // Persistance and loading
1158 1158
1159 1159 Notebook.prototype.get_notebook_id = function () {
1160 1160 return this.notebook_id;
1161 1161 };
1162 1162
1163 1163
1164 1164 Notebook.prototype.get_notebook_name = function () {
1165 1165 return this.notebook_name;
1166 1166 };
1167 1167
1168 1168
1169 1169 Notebook.prototype.set_notebook_name = function (name) {
1170 1170 this.notebook_name = name;
1171 1171 };
1172 1172
1173 1173
1174 1174 Notebook.prototype.test_notebook_name = function (nbname) {
1175 1175 nbname = nbname || '';
1176 1176 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1177 1177 return true;
1178 1178 } else {
1179 1179 return false;
1180 1180 };
1181 1181 };
1182 1182
1183 1183
1184 1184 Notebook.prototype.fromJSON = function (data) {
1185 1185 var ncells = this.ncells();
1186 1186 var i;
1187 1187 for (i=0; i<ncells; i++) {
1188 1188 // Always delete cell 0 as they get renumbered as they are deleted.
1189 1189 this.delete_cell(0);
1190 1190 };
1191 1191 // Save the metadata and name.
1192 1192 this.metadata = data.metadata;
1193 1193 this.notebook_name = data.metadata.name;
1194 1194 // Only handle 1 worksheet for now.
1195 1195 var worksheet = data.worksheets[0];
1196 1196 if (worksheet !== undefined) {
1197 1197 if (worksheet.metadata) {
1198 1198 this.worksheet_metadata = worksheet.metadata;
1199 1199 }
1200 1200 var new_cells = worksheet.cells;
1201 1201 ncells = new_cells.length;
1202 1202 var cell_data = null;
1203 1203 var new_cell = null;
1204 1204 for (i=0; i<ncells; i++) {
1205 1205 cell_data = new_cells[i];
1206 1206 // VERSIONHACK: plaintext -> raw
1207 1207 // handle never-released plaintext name for raw cells
1208 1208 if (cell_data.cell_type === 'plaintext'){
1209 1209 cell_data.cell_type = 'raw';
1210 1210 }
1211 1211
1212 1212 new_cell = this.insert_cell_below(cell_data.cell_type);
1213 1213 new_cell.fromJSON(cell_data);
1214 1214 };
1215 1215 };
1216 1216 if (data.worksheets.length > 1) {
1217 1217 var dialog = $('<div/>');
1218 1218 dialog.html("This notebook has " + data.worksheets.length + " worksheets, " +
1219 1219 "but this version of IPython can only handle the first. " +
1220 1220 "If you save this notebook, worksheets after the first will be lost."
1221 1221 );
1222 1222 this.element.append(dialog);
1223 1223 dialog.dialog({
1224 1224 resizable: false,
1225 1225 modal: true,
1226 1226 title: "Multiple worksheets",
1227 1227 closeText: "",
1228 1228 close: function(event, ui) {$(this).dialog('destroy').remove();},
1229 1229 buttons : {
1230 1230 "OK": function () {
1231 1231 $(this).dialog('close');
1232 1232 }
1233 1233 },
1234 1234 width: 400
1235 1235 });
1236 1236 }
1237 1237 };
1238 1238
1239 1239
1240 1240 Notebook.prototype.toJSON = function () {
1241 1241 var cells = this.get_cells();
1242 1242 var ncells = cells.length;
1243 1243 var cell_array = new Array(ncells);
1244 1244 for (var i=0; i<ncells; i++) {
1245 1245 cell_array[i] = cells[i].toJSON();
1246 1246 };
1247 1247 var data = {
1248 1248 // Only handle 1 worksheet for now.
1249 1249 worksheets : [{
1250 1250 cells: cell_array,
1251 1251 metadata: this.worksheet_metadata
1252 1252 }],
1253 1253 metadata : this.metadata
1254 1254 };
1255 1255 return data;
1256 1256 };
1257 1257
1258 1258 Notebook.prototype.save_notebook = function () {
1259 1259 // We may want to move the name/id/nbformat logic inside toJSON?
1260 1260 var data = this.toJSON();
1261 1261 data.metadata.name = this.notebook_name;
1262 1262 data.nbformat = this.nbformat;
1263 1263 data.nbformat_minor = this.nbformat_minor;
1264 1264 // We do the call with settings so we can set cache to false.
1265 1265 var settings = {
1266 1266 processData : false,
1267 1267 cache : false,
1268 1268 type : "PUT",
1269 1269 data : JSON.stringify(data),
1270 1270 headers : {'Content-Type': 'application/json'},
1271 1271 success : $.proxy(this.save_notebook_success,this),
1272 1272 error : $.proxy(this.save_notebook_error,this)
1273 1273 };
1274 1274 $([IPython.events]).trigger('notebook_saving.Notebook');
1275 1275 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1276 1276 $.ajax(url, settings);
1277 1277 };
1278 1278
1279 1279
1280 1280 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1281 1281 this.dirty = false;
1282 1282 $([IPython.events]).trigger('notebook_saved.Notebook');
1283 1283 };
1284 1284
1285 1285
1286 1286 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1287 1287 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1288 1288 };
1289 1289
1290 1290
1291 1291 Notebook.prototype.load_notebook = function (notebook_id) {
1292 1292 var that = this;
1293 1293 this.notebook_id = notebook_id;
1294 1294 // We do the call with settings so we can set cache to false.
1295 1295 var settings = {
1296 1296 processData : false,
1297 1297 cache : false,
1298 1298 type : "GET",
1299 1299 dataType : "json",
1300 1300 success : $.proxy(this.load_notebook_success,this),
1301 1301 error : $.proxy(this.load_notebook_error,this),
1302 1302 };
1303 1303 $([IPython.events]).trigger('notebook_loading.Notebook');
1304 1304 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1305 1305 $.ajax(url, settings);
1306 1306 };
1307 1307
1308 1308
1309 1309 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1310 1310 this.fromJSON(data);
1311 1311 if (this.ncells() === 0) {
1312 1312 this.insert_cell_below('code');
1313 1313 };
1314 1314 this.dirty = false;
1315 1315 this.select(0);
1316 1316 this.scroll_to_top();
1317 1317 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1318 1318 msg = "This notebook has been converted from an older " +
1319 1319 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1320 1320 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1321 1321 "newer notebook format will be used and older verions of IPython " +
1322 1322 "may not be able to read it. To keep the older version, close the " +
1323 1323 "notebook without saving it.";
1324 1324 var dialog = $('<div/>');
1325 1325 dialog.html(msg);
1326 1326 this.element.append(dialog);
1327 1327 dialog.dialog({
1328 1328 resizable: false,
1329 1329 modal: true,
1330 1330 title: "Notebook converted",
1331 1331 closeText: "",
1332 1332 close: function(event, ui) {$(this).dialog('destroy').remove();},
1333 1333 buttons : {
1334 1334 "OK": function () {
1335 1335 $(this).dialog('close');
1336 1336 }
1337 1337 },
1338 1338 width: 400
1339 1339 });
1340 1340 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1341 1341 var that = this;
1342 1342 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1343 1343 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1344 1344 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1345 1345 this_vs + ". You can still work with this notebook, but some features " +
1346 1346 "introduced in later notebook versions may not be available."
1347 1347
1348 1348 var dialog = $('<div/>');
1349 1349 dialog.html(msg);
1350 1350 this.element.append(dialog);
1351 1351 dialog.dialog({
1352 1352 resizable: false,
1353 1353 modal: true,
1354 1354 title: "Newer Notebook",
1355 1355 closeText: "",
1356 1356 close: function(event, ui) {$(this).dialog('destroy').remove();},
1357 1357 buttons : {
1358 1358 "OK": function () {
1359 1359 $(this).dialog('close');
1360 1360 }
1361 1361 },
1362 1362 width: 400
1363 1363 });
1364 1364
1365 1365 }
1366 1366 // Create the kernel after the notebook is completely loaded to prevent
1367 1367 // code execution upon loading, which is a security risk.
1368 1368 if (! this.read_only) {
1369 1369 this.start_kernel();
1370 1370 }
1371 1371 $([IPython.events]).trigger('notebook_loaded.Notebook');
1372 1372 };
1373 1373
1374 1374
1375 1375 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1376 1376 if (xhr.status === 500) {
1377 1377 var msg = "An error occurred while loading this notebook. Most likely " +
1378 1378 "this notebook is in a newer format than is supported by this " +
1379 1379 "version of IPython. This version can load notebook formats " +
1380 1380 "v"+this.nbformat+" or earlier.";
1381 1381 var dialog = $('<div/>');
1382 1382 dialog.html(msg);
1383 1383 this.element.append(dialog);
1384 1384 dialog.dialog({
1385 1385 resizable: false,
1386 1386 modal: true,
1387 1387 title: "Error loading notebook",
1388 1388 closeText: "",
1389 1389 close: function(event, ui) {$(this).dialog('destroy').remove();},
1390 1390 buttons : {
1391 1391 "OK": function () {
1392 1392 $(this).dialog('close');
1393 1393 }
1394 1394 },
1395 1395 width: 400
1396 1396 });
1397 1397 }
1398 1398 }
1399 1399
1400 1400 IPython.Notebook = Notebook;
1401 1401
1402 1402
1403 1403 return IPython;
1404 1404
1405 1405 }(IPython));
1406 1406
General Comments 0
You need to be logged in to leave comments. Login now