2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 var cellNodeRegex = /^(?:td|th)$/;
\r
10 function getSelectedCells( selection )
\r
12 var ranges = selection.getRanges();
\r
16 function moveOutOfCellGuard( node )
\r
18 // Apply to the first cell only.
\r
19 if ( retval.length > 0 )
\r
22 // If we are exiting from the first </td>, then the td should definitely be
\r
24 if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )
\r
25 && !node.getCustomData( 'selected_cell' ) )
\r
27 CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );
\r
28 retval.push( node );
\r
32 for ( var i = 0 ; i < ranges.length ; i++ )
\r
34 var range = ranges[ i ];
\r
36 if ( range.collapsed )
\r
38 // Walker does not handle collapsed ranges yet - fall back to old API.
\r
39 var startNode = range.getCommonAncestor();
\r
40 var nearestCell = startNode.getAscendant( 'td', true ) || startNode.getAscendant( 'th', true );
\r
42 retval.push( nearestCell );
\r
46 var walker = new CKEDITOR.dom.walker( range );
\r
48 walker.guard = moveOutOfCellGuard;
\r
50 while ( ( node = walker.next() ) )
\r
52 // If may be possible for us to have a range like this:
\r
53 // <td>^1</td><td>^2</td>
\r
54 // The 2nd td shouldn't be included.
\r
56 // So we have to take care to include a td we've entered only when we've
\r
57 // walked into its children.
\r
59 var parent = node.getAscendant( 'td' ) || node.getAscendant( 'th' );
\r
60 if ( parent && !parent.getCustomData( 'selected_cell' ) )
\r
62 CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );
\r
63 retval.push( parent );
\r
69 CKEDITOR.dom.element.clearAllMarkers( database );
\r
74 function getFocusElementAfterDelCells( cellsToDelete ) {
\r
76 last = cellsToDelete.length - 1,
\r
81 while ( ( cell = cellsToDelete[ i++ ] ) )
\r
82 CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );
\r
84 // 1.first we check left or right side focusable cell row by row;
\r
86 while ( ( cell = cellsToDelete[ i++ ] ) )
\r
88 if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )
\r
89 || ( focusedCell = cell.getNext() ) && !focusedCell.getCustomData( 'delete_cell' ) )
\r
91 CKEDITOR.dom.element.clearAllMarkers( database );
\r
96 CKEDITOR.dom.element.clearAllMarkers( database );
\r
98 // 2. then we check the toppest row (outside the selection area square) focusable cell
\r
99 tr = cellsToDelete[ 0 ].getParent();
\r
100 if ( ( tr = tr.getPrevious() ) )
\r
101 return tr.getLast();
\r
103 // 3. last we check the lowerest row focusable cell
\r
104 tr = cellsToDelete[ last ].getParent();
\r
105 if ( ( tr = tr.getNext() ) )
\r
106 return tr.getChild( 0 );
\r
111 function insertRow( selection, insertBefore )
\r
113 var cells = getSelectedCells( selection ),
\r
114 firstCell = cells[ 0 ],
\r
115 table = firstCell.getAscendant( 'table' ),
\r
116 doc = firstCell.getDocument(),
\r
117 startRow = cells[ 0 ].getParent(),
\r
118 startRowIndex = startRow.$.rowIndex,
\r
119 lastCell = cells[ cells.length - 1 ],
\r
120 endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,
\r
121 endRow = new CKEDITOR.dom.element( table.$.rows[ endRowIndex ] ),
\r
122 rowIndex = insertBefore ? startRowIndex : endRowIndex,
\r
123 row = insertBefore ? startRow : endRow;
\r
125 var map = CKEDITOR.tools.buildTableMap( table ),
\r
126 cloneRow = map[ rowIndex ],
\r
127 nextRow = insertBefore ? map[ rowIndex - 1 ] : map[ rowIndex + 1 ],
\r
128 width = map[0].length;
\r
130 var newRow = doc.createElement( 'tr' );
\r
131 for ( var i = 0; cloneRow[ i ] && i < width; i++ )
\r
134 // Check whether there's a spanning row here, do not break it.
\r
135 if ( cloneRow[ i ].rowSpan > 1 && nextRow && cloneRow[ i ] == nextRow[ i ] )
\r
137 cell = cloneRow[ i ];
\r
142 cell = new CKEDITOR.dom.element( cloneRow[ i ] ).clone();
\r
143 cell.removeAttribute( 'rowSpan' );
\r
144 !CKEDITOR.env.ie && cell.appendBogus();
\r
145 newRow.append( cell );
\r
149 i += cell.colSpan - 1;
\r
153 newRow.insertBefore( row ) :
\r
154 newRow.insertAfter( row );
\r
157 function deleteRows( selectionOrRow )
\r
159 if ( selectionOrRow instanceof CKEDITOR.dom.selection )
\r
161 var cells = getSelectedCells( selectionOrRow ),
\r
162 firstCell = cells[ 0 ],
\r
163 table = firstCell.getAscendant( 'table' ),
\r
164 map = CKEDITOR.tools.buildTableMap( table ),
\r
165 startRow = cells[ 0 ].getParent(),
\r
166 startRowIndex = startRow.$.rowIndex,
\r
167 lastCell = cells[ cells.length - 1 ],
\r
168 endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,
\r
171 // Delete cell or reduce cell spans by checking through the table map.
\r
172 for ( var i = startRowIndex; i <= endRowIndex; i++ )
\r
174 var mapRow = map[ i ],
\r
175 row = new CKEDITOR.dom.element( table.$.rows[ i ] );
\r
177 for ( var j = 0; j < mapRow.length; j++ )
\r
179 var cell = new CKEDITOR.dom.element( mapRow[ j ] ),
\r
180 cellRowIndex = cell.getParent().$.rowIndex;
\r
182 if ( cell.$.rowSpan == 1 )
\r
184 // Row spanned cell.
\r
187 // Span row of the cell, reduce spanning.
\r
188 cell.$.rowSpan -= 1;
\r
189 // Root row of the cell, root cell to next row.
\r
190 if ( cellRowIndex == i )
\r
192 var nextMapRow = map[ i + 1 ];
\r
193 nextMapRow[ j - 1 ] ?
\r
194 cell.insertAfter( new CKEDITOR.dom.element( nextMapRow[ j - 1 ] ) )
\r
195 : new CKEDITOR.dom.element( table.$.rows[ i + 1 ] ).append( cell, 1 );
\r
199 j += cell.$.colSpan - 1;
\r
202 rowsToDelete.push( row );
\r
205 var rows = table.$.rows;
\r
207 // Where to put the cursor after rows been deleted?
\r
208 // 1. Into next sibling row if any;
\r
209 // 2. Into previous sibling row if any;
\r
210 // 3. Into table's parent element if it's the very last row.
\r
211 var cursorPosition = new CKEDITOR.dom.element( rows[ endRowIndex + 1 ] || ( startRowIndex > 0 ? rows[ startRowIndex - 1 ] : null ) || table.$.parentNode );
\r
213 for ( i = rowsToDelete.length ; i >= 0 ; i-- )
\r
214 deleteRows( rowsToDelete[ i ] );
\r
216 return cursorPosition;
\r
218 else if ( selectionOrRow instanceof CKEDITOR.dom.element )
\r
220 table = selectionOrRow.getAscendant( 'table' );
\r
222 if ( table.$.rows.length == 1 )
\r
225 selectionOrRow.remove();
\r
231 function getCellColIndex( cell, isStart )
\r
233 var row = cell.getParent(),
\r
234 rowCells = row.$.cells;
\r
237 for ( var i = 0; i < rowCells.length; i++ )
\r
239 var mapCell = rowCells[ i ];
\r
240 colIndex += isStart ? 1 : mapCell.colSpan;
\r
241 if ( mapCell == cell.$ )
\r
245 return colIndex -1;
\r
248 function getColumnsIndices( cells, isStart )
\r
250 var retval = isStart ? Infinity : 0;
\r
251 for ( var i = 0; i < cells.length; i++ )
\r
253 var colIndex = getCellColIndex( cells[ i ], isStart );
\r
254 if ( isStart ? colIndex < retval : colIndex > retval )
\r
260 function insertColumn( selection, insertBefore )
\r
262 var cells = getSelectedCells( selection ),
\r
263 firstCell = cells[ 0 ],
\r
264 table = firstCell.getAscendant( 'table' ),
\r
265 startCol = getColumnsIndices( cells, 1 ),
\r
266 lastCol = getColumnsIndices( cells ),
\r
267 colIndex = insertBefore? startCol : lastCol;
\r
269 var map = CKEDITOR.tools.buildTableMap( table ),
\r
272 height = map.length;
\r
274 for ( var i = 0; i < height; i++ )
\r
276 cloneCol.push( map[ i ][ colIndex ] );
\r
277 var nextCell = insertBefore ? map[ i ][ colIndex - 1 ] : map[ i ][ colIndex + 1 ];
\r
278 nextCell && nextCol.push( nextCell );
\r
281 for ( i = 0; i < height; i++ )
\r
284 // Check whether there's a spanning column here, do not break it.
\r
285 if ( cloneCol[ i ].colSpan > 1
\r
287 && nextCol[ i ] == cloneCol[ i ] )
\r
289 cell = cloneCol[ i ];
\r
294 cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone();
\r
295 cell.removeAttribute( 'colSpan' );
\r
296 !CKEDITOR.env.ie && cell.appendBogus();
\r
297 cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) );
\r
301 i += cell.rowSpan - 1;
\r
305 function deleteColumns( selectionOrCell )
\r
307 var cells = getSelectedCells( selectionOrCell ),
\r
308 firstCell = cells[ 0 ],
\r
309 lastCell = cells[ cells.length - 1 ],
\r
310 table = firstCell.getAscendant( 'table' ),
\r
311 map = CKEDITOR.tools.buildTableMap( table ),
\r
316 // Figure out selected cells' column indices.
\r
317 for ( var i = 0, rows = map.length; i < rows; i++ )
\r
319 for ( var j = 0, cols = map[ i ].length; j < cols; j++ )
\r
321 if ( map[ i ][ j ] == firstCell.$ )
\r
323 if ( map[ i ][ j ] == lastCell.$ )
\r
328 // Delete cell or reduce cell spans by checking through the table map.
\r
329 for ( i = startColIndex; i <= endColIndex; i++ )
\r
331 for ( j = 0; j < map.length; j++ )
\r
333 var mapRow = map[ j ],
\r
334 row = new CKEDITOR.dom.element( table.$.rows[ j ] ),
\r
335 cell = new CKEDITOR.dom.element( mapRow[ i ] );
\r
339 if ( cell.$.colSpan == 1 )
\r
341 // Reduce the col spans.
\r
343 cell.$.colSpan -= 1;
\r
345 j += cell.$.rowSpan - 1;
\r
347 if ( !row.$.cells.length )
\r
348 rowsToDelete.push( row );
\r
353 var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells;
\r
355 // Where to put the cursor after columns been deleted?
\r
356 // 1. Into next cell of the first row if any;
\r
357 // 2. Into previous cell of the first row if any;
\r
358 // 3. Into table's parent element;
\r
359 var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) );
\r
361 // Delete table rows only if all columns are gone (do not remove empty row).
\r
362 if ( rowsToDelete.length == rows )
\r
365 return cursorPosition;
\r
368 function getFocusElementAfterDelCols( cells )
\r
370 var cellIndexList = [],
\r
371 table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),
\r
373 targetIndex, targetCell;
\r
375 // get the cellIndex list of delete cells
\r
376 for ( i = 0, length = cells.length; i < length; i++ )
\r
377 cellIndexList.push( cells[i].$.cellIndex );
\r
379 // get the focusable column index
\r
380 cellIndexList.sort();
\r
381 for ( i = 1, length = cellIndexList.length; i < length; i++ )
\r
383 if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )
\r
385 targetIndex = cellIndexList[ i - 1 ] + 1;
\r
390 if ( !targetIndex )
\r
391 targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )
\r
392 : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );
\r
394 // scan row by row to get the target cell
\r
395 var rows = table.$.rows;
\r
396 for ( i = 0, length = rows.length; i < length ; i++ )
\r
398 targetCell = rows[ i ].cells[ targetIndex ];
\r
403 return targetCell ? new CKEDITOR.dom.element( targetCell ) : table.getPrevious();
\r
406 function insertCell( selection, insertBefore )
\r
408 var startElement = selection.getStartElement();
\r
409 var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );
\r
414 // Create the new cell element to be added.
\r
415 var newCell = cell.clone();
\r
416 if ( !CKEDITOR.env.ie )
\r
417 newCell.appendBogus();
\r
419 if ( insertBefore )
\r
420 newCell.insertBefore( cell );
\r
422 newCell.insertAfter( cell );
\r
425 function deleteCells( selectionOrCell )
\r
427 if ( selectionOrCell instanceof CKEDITOR.dom.selection )
\r
429 var cellsToDelete = getSelectedCells( selectionOrCell );
\r
430 var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );
\r
431 var cellToFocus = getFocusElementAfterDelCells( cellsToDelete );
\r
433 for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )
\r
434 deleteCells( cellsToDelete[ i ] );
\r
437 placeCursorInCell( cellToFocus, true );
\r
441 else if ( selectionOrCell instanceof CKEDITOR.dom.element )
\r
443 var tr = selectionOrCell.getParent();
\r
444 if ( tr.getChildCount() == 1 )
\r
447 selectionOrCell.remove();
\r
451 // Remove filler at end and empty spaces around the cell content.
\r
452 function trimCell( cell )
\r
454 var bogus = cell.getBogus();
\r
455 bogus && bogus.remove();
\r
459 function placeCursorInCell( cell, placeAtEnd )
\r
461 var range = new CKEDITOR.dom.range( cell.getDocument() );
\r
462 if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )
\r
464 range.selectNodeContents( cell );
\r
465 range.collapse( placeAtEnd ? false : true );
\r
467 range.select( true );
\r
470 function cellInRow( tableMap, rowIndex, cell )
\r
472 var oRow = tableMap[ rowIndex ];
\r
473 if ( typeof cell == 'undefined' )
\r
476 for ( var c = 0 ; oRow && c < oRow.length ; c++ )
\r
478 if ( cell.is && oRow[c] == cell.$ )
\r
480 else if ( c == cell )
\r
481 return new CKEDITOR.dom.element( oRow[ c ] );
\r
483 return cell.is ? -1 : null;
\r
486 function cellInCol( tableMap, colIndex )
\r
489 for ( var r = 0; r < tableMap.length; r++ )
\r
491 var row = tableMap[ r ];
\r
492 oCol.push( row[ colIndex ] );
\r
494 // Avoid adding duplicate cells.
\r
495 if ( row[ colIndex ].rowSpan > 1 )
\r
496 r += row[ colIndex ].rowSpan - 1;
\r
501 function mergeCells( selection, mergeDirection, isDetect )
\r
503 var cells = getSelectedCells( selection );
\r
505 // Invalid merge request if:
\r
506 // 1. In batch mode despite that less than two selected.
\r
507 // 2. In solo mode while not exactly only one selected.
\r
508 // 3. Cells distributed in different table groups (e.g. from both thead and tbody).
\r
509 var commonAncestor;
\r
510 if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )
\r
511 || ( commonAncestor = selection.getCommonAncestor() )
\r
512 && commonAncestor.type == CKEDITOR.NODE_ELEMENT
\r
513 && commonAncestor.is( 'table' ) )
\r
519 firstCell = cells[ 0 ],
\r
520 table = firstCell.getAscendant( 'table' ),
\r
521 map = CKEDITOR.tools.buildTableMap( table ),
\r
522 mapHeight = map.length,
\r
523 mapWidth = map[ 0 ].length,
\r
524 startRow = firstCell.getParent().$.rowIndex,
\r
525 startColumn = cellInRow( map, startRow, firstCell );
\r
527 if ( mergeDirection )
\r
532 var rowspan = parseInt( firstCell.getAttribute( 'rowspan' ), 10 ) || 1;
\r
533 var colspan = parseInt( firstCell.getAttribute( 'colspan' ), 10 ) || 1;
\r
536 map[ mergeDirection == 'up' ?
\r
537 ( startRow - rowspan ):
\r
538 mergeDirection == 'down' ? ( startRow + rowspan ) : startRow ] [
\r
539 mergeDirection == 'left' ?
\r
540 ( startColumn - colspan ):
\r
541 mergeDirection == 'right' ? ( startColumn + colspan ) : startColumn ];
\r
549 // 1. No cell could be merged.
\r
550 // 2. Same cell actually.
\r
551 if ( !targetCell || firstCell.$ == targetCell )
\r
554 // Sort in map order regardless of the DOM sequence.
\r
555 cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?
\r
556 'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );
\r
559 // Start from here are merging way ignorance (merge up/right, batch merge).
\r
560 var doc = firstCell.getDocument(),
\r
561 lastRowIndex = startRow,
\r
564 // Use a documentFragment as buffer when appending cell contents.
\r
565 frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),
\r
568 for ( var i = 0; i < cells.length; i++ )
\r
572 var tr = cell.getParent(),
\r
573 cellFirstChild = cell.getFirst(),
\r
574 colSpan = cell.$.colSpan,
\r
575 rowSpan = cell.$.rowSpan,
\r
576 rowIndex = tr.$.rowIndex,
\r
577 colIndex = cellInRow( map, rowIndex, cell );
\r
579 // Accumulated the actual places taken by all selected cells.
\r
580 dimension += colSpan * rowSpan;
\r
581 // Accumulated the maximum virtual spans from column and row.
\r
582 totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;
\r
583 totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );
\r
587 // Trim all cell fillers and check to remove empty cells.
\r
588 if ( trimCell( cell ), cell.getChildren().count() )
\r
590 // Merge vertically cells as two separated paragraphs.
\r
591 if ( rowIndex != lastRowIndex
\r
593 && !( cellFirstChild.isBlockBoundary
\r
594 && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )
\r
596 var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
597 if ( last && !( last.is && last.is( 'br' ) ) )
\r
598 frag.append( 'br' );
\r
601 cell.moveChildren( frag );
\r
603 i ? cell.remove() : cell.setHtml( '' );
\r
605 lastRowIndex = rowIndex;
\r
610 frag.moveChildren( firstCell );
\r
612 if ( !CKEDITOR.env.ie )
\r
613 firstCell.appendBogus();
\r
615 if ( totalColSpan >= mapWidth )
\r
616 firstCell.removeAttribute( 'rowSpan' );
\r
618 firstCell.$.rowSpan = totalRowSpan;
\r
620 if ( totalRowSpan >= mapHeight )
\r
621 firstCell.removeAttribute( 'colSpan' );
\r
623 firstCell.$.colSpan = totalColSpan;
\r
625 // Swip empty <tr> left at the end of table due to the merging.
\r
626 var trs = new CKEDITOR.dom.nodeList( table.$.rows ),
\r
627 count = trs.count();
\r
629 for ( i = count - 1; i >= 0; i-- )
\r
631 var tailTr = trs.getItem( i );
\r
632 if ( !tailTr.$.cells.length )
\r
642 // Be able to merge cells only if actual dimension of selected
\r
643 // cells equals to the caculated rectangle.
\r
645 return ( totalRowSpan * totalColSpan ) == dimension;
\r
648 function verticalSplitCell ( selection, isDetect )
\r
650 var cells = getSelectedCells( selection );
\r
651 if ( cells.length > 1 )
\r
653 else if ( isDetect )
\r
656 var cell = cells[ 0 ],
\r
657 tr = cell.getParent(),
\r
658 table = tr.getAscendant( 'table' ),
\r
659 map = CKEDITOR.tools.buildTableMap( table ),
\r
660 rowIndex = tr.$.rowIndex,
\r
661 colIndex = cellInRow( map, rowIndex, cell ),
\r
662 rowSpan = cell.$.rowSpan,
\r
670 newRowSpan = Math.ceil( rowSpan / 2 );
\r
671 newCellRowSpan = Math.floor( rowSpan / 2 );
\r
672 newRowIndex = rowIndex + newRowSpan;
\r
673 var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),
\r
674 newCellRow = cellInRow( map, newRowIndex ),
\r
677 newCell = cell.clone();
\r
679 // Figure out where to insert the new cell by checking the vitual row.
\r
680 for ( var c = 0; c < newCellRow.length; c++ )
\r
682 candidateCell = newCellRow[ c ];
\r
683 // Catch first cell actually following the column.
\r
684 if ( candidateCell.parentNode == newCellTr.$
\r
687 newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );
\r
691 candidateCell = null;
\r
694 // The destination row is empty, append at will.
\r
695 if ( !candidateCell )
\r
696 newCellTr.append( newCell, true );
\r
700 newCellRowSpan = newRowSpan = 1;
\r
702 newCellTr = tr.clone();
\r
703 newCellTr.insertAfter( tr );
\r
704 newCellTr.append( newCell = cell.clone() );
\r
706 var cellsInSameRow = cellInRow( map, rowIndex );
\r
707 for ( var i = 0; i < cellsInSameRow.length; i++ )
\r
708 cellsInSameRow[ i ].rowSpan++;
\r
711 if ( !CKEDITOR.env.ie )
\r
712 newCell.appendBogus();
\r
714 cell.$.rowSpan = newRowSpan;
\r
715 newCell.$.rowSpan = newCellRowSpan;
\r
716 if ( newRowSpan == 1 )
\r
717 cell.removeAttribute( 'rowSpan' );
\r
718 if ( newCellRowSpan == 1 )
\r
719 newCell.removeAttribute( 'rowSpan' );
\r
724 function horizontalSplitCell( selection, isDetect )
\r
726 var cells = getSelectedCells( selection );
\r
727 if ( cells.length > 1 )
\r
729 else if ( isDetect )
\r
732 var cell = cells[ 0 ],
\r
733 tr = cell.getParent(),
\r
734 table = tr.getAscendant( 'table' ),
\r
735 map = CKEDITOR.tools.buildTableMap( table ),
\r
736 rowIndex = tr.$.rowIndex,
\r
737 colIndex = cellInRow( map, rowIndex, cell ),
\r
738 colSpan = cell.$.colSpan,
\r
745 newColSpan = Math.ceil( colSpan / 2 );
\r
746 newCellColSpan = Math.floor( colSpan / 2 );
\r
750 newCellColSpan = newColSpan = 1;
\r
751 var cellsInSameCol = cellInCol( map, colIndex );
\r
752 for ( var i = 0; i < cellsInSameCol.length; i++ )
\r
753 cellsInSameCol[ i ].colSpan++;
\r
755 newCell = cell.clone();
\r
756 newCell.insertAfter( cell );
\r
757 if ( !CKEDITOR.env.ie )
\r
758 newCell.appendBogus();
\r
760 cell.$.colSpan = newColSpan;
\r
761 newCell.$.colSpan = newCellColSpan;
\r
762 if ( newColSpan == 1 )
\r
763 cell.removeAttribute( 'colSpan' );
\r
764 if ( newCellColSpan == 1 )
\r
765 newCell.removeAttribute( 'colSpan' );
\r
769 // Context menu on table caption incorrect (#3834)
\r
770 var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };
\r
772 CKEDITOR.plugins.tabletools =
\r
774 requires : [ 'table', 'dialog', 'contextmenu' ],
\r
776 init : function( editor )
\r
778 var lang = editor.lang.table;
\r
780 editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );
\r
781 CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );
\r
783 editor.addCommand( 'tableDelete',
\r
785 exec : function( editor )
\r
787 var selection = editor.getSelection(),
\r
788 startElement = selection && selection.getStartElement(),
\r
789 table = startElement && startElement.getAscendant( 'table', 1 );
\r
794 // 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
795 var parent = table.getParent();
\r
796 if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )
\r
799 var range = new CKEDITOR.dom.range( editor.document );
\r
800 range.moveToPosition( table, CKEDITOR.POSITION_BEFORE_START );
\r
806 editor.addCommand( 'rowDelete',
\r
808 exec : function( editor )
\r
810 var selection = editor.getSelection();
\r
811 placeCursorInCell( deleteRows( selection ) );
\r
815 editor.addCommand( 'rowInsertBefore',
\r
817 exec : function( editor )
\r
819 var selection = editor.getSelection();
\r
820 insertRow( selection, true );
\r
824 editor.addCommand( 'rowInsertAfter',
\r
826 exec : function( editor )
\r
828 var selection = editor.getSelection();
\r
829 insertRow( selection );
\r
833 editor.addCommand( 'columnDelete',
\r
835 exec : function( editor )
\r
837 var selection = editor.getSelection();
\r
838 var element = deleteColumns( selection );
\r
839 element && placeCursorInCell( element, true );
\r
843 editor.addCommand( 'columnInsertBefore',
\r
845 exec : function( editor )
\r
847 var selection = editor.getSelection();
\r
848 insertColumn( selection, true );
\r
852 editor.addCommand( 'columnInsertAfter',
\r
854 exec : function( editor )
\r
856 var selection = editor.getSelection();
\r
857 insertColumn( selection );
\r
861 editor.addCommand( 'cellDelete',
\r
863 exec : function( editor )
\r
865 var selection = editor.getSelection();
\r
866 deleteCells( selection );
\r
870 editor.addCommand( 'cellMerge',
\r
872 exec : function( editor )
\r
874 placeCursorInCell( mergeCells( editor.getSelection() ), true );
\r
878 editor.addCommand( 'cellMergeRight',
\r
880 exec : function( editor )
\r
882 placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );
\r
886 editor.addCommand( 'cellMergeDown',
\r
888 exec : function( editor )
\r
890 placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );
\r
894 editor.addCommand( 'cellVerticalSplit',
\r
896 exec : function( editor )
\r
898 placeCursorInCell( verticalSplitCell( editor.getSelection() ) );
\r
902 editor.addCommand( 'cellHorizontalSplit',
\r
904 exec : function( editor )
\r
906 placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );
\r
910 editor.addCommand( 'cellInsertBefore',
\r
912 exec : function( editor )
\r
914 var selection = editor.getSelection();
\r
915 insertCell( selection, true );
\r
919 editor.addCommand( 'cellInsertAfter',
\r
921 exec : function( editor )
\r
923 var selection = editor.getSelection();
\r
924 insertCell( selection );
\r
928 // If the "menu" plugin is loaded, register the menu items.
\r
929 if ( editor.addMenuItems )
\r
931 editor.addMenuItems(
\r
935 label : lang.cell.menu,
\r
936 group : 'tablecell',
\r
938 getItems : function()
\r
940 var selection = editor.getSelection(),
\r
941 cells = getSelectedCells( selection );
\r
943 tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
944 tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
945 tablecell_delete : CKEDITOR.TRISTATE_OFF,
\r
946 tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
947 tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
948 tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
949 tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
950 tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
951 tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
956 tablecell_insertBefore :
\r
958 label : lang.cell.insertBefore,
\r
959 group : 'tablecell',
\r
960 command : 'cellInsertBefore',
\r
964 tablecell_insertAfter :
\r
966 label : lang.cell.insertAfter,
\r
967 group : 'tablecell',
\r
968 command : 'cellInsertAfter',
\r
974 label : lang.cell.deleteCell,
\r
975 group : 'tablecell',
\r
976 command : 'cellDelete',
\r
982 label : lang.cell.merge,
\r
983 group : 'tablecell',
\r
984 command : 'cellMerge',
\r
988 tablecell_merge_right :
\r
990 label : lang.cell.mergeRight,
\r
991 group : 'tablecell',
\r
992 command : 'cellMergeRight',
\r
996 tablecell_merge_down :
\r
998 label : lang.cell.mergeDown,
\r
999 group : 'tablecell',
\r
1000 command : 'cellMergeDown',
\r
1004 tablecell_split_horizontal :
\r
1006 label : lang.cell.splitHorizontal,
\r
1007 group : 'tablecell',
\r
1008 command : 'cellHorizontalSplit',
\r
1012 tablecell_split_vertical :
\r
1014 label : lang.cell.splitVertical,
\r
1015 group : 'tablecell',
\r
1016 command : 'cellVerticalSplit',
\r
1020 tablecell_properties :
\r
1022 label : lang.cell.title,
\r
1023 group : 'tablecellproperties',
\r
1024 command : 'cellProperties',
\r
1030 label : lang.row.menu,
\r
1031 group : 'tablerow',
\r
1033 getItems : function()
\r
1036 tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
1037 tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
1038 tablerow_delete : CKEDITOR.TRISTATE_OFF
\r
1043 tablerow_insertBefore :
\r
1045 label : lang.row.insertBefore,
\r
1046 group : 'tablerow',
\r
1047 command : 'rowInsertBefore',
\r
1051 tablerow_insertAfter :
\r
1053 label : lang.row.insertAfter,
\r
1054 group : 'tablerow',
\r
1055 command : 'rowInsertAfter',
\r
1061 label : lang.row.deleteRow,
\r
1062 group : 'tablerow',
\r
1063 command : 'rowDelete',
\r
1069 label : lang.column.menu,
\r
1070 group : 'tablecolumn',
\r
1072 getItems : function()
\r
1075 tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
1076 tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
1077 tablecolumn_delete : CKEDITOR.TRISTATE_OFF
\r
1082 tablecolumn_insertBefore :
\r
1084 label : lang.column.insertBefore,
\r
1085 group : 'tablecolumn',
\r
1086 command : 'columnInsertBefore',
\r
1090 tablecolumn_insertAfter :
\r
1092 label : lang.column.insertAfter,
\r
1093 group : 'tablecolumn',
\r
1094 command : 'columnInsertAfter',
\r
1098 tablecolumn_delete :
\r
1100 label : lang.column.deleteColumn,
\r
1101 group : 'tablecolumn',
\r
1102 command : 'columnDelete',
\r
1108 // If the "contextmenu" plugin is laoded, register the listeners.
\r
1109 if ( editor.contextMenu )
\r
1111 editor.contextMenu.addListener( function( element, selection )
\r
1113 if ( !element || element.isReadOnly() )
\r
1118 if ( element.getName() in contextMenuTags )
\r
1121 tablecell : CKEDITOR.TRISTATE_OFF,
\r
1122 tablerow : CKEDITOR.TRISTATE_OFF,
\r
1123 tablecolumn : CKEDITOR.TRISTATE_OFF
\r
1126 element = element.getParent();
\r
1134 getSelectedCells : getSelectedCells
\r
1137 CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );
\r
1141 * Create a two-dimension array that reflects the actual layout of table cells,
\r
1142 * with cell spans, with mappings to the original td elements.
\r
1143 * @param table {CKEDITOR.dom.element}
\r
1145 CKEDITOR.tools.buildTableMap = function ( table )
\r
1147 var aRows = table.$.rows ;
\r
1149 // Row and Column counters.
\r
1154 for ( var i = 0 ; i < aRows.length ; i++ )
\r
1157 !aMap[r] && ( aMap[r] = [] );
\r
1161 for ( var j = 0 ; j < aRows[i].cells.length ; j++ )
\r
1163 var oCell = aRows[i].cells[j] ;
\r
1166 while ( aMap[r][c] )
\r
1169 var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;
\r
1170 var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;
\r
1172 for ( var rs = 0 ; rs < iRowSpan ; rs++ )
\r
1174 if ( !aMap[r + rs] )
\r
1175 aMap[r + rs] = [];
\r
1177 for ( var cs = 0 ; cs < iColSpan ; cs++ )
\r
1179 aMap[r + rs][c + cs] = aRows[i].cells[j] ;
\r
1183 c += iColSpan - 1 ;
\r