##// END OF EJS Templates
Refactoring of the notebooks cell management....
Brian Granger -
Show More
@@ -49,7 +49,7 b' var IPython = (function (IPython) {'
49 49
50 50
51 51 FullEditWidget.prototype.open = function () {
52 var cell = IPython.notebook.selected_cell();
52 var cell = IPython.notebook.get_selected_cell();
53 53 if (!this.opened && cell instanceof IPython.CodeCell) {
54 54 $('#fulledit_widget').show();
55 55 $('#main_app').hide();
@@ -75,7 +75,7 b' var IPython = (function (IPython) {'
75 75 $('#menubar').show();
76 76 $('body').css({overflow : 'hidden'});
77 77 var code = this.ace_editor.getSession().getValue();
78 var cell = IPython.notebook.selected_cell();
78 var cell = IPython.notebook.get_selected_cell();
79 79 cell.set_text(code);
80 80 cell.select();
81 81 this.opened = false;
@@ -26,7 +26,7 b' var IPython = (function (IPython) {'
26 26 select : function (event, ui) {
27 27 // The selected cell loses focus when the menu is entered, so we
28 28 // re-select it upon selection.
29 var i = IPython.notebook.selected_index();
29 var i = IPython.notebook.get_selected_index();
30 30 IPython.notebook.select(i);
31 31 }
32 32 });
@@ -100,10 +100,10 b' var IPython = (function (IPython) {'
100 100 });
101 101 // Insert
102 102 this.element.find('#insert_cell_above').click(function () {
103 IPython.notebook.insert_code_cell_above();
103 IPython.notebook.insert_cell_above('code');
104 104 });
105 105 this.element.find('#insert_cell_below').click(function () {
106 IPython.notebook.insert_code_cell_below();
106 IPython.notebook.insert_cell_below('code');
107 107 });
108 108 // Cell
109 109 this.element.find('#full_edit_cell').click(function () {
This diff has been collapsed as it changes many lines, (504 lines changed) Show them Hide them
@@ -47,11 +47,11 b' var IPython = (function (IPython) {'
47 47 // ii) to prevent the div from scrolling up when the last cell is being
48 48 // edited, but is too low on the page, which browsers will do automatically.
49 49 var that = this;
50 var end_space = $('<div class="end_space"></div>').height("30%");
50 var end_space = $('<div/>').addClass('end_space').height("30%");
51 51 end_space.dblclick(function (e) {
52 52 if (that.read_only) return;
53 53 var ncells = that.ncells();
54 that.insert_code_cell_below(ncells-1);
54 that.insert_cell_below('code',ncells-1);
55 55 });
56 56 this.element.append(end_space);
57 57 $('div#notebook').addClass('border-box-sizing');
@@ -69,13 +69,13 b' var IPython = (function (IPython) {'
69 69 event.preventDefault();
70 70 }
71 71 if (event.which === 38 && !event.shiftKey) {
72 var cell = that.selected_cell();
72 var cell = that.get_selected_cell();
73 73 if (cell.at_top()) {
74 74 event.preventDefault();
75 75 that.select_prev();
76 76 };
77 77 } else if (event.which === 40 && !event.shiftKey) {
78 var cell = that.selected_cell();
78 var cell = that.get_selected_cell();
79 79 if (cell.at_bottom()) {
80 80 event.preventDefault();
81 81 that.select_next();
@@ -111,12 +111,12 b' var IPython = (function (IPython) {'
111 111 return false;
112 112 } else if (event.which === 65 && that.control_key_active) {
113 113 // Insert code cell above selected = a
114 that.insert_code_cell_above();
114 that.insert_cell_above('code');
115 115 that.control_key_active = false;
116 116 return false;
117 117 } else if (event.which === 66 && that.control_key_active) {
118 118 // Insert code cell below selected = b
119 that.insert_code_cell_below();
119 that.insert_cell_below('code');
120 120 that.control_key_active = false;
121 121 return false;
122 122 } else if (event.which === 89 && that.control_key_active) {
@@ -234,29 +234,67 b' var IPython = (function (IPython) {'
234 234
235 235 // Cell indexing, retrieval, etc.
236 236
237
238 Notebook.prototype.cell_elements = function () {
237 Notebook.prototype.get_cell_elements = function () {
239 238 return this.element.children("div.cell");
240 239 };
241 240
242 241
242 Notebook.prototype.get_cell_element = function (index) {
243 var result = null;
244 var e = this.get_cell_elements().eq(index);
245 if (e.length !== 0) {
246 result = e;
247 }
248 return result;
249 };
250
251
243 252 Notebook.prototype.ncells = function (cell) {
244 return this.cell_elements().length;
253 return this.get_cell_elements().length;
245 254 };
246 255
247 256
248 257 // TODO: we are often calling cells as cells()[i], which we should optimize
249 258 // to cells(i) or a new method.
250 Notebook.prototype.cells = function () {
251 return this.cell_elements().toArray().map(function (e) {
259 Notebook.prototype.get_cells = function () {
260 return this.get_cell_elements().toArray().map(function (e) {
252 261 return $(e).data("cell");
253 262 });
254 263 };
255 264
256 265
266 Notebook.prototype.get_cell = function (index) {
267 var result = null;
268 var ce = this.get_cell_element(index);
269 if (ce !== null) {
270 result = ce.data('cell');
271 }
272 return result;
273 }
274
275
276 Notebook.prototype.get_next_cell = function (cell) {
277 var result = null;
278 var index = this.find_cell_index(cell);
279 if (index !== null && index < this.ncells()) {
280 result = this.get_cell(index+1);
281 }
282 return result;
283 }
284
285
286 Notebook.prototype.get_prev_cell = function (cell) {
287 var result = null;
288 var index = this.find_cell_index(cell);
289 if (index !== null && index > 1) {
290 result = this.get_cell(index-1);
291 }
292 return result;
293 }
294
257 295 Notebook.prototype.find_cell_index = function (cell) {
258 296 var result = null;
259 this.cell_elements().filter(function (index) {
297 this.get_cell_elements().filter(function (index) {
260 298 if ($(this).data("cell") === cell) {
261 299 result = index;
262 300 };
@@ -267,8 +305,8 b' var IPython = (function (IPython) {'
267 305
268 306 Notebook.prototype.index_or_selected = function (index) {
269 307 var i;
270 if (index === undefined) {
271 i = this.selected_index();
308 if (index === undefined || index === null) {
309 i = this.get_selected_index();
272 310 if (i === null) {
273 311 i = 0;
274 312 }
@@ -279,38 +317,23 b' var IPython = (function (IPython) {'
279 317 };
280 318
281 319
282 Notebook.prototype.select = function (index) {
283 if (index !== undefined && index >= 0 && index < this.ncells()) {
284 if (this.selected_index() !== null) {
285 this.selected_cell().unselect();
286 };
287 this.cells()[index].select();
288 };
289 return this;
290 };
291
292
293 Notebook.prototype.select_next = function () {
294 var index = this.selected_index();
295 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
296 this.select(index+1);
297 };
298 return this;
320 Notebook.prototype.get_selected_cell = function () {
321 var index = this.get_selected_index();
322 return this.get_cell(index);
299 323 };
300 324
301 325
302 Notebook.prototype.select_prev = function () {
303 var index = this.selected_index();
304 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
305 this.select(index-1);
326 Notebook.prototype.is_valid_cell_index = function (index) {
327 if (index !== null && index >= 0 && index < this.ncells()) {
328 return true;
329 } else {
330 return false;
306 331 };
307 return this;
308 };
309
332 }
310 333
311 Notebook.prototype.selected_index = function () {
334 Notebook.prototype.get_selected_index = function () {
312 335 var result = null;
313 this.cell_elements().filter(function (index) {
336 this.get_cell_elements().filter(function (index) {
314 337 if ($(this).data("cell").selected === true) {
315 338 result = index;
316 339 };
@@ -322,7 +345,7 b' var IPython = (function (IPython) {'
322 345 Notebook.prototype.cell_for_msg = function (msg_id) {
323 346 var cell_id = this.msg_cell_map[msg_id];
324 347 var result = null;
325 this.cell_elements().filter(function (index) {
348 this.get_cell_elements().filter(function (index) {
326 349 cell = $(this).data("cell");
327 350 if (cell.cell_id === cell_id) {
328 351 result = cell;
@@ -332,68 +355,45 b' var IPython = (function (IPython) {'
332 355 };
333 356
334 357
335 Notebook.prototype.selected_cell = function () {
336 return this.cell_elements().eq(this.selected_index()).data("cell");
337 };
338
339
340 // Cell insertion, deletion and moving.
358 // Cell selection.
341 359
342 Notebook.prototype.delete_cell = function (index) {
343 var i = this.index_or_selected(index);
344 if (i !== null && i >= 0 && i < this.ncells()) {
345 this.cell_elements().eq(i).remove();
346 if (i === (this.ncells())) {
347 this.select(i-1);
348 } else {
349 this.select(i);
360 Notebook.prototype.select = function (index) {
361 if (index !== undefined && index >= 0 && index < this.ncells()) {
362 sindex = this.get_selected_index()
363 if (sindex !== null) {
364 this.get_cell(sindex).unselect();
350 365 };
366 this.get_cell(index).select();
351 367 };
352 this.dirty = true;
353 368 return this;
354 369 };
355 370
356 371
357 Notebook.prototype.append_cell = function (cell) {
358 this.element.find('div.end_space').before(cell.element);
359 this.dirty = true;
360 return this;
361 };
362
363
364 Notebook.prototype.insert_cell_below = function (cell, index) {
365 var ncells = this.ncells();
366 if (ncells === 0) {
367 this.append_cell(cell);
368 return this;
369 };
370 if (index >= 0 && index < ncells) {
371 this.cell_elements().eq(index).after(cell.element);
372 Notebook.prototype.select_next = function () {
373 var index = this.get_selected_index();
374 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
375 this.select(index+1);
372 376 };
373 this.dirty = true;
374 377 return this;
375 378 };
376 379
377 380
378 Notebook.prototype.insert_cell_above = function (cell, index) {
379 var ncells = this.ncells();
380 if (ncells === 0) {
381 this.append_cell(cell);
382 return this;
383 };
384 if (index >= 0 && index < ncells) {
385 this.cell_elements().eq(index).before(cell.element);
381 Notebook.prototype.select_prev = function () {
382 var index = this.get_selected_index();
383 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
384 this.select(index-1);
386 385 };
387 this.dirty = true;
388 386 return this;
389 387 };
390 388
391 389
390 // Cell movement
391
392 392 Notebook.prototype.move_cell_up = function (index) {
393 var i = index || this.selected_index();
393 var i = this.index_or_selected();
394 394 if (i !== null && i < this.ncells() && i > 0) {
395 var pivot = this.cell_elements().eq(i-1);
396 var tomove = this.cell_elements().eq(i);
395 var pivot = this.get_cell_element(i-1);
396 var tomove = this.get_cell_element(i);
397 397 if (pivot !== null && tomove !== null) {
398 398 tomove.detach();
399 399 pivot.before(tomove);
@@ -406,10 +406,10 b' var IPython = (function (IPython) {'
406 406
407 407
408 408 Notebook.prototype.move_cell_down = function (index) {
409 var i = index || this.selected_index();
409 var i = this.index_or_selected();
410 410 if (i !== null && i < (this.ncells()-1) && i >= 0) {
411 var pivot = this.cell_elements().eq(i+1);
412 var tomove = this.cell_elements().eq(i);
411 var pivot = this.get_cell_element(i+1);
412 var tomove = this.get_cell_element(i);
413 413 if (pivot !== null && tomove !== null) {
414 414 tomove.detach();
415 415 pivot.after(tomove);
@@ -422,14 +422,16 b' var IPython = (function (IPython) {'
422 422
423 423
424 424 Notebook.prototype.sort_cells = function () {
425 // This is not working right now. Calling this will actually crash
426 // the browser. I think there is an infinite loop in here...
425 427 var ncells = this.ncells();
426 var sindex = this.selected_index();
428 var sindex = this.get_selected_index();
427 429 var swapped;
428 430 do {
429 431 swapped = false;
430 432 for (var i=1; i<ncells; i++) {
431 current = this.cell_elements().eq(i).data("cell");
432 previous = this.cell_elements().eq(i-1).data("cell");
433 current = this.get_cell(i);
434 previous = this.get_cell(i-1);
433 435 if (previous.input_prompt_number > current.input_prompt_number) {
434 436 this.move_cell_up(i);
435 437 swapped = true;
@@ -440,146 +442,161 b' var IPython = (function (IPython) {'
440 442 return this;
441 443 };
442 444
445 // Insertion, deletion.
443 446
444 Notebook.prototype.insert_code_cell_above = function (index) {
445 // TODO: Bounds check for i
446 var i = this.index_or_selected(index);
447 var cell = new IPython.CodeCell(this);
448 cell.set_input_prompt();
449 this.insert_cell_above(cell, i);
450 this.select(this.find_cell_index(cell));
451 return cell;
452 };
453
454
455 Notebook.prototype.insert_code_cell_below = function (index) {
456 // TODO: Bounds check for i
457 var i = this.index_or_selected(index);
458 var cell = new IPython.CodeCell(this);
459 cell.set_input_prompt();
460 this.insert_cell_below(cell, i);
461 this.select(this.find_cell_index(cell));
462 return cell;
463 };
464
465
466 Notebook.prototype.insert_html_cell_above = function (index) {
467 // TODO: Bounds check for i
468 var i = this.index_or_selected(index);
469 var cell = new IPython.HTMLCell(this);
470 cell.config_mathjax();
471 this.insert_cell_above(cell, i);
472 this.select(this.find_cell_index(cell));
473 return cell;
474 };
475
476
477 Notebook.prototype.insert_html_cell_below = function (index) {
478 // TODO: Bounds check for i
447 Notebook.prototype.delete_cell = function (index) {
479 448 var i = this.index_or_selected(index);
480 var cell = new IPython.HTMLCell(this);
481 cell.config_mathjax();
482 this.insert_cell_below(cell, i);
483 this.select(this.find_cell_index(cell));
484 return cell;
449 if (this.is_valid_cell_index(i)) {
450 var ce = this.get_cell_element(i);
451 ce.remove();
452 if (i === (this.ncells())) {
453 this.select(i-1);
454 } else {
455 this.select(i);
456 };
457 this.dirty = true;
458 };
459 return this;
485 460 };
486 461
487 462
488 Notebook.prototype.insert_markdown_cell_above = function (index) {
489 // TODO: Bounds check for i
490 var i = this.index_or_selected(index);
491 var cell = new IPython.MarkdownCell(this);
492 cell.config_mathjax();
493 this.insert_cell_above(cell, i);
494 this.select(this.find_cell_index(cell));
495 return cell;
463 Notebook.prototype.insert_cell_below = function (type, index) {
464 // type = ('code','html','markdown')
465 // index = cell index or undefined to insert below selected
466 index = this.index_or_selected(index);
467 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
468 var cell = null;
469 if (type === 'code') {
470 var cell = new IPython.CodeCell(this);
471 cell.set_input_prompt();
472 } else if (type === 'markdown') {
473 var cell = new IPython.MarkdownCell(this);
474 cell.config_mathjax();
475 } else if (type === 'html') {
476 var cell = new IPython.HTMLCell(this);
477 cell.config_mathjax();
478 };
479 if (cell !== null) {
480 if (this.ncells() === 0) {
481 this.element.find('div.end_space').before(cell.element);
482 this.select(this.find_cell_index(cell));
483 this.dirty = true;
484 } else if (this.is_valid_cell_index(index)) {
485 this.get_cell_element(index).after(cell.element);
486 this.select(this.find_cell_index(cell));
487 this.dirty = true;
488 };
489 return cell;
490 };
491 };
496 492 };
497 493
498 494
499 Notebook.prototype.insert_markdown_cell_below = function (index) {
500 // TODO: Bounds check for i
501 var i = this.index_or_selected(index);
502 var cell = new IPython.MarkdownCell(this);
503 cell.config_mathjax();
504 this.insert_cell_below(cell, i);
505 this.select(this.find_cell_index(cell));
506 return cell;
495 Notebook.prototype.insert_cell_above = function (type, index) {
496 // type = ('code','html','markdown')
497 // index = cell index or undefined to insert above selected
498 index = this.index_or_selected(index);
499 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
500 var cell = null;
501 if (type === 'code') {
502 var cell = new IPython.CodeCell(this);
503 cell.set_input_prompt();
504 } else if (type === 'markdown') {
505 var cell = new IPython.MarkdownCell(this);
506 cell.config_mathjax();
507 } else if (type === 'html') {
508 var cell = new IPython.HTMLCell(this);
509 cell.config_mathjax();
510 };
511 if (cell !== null) {
512 if (this.ncells() === 0) {
513 this.element.find('div.end_space').before(cell.element);
514 this.select(this.find_cell_index(cell));
515 this.dirty = true;
516 } else if (this.is_valid_cell_index(index)) {
517 this.get_cell_element(index).before(cell.element);
518 this.select(this.find_cell_index(cell));
519 this.dirty = true;
520 };
521 return cell;
522 };
523 };
507 524 };
508 525
509 526
510 527 Notebook.prototype.to_code = function (index) {
511 // TODO: Bounds check for i
512 528 var i = this.index_or_selected(index);
513 var source_element = this.cell_elements().eq(i);
514 var source_cell = source_element.data("cell");
515 if (!(source_cell instanceof IPython.CodeCell)) {
516 this.insert_code_cell_below(i);
517 var target_cell = this.cells()[i+1];
518 var text = source_cell.get_text();
519 if (text === source_cell.placeholder) {
520 text = '';
521 }
522 target_cell.set_text(text);
523 source_element.remove();
524 target_cell.select();
529 if (this.is_valid_cell_index(i)) {
530 var source_element = this.get_cell_element(i);
531 var source_cell = source_element.data("cell");
532 if (!(source_cell instanceof IPython.CodeCell)) {
533 target_cell = this.insert_cell_below('code',i);
534 var text = source_cell.get_text();
535 if (text === source_cell.placeholder) {
536 text = '';
537 }
538 target_cell.set_text(text);
539 source_element.remove();
540 target_cell.select();
541 };
542 this.dirty = true;
525 543 };
526 this.dirty = true;
527 544 };
528 545
529 546
530 547 Notebook.prototype.to_markdown = function (index) {
531 // TODO: Bounds check for i
532 548 var i = this.index_or_selected(index);
533 var source_element = this.cell_elements().eq(i);
534 var source_cell = source_element.data("cell");
535 var target_cell = null;
536 if (!(source_cell instanceof IPython.MarkdownCell)) {
537 this.insert_markdown_cell_below(i);
538 target_cell = this.cells()[i+1];
539 var text = source_cell.get_text();
540 if (text === source_cell.placeholder) {
541 text = target_cell.placeholder;
549 if (this.is_valid_cell_index(i)) {
550 var source_element = this.get_cell_element(i);
551 var source_cell = source_element.data("cell");
552 var target_cell = null;
553 if (!(source_cell instanceof IPython.MarkdownCell)) {
554 target_cell = this.insert_cell_below('markdown',i);
555 var text = source_cell.get_text();
556 if (text === source_cell.placeholder) {
557 text = target_cell.placeholder;
558 };
559 if (target_cell !== null) {
560 if (text === "") {text = target_cell.placeholder;};
561 // The edit must come before the set_text.
562 target_cell.edit();
563 target_cell.set_text(text);
564 source_element.remove();
565 target_cell.select();
566 }
567 this.dirty = true;
542 568 };
543 if (target_cell !== null) {
544 if (text === "") {text = target_cell.placeholder;};
545 // The edit must come before the set_text.
546 target_cell.edit();
547 target_cell.set_text(text);
548 source_element.remove();
549 target_cell.select();
550 }
551 this.dirty = true;
552 569 };
553 570 };
554 571
555 572
556 573 Notebook.prototype.to_html = function (index) {
557 // TODO: Bounds check for i
558 574 var i = this.index_or_selected(index);
559 var source_element = this.cell_elements().eq(i);
560 var source_cell = source_element.data("cell");
561 var target_cell = null;
562 if (!(source_cell instanceof IPython.HTMLCell)) {
563 this.insert_html_cell_below(i);
564 target_cell = this.cells()[i+1];
565 var text = source_cell.get_text();
566 if (text === source_cell.placeholder) {
567 text = target_cell.placeholder;
575 if (this.is_valid_cell_index(i)) {
576 var source_element = this.get_cell_element(i);
577 var source_cell = source_element.data("cell");
578 var target_cell = null;
579 if (!(source_cell instanceof IPython.HTMLCell)) {
580 target_cell = this.insert_cell_below('html',i);
581 var text = source_cell.get_text();
582 if (text === source_cell.placeholder) {
583 text = target_cell.placeholder;
584 };
585 if (target_cell !== null) {
586 if (text === "") {text = target_cell.placeholder;};
587 // The edit must come before the set_text.
588 target_cell.edit();
589 target_cell.set_text(text);
590 source_element.remove();
591 target_cell.select();
592 }
593 this.dirty = true;
568 594 };
569 if (target_cell !== null) {
570 if (text === "") {text = target_cell.placeholder;};
571 // The edit must come before the set_text.
572 target_cell.edit();
573 target_cell.set_text(text);
574 source_element.remove();
575 target_cell.select();
576 }
577 this.dirty = true;
578 595 };
579 596 };
580 597
581 598
582 // Copy/Paste/Merge/Split
599 // Cut/Copy/Paste
583 600
584 601 Notebook.prototype.enable_paste = function () {
585 602 var that = this;
@@ -611,7 +628,7 b' var IPython = (function (IPython) {'
611 628 }
612 629
613 630 Notebook.prototype.copy_cell = function () {
614 var cell = this.selected_cell();
631 var cell = this.get_selected_cell();
615 632 this.clipboard = cell.toJSON();
616 633 this.enable_paste();
617 634 };
@@ -620,16 +637,11 b' var IPython = (function (IPython) {'
620 637 Notebook.prototype.paste_cell = function () {
621 638 if (this.clipboard !== null && this.paste_enabled) {
622 639 var cell_data = this.clipboard;
623 if (cell_data.cell_type == 'code') {
624 new_cell = this.insert_code_cell_above();
625 } else if (cell_data.cell_type === 'html') {
626 new_cell = this.insert_html_cell_above();
627 } else if (cell_data.cell_type === 'markdown') {
628 new_cell = this.insert_markdown_cell_above();
629 };
640 var new_cell = this.insert_cell_above(cell_data.cell_type);
630 641 new_cell.fromJSON(cell_data);
631 this.select_next();
632 this.delete_cell();
642 old_cell = this.get_next_cell(new_cell);
643 this.delete_cell(this.find_cell_index(old_cell));
644 this.select(this.find_cell_index(new_cell));
633 645 };
634 646 };
635 647
@@ -637,13 +649,7 b' var IPython = (function (IPython) {'
637 649 Notebook.prototype.paste_cell_above = function () {
638 650 if (this.clipboard !== null && this.paste_enabled) {
639 651 var cell_data = this.clipboard;
640 if (cell_data.cell_type == 'code') {
641 new_cell = this.insert_code_cell_above();
642 } else if (cell_data.cell_type === 'html') {
643 new_cell = this.insert_html_cell_above();
644 } else if (cell_data.cell_type === 'markdown') {
645 new_cell = this.insert_markdown_cell_above();
646 };
652 var new_cell = this.insert_cell_above(cell_data.cell_type);
647 653 new_cell.fromJSON(cell_data);
648 654 };
649 655 };
@@ -652,21 +658,17 b' var IPython = (function (IPython) {'
652 658 Notebook.prototype.paste_cell_below = function () {
653 659 if (this.clipboard !== null && this.paste_enabled) {
654 660 var cell_data = this.clipboard;
655 if (cell_data.cell_type == 'code') {
656 new_cell = this.insert_code_cell_below();
657 } else if (cell_data.cell_type === 'html') {
658 new_cell = this.insert_html_cell_below();
659 } else if (cell_data.cell_type === 'markdown') {
660 new_cell = this.insert_markdown_cell_below();
661 };
661 var new_cell = this.insert_cell_below(cell_data.cell_type);
662 662 new_cell.fromJSON(cell_data);
663 663 };
664 664 };
665 665
666 666
667 // Split/merge
668
667 669 Notebook.prototype.split_cell = function () {
668 670 // Todo: implement spliting for other cell types.
669 var cell = this.selected_cell();
671 var cell = this.get_selected_cell();
670 672 if (cell instanceof IPython.CodeCell) {
671 673 var cursor = cell.code_mirror.getCursor();
672 674 var last_line_num = cell.code_mirror.lineCount()-1;
@@ -677,7 +679,7 b' var IPython = (function (IPython) {'
677 679 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
678 680 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
679 681 cell.set_text(texta);
680 var new_cell = this.insert_code_cell_below();
682 var new_cell = this.insert_cell_below('code');
681 683 new_cell.set_text(textb);
682 684 };
683 685 };
@@ -685,11 +687,11 b' var IPython = (function (IPython) {'
685 687
686 688 Notebook.prototype.merge_cell_above = function () {
687 689 // Todo: implement merging for other cell types.
688 var cell = this.selected_cell();
689 var index = this.selected_index();
690 var cell = this.get_selected_cell();
691 var index = this.get_selected_index();
690 692 if (index > 0) {
691 upper_cell = this.cells()[index-1];
692 lower_cell = this.cells()[index];
693 upper_cell = this.get_cell(index-1);
694 lower_cell = this.get_cell(index);
693 695 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
694 696 upper_text = upper_cell.get_text();
695 697 lower_text = lower_cell.get_text();
@@ -702,11 +704,11 b' var IPython = (function (IPython) {'
702 704
703 705 Notebook.prototype.merge_cell_below = function () {
704 706 // Todo: implement merging for other cell types.
705 var cell = this.selected_cell();
706 var index = this.selected_index();
707 var cell = this.get_selected_cell();
708 var index = this.get_selected_index();
707 709 if (index < this.ncells()-1) {
708 upper_cell = this.cells()[index];
709 lower_cell = this.cells()[index+1];
710 upper_cell = this.get_cell(index);
711 lower_cell = this.get_cell(index+1);
710 712 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
711 713 upper_text = upper_cell.get_text();
712 714 lower_text = lower_cell.get_text();
@@ -720,21 +722,21 b' var IPython = (function (IPython) {'
720 722
721 723 Notebook.prototype.collapse = function (index) {
722 724 var i = this.index_or_selected(index);
723 this.cells()[i].collapse();
725 this.get_cell(i).collapse();
724 726 this.dirty = true;
725 727 };
726 728
727 729
728 730 Notebook.prototype.expand = function (index) {
729 731 var i = this.index_or_selected(index);
730 this.cells()[i].expand();
732 this.get_cell(i).expand();
731 733 this.dirty = true;
732 734 };
733 735
734 736
735 737 Notebook.prototype.toggle_output = function (index) {
736 738 var i = this.index_or_selected(index);
737 this.cells()[i].toggle_output();
739 this.get_cell(i).toggle_output();
738 740 this.dirty = true;
739 741 };
740 742
@@ -752,7 +754,7 b' var IPython = (function (IPython) {'
752 754 };
753 755
754 756 Notebook.prototype.set_autoindent = function (state) {
755 var cells = this.cells();
757 var cells = this.get_cells();
756 758 len = cells.length;
757 759 for (var i=0; i<len; i++) {
758 760 cells[i].set_autoindent(state);
@@ -762,7 +764,7 b' var IPython = (function (IPython) {'
762 764
763 765 Notebook.prototype.clear_all_output = function () {
764 766 var ncells = this.ncells();
765 var cells = this.cells();
767 var cells = this.get_cells();
766 768 for (var i=0; i<ncells; i++) {
767 769 if (cells[i] instanceof IPython.CodeCell) {
768 770 cells[i].clear_output(true,true,true);
@@ -774,7 +776,7 b' var IPython = (function (IPython) {'
774 776 // Other cell functions: line numbers, ...
775 777
776 778 Notebook.prototype.cell_toggle_line_numbers = function() {
777 this.selected_cell().toggle_line_numbers();
779 this.get_selected_cell().toggle_line_numbers();
778 780 };
779 781
780 782 // Kernel related things
@@ -861,7 +863,7 b' var IPython = (function (IPython) {'
861 863 }
862 864 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
863 865 var index = this.find_cell_index(cell);
864 var new_cell = this.insert_code_cell_below(index);
866 var new_cell = this.insert_cell_below('code',index);
865 867 new_cell.set_text(payload[i].text);
866 868 this.dirty = true;
867 869 }
@@ -981,7 +983,7 b' var IPython = (function (IPython) {'
981 983 default_options = {terminal: false, add_new: true};
982 984 $.extend(default_options, options);
983 985 var that = this;
984 var cell = that.selected_cell();
986 var cell = that.get_selected_cell();
985 987 var cell_index = that.find_cell_index(cell);
986 988 if (cell instanceof IPython.CodeCell) {
987 989 cell.clear_output(true, true, true);
@@ -997,7 +999,7 b' var IPython = (function (IPython) {'
997 999 cell.select_all();
998 1000 } else {
999 1001 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1000 that.insert_code_cell_below();
1002 that.insert_cell_below('code');
1001 1003 // If we are adding a new cell at the end, scroll down to show it.
1002 1004 that.scroll_to_bottom();
1003 1005 } else {
@@ -1012,7 +1014,7 b' var IPython = (function (IPython) {'
1012 1014 var ncells = this.ncells();
1013 1015 for (var i=0; i<ncells; i++) {
1014 1016 this.select(i);
1015 this.execute_selected_cell({add_new:false});
1017 this.execute_get_selected_cell({add_new:false});
1016 1018 };
1017 1019 this.scroll_to_bottom();
1018 1020 };
@@ -1068,23 +1070,15 b' var IPython = (function (IPython) {'
1068 1070 var new_cell = null;
1069 1071 for (i=0; i<ncells; i++) {
1070 1072 cell_data = new_cells[i];
1071 if (cell_data.cell_type == 'code') {
1072 new_cell = this.insert_code_cell_below();
1073 new_cell.fromJSON(cell_data);
1074 } else if (cell_data.cell_type === 'html') {
1075 new_cell = this.insert_html_cell_below();
1076 new_cell.fromJSON(cell_data);
1077 } else if (cell_data.cell_type === 'markdown') {
1078 new_cell = this.insert_markdown_cell_below();
1079 new_cell.fromJSON(cell_data);
1080 };
1073 new_cell = this.insert_cell_below(cell_data.cell_type);
1074 new_cell.fromJSON(cell_data);
1081 1075 };
1082 1076 };
1083 1077 };
1084 1078
1085 1079
1086 1080 Notebook.prototype.toJSON = function () {
1087 var cells = this.cells();
1081 var cells = this.get_cells();
1088 1082 var ncells = cells.length;
1089 1083 cell_array = new Array(ncells);
1090 1084 for (var i=0; i<ncells; i++) {
@@ -1161,7 +1155,7 b' var IPython = (function (IPython) {'
1161 1155 var allowed = xhr.getResponseHeader('Allow');
1162 1156 this.fromJSON(data);
1163 1157 if (this.ncells() === 0) {
1164 this.insert_code_cell_below();
1158 this.insert_cell_below('code');
1165 1159 };
1166 1160 IPython.save_widget.status_last_saved();
1167 1161 IPython.save_widget.set_notebook_name(data.metadata.name);
General Comments 0
You need to be logged in to leave comments. Login now