2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 function removeRawAttribute( $node, attr )
\r
10 if ( CKEDITOR.env.ie )
\r
11 $node.removeAttribute( attr );
\r
13 delete $node[ attr ];
\r
16 var cellNodeRegex = /^(?:td|th)$/;
\r
18 function getSelectedCells( selection )
\r
20 // Walker will try to split text nodes, which will make the current selection
\r
21 // invalid. So save bookmarks before doing anything.
\r
22 var bookmarks = selection.createBookmarks();
\r
24 var ranges = selection.getRanges();
\r
28 function moveOutOfCellGuard( node )
\r
30 // Apply to the first cell only.
\r
31 if ( retval.length > 0 )
\r
34 // If we are exiting from the first </td>, then the td should definitely be
\r
36 if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )
\r
37 && !node.getCustomData( 'selected_cell' ) )
\r
39 CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );
\r
40 retval.push( node );
\r
44 for ( var i = 0 ; i < ranges.length ; i++ )
\r
46 var range = ranges[ i ];
\r
48 if ( range.collapsed )
\r
50 // Walker does not handle collapsed ranges yet - fall back to old API.
\r
51 var startNode = range.getCommonAncestor();
\r
52 var nearestCell = startNode.getAscendant( 'td', true ) || startNode.getAscendant( 'th', true );
\r
54 retval.push( nearestCell );
\r
58 var walker = new CKEDITOR.dom.walker( range );
\r
60 walker.guard = moveOutOfCellGuard;
\r
62 while ( ( node = walker.next() ) )
\r
64 // If may be possible for us to have a range like this:
\r
65 // <td>^1</td><td>^2</td>
\r
66 // The 2nd td shouldn't be included.
\r
68 // So we have to take care to include a td we've entered only when we've
\r
69 // walked into its children.
\r
71 var parent = node.getParent();
\r
72 if ( parent && cellNodeRegex.test( parent.getName() ) && !parent.getCustomData( 'selected_cell' ) )
\r
74 CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );
\r
75 retval.push( parent );
\r
81 CKEDITOR.dom.element.clearAllMarkers( database );
\r
83 // Restore selection position.
\r
84 selection.selectBookmarks( bookmarks );
\r
89 function getFocusElementAfterDelCells( cellsToDelete ) {
\r
91 last = cellsToDelete.length - 1,
\r
96 while ( ( cell = cellsToDelete[ i++ ] ) )
\r
97 CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );
\r
99 // 1.first we check left or right side focusable cell row by row;
\r
101 while ( ( cell = cellsToDelete[ i++ ] ) )
\r
103 if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )
\r
104 || ( focusedCell = cell.getNext() ) && !focusedCell.getCustomData( 'delete_cell' ) )
\r
106 CKEDITOR.dom.element.clearAllMarkers( database );
\r
107 return focusedCell;
\r
111 CKEDITOR.dom.element.clearAllMarkers( database );
\r
113 // 2. then we check the toppest row (outside the selection area square) focusable cell
\r
114 tr = cellsToDelete[ 0 ].getParent();
\r
115 if ( ( tr = tr.getPrevious() ) )
\r
116 return tr.getLast();
\r
118 // 3. last we check the lowerest row focusable cell
\r
119 tr = cellsToDelete[ last ].getParent();
\r
120 if ( ( tr = tr.getNext() ) )
\r
121 return tr.getChild( 0 );
\r
126 function clearRow( $tr )
\r
128 // Get the array of row's cells.
\r
129 var $cells = $tr.cells;
\r
131 // Empty all cells.
\r
132 for ( var i = 0 ; i < $cells.length ; i++ )
\r
134 $cells[ i ].innerHTML = '';
\r
136 if ( !CKEDITOR.env.ie )
\r
137 ( new CKEDITOR.dom.element( $cells[ i ] ) ).appendBogus();
\r
141 function insertRow( selection, insertBefore )
\r
143 // Get the row where the selection is placed in.
\r
144 var row = selection.getStartElement().getAscendant( 'tr' );
\r
148 // Create a clone of the row.
\r
149 var newRow = row.clone( true );
\r
152 newRow.insertBefore( row ) :
\r
153 newRow.insertAfter( row );
\r
155 // Clean the new row.
\r
156 clearRow( newRow.$ );
\r
159 function deleteRows( selectionOrRow )
\r
161 if ( selectionOrRow instanceof CKEDITOR.dom.selection )
\r
163 var cells = getSelectedCells( selectionOrRow ),
\r
164 cellsCount = cells.length,
\r
170 // Queue up the rows - it's possible and likely that we have duplicates.
\r
171 for ( var i = 0 ; i < cellsCount ; i++ )
\r
173 var row = cells[ i ].getParent(),
\r
174 rowIndex = row.$.rowIndex;
\r
176 !i && ( previousRowIndex = rowIndex - 1 );
\r
177 rowsToDelete[ rowIndex ] = row;
\r
178 i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 );
\r
181 var table = row.getAscendant( 'table' ),
\r
182 rows = table.$.rows,
\r
183 rowCount = rows.length;
\r
185 // Where to put the cursor after rows been deleted?
\r
186 // 1. Into next sibling row if any;
\r
187 // 2. Into previous sibling row if any;
\r
188 // 3. Into table's parent element if it's the very last row.
\r
189 cursorPosition = new CKEDITOR.dom.element(
\r
190 nextRowIndex < rowCount && table.$.rows[ nextRowIndex ] ||
\r
191 previousRowIndex > 0 && table.$.rows[ previousRowIndex ] ||
\r
192 table.$.parentNode );
\r
194 for ( i = rowsToDelete.length ; i >= 0 ; i-- )
\r
196 if ( rowsToDelete[ i ] )
\r
197 deleteRows( rowsToDelete[ i ] );
\r
200 return cursorPosition;
\r
202 else if ( selectionOrRow instanceof CKEDITOR.dom.element )
\r
204 table = selectionOrRow.getAscendant( 'table' );
\r
206 if ( table.$.rows.length == 1 )
\r
209 selectionOrRow.remove();
\r
215 function insertColumn( selection, insertBefore )
\r
217 // Get the cell where the selection is placed in.
\r
218 var startElement = selection.getStartElement();
\r
219 var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );
\r
224 // Get the cell's table.
\r
225 var table = cell.getAscendant( 'table' );
\r
226 var cellIndex = cell.$.cellIndex;
\r
228 // Loop through all rows available in the table.
\r
229 for ( var i = 0 ; i < table.$.rows.length ; i++ )
\r
231 var $row = table.$.rows[ i ];
\r
233 // If the row doesn't have enough cells, ignore it.
\r
234 if ( $row.cells.length < ( cellIndex + 1 ) )
\r
237 cell = ( new CKEDITOR.dom.element( $row.cells[ cellIndex ] ) ).clone( false );
\r
239 if ( !CKEDITOR.env.ie )
\r
240 cell.appendBogus();
\r
242 // Get back the currently selected cell.
\r
243 var baseCell = new CKEDITOR.dom.element( $row.cells[ cellIndex ] );
\r
244 if ( insertBefore )
\r
245 cell.insertBefore( baseCell );
\r
247 cell.insertAfter( baseCell );
\r
251 function getFocusElementAfterDelCols( cells )
\r
253 var cellIndexList = [],
\r
254 table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),
\r
256 targetIndex, targetCell;
\r
258 // get the cellIndex list of delete cells
\r
259 for ( i = 0, length = cells.length; i < length; i++ )
\r
260 cellIndexList.push( cells[i].$.cellIndex );
\r
262 // get the focusable column index
\r
263 cellIndexList.sort();
\r
264 for ( i = 1, length = cellIndexList.length; i < length; i++ )
\r
266 if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )
\r
268 targetIndex = cellIndexList[ i - 1 ] + 1;
\r
273 if ( !targetIndex )
\r
274 targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )
\r
275 : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );
\r
277 // scan row by row to get the target cell
\r
278 var rows = table.$.rows;
\r
279 for ( i = 0, length = rows.length; i < length ; i++ )
\r
281 targetCell = rows[ i ].cells[ targetIndex ];
\r
286 return targetCell ? new CKEDITOR.dom.element( targetCell ) : table.getPrevious();
\r
289 function deleteColumns( selectionOrCell )
\r
291 if ( selectionOrCell instanceof CKEDITOR.dom.selection )
\r
293 var colsToDelete = getSelectedCells( selectionOrCell ),
\r
294 elementToFocus = getFocusElementAfterDelCols( colsToDelete );
\r
296 for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- )
\r
298 if ( colsToDelete[ i ] )
\r
299 deleteColumns( colsToDelete[ i ] );
\r
302 return elementToFocus;
\r
304 else if ( selectionOrCell instanceof CKEDITOR.dom.element )
\r
306 // Get the cell's table.
\r
307 var table = selectionOrCell.getAscendant( 'table' );
\r
311 // Get the cell index.
\r
312 var cellIndex = selectionOrCell.$.cellIndex;
\r
315 * Loop through all rows from down to up, coz it's possible that some rows
\r
318 for ( i = table.$.rows.length - 1 ; i >= 0 ; i-- )
\r
321 var row = new CKEDITOR.dom.element( table.$.rows[ i ] );
\r
323 // If the cell to be removed is the first one and the row has just one cell.
\r
324 if ( !cellIndex && row.$.cells.length == 1 )
\r
330 // Else, just delete the cell.
\r
331 if ( row.$.cells[ cellIndex ] )
\r
332 row.$.removeChild( row.$.cells[ cellIndex ] );
\r
339 function insertCell( selection, insertBefore )
\r
341 var startElement = selection.getStartElement();
\r
342 var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );
\r
347 // Create the new cell element to be added.
\r
348 var newCell = cell.clone();
\r
349 if ( !CKEDITOR.env.ie )
\r
350 newCell.appendBogus();
\r
352 if ( insertBefore )
\r
353 newCell.insertBefore( cell );
\r
355 newCell.insertAfter( cell );
\r
358 function deleteCells( selectionOrCell )
\r
360 if ( selectionOrCell instanceof CKEDITOR.dom.selection )
\r
362 var cellsToDelete = getSelectedCells( selectionOrCell );
\r
363 var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );
\r
364 var cellToFocus = getFocusElementAfterDelCells( cellsToDelete );
\r
366 for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )
\r
367 deleteCells( cellsToDelete[ i ] );
\r
370 placeCursorInCell( cellToFocus, true );
\r
374 else if ( selectionOrCell instanceof CKEDITOR.dom.element )
\r
376 var tr = selectionOrCell.getParent();
\r
377 if ( tr.getChildCount() == 1 )
\r
380 selectionOrCell.remove();
\r
384 // Remove filler at end and empty spaces around the cell content.
\r
385 function trimCell( cell )
\r
387 var bogus = cell.getBogus();
\r
388 bogus && bogus.remove();
\r
392 function placeCursorInCell( cell, placeAtEnd )
\r
394 var range = new CKEDITOR.dom.range( cell.getDocument() );
\r
395 if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )
\r
397 range.selectNodeContents( cell );
\r
398 range.collapse( placeAtEnd ? false : true );
\r
400 range.select( true );
\r
403 function cellInRow( tableMap, rowIndex, cell )
\r
405 var oRow = tableMap[ rowIndex ];
\r
406 if ( typeof cell == 'undefined' )
\r
409 for ( var c = 0 ; oRow && c < oRow.length ; c++ )
\r
411 if ( cell.is && oRow[c] == cell.$ )
\r
413 else if ( c == cell )
\r
414 return new CKEDITOR.dom.element( oRow[ c ] );
\r
416 return cell.is ? -1 : null;
\r
419 function cellInCol( tableMap, colIndex, cell )
\r
422 for ( var r = 0; r < tableMap.length; r++ )
\r
424 var row = tableMap[ r ];
\r
425 if ( typeof cell == 'undefined' )
\r
426 oCol.push( row[ colIndex ] );
\r
427 else if ( cell.is && row[ colIndex ] == cell.$ )
\r
429 else if ( r == cell )
\r
430 return new CKEDITOR.dom.element( row[ colIndex ] );
\r
433 return ( typeof cell == 'undefined' )? oCol : cell.is ? -1 : null;
\r
436 function mergeCells( selection, mergeDirection, isDetect )
\r
438 var cells = getSelectedCells( selection );
\r
440 // Invalid merge request if:
\r
441 // 1. In batch mode despite that less than two selected.
\r
442 // 2. In solo mode while not exactly only one selected.
\r
443 // 3. Cells distributed in different table groups (e.g. from both thead and tbody).
\r
444 var commonAncestor;
\r
445 if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )
\r
446 || ( commonAncestor = selection.getCommonAncestor() )
\r
447 && commonAncestor.type == CKEDITOR.NODE_ELEMENT
\r
448 && commonAncestor.is( 'table' ) )
\r
454 firstCell = cells[ 0 ],
\r
455 table = firstCell.getAscendant( 'table' ),
\r
456 map = CKEDITOR.tools.buildTableMap( table ),
\r
457 mapHeight = map.length,
\r
458 mapWidth = map[ 0 ].length,
\r
459 startRow = firstCell.getParent().$.rowIndex,
\r
460 startColumn = cellInRow( map, startRow, firstCell );
\r
462 if ( mergeDirection )
\r
468 map[ mergeDirection == 'up' ?
\r
470 mergeDirection == 'down' ? ( startRow + 1 ) : startRow ] [
\r
471 mergeDirection == 'left' ?
\r
472 ( startColumn - 1 ):
\r
473 mergeDirection == 'right' ? ( startColumn + 1 ) : startColumn ];
\r
481 // 1. No cell could be merged.
\r
482 // 2. Same cell actually.
\r
483 if ( !targetCell || firstCell.$ == targetCell )
\r
486 // Sort in map order regardless of the DOM sequence.
\r
487 cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?
\r
488 'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );
\r
491 // Start from here are merging way ignorance (merge up/right, batch merge).
\r
492 var doc = firstCell.getDocument(),
\r
493 lastRowIndex = startRow,
\r
496 // Use a documentFragment as buffer when appending cell contents.
\r
497 frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),
\r
500 for ( var i = 0; i < cells.length; i++ )
\r
504 var tr = cell.getParent(),
\r
505 cellFirstChild = cell.getFirst(),
\r
506 colSpan = cell.$.colSpan,
\r
507 rowSpan = cell.$.rowSpan,
\r
508 rowIndex = tr.$.rowIndex,
\r
509 colIndex = cellInRow( map, rowIndex, cell );
\r
511 // Accumulated the actual places taken by all selected cells.
\r
512 dimension += colSpan * rowSpan;
\r
513 // Accumulated the maximum virtual spans from column and row.
\r
514 totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;
\r
515 totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );
\r
519 // Trim all cell fillers and check to remove empty cells.
\r
520 if ( trimCell( cell ), cell.getChildren().count() )
\r
522 // Merge vertically cells as two separated paragraphs.
\r
523 if ( rowIndex != lastRowIndex
\r
525 && !( cellFirstChild.isBlockBoundary
\r
526 && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )
\r
528 var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
529 if ( last && !( last.is && last.is( 'br' ) ) )
\r
530 frag.append( new CKEDITOR.dom.element( 'br' ) );
\r
533 cell.moveChildren( frag );
\r
535 i ? cell.remove() : cell.setHtml( '' );
\r
537 lastRowIndex = rowIndex;
\r
542 frag.moveChildren( firstCell );
\r
544 if ( !CKEDITOR.env.ie )
\r
545 firstCell.appendBogus();
\r
547 if ( totalColSpan >= mapWidth )
\r
548 firstCell.removeAttribute( 'rowSpan' );
\r
550 firstCell.$.rowSpan = totalRowSpan;
\r
552 if ( totalRowSpan >= mapHeight )
\r
553 firstCell.removeAttribute( 'colSpan' );
\r
555 firstCell.$.colSpan = totalColSpan;
\r
557 // Swip empty <tr> left at the end of table due to the merging.
\r
558 var trs = new CKEDITOR.dom.nodeList( table.$.rows ),
\r
559 count = trs.count();
\r
561 for ( i = count - 1; i >= 0; i-- )
\r
563 var tailTr = trs.getItem( i );
\r
564 if ( !tailTr.$.cells.length )
\r
574 // Be able to merge cells only if actual dimension of selected
\r
575 // cells equals to the caculated rectangle.
\r
577 return ( totalRowSpan * totalColSpan ) == dimension;
\r
580 function verticalSplitCell ( selection, isDetect )
\r
582 var cells = getSelectedCells( selection );
\r
583 if ( cells.length > 1 )
\r
585 else if ( isDetect )
\r
588 var cell = cells[ 0 ],
\r
589 tr = cell.getParent(),
\r
590 table = tr.getAscendant( 'table' ),
\r
591 map = CKEDITOR.tools.buildTableMap( table ),
\r
592 rowIndex = tr.$.rowIndex,
\r
593 colIndex = cellInRow( map, rowIndex, cell ),
\r
594 rowSpan = cell.$.rowSpan,
\r
602 newRowSpan = Math.ceil( rowSpan / 2 );
\r
603 newCellRowSpan = Math.floor( rowSpan / 2 );
\r
604 newRowIndex = rowIndex + newRowSpan;
\r
605 var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),
\r
606 newCellRow = cellInRow( map, newRowIndex ),
\r
609 newCell = cell.clone();
\r
611 // Figure out where to insert the new cell by checking the vitual row.
\r
612 for ( var c = 0; c < newCellRow.length; c++ )
\r
614 candidateCell = newCellRow[ c ];
\r
615 // Catch first cell actually following the column.
\r
616 if ( candidateCell.parentNode == newCellTr.$
\r
619 newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );
\r
623 candidateCell = null;
\r
626 // The destination row is empty, append at will.
\r
627 if ( !candidateCell )
\r
628 newCellTr.append( newCell, true );
\r
632 newCellRowSpan = newRowSpan = 1;
\r
634 newCellTr = tr.clone();
\r
635 newCellTr.insertAfter( tr );
\r
636 newCellTr.append( newCell = cell.clone() );
\r
638 var cellsInSameRow = cellInRow( map, rowIndex );
\r
639 for ( var i = 0; i < cellsInSameRow.length; i++ )
\r
640 cellsInSameRow[ i ].rowSpan++;
\r
643 if ( !CKEDITOR.env.ie )
\r
644 newCell.appendBogus();
\r
646 cell.$.rowSpan = newRowSpan;
\r
647 newCell.$.rowSpan = newCellRowSpan;
\r
648 if ( newRowSpan == 1 )
\r
649 cell.removeAttribute( 'rowSpan' );
\r
650 if ( newCellRowSpan == 1 )
\r
651 newCell.removeAttribute( 'rowSpan' );
\r
656 function horizontalSplitCell( selection, isDetect )
\r
658 var cells = getSelectedCells( selection );
\r
659 if ( cells.length > 1 )
\r
661 else if ( isDetect )
\r
664 var cell = cells[ 0 ],
\r
665 tr = cell.getParent(),
\r
666 table = tr.getAscendant( 'table' ),
\r
667 map = CKEDITOR.tools.buildTableMap( table ),
\r
668 rowIndex = tr.$.rowIndex,
\r
669 colIndex = cellInRow( map, rowIndex, cell ),
\r
670 colSpan = cell.$.colSpan,
\r
677 newColSpan = Math.ceil( colSpan / 2 );
\r
678 newCellColSpan = Math.floor( colSpan / 2 );
\r
682 newCellColSpan = newColSpan = 1;
\r
683 var cellsInSameCol = cellInCol( map, colIndex );
\r
684 for ( var i = 0; i < cellsInSameCol.length; i++ )
\r
685 cellsInSameCol[ i ].colSpan++;
\r
687 newCell = cell.clone();
\r
688 newCell.insertAfter( cell );
\r
689 if ( !CKEDITOR.env.ie )
\r
690 newCell.appendBogus();
\r
692 cell.$.colSpan = newColSpan;
\r
693 newCell.$.colSpan = newCellColSpan;
\r
694 if ( newColSpan == 1 )
\r
695 cell.removeAttribute( 'colSpan' );
\r
696 if ( newCellColSpan == 1 )
\r
697 newCell.removeAttribute( 'colSpan' );
\r
701 // Context menu on table caption incorrect (#3834)
\r
702 var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };
\r
704 CKEDITOR.plugins.tabletools =
\r
706 init : function( editor )
\r
708 var lang = editor.lang.table;
\r
710 editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );
\r
711 CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );
\r
713 editor.addCommand( 'tableDelete',
\r
715 exec : function( editor )
\r
717 var selection = editor.getSelection();
\r
718 var startElement = selection && selection.getStartElement();
\r
719 var table = startElement && startElement.getAscendant( 'table', true );
\r
724 // Maintain the selection point at where the table was deleted.
\r
725 selection.selectElement( table );
\r
726 var range = selection.getRanges()[0];
\r
728 selection.selectRanges( [ range ] );
\r
730 // If the table's parent has only one child, remove it,except body,as well.( #5416 )
\r
731 var parent = table.getParent();
\r
732 if ( parent.getChildCount() == 1 && parent.getName() != 'body' )
\r
739 editor.addCommand( 'rowDelete',
\r
741 exec : function( editor )
\r
743 var selection = editor.getSelection();
\r
744 placeCursorInCell( deleteRows( selection ) );
\r
748 editor.addCommand( 'rowInsertBefore',
\r
750 exec : function( editor )
\r
752 var selection = editor.getSelection();
\r
753 insertRow( selection, true );
\r
757 editor.addCommand( 'rowInsertAfter',
\r
759 exec : function( editor )
\r
761 var selection = editor.getSelection();
\r
762 insertRow( selection );
\r
766 editor.addCommand( 'columnDelete',
\r
768 exec : function( editor )
\r
770 var selection = editor.getSelection();
\r
771 var element = deleteColumns( selection );
\r
772 element && placeCursorInCell( element, true );
\r
776 editor.addCommand( 'columnInsertBefore',
\r
778 exec : function( editor )
\r
780 var selection = editor.getSelection();
\r
781 insertColumn( selection, true );
\r
785 editor.addCommand( 'columnInsertAfter',
\r
787 exec : function( editor )
\r
789 var selection = editor.getSelection();
\r
790 insertColumn( selection );
\r
794 editor.addCommand( 'cellDelete',
\r
796 exec : function( editor )
\r
798 var selection = editor.getSelection();
\r
799 deleteCells( selection );
\r
803 editor.addCommand( 'cellMerge',
\r
805 exec : function( editor )
\r
807 placeCursorInCell( mergeCells( editor.getSelection() ), true );
\r
811 editor.addCommand( 'cellMergeRight',
\r
813 exec : function( editor )
\r
815 placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );
\r
819 editor.addCommand( 'cellMergeDown',
\r
821 exec : function( editor )
\r
823 placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );
\r
827 editor.addCommand( 'cellVerticalSplit',
\r
829 exec : function( editor )
\r
831 placeCursorInCell( verticalSplitCell( editor.getSelection() ) );
\r
835 editor.addCommand( 'cellHorizontalSplit',
\r
837 exec : function( editor )
\r
839 placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );
\r
843 editor.addCommand( 'cellInsertBefore',
\r
845 exec : function( editor )
\r
847 var selection = editor.getSelection();
\r
848 insertCell( selection, true );
\r
852 editor.addCommand( 'cellInsertAfter',
\r
854 exec : function( editor )
\r
856 var selection = editor.getSelection();
\r
857 insertCell( selection );
\r
861 // If the "menu" plugin is loaded, register the menu items.
\r
862 if ( editor.addMenuItems )
\r
864 editor.addMenuItems(
\r
868 label : lang.cell.menu,
\r
869 group : 'tablecell',
\r
871 getItems : function()
\r
873 var selection = editor.getSelection(),
\r
874 cells = getSelectedCells( selection );
\r
876 tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
877 tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
878 tablecell_delete : CKEDITOR.TRISTATE_OFF,
\r
879 tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
880 tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
881 tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
882 tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
883 tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
884 tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
889 tablecell_insertBefore :
\r
891 label : lang.cell.insertBefore,
\r
892 group : 'tablecell',
\r
893 command : 'cellInsertBefore',
\r
897 tablecell_insertAfter :
\r
899 label : lang.cell.insertAfter,
\r
900 group : 'tablecell',
\r
901 command : 'cellInsertAfter',
\r
907 label : lang.cell.deleteCell,
\r
908 group : 'tablecell',
\r
909 command : 'cellDelete',
\r
915 label : lang.cell.merge,
\r
916 group : 'tablecell',
\r
917 command : 'cellMerge',
\r
921 tablecell_merge_right :
\r
923 label : lang.cell.mergeRight,
\r
924 group : 'tablecell',
\r
925 command : 'cellMergeRight',
\r
929 tablecell_merge_down :
\r
931 label : lang.cell.mergeDown,
\r
932 group : 'tablecell',
\r
933 command : 'cellMergeDown',
\r
937 tablecell_split_horizontal :
\r
939 label : lang.cell.splitHorizontal,
\r
940 group : 'tablecell',
\r
941 command : 'cellHorizontalSplit',
\r
945 tablecell_split_vertical :
\r
947 label : lang.cell.splitVertical,
\r
948 group : 'tablecell',
\r
949 command : 'cellVerticalSplit',
\r
953 tablecell_properties :
\r
955 label : lang.cell.title,
\r
956 group : 'tablecellproperties',
\r
957 command : 'cellProperties',
\r
963 label : lang.row.menu,
\r
964 group : 'tablerow',
\r
966 getItems : function()
\r
969 tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
970 tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
971 tablerow_delete : CKEDITOR.TRISTATE_OFF
\r
976 tablerow_insertBefore :
\r
978 label : lang.row.insertBefore,
\r
979 group : 'tablerow',
\r
980 command : 'rowInsertBefore',
\r
984 tablerow_insertAfter :
\r
986 label : lang.row.insertAfter,
\r
987 group : 'tablerow',
\r
988 command : 'rowInsertAfter',
\r
994 label : lang.row.deleteRow,
\r
995 group : 'tablerow',
\r
996 command : 'rowDelete',
\r
1002 label : lang.column.menu,
\r
1003 group : 'tablecolumn',
\r
1005 getItems : function()
\r
1008 tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,
\r
1009 tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,
\r
1010 tablecolumn_delete : CKEDITOR.TRISTATE_OFF
\r
1015 tablecolumn_insertBefore :
\r
1017 label : lang.column.insertBefore,
\r
1018 group : 'tablecolumn',
\r
1019 command : 'columnInsertBefore',
\r
1023 tablecolumn_insertAfter :
\r
1025 label : lang.column.insertAfter,
\r
1026 group : 'tablecolumn',
\r
1027 command : 'columnInsertAfter',
\r
1031 tablecolumn_delete :
\r
1033 label : lang.column.deleteColumn,
\r
1034 group : 'tablecolumn',
\r
1035 command : 'columnDelete',
\r
1041 // If the "contextmenu" plugin is laoded, register the listeners.
\r
1042 if ( editor.contextMenu )
\r
1044 editor.contextMenu.addListener( function( element, selection )
\r
1046 if ( !element || element.isReadOnly() )
\r
1051 if ( element.getName() in contextMenuTags )
\r
1054 tablecell : CKEDITOR.TRISTATE_OFF,
\r
1055 tablerow : CKEDITOR.TRISTATE_OFF,
\r
1056 tablecolumn : CKEDITOR.TRISTATE_OFF
\r
1059 element = element.getParent();
\r
1067 getSelectedCells : getSelectedCells
\r
1070 CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );
\r
1074 * Create a two-dimension array that reflects the actual layout of table cells,
\r
1075 * with cell spans, with mappings to the original td elements.
\r
1076 * @param table {CKEDITOR.dom.element}
\r
1078 CKEDITOR.tools.buildTableMap = function ( table )
\r
1080 var aRows = table.$.rows ;
\r
1082 // Row and Column counters.
\r
1087 for ( var i = 0 ; i < aRows.length ; i++ )
\r
1090 !aMap[r] && ( aMap[r] = [] );
\r
1094 for ( var j = 0 ; j < aRows[i].cells.length ; j++ )
\r
1096 var oCell = aRows[i].cells[j] ;
\r
1099 while ( aMap[r][c] )
\r
1102 var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;
\r
1103 var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;
\r
1105 for ( var rs = 0 ; rs < iRowSpan ; rs++ )
\r
1107 if ( !aMap[r + rs] )
\r
1108 aMap[r + rs] = [];
\r
1110 for ( var cs = 0 ; cs < iColSpan ; cs++ )
\r
1112 aMap[r + rs][c + cs] = aRows[i].cells[j] ;
\r
1116 c += iColSpan - 1 ;
\r