2 Copyright (c) 2003-2013, 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 nextCol.push( nextCell );
\r
281 for ( i = 0; i < height; i++ )
\r
285 if ( !cloneCol[ i ] )
\r
288 // Check whether there's a spanning column here, do not break it.
\r
289 if ( cloneCol[ i ].colSpan > 1
\r
290 && nextCol[ i ] == cloneCol[ i ] )
\r
292 cell = cloneCol[ i ];
\r
297 cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone();
\r
298 cell.removeAttribute( 'colSpan' );
\r
299 !CKEDITOR.env.ie && cell.appendBogus();
\r
300 cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) );
\r
304 i += cell.rowSpan - 1;
\r
308 function deleteColumns( selectionOrCell )
\r
310 var cells = getSelectedCells( selectionOrCell ),
\r
311 firstCell = cells[ 0 ],
\r
312 lastCell = cells[ cells.length - 1 ],
\r
313 table = firstCell.getAscendant( 'table' ),
\r
314 map = CKEDITOR.tools.buildTableMap( table ),
\r
319 // Figure out selected cells' column indices.
\r
320 for ( var i = 0, rows = map.length; i < rows; i++ )
\r
322 for ( var j = 0, cols = map[ i ].length; j < cols; j++ )
\r
324 if ( map[ i ][ j ] == firstCell.$ )
\r
326 if ( map[ i ][ j ] == lastCell.$ )
\r
331 // Delete cell or reduce cell spans by checking through the table map.
\r
332 for ( i = startColIndex; i <= endColIndex; i++ )
\r
334 for ( j = 0; j < map.length; j++ )
\r
336 var mapRow = map[ j ],
\r
337 row = new CKEDITOR.dom.element( table.$.rows[ j ] ),
\r
338 cell = new CKEDITOR.dom.element( mapRow[ i ] );
\r
342 if ( cell.$.colSpan == 1 )
\r
344 // Reduce the col spans.
\r
346 cell.$.colSpan -= 1;
\r
348 j += cell.$.rowSpan - 1;
\r
350 if ( !row.$.cells.length )
\r
351 rowsToDelete.push( row );
\r
356 var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells;
\r
358 // Where to put the cursor after columns been deleted?
\r
359 // 1. Into next cell of the first row if any;
\r
360 // 2. Into previous cell of the first row if any;
\r
361 // 3. Into table's parent element;
\r
362 var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) );
\r
364 // Delete table rows only if all columns are gone (do not remove empty row).
\r
365 if ( rowsToDelete.length == rows )
\r
368 return cursorPosition;
\r
371 function getFocusElementAfterDelCols( cells )
\r
373 var cellIndexList = [],
\r
374 table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),
\r
376 targetIndex, targetCell;
\r
378 // get the cellIndex list of delete cells
\r
379 for ( i = 0, length = cells.length; i < length; i++ )
\r
380 cellIndexList.push( cells[i].$.cellIndex );
\r
382 // get the focusable column index
\r
383 cellIndexList.sort();
\r
384 for ( i = 1, length = cellIndexList.length; i < length; i++ )
\r
386 if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )
\r
388 targetIndex = cellIndexList[ i - 1 ] + 1;
\r
393 if ( !targetIndex )
\r
394 targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )
\r
395 : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );
\r
397 // scan row by row to get the target cell
\r
398 var rows = table.$.rows;
\r
399 for ( i = 0, length = rows.length; i < length ; i++ )
\r
401 targetCell = rows[ i ].cells[ targetIndex ];
\r
406 return targetCell ? new CKEDITOR.dom.element( targetCell ) : table.getPrevious();
\r
409 function insertCell( selection, insertBefore )
\r
411 var startElement = selection.getStartElement();
\r
412 var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );
\r
417 // Create the new cell element to be added.
\r
418 var newCell = cell.clone();
\r
419 if ( !CKEDITOR.env.ie )
\r
420 newCell.appendBogus();
\r
422 if ( insertBefore )
\r
423 newCell.insertBefore( cell );
\r
425 newCell.insertAfter( cell );
\r
428 function deleteCells( selectionOrCell )
\r
430 if ( selectionOrCell instanceof CKEDITOR.dom.selection )
\r
432 var cellsToDelete = getSelectedCells( selectionOrCell );
\r
433 var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );
\r
434 var cellToFocus = getFocusElementAfterDelCells( cellsToDelete );
\r
436 for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )
\r
437 deleteCells( cellsToDelete[ i ] );
\r
440 placeCursorInCell( cellToFocus, true );
\r
444 else if ( selectionOrCell instanceof CKEDITOR.dom.element )
\r
446 var tr = selectionOrCell.getParent();
\r
447 if ( tr.getChildCount() == 1 )
\r
450 selectionOrCell.remove();
\r
454 // Remove filler at end and empty spaces around the cell content.
\r
455 function trimCell( cell )
\r
457 var bogus = cell.getBogus();
\r
458 bogus && bogus.remove();
\r
462 function placeCursorInCell( cell, placeAtEnd )
\r
464 var range = new CKEDITOR.dom.range( cell.getDocument() );
\r
465 if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )
\r
467 range.selectNodeContents( cell );
\r
468 range.collapse( placeAtEnd ? false : true );
\r
470 range.select( true );
\r
473 function cellInRow( tableMap, rowIndex, cell )
\r
475 var oRow = tableMap[ rowIndex ];
\r
476 if ( typeof cell == 'undefined' )
\r
479 for ( var c = 0 ; oRow && c < oRow.length ; c++ )
\r
481 if ( cell.is && oRow[c] == cell.$ )
\r
483 else if ( c == cell )
\r
484 return new CKEDITOR.dom.element( oRow[ c ] );
\r
486 return cell.is ? -1 : null;
\r
489 function cellInCol( tableMap, colIndex )
\r
492 for ( var r = 0; r < tableMap.length; r++ )
\r
494 var row = tableMap[ r ];
\r
495 oCol.push( row[ colIndex ] );
\r
497 // Avoid adding duplicate cells.
\r
498 if ( row[ colIndex ].rowSpan > 1 )
\r
499 r += row[ colIndex ].rowSpan - 1;
\r
504 function mergeCells( selection, mergeDirection, isDetect )
\r
506 var cells = getSelectedCells( selection );
\r
508 // Invalid merge request if:
\r
509 // 1. In batch mode despite that less than two selected.
\r
510 // 2. In solo mode while not exactly only one selected.
\r
511 // 3. Cells distributed in different table groups (e.g. from both thead and tbody).
\r
512 var commonAncestor;
\r
513 if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )
\r
514 || ( commonAncestor = selection.getCommonAncestor() )
\r
515 && commonAncestor.type == CKEDITOR.NODE_ELEMENT
\r
516 && commonAncestor.is( 'table' ) )
\r
522 firstCell = cells[ 0 ],
\r
523 table = firstCell.getAscendant( 'table' ),
\r
524 map = CKEDITOR.tools.buildTableMap( table ),
\r
525 mapHeight = map.length,
\r
526 mapWidth = map[ 0 ].length,
\r
527 startRow = firstCell.getParent().$.rowIndex,
\r
528 startColumn = cellInRow( map, startRow, firstCell );
\r
530 if ( mergeDirection )
\r
535 var rowspan = parseInt( firstCell.getAttribute( 'rowspan' ), 10 ) || 1;
\r
536 var colspan = parseInt( firstCell.getAttribute( 'colspan' ), 10 ) || 1;
\r
539 map[ mergeDirection == 'up' ?
\r
540 ( startRow - rowspan ):
\r
541 mergeDirection == 'down' ? ( startRow + rowspan ) : startRow ] [
\r
542 mergeDirection == 'left' ?
\r
543 ( startColumn - colspan ):
\r
544 mergeDirection == 'right' ? ( startColumn + colspan ) : startColumn ];
\r
552 // 1. No cell could be merged.
\r
553 // 2. Same cell actually.
\r
554 if ( !targetCell || firstCell.$ == targetCell )
\r
557 // Sort in map order regardless of the DOM sequence.
\r
558 cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?
\r
559 'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );
\r
562 // Start from here are merging way ignorance (merge up/right, batch merge).
\r
563 var doc = firstCell.getDocument(),
\r
564 lastRowIndex = startRow,
\r
567 // Use a documentFragment as buffer when appending cell contents.
\r
568 frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),
\r
571 for ( var i = 0; i < cells.length; i++ )
\r
575 var tr = cell.getParent(),
\r
576 cellFirstChild = cell.getFirst(),
\r
577 colSpan = cell.$.colSpan,
\r
578 rowSpan = cell.$.rowSpan,
\r
579 rowIndex = tr.$.rowIndex,
\r
580 colIndex = cellInRow( map, rowIndex, cell );
\r
582 // Accumulated the actual places taken by all selected cells.
\r
583 dimension += colSpan * rowSpan;
\r
584 // Accumulated the maximum virtual spans from column and row.
\r
585 totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;
\r
586 totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );
\r
590 // Trim all cell fillers and check to remove empty cells.
\r
591 if ( trimCell( cell ), cell.getChildren().count() )
\r
593 // Merge vertically cells as two separated paragraphs.
\r
594 if ( rowIndex != lastRowIndex
\r
596 && !( cellFirstChild.isBlockBoundary
\r
597 && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )
\r
599 var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
600 if ( last && !( last.is && last.is( 'br' ) ) )
\r
601 frag.append( 'br' );
\r
604 cell.moveChildren( frag );
\r
606 i ? cell.remove() : cell.setHtml( '' );
\r
608 lastRowIndex = rowIndex;
\r
613 frag.moveChildren( firstCell );
\r
615 if ( !CKEDITOR.env.ie )
\r
616 firstCell.appendBogus();
\r
618 if ( totalColSpan >= mapWidth )
\r
619 firstCell.removeAttribute( 'rowSpan' );
\r
621 firstCell.$.rowSpan = totalRowSpan;
\r
623 if ( totalRowSpan >= mapHeight )
\r
624 firstCell.removeAttribute( 'colSpan' );
\r
626 firstCell.$.colSpan = totalColSpan;
\r
628 // Swip empty <tr> left at the end of table due to the merging.
\r
629 var trs = new CKEDITOR.dom.nodeList( table.$.rows ),
\r
630 count = trs.count();
\r
632 for ( i = count - 1; i >= 0; i-- )
\r
634 var tailTr = trs.getItem( i );
\r
635 if ( !tailTr.$.cells.length )
\r
645 // Be able to merge cells only if actual dimension of selected
\r
646 // cells equals to the caculated rectangle.
\r
648 return ( totalRowSpan * totalColSpan ) == dimension;
\r
651 function verticalSplitCell ( selection, isDetect )
\r
653 var cells = getSelectedCells( selection );
\r
654 if ( cells.length > 1 )
\r
656 else if ( isDetect )
\r
659 var cell = cells[ 0 ],
\r
660 tr = cell.getParent(),
\r
661 table = tr.getAscendant( 'table' ),
\r
662 map = CKEDITOR.tools.buildTableMap( table ),
\r
663 rowIndex = tr.$.rowIndex,
\r
664 colIndex = cellInRow( map, rowIndex, cell ),
\r
665 rowSpan = cell.$.rowSpan,
\r
673 newRowSpan = Math.ceil( rowSpan / 2 );
\r
674 newCellRowSpan = Math.floor( rowSpan / 2 );
\r
675 newRowIndex = rowIndex + newRowSpan;
\r
676 var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),
\r
677 newCellRow = cellInRow( map, newRowIndex ),
\r
680 newCell = cell.clone();
\r
682 // Figure out where to insert the new cell by checking the vitual row.
\r
683 for ( var c = 0; c < newCellRow.length; c++ )
\r
685 candidateCell = newCellRow[ c ];
\r
686 // Catch first cell actually following the column.
\r
687 if ( candidateCell.parentNode == newCellTr.$
\r
690 newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );
\r
694 candidateCell = null;
\r
697 // The destination row is empty, append at will.
\r
698 if ( !candidateCell )
\r
699 newCellTr.append( newCell, true );
\r
703 newCellRowSpan = newRowSpan = 1;
\r
705 newCellTr = tr.clone();
\r
706 newCellTr.insertAfter( tr );
\r
707 newCellTr.append( newCell = cell.clone() );
\r
709 var cellsInSameRow = cellInRow( map, rowIndex );
\r
710 for ( var i = 0; i < cellsInSameRow.length; i++ )
\r
711 cellsInSameRow[ i ].rowSpan++;
\r
714 if ( !CKEDITOR.env.ie )
\r
715 newCell.appendBogus();
\r
717 cell.$.rowSpan = newRowSpan;
\r
718 newCell.$.rowSpan = newCellRowSpan;
\r
719 if ( newRowSpan == 1 )
\r
720 cell.removeAttribute( 'rowSpan' );
\r
721 if ( newCellRowSpan == 1 )
\r
722 newCell.removeAttribute( 'rowSpan' );
\r
727 function horizontalSplitCell( selection, isDetect )
\r
729 var cells = getSelectedCells( selection );
\r
730 if ( cells.length > 1 )
\r
732 else if ( isDetect )
\r
735 var cell = cells[ 0 ],
\r
736 tr = cell.getParent(),
\r
737 table = tr.getAscendant( 'table' ),
\r
738 map = CKEDITOR.tools.buildTableMap( table ),
\r
739 rowIndex = tr.$.rowIndex,
\r
740 colIndex = cellInRow( map, rowIndex, cell ),
\r
741 colSpan = cell.$.colSpan,
\r
748 newColSpan = Math.ceil( colSpan / 2 );
\r
749 newCellColSpan = Math.floor( colSpan / 2 );
\r
753 newCellColSpan = newColSpan = 1;
\r
754 var cellsInSameCol = cellInCol( map, colIndex );
\r
755 for ( var i = 0; i < cellsInSameCol.length; i++ )
\r
756 cellsInSameCol[ i ].colSpan++;
\r
758 newCell = cell.clone();
\r
759 newCell.insertAfter( cell );
\r
760 if ( !CKEDITOR.env.ie )
\r
761 newCell.appendBogus();
\r
763 cell.$.colSpan = newColSpan;
\r
764 newCell.$.colSpan = newCellColSpan;
\r
765 if ( newColSpan == 1 )
\r
766 cell.removeAttribute( 'colSpan' );
\r
767 if ( newCellColSpan == 1 )
\r
768 newCell.removeAttribute( 'colSpan' );
\r
772 // Context menu on table caption incorrect (#3834)
\r
773 var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };
\r
775 CKEDITOR.plugins.tabletools =
\r
777 requires : [ 'table', 'dialog' ],
\r
779 init : function( editor )
\r
781 var lang = editor.lang.table;
\r
783 editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );
\r
784 CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );
\r
786 editor.addCommand( 'tableDelete',
\r
788 exec : function( editor )
\r
790 var selection = editor.getSelection(),
\r
791 startElement = selection && selection.getStartElement(),
\r
792 table = startElement && startElement.getAscendant( 'table', 1 );
\r
797 // 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
798 var parent = table.getParent();
\r
799 if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )
\r
802 var range = new CKEDITOR.dom.range( editor.document );
\r
803 range.moveToPosition( table, CKEDITOR.POSITION_BEFORE_START );
\r
809 editor.addCommand( 'rowDelete',
\r
811 exec : function( editor )
\r
813 var selection = editor.getSelection();
\r
814 placeCursorInCell( deleteRows( selection ) );
\r
818 editor.addCommand( 'rowInsertBefore',
\r
820 exec : function( editor )
\r
822 var selection = editor.getSelection();
\r
823 insertRow( selection, true );
\r
827 editor.addCommand( 'rowInsertAfter',
\r
829 exec : function( editor )
\r
831 var selection = editor.getSelection();
\r
832 insertRow( selection );
\r
836 editor.addCommand( 'columnDelete',
\r
838 exec : function( editor )
\r
840 var selection = editor.getSelection();
\r
841 var element = deleteColumns( selection );
\r
842 element && placeCursorInCell( element, true );
\r
846 editor.addCommand( 'columnInsertBefore',
\r
848 exec : function( editor )
\r
850 var selection = editor.getSelection();
\r
851 insertColumn( selection, true );
\r
855 editor.addCommand( 'columnInsertAfter',
\r
857 exec : function( editor )
\r
859 var selection = editor.getSelection();
\r
860 insertColumn( selection );
\r
864 editor.addCommand( 'cellDelete',
\r
866 exec : function( editor )
\r
868 var selection = editor.getSelection();
\r
869 deleteCells( selection );
\r
873 editor.addCommand( 'cellMerge',
\r
875 exec : function( editor )
\r
877 placeCursorInCell( mergeCells( editor.getSelection() ), true );
\r
881 editor.addCommand( 'cellMergeRight',
\r
883 exec : function( editor )
\r
885 placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );
\r
889 editor.addCommand( 'cellMergeDown',
\r
891 exec : function( editor )
\r
893 placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );
\r
897 editor.addCommand( 'cellVerticalSplit',
\r
899 exec : function( editor )
\r
901 placeCursorInCell( verticalSplitCell( editor.getSelection() ) );
\r
905 editor.addCommand( 'cellHorizontalSplit',
\r
907 exec : function( editor )
\r
909 placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );
\r
913 editor.addCommand( 'cellInsertBefore',
\r
915 exec : function( editor )
\r
917 var selection = editor.getSelection();
\r
918 insertCell( selection, true );
\r
922 editor.addCommand( 'cellInsertAfter',
\r
924 exec : function( editor )
\r
926 var selection = editor.getSelection();
\r
927 insertCell( selection );
\r
931 // If the "menu" plugin is loaded, register the menu items.
\r
932 if ( editor.addMenuItems )
\r
934 editor.addMenuItems(
\r
938 label : lang.cell.menu,
\r
939 group : 'tablecell',
\r
941 getItems : function()
\r
943 var selection = editor.getSelection(),
\r
944 cells = getSelectedCells( selection );
\r
946 tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
947 tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
948 tablecell_delete : CKEDITOR.TRISTATE_OFF,
\r
949 tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
950 tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
951 tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
952 tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
953 tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
954 tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
959 tablecell_insertBefore :
\r
961 label : lang.cell.insertBefore,
\r
962 group : 'tablecell',
\r
963 command : 'cellInsertBefore',
\r
967 tablecell_insertAfter :
\r
969 label : lang.cell.insertAfter,
\r
970 group : 'tablecell',
\r
971 command : 'cellInsertAfter',
\r
977 label : lang.cell.deleteCell,
\r
978 group : 'tablecell',
\r
979 command : 'cellDelete',
\r
985 label : lang.cell.merge,
\r
986 group : 'tablecell',
\r
987 command : 'cellMerge',
\r
991 tablecell_merge_right :
\r
993 label : lang.cell.mergeRight,
\r
994 group : 'tablecell',
\r
995 command : 'cellMergeRight',
\r
999 tablecell_merge_down :
\r
1001 label : lang.cell.mergeDown,
\r
1002 group : 'tablecell',
\r
1003 command : 'cellMergeDown',
\r
1007 tablecell_split_horizontal :
\r
1009 label : lang.cell.splitHorizontal,
\r
1010 group : 'tablecell',
\r
1011 command : 'cellHorizontalSplit',
\r
1015 tablecell_split_vertical :
\r
1017 label : lang.cell.splitVertical,
\r
1018 group : 'tablecell',
\r
1019 command : 'cellVerticalSplit',
\r
1023 tablecell_properties :
\r
1025 label : lang.cell.title,
\r
1026 group : 'tablecellproperties',
\r
1027 command : 'cellProperties',
\r
1033 label : lang.row.menu,
\r
1034 group : 'tablerow',
\r
1036 getItems : function()
\r
1039 tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
1040 tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
1041 tablerow_delete : CKEDITOR.TRISTATE_OFF
\r
1046 tablerow_insertBefore :
\r
1048 label : lang.row.insertBefore,
\r
1049 group : 'tablerow',
\r
1050 command : 'rowInsertBefore',
\r
1054 tablerow_insertAfter :
\r
1056 label : lang.row.insertAfter,
\r
1057 group : 'tablerow',
\r
1058 command : 'rowInsertAfter',
\r
1064 label : lang.row.deleteRow,
\r
1065 group : 'tablerow',
\r
1066 command : 'rowDelete',
\r
1072 label : lang.column.menu,
\r
1073 group : 'tablecolumn',
\r
1075 getItems : function()
\r
1078 tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
1079 tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
1080 tablecolumn_delete : CKEDITOR.TRISTATE_OFF
\r
1085 tablecolumn_insertBefore :
\r
1087 label : lang.column.insertBefore,
\r
1088 group : 'tablecolumn',
\r
1089 command : 'columnInsertBefore',
\r
1093 tablecolumn_insertAfter :
\r
1095 label : lang.column.insertAfter,
\r
1096 group : 'tablecolumn',
\r
1097 command : 'columnInsertAfter',
\r
1101 tablecolumn_delete :
\r
1103 label : lang.column.deleteColumn,
\r
1104 group : 'tablecolumn',
\r
1105 command : 'columnDelete',
\r
1111 // If the "contextmenu" plugin is laoded, register the listeners.
\r
1112 if ( editor.contextMenu )
\r
1114 editor.contextMenu.addListener( function( element, selection )
\r
1116 if ( !element || element.isReadOnly() )
\r
1121 if ( element.getName() in contextMenuTags )
\r
1124 tablecell : CKEDITOR.TRISTATE_OFF,
\r
1125 tablerow : CKEDITOR.TRISTATE_OFF,
\r
1126 tablecolumn : CKEDITOR.TRISTATE_OFF
\r
1129 element = element.getParent();
\r
1137 getSelectedCells : getSelectedCells
\r
1140 CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );
\r
1144 * Create a two-dimension array that reflects the actual layout of table cells,
\r
1145 * with cell spans, with mappings to the original td elements.
\r
1146 * @param table {CKEDITOR.dom.element}
\r
1148 CKEDITOR.tools.buildTableMap = function ( table )
\r
1150 var aRows = table.$.rows ;
\r
1152 // Row and Column counters.
\r
1157 for ( var i = 0 ; i < aRows.length ; i++ )
\r
1160 !aMap[r] && ( aMap[r] = [] );
\r
1164 for ( var j = 0 ; j < aRows[i].cells.length ; j++ )
\r
1166 var oCell = aRows[i].cells[j] ;
\r
1169 while ( aMap[r][c] )
\r
1172 var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;
\r
1173 var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;
\r
1175 for ( var rs = 0 ; rs < iRowSpan ; rs++ )
\r
1177 if ( !aMap[r + rs] )
\r
1178 aMap[r + rs] = [];
\r
1180 for ( var cs = 0 ; cs < iColSpan ; cs++ )
\r
1182 aMap[r + rs][c + cs] = aRows[i].cells[j] ;
\r
1186 c += iColSpan - 1 ;
\r