JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1.1
[ckeditor.git] / _source / plugins / tabletools / plugin.js
index 6266912..4a3a004 100644 (file)
@@ -123,31 +123,56 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\r
                if ( selectionOrRow instanceof CKEDITOR.dom.selection )\r
                {\r
-                       var cells = getSelectedCells( selectionOrRow );\r
-                       var rowsToDelete = [];\r
+                       var cells = getSelectedCells( selectionOrRow ),\r
+                               cellsCount = cells.length,\r
+                               rowsToDelete = [],\r
+                               cursorPosition,\r
+                               previousRowIndex,\r
+                               nextRowIndex;\r
 \r
                        // Queue up the rows - it's possible and likely that we have duplicates.\r
-                       for ( var i = 0 ; i < cells.length ; i++ )\r
+                       for ( var i = 0 ; i < cellsCount ; i++ )\r
                        {\r
-                               var row = cells[ i ].getParent();\r
-                               rowsToDelete[ row.$.rowIndex ] = row;\r
+                               var row = cells[ i ].getParent(),\r
+                                               rowIndex = row.$.rowIndex;\r
+\r
+                               !i && ( previousRowIndex = rowIndex - 1 );\r
+                               rowsToDelete[ rowIndex ] = row;\r
+                               i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 );\r
                        }\r
 \r
+                       var table = row.getAscendant( 'table' ),\r
+                                       rows =  table.$.rows,\r
+                                       rowCount = rows.length;\r
+\r
+                       // Where to put the cursor after rows been deleted?\r
+                       // 1. Into next sibling row if any;\r
+                       // 2. Into previous sibling row if any;\r
+                       // 3. Into table's parent element if it's the very last row.\r
+                       cursorPosition = new CKEDITOR.dom.element(\r
+                               nextRowIndex < rowCount && table.$.rows[ nextRowIndex ] ||\r
+                               previousRowIndex > 0 && table.$.rows[ previousRowIndex ] ||\r
+                               table.$.parentNode );\r
+\r
                        for ( i = rowsToDelete.length ; i >= 0 ; i-- )\r
                        {\r
                                if ( rowsToDelete[ i ] )\r
                                        deleteRows( rowsToDelete[ i ] );\r
                        }\r
+\r
+                       return cursorPosition;\r
                }\r
                else if ( selectionOrRow instanceof CKEDITOR.dom.element )\r
                {\r
-                       var table = selectionOrRow.getAscendant( 'table' );\r
+                       table = selectionOrRow.getAscendant( 'table' );\r
 \r
                        if ( table.$.rows.length == 1 )\r
                                table.remove();\r
                        else\r
                                selectionOrRow.remove();\r
                }\r
+\r
+               return 0;\r
        }\r
 \r
        function insertColumn( selection, insertBefore )\r
@@ -331,14 +356,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function cellInRow( tableMap, rowIndex, cell )\r
        {\r
                var oRow = tableMap[ rowIndex ];\r
-               if( typeof cell == 'undefined' )\r
+               if ( typeof cell == 'undefined' )\r
                        return oRow;\r
 \r
                for ( var c = 0 ; oRow && c < oRow.length ; c++ )\r
                {\r
                        if ( cell.is && oRow[c] == cell.$ )\r
                                return c;\r
-                       else if( c == cell )\r
+                       else if ( c == cell )\r
                                return new CKEDITOR.dom.element( oRow[ c ] );\r
                }\r
                return cell.is ? -1 : null;\r
@@ -350,11 +375,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( var r = 0; r < tableMap.length; r++ )\r
                {\r
                        var row = tableMap[ r ];\r
-                       if( typeof cell == 'undefined' )\r
+                       if ( typeof cell == 'undefined' )\r
                                oCol.push( row[ colIndex ] );\r
-                       else if( cell.is && row[ colIndex ] == cell.$ )\r
+                       else if ( cell.is && row[ colIndex ] == cell.$ )\r
                                return r;\r
-                       else if( r == cell )\r
+                       else if ( r == cell )\r
                                return new CKEDITOR.dom.element( row[ colIndex ] );\r
                }\r
 \r
@@ -387,7 +412,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        startRow = firstCell.getParent().$.rowIndex,\r
                        startColumn = cellInRow( map, startRow, firstCell );\r
 \r
-               if( mergeDirection )\r
+               if ( mergeDirection )\r
                {\r
                        var targetCell;\r
                        try\r
@@ -408,7 +433,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        // 1. No cell could be merged.\r
                        // 2. Same cell actually.\r
-                       if( !targetCell || firstCell.$ == targetCell  )\r
+                       if ( !targetCell || firstCell.$ == targetCell  )\r
                                return false;\r
 \r
                        // Sort in map order regardless of the DOM sequence.\r
@@ -445,16 +470,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( !isDetect )\r
                        {\r
                                // Trim all cell fillers and check to remove empty cells.\r
-                               if( trimCell( cell ), cell.getChildren().count() )\r
+                               if ( trimCell( cell ), cell.getChildren().count() )\r
                                {\r
                                        // Merge vertically cells as two separated paragraphs.\r
-                                       if( rowIndex != lastRowIndex\r
+                                       if ( rowIndex != lastRowIndex\r
                                                && cellFirstChild\r
                                                && !( cellFirstChild.isBlockBoundary\r
                                                          && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )\r
                                        {\r
                                                var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
-                                               if( last && !( last.is && last.is( 'br' ) ) )\r
+                                               if ( last && !( last.is && last.is( 'br' ) ) )\r
                                                        frag.append( new CKEDITOR.dom.element( 'br' ) );\r
                                        }\r
 \r
@@ -469,15 +494,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        frag.moveChildren( firstCell );\r
 \r
-                       if( !CKEDITOR.env.ie )\r
+                       if ( !CKEDITOR.env.ie )\r
                                firstCell.appendBogus();\r
 \r
-                       if( totalColSpan >= mapWidth )\r
+                       if ( totalColSpan >= mapWidth )\r
                                firstCell.removeAttribute( 'rowSpan' );\r
                        else\r
                                firstCell.$.rowSpan = totalRowSpan;\r
 \r
-                       if( totalRowSpan >= mapHeight )\r
+                       if ( totalRowSpan >= mapHeight )\r
                                firstCell.removeAttribute( 'colSpan' );\r
                        else\r
                                firstCell.$.colSpan = totalColSpan;\r
@@ -489,7 +514,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        for ( i = count - 1; i >= 0; i-- )\r
                        {\r
                                var tailTr = trs.getItem( i );\r
-                               if( !tailTr.$.cells.length )\r
+                               if ( !tailTr.$.cells.length )\r
                                {\r
                                        tailTr.remove();\r
                                        count++;\r
@@ -508,9 +533,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function verticalSplitCell ( selection, isDetect )\r
        {\r
                var cells = getSelectedCells( selection );\r
-               if( cells.length > 1 )\r
+               if ( cells.length > 1 )\r
                        return false;\r
-               else if( isDetect )\r
+               else if ( isDetect )\r
                        return true;\r
 \r
                var cell = cells[ 0 ],\r
@@ -525,7 +550,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        newCellRowSpan,\r
                        newRowIndex;\r
 \r
-               if( rowSpan > 1 )\r
+               if ( rowSpan > 1 )\r
                {\r
                        newRowSpan = Math.ceil( rowSpan / 2 );\r
                        newCellRowSpan = Math.floor( rowSpan / 2 );\r
@@ -541,7 +566,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                candidateCell = newCellRow[ c ];\r
                                // Catch first cell actually following the column.\r
-                               if( candidateCell.parentNode == newCellTr.$\r
+                               if ( candidateCell.parentNode == newCellTr.$\r
                                        && c > colIndex )\r
                                {\r
                                        newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );\r
@@ -552,7 +577,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
 \r
                        // The destination row is empty, append at will.\r
-                       if( !candidateCell )\r
+                       if ( !candidateCell )\r
                                newCellTr.append( newCell, true );\r
                }\r
                else\r
@@ -568,14 +593,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                cellsInSameRow[ i ].rowSpan++;\r
                }\r
 \r
-               if( !CKEDITOR.env.ie )\r
+               if ( !CKEDITOR.env.ie )\r
                        newCell.appendBogus();\r
 \r
                cell.$.rowSpan = newRowSpan;\r
                newCell.$.rowSpan = newCellRowSpan;\r
-               if( newRowSpan == 1 )\r
+               if ( newRowSpan == 1 )\r
                        cell.removeAttribute( 'rowSpan' );\r
-               if( newCellRowSpan == 1 )\r
+               if ( newCellRowSpan == 1 )\r
                        newCell.removeAttribute( 'rowSpan' );\r
 \r
                return newCell;\r
@@ -584,9 +609,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function horizontalSplitCell( selection, isDetect )\r
        {\r
                var cells = getSelectedCells( selection );\r
-               if( cells.length > 1 )\r
+               if ( cells.length > 1 )\r
                        return false;\r
-               else if( isDetect )\r
+               else if ( isDetect )\r
                        return true;\r
 \r
                var cell = cells[ 0 ],\r
@@ -600,7 +625,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        newColSpan,\r
                        newCellColSpan;\r
 \r
-               if( colSpan > 1 )\r
+               if ( colSpan > 1 )\r
                {\r
                        newColSpan = Math.ceil( colSpan / 2 );\r
                        newCellColSpan = Math.floor( colSpan / 2 );\r
@@ -614,14 +639,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
                newCell = cell.clone();\r
                newCell.insertAfter( cell );\r
-               if( !CKEDITOR.env.ie )\r
+               if ( !CKEDITOR.env.ie )\r
                        newCell.appendBogus();\r
 \r
                cell.$.colSpan = newColSpan;\r
                newCell.$.colSpan = newCellColSpan;\r
-               if( newColSpan == 1 )\r
+               if ( newColSpan == 1 )\r
                        cell.removeAttribute( 'colSpan' );\r
-               if( newCellColSpan == 1 )\r
+               if ( newCellColSpan == 1 )\r
                        newCell.removeAttribute( 'colSpan' );\r
 \r
                return newCell;\r
@@ -668,7 +693,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        exec : function( editor )\r
                                        {\r
                                                var selection = editor.getSelection();\r
-                                               deleteRows( selection );\r
+                                               placeCursorInCell( deleteRows( selection ) );\r
                                        }\r
                                } );\r
 \r