Show More
@@ -565,17 +565,51 var IPython = (function (IPython) { | |||
|
565 | 565 | return this.insert_cell_below(type,len-1); |
|
566 | 566 | } |
|
567 | 567 | |
|
568 | /** | |
|
569 | * Insert a cell of given type below given index, or at bottom | |
|
570 | * of notebook if index greater thatn number of cell | |
|
571 | * | |
|
572 | * default index value is the one of currently selected cell | |
|
573 | * | |
|
574 | * @param type {string} cell type | |
|
575 | * @param [index] {integer} | |
|
576 | * | |
|
577 | * @return handle to created cell or null | |
|
578 | * | |
|
579 | **/ | |
|
568 | 580 | Notebook.prototype.insert_cell_below = function (type, index) { |
|
569 | // type = ('code','html','markdown') | |
|
570 | // index = cell index or undefined to insert below selected | |
|
571 | 581 | index = this.index_or_selected(index); |
|
572 | var cell = null; | |
|
573 | // This is intentionally < rather than <= for the sake of more | |
|
574 | // sensible behavior in some cases. | |
|
575 | if (this.undelete_index !== null && index < this.undelete_index) { | |
|
582 | return this.insert_cell_at_index(type, index+1); | |
|
583 | }; | |
|
584 | ||
|
585 | /** | |
|
586 | * Insert a cell so that after insertion the cell is at given index. | |
|
587 | * | |
|
588 | * Similar to insert_above, but index parameter is mandatory | |
|
589 | * | |
|
590 | * Index will be brought back into the accissible range [0,n] | |
|
591 | * | |
|
592 | * @param type {string} in ['code','html','markdown','heading'] | |
|
593 | * @param index {int} a valid index where to inser cell | |
|
594 | * | |
|
595 | * return created cell or null | |
|
596 | **/ | |
|
597 | Notebook.prototype.insert_cell_at_index = function(type, index){ | |
|
598 | var ncell = this.ncells() | |
|
599 | var index = Math.min(index,ncells); | |
|
600 | index = Math.max(index,0); | |
|
601 | ||
|
602 | var cell = null | |
|
603 | ||
|
604 | ||
|
605 | /// this use to be index < this.undelete_index in some case | |
|
606 | if (this.undelete_index !== null && index <= this.undelete_index) { | |
|
576 | 607 | this.undelete_index = this.undelete_index + 1; |
|
608 | this.dirty = true; | |
|
577 | 609 | } |
|
578 | if (this.ncells() === 0 || this.is_valid_cell_index(index)) { | |
|
610 | ||
|
611 | // this should be alway true now | |
|
612 | if (ncells === 0 || this.is_valid_cell_index(index) || index== ncell) { | |
|
579 | 613 | if (type === 'code') { |
|
580 | 614 | cell = new IPython.CodeCell(this.kernel); |
|
581 | 615 | cell.set_input_prompt(); |
@@ -589,10 +623,15 var IPython = (function (IPython) { | |||
|
589 | 623 | cell = new IPython.HeadingCell(); |
|
590 | 624 | }; |
|
591 | 625 | if (cell !== null) { |
|
592 |
if ( |
|
|
626 | if (ncells === 0) { | |
|
627 | // special case append if empty | |
|
593 | 628 | this.element.find('div.end_space').before(cell.element); |
|
629 | } else if ( ncell == index ) { | |
|
630 | // special case append it the end, but not empty | |
|
631 | this.get_cell_element(index-1).after(cell.element); | |
|
594 | 632 | } else if (this.is_valid_cell_index(index)) { |
|
595 | this.get_cell_element(index).after(cell.element); | |
|
633 | // otherwise always somewhere to append to | |
|
634 | this.get_cell_element(index).before(cell.element); | |
|
596 | 635 | }; |
|
597 | 636 | cell.render(); |
|
598 | 637 | this.select(this.find_cell_index(cell)); |
@@ -601,43 +640,24 var IPython = (function (IPython) { | |||
|
601 | 640 | }; |
|
602 | 641 | }; |
|
603 | 642 | return cell; |
|
604 | }; | |
|
605 | 643 | |
|
606 | 644 | |
|
645 | }; | |
|
646 | ||
|
647 | /** | |
|
648 | * Insert a cell of given type above given index, or at top | |
|
649 | * of notebook if index smaller than 0. | |
|
650 | * | |
|
651 | * default index value is the one of currently selected cell | |
|
652 | * | |
|
653 | * @param type {string} cell type | |
|
654 | * @param [index] {integer} | |
|
655 | * | |
|
656 | * @return handle to created cell or null | |
|
657 | **/ | |
|
607 | 658 | Notebook.prototype.insert_cell_above = function (type, index) { |
|
608 | // type = ('code','html','markdown') | |
|
609 | // index = cell index or undefined to insert above selected | |
|
610 | 659 | index = this.index_or_selected(index); |
|
611 | var cell = null; | |
|
612 | if (this.undelete_index !== null && index <= this.undelete_index) { | |
|
613 | this.undelete_index = this.undelete_index + 1; | |
|
614 | } | |
|
615 | if (this.ncells() === 0 || this.is_valid_cell_index(index)) { | |
|
616 | if (type === 'code') { | |
|
617 | cell = new IPython.CodeCell(this.kernel); | |
|
618 | cell.set_input_prompt(); | |
|
619 | } else if (type === 'markdown') { | |
|
620 | cell = new IPython.MarkdownCell(); | |
|
621 | } else if (type === 'html') { | |
|
622 | cell = new IPython.HTMLCell(); | |
|
623 | } else if (type === 'raw') { | |
|
624 | cell = new IPython.RawCell(); | |
|
625 | } else if (type === 'heading') { | |
|
626 | cell = new IPython.HeadingCell(); | |
|
627 | }; | |
|
628 | if (cell !== null) { | |
|
629 | if (this.ncells() === 0) { | |
|
630 | this.element.find('div.end_space').before(cell.element); | |
|
631 | } else if (this.is_valid_cell_index(index)) { | |
|
632 | this.get_cell_element(index).before(cell.element); | |
|
633 | }; | |
|
634 | cell.render(); | |
|
635 | this.select(this.find_cell_index(cell)); | |
|
636 | this.dirty = true; | |
|
637 | return cell; | |
|
638 | }; | |
|
639 | }; | |
|
640 | return cell; | |
|
660 | return this.insert_cell_at_index(type, index); | |
|
641 | 661 | }; |
|
642 | 662 | |
|
643 | 663 |
General Comments 0
You need to be logged in to leave comments.
Login now