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