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 ); |
| 238 | var cells = getSelectedCells( selection ), |
| 239 | firstCell = cells[ 0 ], |
| 240 | table = firstCell.getAscendant( 'table' ), |
| 241 | lastCell = cells[ cells.length - 1 ], |
| 242 | startCol = firstCell.$.cellIndex, |
| 243 | lastCol = lastCell.$.cellIndex + lastCell.$.colSpan - 1, |
| 244 | colIndex = insertBefore? startCol : lastCol; |
233 | | // If the row doesn't have enough cells, ignore it. |
234 | | if ( $row.cells.length < ( cellIndex + 1 ) ) |
235 | | continue; |
| 259 | for ( i = 0; i < height; i++ ) |
| 260 | { |
| 261 | var cell; |
| 262 | // Check whether there's a spanning column here, do not break it. |
| 263 | if ( cloneCol[ i ].colSpan > 1 |
| 264 | && nextCol.length |
| 265 | && nextCol[ i ] == cloneCol[ i ] ) |
| 266 | { |
| 267 | cell = cloneCol[ i ]; |
| 268 | cell.colSpan += 1; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone(); |
| 273 | cell.removeAttribute( 'colSpan' ); |
| 274 | !CKEDITOR.env.ie && cell.appendBogus(); |
| 275 | cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) ); |
| 276 | cell = cell.$; |
| 277 | } |
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 | | } |
| 294 | // Figure out selected cells' column indices. |
| 295 | for ( var i = 0, rows = map.length; i < rows; i++ ) |
| 296 | { |
| 297 | for ( var j = 0, cols = map[ i ].length; j < cols; j++ ) |
| 298 | { |
| 299 | if ( map[ i ][ j ] == firstCell.$ ) |
| 300 | startColIndex = j; |
| 301 | if ( map[ i ][ j ] == lastCell.$ ) |
| 302 | endColIndex = j; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Delete cell or reduce cell spans by checking through the table map. |
| 307 | for ( i = startColIndex; i <= endColIndex; i++ ) |
| 308 | { |
| 309 | for ( j = 0; j < map.length; j++ ) |
| 310 | { |
| 311 | var mapRow = map[ j ], |
| 312 | row = new CKEDITOR.dom.element( table.$.rows[ j ] ), |
| 313 | cell = new CKEDITOR.dom.element( mapRow[ i ] ); |
| 314 | |
| 315 | if ( cell.$.colSpan == 1 ) |
| 316 | cell.remove(); |
| 317 | // Reduce the col spans. |
| 318 | else |
| 319 | cell.$.colSpan -= 1; |
| 320 | |
| 321 | j += cell.$.rowSpan - 1; |
| 322 | |
| 323 | if ( !row.$.cells.length ) |
| 324 | rowsToDelete.push( row ); |
| 325 | } |
| 326 | } |
| 328 | var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells; |
| 329 | |
| 330 | // Where to put the cursor after columns been deleted? |
| 331 | // 1. Into next cell of the first row if any; |
| 332 | // 2. Into previous cell of the first row if any; |
| 333 | // 3. Into table's parent element; |
| 334 | var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) ); |
| 335 | |
| 336 | // Delete table rows only if all columns are gone (do not remove empty row). |
| 337 | if ( rowsToDelete.length == rows ) |
| 338 | table.remove(); |
| 339 | |
| 340 | return cursorPosition; |
| 341 | } |
| 342 | |
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 | | |