| 128 | | // Get the array of row's cells. |
| 129 | | var $cells = $tr.cells; |
| | 120 | var cells = getSelectedCells( selection ), |
| | 121 | firstCell = cells[ 0 ], |
| | 122 | table = firstCell.getAscendant( 'table' ), |
| | 123 | doc = firstCell.getDocument(), |
| | 124 | startRow = cells[ 0 ].getParent(), |
| | 125 | startRowIndex = startRow.$.rowIndex, |
| | 126 | lastCell = cells[ cells.length - 1 ], |
| | 127 | endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1, |
| | 128 | endRow = new CKEDITOR.dom.element( table.$.rows[ endRowIndex ] ), |
| | 129 | rowIndex = insertBefore ? startRowIndex : endRowIndex, |
| | 130 | row = insertBefore ? startRow : endRow; |
| 136 | | if ( !CKEDITOR.env.ie ) |
| 137 | | ( new CKEDITOR.dom.element( $cells[ i ] ) ).appendBogus(); |
| 138 | | } |
| 139 | | } |
| | 137 | var newRow = doc.createElement( 'tr' ); |
| | 138 | for ( var i = 0; i < width; i++ ) |
| | 139 | { |
| | 140 | var cell; |
| | 141 | // Check whether there's a spanning row here, do not break it. |
| | 142 | if ( cloneRow[ i ].rowSpan > 1 && nextRow && cloneRow[ i ] == nextRow[ i ] ) |
| | 143 | { |
| | 144 | cell = cloneRow[ i ]; |
| | 145 | cell.rowSpan += 1; |
| | 146 | } |
| | 147 | else |
| | 148 | { |
| | 149 | cell = new CKEDITOR.dom.element( cloneRow[ i ] ).clone(); |
| | 150 | cell.removeAttribute( 'rowSpan' ); |
| | 151 | !CKEDITOR.env.ie && cell.appendBogus(); |
| | 152 | newRow.append( cell ); |
| | 153 | cell = cell.$; |
| | 154 | } |
| 164 | | cellsCount = cells.length, |
| 165 | | rowsToDelete = [], |
| 166 | | cursorPosition, |
| 167 | | previousRowIndex, |
| 168 | | nextRowIndex; |
| | 169 | firstCell = cells[ 0 ], |
| | 170 | table = firstCell.getAscendant( 'table' ), |
| | 171 | map = CKEDITOR.tools.buildTableMap( table ), |
| | 172 | startRow = cells[ 0 ].getParent(), |
| | 173 | startRowIndex = startRow.$.rowIndex, |
| | 174 | lastCell = cells[ cells.length - 1 ], |
| | 175 | endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1, |
| | 176 | rowsToDelete = []; |
| 176 | | !i && ( previousRowIndex = rowIndex - 1 ); |
| 177 | | rowsToDelete[ rowIndex ] = row; |
| 178 | | i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 ); |
| 179 | | } |
| | 184 | for ( var j = 0; j < mapRow.length; j++ ) |
| | 185 | { |
| | 186 | var cell = new CKEDITOR.dom.element( mapRow[ j ] ), |
| | 187 | cellRowIndex = cell.getParent().$.rowIndex; |
| | 188 | |
| | 189 | if ( cell.$.rowSpan == 1 ) |
| | 190 | cell.remove(); |
| | 191 | // Row spanned cell. |
| | 192 | else |
| | 193 | { |
| | 194 | // Span row of the cell, reduce spanning. |
| | 195 | cell.$.rowSpan -= 1; |
| | 196 | // Root row of the cell, root cell to next row. |
| | 197 | if ( cellRowIndex == i ) |
| | 198 | { |
| | 199 | var nextMapRow = map[ i + 1 ]; |
| | 200 | nextMapRow[ j - 1 ] ? |
| | 201 | cell.insertAfter( new CKEDITOR.dom.element( nextMapRow[ j - 1 ] ) ) |
| | 202 | : new CKEDITOR.dom.element( table.$.rows[ i + 1 ] ).append( cell, 1 ); |
| | 203 | } |
| | 204 | } |
| 217 | | // Get the cell where the selection is placed in. |
| 218 | | var startElement = selection.getStartElement(); |
| 219 | | var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 ); |
| | 267 | var cells = getSelectedCells( selection ), |
| | 268 | firstCell = cells[ 0 ], |
| | 269 | table = firstCell.getAscendant( 'table' ), |
| | 270 | startCol = getColumnsIndices( cells, 1 ), |
| | 271 | lastCol = getColumnsIndices( cells ), |
| | 272 | colIndex = insertBefore? startCol : lastCol; |
| 231 | | var $row = table.$.rows[ i ]; |
| | 288 | var cell; |
| | 289 | // Check whether there's a spanning column here, do not break it. |
| | 290 | if ( cloneCol[ i ].colSpan > 1 |
| | 291 | && nextCol.length |
| | 292 | && nextCol[ i ] == cloneCol[ i ] ) |
| | 293 | { |
| | 294 | cell = cloneCol[ i ]; |
| | 295 | cell.colSpan += 1; |
| | 296 | } |
| | 297 | else |
| | 298 | { |
| | 299 | cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone(); |
| | 300 | cell.removeAttribute( 'colSpan' ); |
| | 301 | !CKEDITOR.env.ie && cell.appendBogus(); |
| | 302 | cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) ); |
| | 303 | cell = cell.$; |
| | 304 | } |
| 242 | | // Get back the currently selected cell. |
| 243 | | var baseCell = new CKEDITOR.dom.element( $row.cells[ cellIndex ] ); |
| 244 | | if ( insertBefore ) |
| 245 | | cell.insertBefore( baseCell ); |
| 246 | | else |
| 247 | | cell.insertAfter( baseCell ); |
| 248 | | } |
| 249 | | } |
| | 333 | // Delete cell or reduce cell spans by checking through the table map. |
| | 334 | for ( i = startColIndex; i <= endColIndex; i++ ) |
| | 335 | { |
| | 336 | for ( j = 0; j < map.length; j++ ) |
| | 337 | { |
| | 338 | var mapRow = map[ j ], |
| | 339 | row = new CKEDITOR.dom.element( table.$.rows[ j ] ), |
| | 340 | cell = new CKEDITOR.dom.element( mapRow[ i ] ); |
| | 341 | |
| | 342 | if ( cell.$.colSpan == 1 ) |
| | 343 | cell.remove(); |
| | 344 | // Reduce the col spans. |
| | 345 | else |
| | 346 | cell.$.colSpan -= 1; |
| | 347 | |
| | 348 | j += cell.$.rowSpan - 1; |
| | 349 | |
| | 350 | if ( !row.$.cells.length ) |
| | 351 | rowsToDelete.push( row ); |
| | 352 | } |
| | 353 | } |
| | 355 | var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells; |
| | 356 | |
| | 357 | // Where to put the cursor after columns been deleted? |
| | 358 | // 1. Into next cell of the first row if any; |
| | 359 | // 2. Into previous cell of the first row if any; |
| | 360 | // 3. Into table's parent element; |
| | 361 | var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) ); |
| | 362 | |
| | 363 | // Delete table rows only if all columns are gone (do not remove empty row). |
| | 364 | if ( rowsToDelete.length == rows ) |
| | 365 | table.remove(); |
| | 366 | |
| | 367 | return cursorPosition; |
| | 368 | } |
| | 369 | |
| 289 | | function deleteColumns( selectionOrCell ) |
| 290 | | { |
| 291 | | if ( selectionOrCell instanceof CKEDITOR.dom.selection ) |
| 292 | | { |
| 293 | | var colsToDelete = getSelectedCells( selectionOrCell ), |
| 294 | | elementToFocus = getFocusElementAfterDelCols( colsToDelete ); |
| 295 | | |
| 296 | | for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- ) |
| 297 | | { |
| 298 | | if ( colsToDelete[ i ] ) |
| 299 | | deleteColumns( colsToDelete[ i ] ); |
| 300 | | } |
| 301 | | |
| 302 | | return elementToFocus; |
| 303 | | } |
| 304 | | else if ( selectionOrCell instanceof CKEDITOR.dom.element ) |
| 305 | | { |
| 306 | | // Get the cell's table. |
| 307 | | var table = selectionOrCell.getAscendant( 'table' ); |
| 308 | | if ( !table ) |
| 309 | | return null; |
| 310 | | |
| 311 | | // Get the cell index. |
| 312 | | var cellIndex = selectionOrCell.$.cellIndex; |
| 313 | | |
| 314 | | /* |
| 315 | | * Loop through all rows from down to up, coz it's possible that some rows |
| 316 | | * will be deleted. |
| 317 | | */ |
| 318 | | for ( i = table.$.rows.length - 1 ; i >= 0 ; i-- ) |
| 319 | | { |
| 320 | | // Get the row. |
| 321 | | var row = new CKEDITOR.dom.element( table.$.rows[ i ] ); |
| 322 | | |
| 323 | | // If the cell to be removed is the first one and the row has just one cell. |
| 324 | | if ( !cellIndex && row.$.cells.length == 1 ) |
| 325 | | { |
| 326 | | deleteRows( row ); |
| 327 | | continue; |
| 328 | | } |
| 329 | | |
| 330 | | // Else, just delete the cell. |
| 331 | | if ( row.$.cells[ cellIndex ] ) |
| 332 | | row.$.removeChild( row.$.cells[ cellIndex ] ); |
| 333 | | } |
| 334 | | } |
| 335 | | |
| 336 | | return null; |
| 337 | | } |
| 338 | | |