+++ /dev/null
-/*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
-For licensing, see LICENSE.html or http://ckeditor.com/license\r
-*/\r
-\r
-(function()\r
-{\r
- var cellNodeRegex = /^(?:td|th)$/;\r
-\r
- function getSelectedCells( selection )\r
- {\r
- // Walker will try to split text nodes, which will make the current selection\r
- // invalid. So save bookmarks before doing anything.\r
- var bookmarks = selection.createBookmarks();\r
-\r
- var ranges = selection.getRanges();\r
- var retval = [];\r
- var database = {};\r
-\r
- function moveOutOfCellGuard( node )\r
- {\r
- // Apply to the first cell only.\r
- if ( retval.length > 0 )\r
- return;\r
-\r
- // If we are exiting from the first </td>, then the td should definitely be\r
- // included.\r
- if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )\r
- && !node.getCustomData( 'selected_cell' ) )\r
- {\r
- CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );\r
- retval.push( node );\r
- }\r
- }\r
-\r
- for ( var i = 0 ; i < ranges.length ; i++ )\r
- {\r
- var range = ranges[ i ];\r
-\r
- if ( range.collapsed )\r
- {\r
- // Walker does not handle collapsed ranges yet - fall back to old API.\r
- var startNode = range.getCommonAncestor();\r
- var nearestCell = startNode.getAscendant( 'td', true ) || startNode.getAscendant( 'th', true );\r
- if ( nearestCell )\r
- retval.push( nearestCell );\r
- }\r
- else\r
- {\r
- var walker = new CKEDITOR.dom.walker( range );\r
- var node;\r
- walker.guard = moveOutOfCellGuard;\r
-\r
- while ( ( node = walker.next() ) )\r
- {\r
- // If may be possible for us to have a range like this:\r
- // <td>^1</td><td>^2</td>\r
- // The 2nd td shouldn't be included.\r
- //\r
- // So we have to take care to include a td we've entered only when we've\r
- // walked into its children.\r
-\r
- var parent = node.getAscendant( 'td' ) || node.getAscendant( 'th' );\r
- if ( parent && !parent.getCustomData( 'selected_cell' ) )\r
- {\r
- CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );\r
- retval.push( parent );\r
- }\r
- }\r
- }\r
- }\r
-\r
- CKEDITOR.dom.element.clearAllMarkers( database );\r
-\r
- // Restore selection position.\r
- selection.selectBookmarks( bookmarks );\r
-\r
- return retval;\r
- }\r
-\r
- function getFocusElementAfterDelCells( cellsToDelete ) {\r
- var i = 0,\r
- last = cellsToDelete.length - 1,\r
- database = {},\r
- cell,focusedCell,\r
- tr;\r
-\r
- while ( ( cell = cellsToDelete[ i++ ] ) )\r
- CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );\r
-\r
- // 1.first we check left or right side focusable cell row by row;\r
- i = 0;\r
- while ( ( cell = cellsToDelete[ i++ ] ) )\r
- {\r
- if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )\r
- || ( focusedCell = cell.getNext() ) && !focusedCell.getCustomData( 'delete_cell' ) )\r
- {\r
- CKEDITOR.dom.element.clearAllMarkers( database );\r
- return focusedCell;\r
- }\r
- }\r
-\r
- CKEDITOR.dom.element.clearAllMarkers( database );\r
-\r
- // 2. then we check the toppest row (outside the selection area square) focusable cell\r
- tr = cellsToDelete[ 0 ].getParent();\r
- if ( ( tr = tr.getPrevious() ) )\r
- return tr.getLast();\r
-\r
- // 3. last we check the lowerest row focusable cell\r
- tr = cellsToDelete[ last ].getParent();\r
- if ( ( tr = tr.getNext() ) )\r
- return tr.getChild( 0 );\r
-\r
- return null;\r
- }\r
-\r
- function insertRow( selection, insertBefore )\r
- {\r
- var cells = getSelectedCells( selection ),\r
- firstCell = cells[ 0 ],\r
- table = firstCell.getAscendant( 'table' ),\r
- doc = firstCell.getDocument(),\r
- startRow = cells[ 0 ].getParent(),\r
- startRowIndex = startRow.$.rowIndex,\r
- lastCell = cells[ cells.length - 1 ],\r
- endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,\r
- endRow = new CKEDITOR.dom.element( table.$.rows[ endRowIndex ] ),\r
- rowIndex = insertBefore ? startRowIndex : endRowIndex,\r
- row = insertBefore ? startRow : endRow;\r
-\r
- var map = CKEDITOR.tools.buildTableMap( table ),\r
- cloneRow = map[ rowIndex ],\r
- nextRow = insertBefore ? map[ rowIndex - 1 ] : map[ rowIndex + 1 ],\r
- width = map[0].length;\r
-\r
- var newRow = doc.createElement( 'tr' );\r
- for ( var i = 0; i < width; i++ )\r
- {\r
- var cell;\r
- // Check whether there's a spanning row here, do not break it.\r
- if ( cloneRow[ i ].rowSpan > 1 && nextRow && cloneRow[ i ] == nextRow[ i ] )\r
- {\r
- cell = cloneRow[ i ];\r
- cell.rowSpan += 1;\r
- }\r
- else\r
- {\r
- cell = new CKEDITOR.dom.element( cloneRow[ i ] ).clone();\r
- cell.removeAttribute( 'rowSpan' );\r
- !CKEDITOR.env.ie && cell.appendBogus();\r
- newRow.append( cell );\r
- cell = cell.$;\r
- }\r
-\r
- i += cell.colSpan - 1;\r
- }\r
-\r
- insertBefore ?\r
- newRow.insertBefore( row ) :\r
- newRow.insertAfter( row );\r
- }\r
-\r
- function deleteRows( selectionOrRow )\r
- {\r
- if ( selectionOrRow instanceof CKEDITOR.dom.selection )\r
- {\r
- var cells = getSelectedCells( selectionOrRow ),\r
- firstCell = cells[ 0 ],\r
- table = firstCell.getAscendant( 'table' ),\r
- map = CKEDITOR.tools.buildTableMap( table ),\r
- startRow = cells[ 0 ].getParent(),\r
- startRowIndex = startRow.$.rowIndex,\r
- lastCell = cells[ cells.length - 1 ],\r
- endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,\r
- rowsToDelete = [];\r
-\r
- // Delete cell or reduce cell spans by checking through the table map.\r
- for ( var i = startRowIndex; i <= endRowIndex; i++ )\r
- {\r
- var mapRow = map[ i ],\r
- row = new CKEDITOR.dom.element( table.$.rows[ i ] );\r
-\r
- for ( var j = 0; j < mapRow.length; j++ )\r
- {\r
- var cell = new CKEDITOR.dom.element( mapRow[ j ] ),\r
- cellRowIndex = cell.getParent().$.rowIndex;\r
-\r
- if ( cell.$.rowSpan == 1 )\r
- cell.remove();\r
- // Row spanned cell.\r
- else\r
- {\r
- // Span row of the cell, reduce spanning.\r
- cell.$.rowSpan -= 1;\r
- // Root row of the cell, root cell to next row.\r
- if ( cellRowIndex == i )\r
- {\r
- var nextMapRow = map[ i + 1 ];\r
- nextMapRow[ j - 1 ] ?\r
- cell.insertAfter( new CKEDITOR.dom.element( nextMapRow[ j - 1 ] ) )\r
- : new CKEDITOR.dom.element( table.$.rows[ i + 1 ] ).append( cell, 1 );\r
- }\r
- }\r
-\r
- j += cell.$.colSpan - 1;\r
- }\r
-\r
- rowsToDelete.push( row );\r
- }\r
-\r
- var rows = table.$.rows;\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
- var cursorPosition = new CKEDITOR.dom.element( rows[ endRowIndex + 1 ] || ( startRowIndex > 0 ? rows[ startRowIndex - 1 ] : null ) || table.$.parentNode );\r
-\r
- for ( i = rowsToDelete.length ; i >= 0 ; i-- )\r
- deleteRows( rowsToDelete[ i ] );\r
-\r
- return cursorPosition;\r
- }\r
- else if ( selectionOrRow instanceof CKEDITOR.dom.element )\r
- {\r
- table = selectionOrRow.getAscendant( 'table' );\r
-\r
- if ( table.$.rows.length == 1 )\r
- table.remove();\r
- else\r
- selectionOrRow.remove();\r
- }\r
-\r
- return null;\r
- }\r
-\r
- function getCellColIndex( cell, isStart )\r
- {\r
- var row = cell.getParent(),\r
- rowCells = row.$.cells;\r
-\r
- var colIndex = 0;\r
- for ( var i = 0; i < rowCells.length; i++ )\r
- {\r
- var mapCell = rowCells[ i ];\r
- colIndex += isStart ? 1 : mapCell.colSpan;\r
- if ( mapCell == cell.$ )\r
- break;\r
- }\r
-\r
- return colIndex -1;\r
- }\r
-\r
- function getColumnsIndices( cells, isStart )\r
- {\r
- var retval = isStart ? Infinity : 0;\r
- for ( var i = 0; i < cells.length; i++ )\r
- {\r
- var colIndex = getCellColIndex( cells[ i ], isStart );\r
- if ( isStart ? colIndex < retval : colIndex > retval )\r
- retval = colIndex;\r
- }\r
- return retval;\r
- }\r
-\r
- function insertColumn( selection, insertBefore )\r
- {\r
- var cells = getSelectedCells( selection ),\r
- firstCell = cells[ 0 ],\r
- table = firstCell.getAscendant( 'table' ),\r
- startCol = getColumnsIndices( cells, 1 ),\r
- lastCol = getColumnsIndices( cells ),\r
- colIndex = insertBefore? startCol : lastCol;\r
-\r
- var map = CKEDITOR.tools.buildTableMap( table ),\r
- cloneCol = [],\r
- nextCol = [],\r
- height = map.length;\r
-\r
- for ( var i = 0; i < height; i++ )\r
- {\r
- cloneCol.push( map[ i ][ colIndex ] );\r
- var nextCell = insertBefore ? map[ i ][ colIndex - 1 ] : map[ i ][ colIndex + 1 ];\r
- nextCell && nextCol.push( nextCell );\r
- }\r
-\r
- for ( i = 0; i < height; i++ )\r
- {\r
- var cell;\r
- // Check whether there's a spanning column here, do not break it.\r
- if ( cloneCol[ i ].colSpan > 1\r
- && nextCol.length\r
- && nextCol[ i ] == cloneCol[ i ] )\r
- {\r
- cell = cloneCol[ i ];\r
- cell.colSpan += 1;\r
- }\r
- else\r
- {\r
- cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone();\r
- cell.removeAttribute( 'colSpan' );\r
- !CKEDITOR.env.ie && cell.appendBogus();\r
- cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) );\r
- cell = cell.$;\r
- }\r
-\r
- i += cell.rowSpan - 1;\r
- }\r
- }\r
-\r
- function deleteColumns( selectionOrCell )\r
- {\r
- var cells = getSelectedCells( selectionOrCell ),\r
- firstCell = cells[ 0 ],\r
- lastCell = cells[ cells.length - 1 ],\r
- table = firstCell.getAscendant( 'table' ),\r
- map = CKEDITOR.tools.buildTableMap( table ),\r
- startColIndex,\r
- endColIndex,\r
- rowsToDelete = [];\r
-\r
- // Figure out selected cells' column indices.\r
- for ( var i = 0, rows = map.length; i < rows; i++ )\r
- {\r
- for ( var j = 0, cols = map[ i ].length; j < cols; j++ )\r
- {\r
- if ( map[ i ][ j ] == firstCell.$ )\r
- startColIndex = j;\r
- if ( map[ i ][ j ] == lastCell.$ )\r
- endColIndex = j;\r
- }\r
- }\r
-\r
- // Delete cell or reduce cell spans by checking through the table map.\r
- for ( i = startColIndex; i <= endColIndex; i++ )\r
- {\r
- for ( j = 0; j < map.length; j++ )\r
- {\r
- var mapRow = map[ j ],\r
- row = new CKEDITOR.dom.element( table.$.rows[ j ] ),\r
- cell = new CKEDITOR.dom.element( mapRow[ i ] );\r
-\r
- if ( cell.$ )\r
- {\r
- if ( cell.$.colSpan == 1 )\r
- cell.remove();\r
- // Reduce the col spans.\r
- else\r
- cell.$.colSpan -= 1;\r
-\r
- j += cell.$.rowSpan - 1;\r
-\r
- if ( !row.$.cells.length )\r
- rowsToDelete.push( row );\r
- }\r
- }\r
- }\r
-\r
- var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells;\r
-\r
- // Where to put the cursor after columns been deleted?\r
- // 1. Into next cell of the first row if any;\r
- // 2. Into previous cell of the first row if any;\r
- // 3. Into table's parent element;\r
- var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) );\r
-\r
- // Delete table rows only if all columns are gone (do not remove empty row).\r
- if ( rowsToDelete.length == rows )\r
- table.remove();\r
-\r
- return cursorPosition;\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 insertCell( selection, insertBefore )\r
- {\r
- var startElement = selection.getStartElement();\r
- var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );\r
-\r
- if ( !cell )\r
- return;\r
-\r
- // Create the new cell element to be added.\r
- var newCell = cell.clone();\r
- if ( !CKEDITOR.env.ie )\r
- newCell.appendBogus();\r
-\r
- if ( insertBefore )\r
- newCell.insertBefore( cell );\r
- else\r
- newCell.insertAfter( cell );\r
- }\r
-\r
- function deleteCells( selectionOrCell )\r
- {\r
- if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
- {\r
- var cellsToDelete = getSelectedCells( selectionOrCell );\r
- var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
- var cellToFocus = getFocusElementAfterDelCells( cellsToDelete );\r
-\r
- for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
- deleteCells( cellsToDelete[ i ] );\r
-\r
- if ( cellToFocus )\r
- placeCursorInCell( cellToFocus, true );\r
- else if ( table )\r
- table.remove();\r
- }\r
- else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
- {\r
- var tr = selectionOrCell.getParent();\r
- if ( tr.getChildCount() == 1 )\r
- tr.remove();\r
- else\r
- selectionOrCell.remove();\r
- }\r
- }\r
-\r
- // Remove filler at end and empty spaces around the cell content.\r
- function trimCell( cell )\r
- {\r
- var bogus = cell.getBogus();\r
- bogus && bogus.remove();\r
- cell.trim();\r
- }\r
-\r
- function placeCursorInCell( cell, placeAtEnd )\r
- {\r
- var range = new CKEDITOR.dom.range( cell.getDocument() );\r
- if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )\r
- {\r
- range.selectNodeContents( cell );\r
- range.collapse( placeAtEnd ? false : true );\r
- }\r
- range.select( true );\r
- }\r
-\r
- function cellInRow( tableMap, rowIndex, cell )\r
- {\r
- var oRow = tableMap[ rowIndex ];\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
- return new CKEDITOR.dom.element( oRow[ c ] );\r
- }\r
- return cell.is ? -1 : null;\r
- }\r
-\r
- function cellInCol( tableMap, colIndex, cell )\r
- {\r
- var oCol = [];\r
- for ( var r = 0; r < tableMap.length; r++ )\r
- {\r
- var row = tableMap[ r ];\r
- if ( typeof cell == 'undefined' )\r
- oCol.push( row[ colIndex ] );\r
- else if ( cell.is && row[ colIndex ] == cell.$ )\r
- return r;\r
- else if ( r == cell )\r
- return new CKEDITOR.dom.element( row[ colIndex ] );\r
- }\r
-\r
- return ( typeof cell == 'undefined' )? oCol : cell.is ? -1 : null;\r
- }\r
-\r
- function mergeCells( selection, mergeDirection, isDetect )\r
- {\r
- var cells = getSelectedCells( selection );\r
-\r
- // Invalid merge request if:\r
- // 1. In batch mode despite that less than two selected.\r
- // 2. In solo mode while not exactly only one selected.\r
- // 3. Cells distributed in different table groups (e.g. from both thead and tbody).\r
- var commonAncestor;\r
- if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )\r
- || ( commonAncestor = selection.getCommonAncestor() )\r
- && commonAncestor.type == CKEDITOR.NODE_ELEMENT\r
- && commonAncestor.is( 'table' ) )\r
- {\r
- return false;\r
- }\r
-\r
- var cell,\r
- firstCell = cells[ 0 ],\r
- table = firstCell.getAscendant( 'table' ),\r
- map = CKEDITOR.tools.buildTableMap( table ),\r
- mapHeight = map.length,\r
- mapWidth = map[ 0 ].length,\r
- startRow = firstCell.getParent().$.rowIndex,\r
- startColumn = cellInRow( map, startRow, firstCell );\r
-\r
- if ( mergeDirection )\r
- {\r
- var targetCell;\r
- try\r
- {\r
- var rowspan = parseInt( firstCell.getAttribute( 'rowspan' ), 10 ) || 1;\r
- var colspan = parseInt( firstCell.getAttribute( 'colspan' ), 10 ) || 1;\r
-\r
- targetCell =\r
- map[ mergeDirection == 'up' ?\r
- ( startRow - rowspan ):\r
- mergeDirection == 'down' ? ( startRow + rowspan ) : startRow ] [\r
- mergeDirection == 'left' ?\r
- ( startColumn - colspan ):\r
- mergeDirection == 'right' ? ( startColumn + colspan ) : startColumn ];\r
-\r
- }\r
- catch( er )\r
- {\r
- return false;\r
- }\r
-\r
- // 1. No cell could be merged.\r
- // 2. Same cell actually.\r
- if ( !targetCell || firstCell.$ == targetCell )\r
- return false;\r
-\r
- // Sort in map order regardless of the DOM sequence.\r
- cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?\r
- 'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );\r
- }\r
-\r
- // Start from here are merging way ignorance (merge up/right, batch merge).\r
- var doc = firstCell.getDocument(),\r
- lastRowIndex = startRow,\r
- totalRowSpan = 0,\r
- totalColSpan = 0,\r
- // Use a documentFragment as buffer when appending cell contents.\r
- frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),\r
- dimension = 0;\r
-\r
- for ( var i = 0; i < cells.length; i++ )\r
- {\r
- cell = cells[ i ];\r
-\r
- var tr = cell.getParent(),\r
- cellFirstChild = cell.getFirst(),\r
- colSpan = cell.$.colSpan,\r
- rowSpan = cell.$.rowSpan,\r
- rowIndex = tr.$.rowIndex,\r
- colIndex = cellInRow( map, rowIndex, cell );\r
-\r
- // Accumulated the actual places taken by all selected cells.\r
- dimension += colSpan * rowSpan;\r
- // Accumulated the maximum virtual spans from column and row.\r
- totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;\r
- totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );\r
-\r
- if ( !isDetect )\r
- {\r
- // Trim all cell fillers and check to remove empty cells.\r
- if ( trimCell( cell ), cell.getChildren().count() )\r
- {\r
- // Merge vertically cells as two separated paragraphs.\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
- frag.append( 'br' );\r
- }\r
-\r
- cell.moveChildren( frag );\r
- }\r
- i ? cell.remove() : cell.setHtml( '' );\r
- }\r
- lastRowIndex = rowIndex;\r
- }\r
-\r
- if ( !isDetect )\r
- {\r
- frag.moveChildren( firstCell );\r
-\r
- if ( !CKEDITOR.env.ie )\r
- firstCell.appendBogus();\r
-\r
- if ( totalColSpan >= mapWidth )\r
- firstCell.removeAttribute( 'rowSpan' );\r
- else\r
- firstCell.$.rowSpan = totalRowSpan;\r
-\r
- if ( totalRowSpan >= mapHeight )\r
- firstCell.removeAttribute( 'colSpan' );\r
- else\r
- firstCell.$.colSpan = totalColSpan;\r
-\r
- // Swip empty <tr> left at the end of table due to the merging.\r
- var trs = new CKEDITOR.dom.nodeList( table.$.rows ),\r
- count = trs.count();\r
-\r
- for ( i = count - 1; i >= 0; i-- )\r
- {\r
- var tailTr = trs.getItem( i );\r
- if ( !tailTr.$.cells.length )\r
- {\r
- tailTr.remove();\r
- count++;\r
- continue;\r
- }\r
- }\r
-\r
- return firstCell;\r
- }\r
- // Be able to merge cells only if actual dimension of selected\r
- // cells equals to the caculated rectangle.\r
- else\r
- return ( totalRowSpan * totalColSpan ) == dimension;\r
- }\r
-\r
- function verticalSplitCell ( selection, isDetect )\r
- {\r
- var cells = getSelectedCells( selection );\r
- if ( cells.length > 1 )\r
- return false;\r
- else if ( isDetect )\r
- return true;\r
-\r
- var cell = cells[ 0 ],\r
- tr = cell.getParent(),\r
- table = tr.getAscendant( 'table' ),\r
- map = CKEDITOR.tools.buildTableMap( table ),\r
- rowIndex = tr.$.rowIndex,\r
- colIndex = cellInRow( map, rowIndex, cell ),\r
- rowSpan = cell.$.rowSpan,\r
- newCell,\r
- newRowSpan,\r
- newCellRowSpan,\r
- newRowIndex;\r
-\r
- if ( rowSpan > 1 )\r
- {\r
- newRowSpan = Math.ceil( rowSpan / 2 );\r
- newCellRowSpan = Math.floor( rowSpan / 2 );\r
- newRowIndex = rowIndex + newRowSpan;\r
- var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),\r
- newCellRow = cellInRow( map, newRowIndex ),\r
- candidateCell;\r
-\r
- newCell = cell.clone();\r
-\r
- // Figure out where to insert the new cell by checking the vitual row.\r
- for ( var c = 0; c < newCellRow.length; c++ )\r
- {\r
- candidateCell = newCellRow[ c ];\r
- // Catch first cell actually following the column.\r
- if ( candidateCell.parentNode == newCellTr.$\r
- && c > colIndex )\r
- {\r
- newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );\r
- break;\r
- }\r
- else\r
- candidateCell = null;\r
- }\r
-\r
- // The destination row is empty, append at will.\r
- if ( !candidateCell )\r
- newCellTr.append( newCell, true );\r
- }\r
- else\r
- {\r
- newCellRowSpan = newRowSpan = 1;\r
-\r
- newCellTr = tr.clone();\r
- newCellTr.insertAfter( tr );\r
- newCellTr.append( newCell = cell.clone() );\r
-\r
- var cellsInSameRow = cellInRow( map, rowIndex );\r
- for ( var i = 0; i < cellsInSameRow.length; i++ )\r
- cellsInSameRow[ i ].rowSpan++;\r
- }\r
-\r
- if ( !CKEDITOR.env.ie )\r
- newCell.appendBogus();\r
-\r
- cell.$.rowSpan = newRowSpan;\r
- newCell.$.rowSpan = newCellRowSpan;\r
- if ( newRowSpan == 1 )\r
- cell.removeAttribute( 'rowSpan' );\r
- if ( newCellRowSpan == 1 )\r
- newCell.removeAttribute( 'rowSpan' );\r
-\r
- return newCell;\r
- }\r
-\r
- function horizontalSplitCell( selection, isDetect )\r
- {\r
- var cells = getSelectedCells( selection );\r
- if ( cells.length > 1 )\r
- return false;\r
- else if ( isDetect )\r
- return true;\r
-\r
- var cell = cells[ 0 ],\r
- tr = cell.getParent(),\r
- table = tr.getAscendant( 'table' ),\r
- map = CKEDITOR.tools.buildTableMap( table ),\r
- rowIndex = tr.$.rowIndex,\r
- colIndex = cellInRow( map, rowIndex, cell ),\r
- colSpan = cell.$.colSpan,\r
- newCell,\r
- newColSpan,\r
- newCellColSpan;\r
-\r
- if ( colSpan > 1 )\r
- {\r
- newColSpan = Math.ceil( colSpan / 2 );\r
- newCellColSpan = Math.floor( colSpan / 2 );\r
- }\r
- else\r
- {\r
- newCellColSpan = newColSpan = 1;\r
- var cellsInSameCol = cellInCol( map, colIndex );\r
- for ( var i = 0; i < cellsInSameCol.length; i++ )\r
- cellsInSameCol[ i ].colSpan++;\r
- }\r
- newCell = cell.clone();\r
- newCell.insertAfter( cell );\r
- if ( !CKEDITOR.env.ie )\r
- newCell.appendBogus();\r
-\r
- cell.$.colSpan = newColSpan;\r
- newCell.$.colSpan = newCellColSpan;\r
- if ( newColSpan == 1 )\r
- cell.removeAttribute( 'colSpan' );\r
- if ( newCellColSpan == 1 )\r
- newCell.removeAttribute( 'colSpan' );\r
-\r
- return newCell;\r
- }\r
- // Context menu on table caption incorrect (#3834)\r
- var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };\r
-\r
- CKEDITOR.plugins.tabletools =\r
- {\r
- init : function( editor )\r
- {\r
- var lang = editor.lang.table;\r
-\r
- editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );\r
- CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );\r
-\r
- editor.addCommand( 'tableDelete',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection(),\r
- startElement = selection && selection.getStartElement(),\r
- table = startElement && startElement.getAscendant( 'table', 1 );\r
-\r
- if ( !table )\r
- return;\r
-\r
- // If the table's parent has only one child remove it as well (unless it's the body or a table cell) (#5416, #6289)\r
- var parent = table.getParent();\r
- if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )\r
- table = parent;\r
-\r
- var range = new CKEDITOR.dom.range( editor.document );\r
- range.moveToPosition( table, CKEDITOR.POSITION_BEFORE_START );\r
- table.remove();\r
- range.select();\r
- }\r
- } );\r
-\r
- editor.addCommand( 'rowDelete',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- placeCursorInCell( deleteRows( selection ) );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'rowInsertBefore',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertRow( selection, true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'rowInsertAfter',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertRow( selection );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'columnDelete',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- var element = deleteColumns( selection );\r
- element && placeCursorInCell( element, true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'columnInsertBefore',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertColumn( selection, true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'columnInsertAfter',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertColumn( selection );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellDelete',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- deleteCells( selection );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellMerge',\r
- {\r
- exec : function( editor )\r
- {\r
- placeCursorInCell( mergeCells( editor.getSelection() ), true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellMergeRight',\r
- {\r
- exec : function( editor )\r
- {\r
- placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellMergeDown',\r
- {\r
- exec : function( editor )\r
- {\r
- placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellVerticalSplit',\r
- {\r
- exec : function( editor )\r
- {\r
- placeCursorInCell( verticalSplitCell( editor.getSelection() ) );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellHorizontalSplit',\r
- {\r
- exec : function( editor )\r
- {\r
- placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellInsertBefore',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertCell( selection, true );\r
- }\r
- } );\r
-\r
- editor.addCommand( 'cellInsertAfter',\r
- {\r
- exec : function( editor )\r
- {\r
- var selection = editor.getSelection();\r
- insertCell( selection );\r
- }\r
- } );\r
-\r
- // If the "menu" plugin is loaded, register the menu items.\r
- if ( editor.addMenuItems )\r
- {\r
- editor.addMenuItems(\r
- {\r
- tablecell :\r
- {\r
- label : lang.cell.menu,\r
- group : 'tablecell',\r
- order : 1,\r
- getItems : function()\r
- {\r
- var selection = editor.getSelection(),\r
- cells = getSelectedCells( selection );\r
- return {\r
- tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,\r
- tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,\r
- tablecell_delete : CKEDITOR.TRISTATE_OFF,\r
- tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
- tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
- tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
- tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
- tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
- tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
- };\r
- }\r
- },\r
-\r
- tablecell_insertBefore :\r
- {\r
- label : lang.cell.insertBefore,\r
- group : 'tablecell',\r
- command : 'cellInsertBefore',\r
- order : 5\r
- },\r
-\r
- tablecell_insertAfter :\r
- {\r
- label : lang.cell.insertAfter,\r
- group : 'tablecell',\r
- command : 'cellInsertAfter',\r
- order : 10\r
- },\r
-\r
- tablecell_delete :\r
- {\r
- label : lang.cell.deleteCell,\r
- group : 'tablecell',\r
- command : 'cellDelete',\r
- order : 15\r
- },\r
-\r
- tablecell_merge :\r
- {\r
- label : lang.cell.merge,\r
- group : 'tablecell',\r
- command : 'cellMerge',\r
- order : 16\r
- },\r
-\r
- tablecell_merge_right :\r
- {\r
- label : lang.cell.mergeRight,\r
- group : 'tablecell',\r
- command : 'cellMergeRight',\r
- order : 17\r
- },\r
-\r
- tablecell_merge_down :\r
- {\r
- label : lang.cell.mergeDown,\r
- group : 'tablecell',\r
- command : 'cellMergeDown',\r
- order : 18\r
- },\r
-\r
- tablecell_split_horizontal :\r
- {\r
- label : lang.cell.splitHorizontal,\r
- group : 'tablecell',\r
- command : 'cellHorizontalSplit',\r
- order : 19\r
- },\r
-\r
- tablecell_split_vertical :\r
- {\r
- label : lang.cell.splitVertical,\r
- group : 'tablecell',\r
- command : 'cellVerticalSplit',\r
- order : 20\r
- },\r
-\r
- tablecell_properties :\r
- {\r
- label : lang.cell.title,\r
- group : 'tablecellproperties',\r
- command : 'cellProperties',\r
- order : 21\r
- },\r
-\r
- tablerow :\r
- {\r
- label : lang.row.menu,\r
- group : 'tablerow',\r
- order : 1,\r
- getItems : function()\r
- {\r
- return {\r
- tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,\r
- tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,\r
- tablerow_delete : CKEDITOR.TRISTATE_OFF\r
- };\r
- }\r
- },\r
-\r
- tablerow_insertBefore :\r
- {\r
- label : lang.row.insertBefore,\r
- group : 'tablerow',\r
- command : 'rowInsertBefore',\r
- order : 5\r
- },\r
-\r
- tablerow_insertAfter :\r
- {\r
- label : lang.row.insertAfter,\r
- group : 'tablerow',\r
- command : 'rowInsertAfter',\r
- order : 10\r
- },\r
-\r
- tablerow_delete :\r
- {\r
- label : lang.row.deleteRow,\r
- group : 'tablerow',\r
- command : 'rowDelete',\r
- order : 15\r
- },\r
-\r
- tablecolumn :\r
- {\r
- label : lang.column.menu,\r
- group : 'tablecolumn',\r
- order : 1,\r
- getItems : function()\r
- {\r
- return {\r
- tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,\r
- tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,\r
- tablecolumn_delete : CKEDITOR.TRISTATE_OFF\r
- };\r
- }\r
- },\r
-\r
- tablecolumn_insertBefore :\r
- {\r
- label : lang.column.insertBefore,\r
- group : 'tablecolumn',\r
- command : 'columnInsertBefore',\r
- order : 5\r
- },\r
-\r
- tablecolumn_insertAfter :\r
- {\r
- label : lang.column.insertAfter,\r
- group : 'tablecolumn',\r
- command : 'columnInsertAfter',\r
- order : 10\r
- },\r
-\r
- tablecolumn_delete :\r
- {\r
- label : lang.column.deleteColumn,\r
- group : 'tablecolumn',\r
- command : 'columnDelete',\r
- order : 15\r
- }\r
- });\r
- }\r
-\r
- // If the "contextmenu" plugin is laoded, register the listeners.\r
- if ( editor.contextMenu )\r
- {\r
- editor.contextMenu.addListener( function( element, selection )\r
- {\r
- if ( !element || element.isReadOnly() )\r
- return null;\r
-\r
- while ( element )\r
- {\r
- if ( element.getName() in contextMenuTags )\r
- {\r
- return {\r
- tablecell : CKEDITOR.TRISTATE_OFF,\r
- tablerow : CKEDITOR.TRISTATE_OFF,\r
- tablecolumn : CKEDITOR.TRISTATE_OFF\r
- };\r
- }\r
- element = element.getParent();\r
- }\r
-\r
- return null;\r
- } );\r
- }\r
- },\r
-\r
- getSelectedCells : getSelectedCells\r
-\r
- };\r
- CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );\r
-})();\r
-\r
-/**\r
- * Create a two-dimension array that reflects the actual layout of table cells,\r
- * with cell spans, with mappings to the original td elements.\r
- * @param table {CKEDITOR.dom.element}\r
- */\r
-CKEDITOR.tools.buildTableMap = function ( table )\r
-{\r
- var aRows = table.$.rows ;\r
-\r
- // Row and Column counters.\r
- var r = -1 ;\r
-\r
- var aMap = [];\r
-\r
- for ( var i = 0 ; i < aRows.length ; i++ )\r
- {\r
- r++ ;\r
- !aMap[r] && ( aMap[r] = [] );\r
-\r
- var c = -1 ;\r
-\r
- for ( var j = 0 ; j < aRows[i].cells.length ; j++ )\r
- {\r
- var oCell = aRows[i].cells[j] ;\r
-\r
- c++ ;\r
- while ( aMap[r][c] )\r
- c++ ;\r
-\r
- var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;\r
- var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;\r
-\r
- for ( var rs = 0 ; rs < iRowSpan ; rs++ )\r
- {\r
- if ( !aMap[r + rs] )\r
- aMap[r + rs] = [];\r
-\r
- for ( var cs = 0 ; cs < iColSpan ; cs++ )\r
- {\r
- aMap[r + rs][c + cs] = aRows[i].cells[j] ;\r
- }\r
- }\r
-\r
- c += iColSpan - 1 ;\r
- }\r
- }\r
- return aMap ;\r
-};\r