##// END OF EJS Templates
refactor cellc
Matthias BUSSONNIER -
Show More
@@ -565,17 +565,51 b' var IPython = (function (IPython) {'
565 return this.insert_cell_below(type,len-1);
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 Notebook.prototype.insert_cell_below = function (type, index) {
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 index = this.index_or_selected(index);
581 index = this.index_or_selected(index);
572 var cell = null;
582 return this.insert_cell_at_index(type, index+1);
573 // This is intentionally < rather than <= for the sake of more
583 };
574 // sensible behavior in some cases.
584
575 if (this.undelete_index !== null && index < this.undelete_index) {
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 this.undelete_index = this.undelete_index + 1;
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 if (type === 'code') {
613 if (type === 'code') {
580 cell = new IPython.CodeCell(this.kernel);
614 cell = new IPython.CodeCell(this.kernel);
581 cell.set_input_prompt();
615 cell.set_input_prompt();
@@ -589,10 +623,15 b' var IPython = (function (IPython) {'
589 cell = new IPython.HeadingCell();
623 cell = new IPython.HeadingCell();
590 };
624 };
591 if (cell !== null) {
625 if (cell !== null) {
592 if (this.ncells() === 0) {
626 if (ncells === 0) {
627 // special case append if empty
593 this.element.find('div.end_space').before(cell.element);
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 } else if (this.is_valid_cell_index(index)) {
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 cell.render();
636 cell.render();
598 this.select(this.find_cell_index(cell));
637 this.select(this.find_cell_index(cell));
@@ -601,43 +640,24 b' var IPython = (function (IPython) {'
601 };
640 };
602 };
641 };
603 return cell;
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 Notebook.prototype.insert_cell_above = function (type, index) {
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 index = this.index_or_selected(index);
659 index = this.index_or_selected(index);
611 var cell = null;
660 return this.insert_cell_at_index(type, index);
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;
641 };
661 };
642
662
643
663
General Comments 0
You need to be logged in to leave comments. Login now