##// END OF EJS Templates
abstract, cleanup and document...
Bussonnier Matthias -
Show More
@@ -560,27 +560,7 b' var IPython = (function (IPython) {'
560 };
560 };
561
561
562
562
563 Notebook.prototype.insert_cell_at_bottom = function (type){
564 var len = this.ncells();
565 return this.insert_cell_below(type,len-1);
566 }
567
563
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 **/
580 Notebook.prototype.insert_cell_below = function (type, index) {
581 index = this.index_or_selected(index);
582 return this.insert_cell_at_index(type, index+1);
583 };
584
564
585 /**
565 /**
586 * Insert a cell so that after insertion the cell is at given index.
566 * Insert a cell so that after insertion the cell is at given index.
@@ -590,26 +570,18 b' var IPython = (function (IPython) {'
590 * Index will be brought back into the accissible range [0,n]
570 * Index will be brought back into the accissible range [0,n]
591 *
571 *
592 * @param type {string} in ['code','html','markdown','heading']
572 * @param type {string} in ['code','html','markdown','heading']
593 * @param index {int} a valid index where to inser cell
573 * @param [index] {int} a valid index where to inser cell
594 *
574 *
595 * return created cell or null
575 * @return cell {cell|null} created cell or null
596 **/
576 **/
597 Notebook.prototype.insert_cell_at_index = function(type, index){
577 Notebook.prototype.insert_cell_at_index = function(type, index){
598 var ncell = this.ncells()
578
579 var ncells = this.ncells();
599 var index = Math.min(index,ncells);
580 var index = Math.min(index,ncells);
600 index = Math.max(index,0);
581 index = Math.max(index,0);
582 var cell = null;
601
583
602 var cell = null
584 if (ncells === 0 || this.is_valid_cell_index(index) || index== ncells) {
603
604
605 /// this use to be index < this.undelete_index in some case
606 if (this.undelete_index !== null && index <= this.undelete_index) {
607 this.undelete_index = this.undelete_index + 1;
608 this.dirty = true;
609 }
610
611 // this should be alway true now
612 if (ncells === 0 || this.is_valid_cell_index(index) || index== ncell) {
613 if (type === 'code') {
585 if (type === 'code') {
614 cell = new IPython.CodeCell(this.kernel);
586 cell = new IPython.CodeCell(this.kernel);
615 cell.set_input_prompt();
587 cell.set_input_prompt();
@@ -621,27 +593,53 b' var IPython = (function (IPython) {'
621 cell = new IPython.RawCell();
593 cell = new IPython.RawCell();
622 } else if (type === 'heading') {
594 } else if (type === 'heading') {
623 cell = new IPython.HeadingCell();
595 cell = new IPython.HeadingCell();
624 };
596 }
625 if (cell !== null) {
597
626 if (ncells === 0) {
598 if(this._insert_element_at_index(cell.element,index)){
627 // special case append if empty
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);
632 } else if (this.is_valid_cell_index(index)) {
633 // otherwise always somewhere to append to
634 this.get_cell_element(index).before(cell.element);
635 };
636 cell.render();
599 cell.render();
637 this.select(this.find_cell_index(cell));
600 this.select(this.find_cell_index(cell));
638 this.dirty = true;
601 this.dirty = true;
639 return cell;
602 }
640 };
603 }
641 };
642 return cell;
604 return cell;
643
605
606 };
644
607
608 /**
609 * Insert an element at given cell index.
610 *
611 * @param element {dom element} a cell element
612 * @param [index] {int} a valid index where to inser cell
613 * @private
614 *
615 * return true if everything whent fine.
616 **/
617 Notebook.prototype._insert_element_at_index = function(element, index){
618 var ncells = this.ncells();
619
620 /// this use to be index < this.undelete_index in some case
621 if (this.undelete_index !== null && index <= this.undelete_index) {
622 this.undelete_index = this.undelete_index + 1;
623 this.dirty = true;
624 }
625
626 // this should be alway true now
627 if (ncells === 0 || this.is_valid_cell_index(index) || index== ncells) {
628 if (element !== null) {
629 if (ncells === 0) {
630 // special case append if empty
631 this.element.find('div.end_space').before(element);
632 } else if ( ncells == index ) {
633 // special case append it the end, but not empty
634 this.get_cell_element(index-1).after(element);
635 } else if (this.is_valid_cell_index(index)) {
636 // otherwise always somewhere to append to
637 this.get_cell_element(index).before(element);
638 }
639 return true;
640 }
641 }
642 return false;
645 };
643 };
646
644
647 /**
645 /**
@@ -660,6 +658,39 b' var IPython = (function (IPython) {'
660 return this.insert_cell_at_index(type, index);
658 return this.insert_cell_at_index(type, index);
661 };
659 };
662
660
661 /**
662 * Insert a cell of given type below given index, or at bottom
663 * of notebook if index greater thatn number of cell
664 *
665 * default index value is the one of currently selected cell
666 *
667 * @method insert_cell_below
668 * @param type {string} cell type
669 * @param [index] {integer}
670 *
671 * @return handle to created cell or null
672 *
673 **/
674 Notebook.prototype.insert_cell_below = function (type, index) {
675 index = this.index_or_selected(index);
676 return this.insert_cell_at_index(type, index+1);
677 };
678
679
680 /**
681 * Insert cell at end of notebook
682 *
683 * @method insert_cell_at_bottom
684 * @param type {String} cell type
685 *
686 * @return the added cell; or null
687 **/
688 Notebook.prototype.insert_cell_at_bottom = function (type){
689 var len = this.ncells();
690 return this.insert_cell_below(type,len-1);
691 };
692
693
663
694
664 Notebook.prototype.to_code = function (index) {
695 Notebook.prototype.to_code = function (index) {
665 var i = this.index_or_selected(index);
696 var i = this.index_or_selected(index);
General Comments 0
You need to be logged in to leave comments. Login now