JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / tabletools / plugin.js
index 7590078..1f2224c 100644 (file)
@@ -86,7 +86,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                return retval;\r
        }\r
 \r
-       function getFocusedCell( cellsToDelete ) {\r
+       function getFocusElementAfterDelCells( cellsToDelete ) {\r
                var i = 0,\r
                        last = cellsToDelete.length - 1,\r
                        database = {},\r
@@ -248,21 +248,65 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
+       function getFocusElementAfterDelCols( cells )\r
+       {\r
+               var cellIndexList = [],\r
+                       table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),\r
+                       i, length,\r
+                       targetIndex, targetCell;\r
+\r
+               // get the cellIndex list of delete cells\r
+               for ( i = 0, length = cells.length; i < length; i++ )\r
+                       cellIndexList.push( cells[i].$.cellIndex );\r
+\r
+               // get the focusable column index\r
+               cellIndexList.sort();\r
+               for ( i = 1, length = cellIndexList.length; i < length; i++ )\r
+               {\r
+                       if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )\r
+                       {\r
+                               targetIndex = cellIndexList[ i - 1 ] + 1;\r
+                               break;\r
+                       }\r
+               }\r
+\r
+               if ( !targetIndex )\r
+                       targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )\r
+                                                       : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );\r
+\r
+               // scan row by row to get the target cell\r
+               var rows = table.$.rows;\r
+               for ( i = 0, length = rows.length; i < length ; i++ )\r
+               {\r
+                       targetCell = rows[ i ].cells[ targetIndex ];\r
+                       if ( targetCell )\r
+                               break;\r
+               }\r
+\r
+               return targetCell ?  new CKEDITOR.dom.element( targetCell ) :  table.getPrevious();\r
+       }\r
+\r
        function deleteColumns( selectionOrCell )\r
        {\r
                if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
                {\r
-                       var colsToDelete = getSelectedCells( selectionOrCell );\r
-                       for ( var i = colsToDelete.length ; i >= 0 ; i-- )\r
+                       var colsToDelete = getSelectedCells( selectionOrCell ),\r
+                               elementToFocus = getFocusElementAfterDelCols( colsToDelete );\r
+\r
+                       for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- )\r
                        {\r
                                if ( colsToDelete[ i ] )\r
                                        deleteColumns( colsToDelete[ i ] );\r
                        }\r
+\r
+                       return elementToFocus;\r
                }\r
                else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
                {\r
                        // Get the cell's table.\r
                        var table = selectionOrCell.getAscendant( 'table' );\r
+                       if ( !table )\r
+                               return null;\r
 \r
                        // Get the cell index.\r
                        var cellIndex = selectionOrCell.$.cellIndex;\r
@@ -288,6 +332,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        row.$.removeChild( row.$.cells[ cellIndex ] );\r
                        }\r
                }\r
+\r
+               return null;\r
        }\r
 \r
        function insertCell( selection, insertBefore )\r
@@ -315,7 +361,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        var cellsToDelete = getSelectedCells( selectionOrCell );\r
                        var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
-                       var cellToFocus   = getFocusedCell( cellsToDelete );\r
+                       var cellToFocus   = getFocusElementAfterDelCells( cellsToDelete );\r
 \r
                        for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
                                deleteCells( cellsToDelete[ i ] );\r
@@ -726,9 +772,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                range.collapse();\r
                                                selection.selectRanges( [ range ] );\r
 \r
-                                               // If the table's parent has only one child, remove it as well.\r
-                                               if ( table.getParent().getChildCount() == 1 )\r
-                                                       table.getParent().remove();\r
+                                               // If the table's parent has only one child, remove it,except body,as well.( #5416 )\r
+                                               var parent = table.getParent();\r
+                                               if ( parent.getChildCount() == 1 && parent.getName() != 'body' )\r
+                                                       parent.remove();\r
                                                else\r
                                                        table.remove();\r
                                        }\r
@@ -766,7 +813,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        exec : function( editor )\r
                                        {\r
                                                var selection = editor.getSelection();\r
-                                               deleteColumns( selection );\r
+                                               var element = deleteColumns( selection );\r
+                                               element &&  placeCursorInCell( element, true );\r
                                        }\r
                                } );\r
 \r