JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
75900780f069e0ce5a0bfb4b04ba04064e597ae3
[ckeditor.git] / _source / plugins / tabletools / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         function removeRawAttribute( $node, attr )\r
9         {\r
10                 if ( CKEDITOR.env.ie )\r
11                         $node.removeAttribute( attr );\r
12                 else\r
13                         delete $node[ attr ];\r
14         }\r
15 \r
16         var cellNodeRegex = /^(?:td|th)$/;\r
17 \r
18         function getSelectedCells( selection )\r
19         {\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
23 \r
24                 var ranges = selection.getRanges();\r
25                 var retval = [];\r
26                 var database = {};\r
27 \r
28                 function moveOutOfCellGuard( node )\r
29                 {\r
30                         // Apply to the first cell only.\r
31                         if ( retval.length > 0 )\r
32                                 return;\r
33 \r
34                         // If we are exiting from the first </td>, then the td should definitely be\r
35                         // included.\r
36                         if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )\r
37                                         && !node.getCustomData( 'selected_cell' ) )\r
38                         {\r
39                                 CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );\r
40                                 retval.push( node );\r
41                         }\r
42                 }\r
43 \r
44                 for ( var i = 0 ; i < ranges.length ; i++ )\r
45                 {\r
46                         var range = ranges[ i ];\r
47 \r
48                         if ( range.collapsed )\r
49                         {\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
53                                 if ( nearestCell )\r
54                                         retval.push( nearestCell );\r
55                         }\r
56                         else\r
57                         {\r
58                                 var walker = new CKEDITOR.dom.walker( range );\r
59                                 var node;\r
60                                 walker.guard = moveOutOfCellGuard;\r
61 \r
62                                 while ( ( node = walker.next() ) )\r
63                                 {\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
67                                         //\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
70 \r
71                                         var parent = node.getParent();\r
72                                         if ( parent && cellNodeRegex.test( parent.getName() ) && !parent.getCustomData( 'selected_cell' ) )\r
73                                         {\r
74                                                 CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );\r
75                                                 retval.push( parent );\r
76                                         }\r
77                                 }\r
78                         }\r
79                 }\r
80 \r
81                 CKEDITOR.dom.element.clearAllMarkers( database );\r
82 \r
83                 // Restore selection position.\r
84                 selection.selectBookmarks( bookmarks );\r
85 \r
86                 return retval;\r
87         }\r
88 \r
89         function getFocusedCell( cellsToDelete ) {\r
90                 var i = 0,\r
91                         last = cellsToDelete.length - 1,\r
92                         database = {},\r
93                         cell,focusedCell,\r
94                         tr;\r
95 \r
96                 while ( ( cell = cellsToDelete[ i++ ] ) )\r
97                         CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );\r
98 \r
99                 // 1.first we check left or right side focusable cell row by row;\r
100                 i = 0;\r
101                 while ( ( cell = cellsToDelete[ i++ ] ) )\r
102                 {\r
103                         if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )\r
104                           || ( focusedCell = cell.getNext()     ) && !focusedCell.getCustomData( 'delete_cell' ) )\r
105                         {\r
106                                 CKEDITOR.dom.element.clearAllMarkers( database );\r
107                                 return focusedCell;\r
108                         }\r
109                 }\r
110 \r
111                 CKEDITOR.dom.element.clearAllMarkers( database );\r
112 \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
117 \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
122 \r
123                 return null;\r
124         }\r
125 \r
126         function clearRow( $tr )\r
127         {\r
128                 // Get the array of row's cells.\r
129                 var $cells = $tr.cells;\r
130 \r
131                 // Empty all cells.\r
132                 for ( var i = 0 ; i < $cells.length ; i++ )\r
133                 {\r
134                         $cells[ i ].innerHTML = '';\r
135 \r
136                         if ( !CKEDITOR.env.ie )\r
137                                 ( new CKEDITOR.dom.element( $cells[ i ] ) ).appendBogus();\r
138                 }\r
139         }\r
140 \r
141         function insertRow( selection, insertBefore )\r
142         {\r
143                 // Get the row where the selection is placed in.\r
144                 var row = selection.getStartElement().getAscendant( 'tr' );\r
145                 if ( !row )\r
146                         return;\r
147 \r
148                 // Create a clone of the row.\r
149                 var newRow = row.clone( true );\r
150 \r
151                 // Insert the new row before of it.\r
152                 newRow.insertBefore( row );\r
153 \r
154                 // Clean one of the rows to produce the illusion of inserting an empty row\r
155                 // before or after.\r
156                 clearRow( insertBefore ? newRow.$ : row.$ );\r
157         }\r
158 \r
159         function deleteRows( selectionOrRow )\r
160         {\r
161                 if ( selectionOrRow instanceof CKEDITOR.dom.selection )\r
162                 {\r
163                         var cells = getSelectedCells( selectionOrRow ),\r
164                                 cellsCount = cells.length,\r
165                                 rowsToDelete = [],\r
166                                 cursorPosition,\r
167                                 previousRowIndex,\r
168                                 nextRowIndex;\r
169 \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
172                         {\r
173                                 var row = cells[ i ].getParent(),\r
174                                                 rowIndex = row.$.rowIndex;\r
175 \r
176                                 !i && ( previousRowIndex = rowIndex - 1 );\r
177                                 rowsToDelete[ rowIndex ] = row;\r
178                                 i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 );\r
179                         }\r
180 \r
181                         var table = row.getAscendant( 'table' ),\r
182                                         rows =  table.$.rows,\r
183                                         rowCount = rows.length;\r
184 \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
193 \r
194                         for ( i = rowsToDelete.length ; i >= 0 ; i-- )\r
195                         {\r
196                                 if ( rowsToDelete[ i ] )\r
197                                         deleteRows( rowsToDelete[ i ] );\r
198                         }\r
199 \r
200                         return cursorPosition;\r
201                 }\r
202                 else if ( selectionOrRow instanceof CKEDITOR.dom.element )\r
203                 {\r
204                         table = selectionOrRow.getAscendant( 'table' );\r
205 \r
206                         if ( table.$.rows.length == 1 )\r
207                                 table.remove();\r
208                         else\r
209                                 selectionOrRow.remove();\r
210                 }\r
211 \r
212                 return 0;\r
213         }\r
214 \r
215         function insertColumn( selection, insertBefore )\r
216         {\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
220 \r
221                 if ( !cell )\r
222                         return;\r
223 \r
224                 // Get the cell's table.\r
225                 var table = cell.getAscendant( 'table' );\r
226                 var cellIndex = cell.$.cellIndex;\r
227 \r
228                 // Loop through all rows available in the table.\r
229                 for ( var i = 0 ; i < table.$.rows.length ; i++ )\r
230                 {\r
231                         var $row = table.$.rows[ i ];\r
232 \r
233                         // If the row doesn't have enough cells, ignore it.\r
234                         if ( $row.cells.length < ( cellIndex + 1 ) )\r
235                                 continue;\r
236 \r
237                         cell = new CKEDITOR.dom.element( $row.cells[ cellIndex ].cloneNode( false ) );\r
238 \r
239                         if ( !CKEDITOR.env.ie )\r
240                                 cell.appendBogus();\r
241 \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
246                         else\r
247                                 cell.insertAfter( baseCell );\r
248                 }\r
249         }\r
250 \r
251         function deleteColumns( selectionOrCell )\r
252         {\r
253                 if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
254                 {\r
255                         var colsToDelete = getSelectedCells( selectionOrCell );\r
256                         for ( var i = colsToDelete.length ; i >= 0 ; i-- )\r
257                         {\r
258                                 if ( colsToDelete[ i ] )\r
259                                         deleteColumns( colsToDelete[ i ] );\r
260                         }\r
261                 }\r
262                 else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
263                 {\r
264                         // Get the cell's table.\r
265                         var table = selectionOrCell.getAscendant( 'table' );\r
266 \r
267                         // Get the cell index.\r
268                         var cellIndex = selectionOrCell.$.cellIndex;\r
269 \r
270                         /*\r
271                          * Loop through all rows from down to up, coz it's possible that some rows\r
272                          * will be deleted.\r
273                          */\r
274                         for ( i = table.$.rows.length - 1 ; i >= 0 ; i-- )\r
275                         {\r
276                                 // Get the row.\r
277                                 var row = new CKEDITOR.dom.element( table.$.rows[ i ] );\r
278 \r
279                                 // If the cell to be removed is the first one and the row has just one cell.\r
280                                 if ( !cellIndex && row.$.cells.length == 1 )\r
281                                 {\r
282                                         deleteRows( row );\r
283                                         continue;\r
284                                 }\r
285 \r
286                                 // Else, just delete the cell.\r
287                                 if ( row.$.cells[ cellIndex ] )\r
288                                         row.$.removeChild( row.$.cells[ cellIndex ] );\r
289                         }\r
290                 }\r
291         }\r
292 \r
293         function insertCell( selection, insertBefore )\r
294         {\r
295                 var startElement = selection.getStartElement();\r
296                 var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );\r
297 \r
298                 if ( !cell )\r
299                         return;\r
300 \r
301                 // Create the new cell element to be added.\r
302                 var newCell = cell.clone();\r
303                 if ( !CKEDITOR.env.ie )\r
304                         newCell.appendBogus();\r
305 \r
306                 if ( insertBefore )\r
307                         newCell.insertBefore( cell );\r
308                 else\r
309                         newCell.insertAfter( cell );\r
310         }\r
311 \r
312         function deleteCells( selectionOrCell )\r
313         {\r
314                 if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
315                 {\r
316                         var cellsToDelete = getSelectedCells( selectionOrCell );\r
317                         var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
318                         var cellToFocus   = getFocusedCell( cellsToDelete );\r
319 \r
320                         for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
321                                 deleteCells( cellsToDelete[ i ] );\r
322 \r
323                         if ( cellToFocus )\r
324                                 placeCursorInCell( cellToFocus, true );\r
325                         else if ( table )\r
326                                 table.remove();\r
327                 }\r
328                 else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
329                 {\r
330                         var tr = selectionOrCell.getParent();\r
331                         if ( tr.getChildCount() == 1 )\r
332                                 tr.remove();\r
333                         else\r
334                                 selectionOrCell.remove();\r
335                 }\r
336         }\r
337 \r
338         // Remove filler at end and empty spaces around the cell content.\r
339         function trimCell( cell )\r
340         {\r
341                 var bogus = cell.getBogus();\r
342                 bogus && bogus.remove();\r
343                 cell.trim();\r
344         }\r
345 \r
346         function placeCursorInCell( cell, placeAtEnd )\r
347         {\r
348                 var range = new CKEDITOR.dom.range( cell.getDocument() );\r
349                 if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )\r
350                 {\r
351                         range.selectNodeContents( cell );\r
352                         range.collapse( placeAtEnd ? false : true );\r
353                 }\r
354                 range.select( true );\r
355         }\r
356 \r
357         function buildTableMap( table )\r
358         {\r
359 \r
360                 var aRows = table.$.rows ;\r
361 \r
362                 // Row and Column counters.\r
363                 var r = -1 ;\r
364 \r
365                 var aMap = [];\r
366 \r
367                 for ( var i = 0 ; i < aRows.length ; i++ )\r
368                 {\r
369                         r++ ;\r
370                         !aMap[r] && ( aMap[r] = [] );\r
371 \r
372                         var c = -1 ;\r
373 \r
374                         for ( var j = 0 ; j < aRows[i].cells.length ; j++ )\r
375                         {\r
376                                 var oCell = aRows[i].cells[j] ;\r
377 \r
378                                 c++ ;\r
379                                 while ( aMap[r][c] )\r
380                                         c++ ;\r
381 \r
382                                 var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;\r
383                                 var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;\r
384 \r
385                                 for ( var rs = 0 ; rs < iRowSpan ; rs++ )\r
386                                 {\r
387                                         if ( !aMap[r + rs] )\r
388                                                 aMap[r + rs] = new Array() ;\r
389 \r
390                                         for ( var cs = 0 ; cs < iColSpan ; cs++ )\r
391                                         {\r
392                                                 aMap[r + rs][c + cs] = aRows[i].cells[j] ;\r
393                                         }\r
394                                 }\r
395 \r
396                                 c += iColSpan - 1 ;\r
397                         }\r
398                 }\r
399                 return aMap ;\r
400         }\r
401 \r
402         function cellInRow( tableMap, rowIndex, cell )\r
403         {\r
404                 var oRow = tableMap[ rowIndex ];\r
405                 if ( typeof cell == 'undefined' )\r
406                         return oRow;\r
407 \r
408                 for ( var c = 0 ; oRow && c < oRow.length ; c++ )\r
409                 {\r
410                         if ( cell.is && oRow[c] == cell.$ )\r
411                                 return c;\r
412                         else if ( c == cell )\r
413                                 return new CKEDITOR.dom.element( oRow[ c ] );\r
414                 }\r
415                 return cell.is ? -1 : null;\r
416         }\r
417 \r
418         function cellInCol( tableMap, colIndex, cell )\r
419         {\r
420                 var oCol = [];\r
421                 for ( var r = 0; r < tableMap.length; r++ )\r
422                 {\r
423                         var row = tableMap[ r ];\r
424                         if ( typeof cell == 'undefined' )\r
425                                 oCol.push( row[ colIndex ] );\r
426                         else if ( cell.is && row[ colIndex ] == cell.$ )\r
427                                 return r;\r
428                         else if ( r == cell )\r
429                                 return new CKEDITOR.dom.element( row[ colIndex ] );\r
430                 }\r
431 \r
432                 return ( typeof cell == 'undefined' )? oCol : cell.is ? -1 :  null;\r
433         }\r
434 \r
435         function mergeCells( selection, mergeDirection, isDetect )\r
436         {\r
437                 var cells = getSelectedCells( selection );\r
438 \r
439                 // Invalid merge request if:\r
440                 // 1. In batch mode despite that less than two selected.\r
441                 // 2. In solo mode while not exactly only one selected.\r
442                 // 3. Cells distributed in different table groups (e.g. from both thead and tbody).\r
443                 var commonAncestor;\r
444                 if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )\r
445                                 || ( commonAncestor = selection.getCommonAncestor() )\r
446                                 && commonAncestor.type == CKEDITOR.NODE_ELEMENT\r
447                                 && commonAncestor.is( 'table' ) )\r
448                 {\r
449                         return false;\r
450                 }\r
451 \r
452                 var     cell,\r
453                         firstCell = cells[ 0 ],\r
454                         table = firstCell.getAscendant( 'table' ),\r
455                         map = buildTableMap( table ),\r
456                         mapHeight = map.length,\r
457                         mapWidth = map[ 0 ].length,\r
458                         startRow = firstCell.getParent().$.rowIndex,\r
459                         startColumn = cellInRow( map, startRow, firstCell );\r
460 \r
461                 if ( mergeDirection )\r
462                 {\r
463                         var targetCell;\r
464                         try\r
465                         {\r
466                                 targetCell =\r
467                                         map[ mergeDirection == 'up' ?\r
468                                                         ( startRow - 1 ):\r
469                                                         mergeDirection == 'down' ? ( startRow + 1 ) : startRow  ] [\r
470                                                  mergeDirection == 'left' ?\r
471                                                         ( startColumn - 1 ):\r
472                                                  mergeDirection == 'right' ?  ( startColumn + 1 ) : startColumn ];\r
473 \r
474                         }\r
475                         catch( er )\r
476                         {\r
477                                 return false;\r
478                         }\r
479 \r
480                         // 1. No cell could be merged.\r
481                         // 2. Same cell actually.\r
482                         if ( !targetCell || firstCell.$ == targetCell  )\r
483                                 return false;\r
484 \r
485                         // Sort in map order regardless of the DOM sequence.\r
486                         cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?\r
487                                  'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );\r
488                 }\r
489 \r
490                 // Start from here are merging way ignorance (merge up/right, batch merge).\r
491                 var     doc = firstCell.getDocument(),\r
492                         lastRowIndex = startRow,\r
493                         totalRowSpan = 0,\r
494                         totalColSpan = 0,\r
495                         // Use a documentFragment as buffer when appending cell contents.\r
496                         frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),\r
497                         dimension = 0;\r
498 \r
499                 for ( var i = 0; i < cells.length; i++ )\r
500                 {\r
501                         cell = cells[ i ];\r
502 \r
503                         var tr = cell.getParent(),\r
504                                 cellFirstChild = cell.getFirst(),\r
505                                 colSpan = cell.$.colSpan,\r
506                                 rowSpan = cell.$.rowSpan,\r
507                                 rowIndex = tr.$.rowIndex,\r
508                                 colIndex = cellInRow( map, rowIndex, cell );\r
509 \r
510                         // Accumulated the actual places taken by all selected cells.\r
511                         dimension += colSpan * rowSpan;\r
512                         // Accumulated the maximum virtual spans from column and row.\r
513                         totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;\r
514                         totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );\r
515 \r
516                         if ( !isDetect )\r
517                         {\r
518                                 // Trim all cell fillers and check to remove empty cells.\r
519                                 if ( trimCell( cell ), cell.getChildren().count() )\r
520                                 {\r
521                                         // Merge vertically cells as two separated paragraphs.\r
522                                         if ( rowIndex != lastRowIndex\r
523                                                 && cellFirstChild\r
524                                                 && !( cellFirstChild.isBlockBoundary\r
525                                                           && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )\r
526                                         {\r
527                                                 var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
528                                                 if ( last && !( last.is && last.is( 'br' ) ) )\r
529                                                         frag.append( new CKEDITOR.dom.element( 'br' ) );\r
530                                         }\r
531 \r
532                                         cell.moveChildren( frag );\r
533                                 }\r
534                                 i ? cell.remove() : cell.setHtml( '' );\r
535                         }\r
536                         lastRowIndex = rowIndex;\r
537                 }\r
538 \r
539                 if ( !isDetect )\r
540                 {\r
541                         frag.moveChildren( firstCell );\r
542 \r
543                         if ( !CKEDITOR.env.ie )\r
544                                 firstCell.appendBogus();\r
545 \r
546                         if ( totalColSpan >= mapWidth )\r
547                                 firstCell.removeAttribute( 'rowSpan' );\r
548                         else\r
549                                 firstCell.$.rowSpan = totalRowSpan;\r
550 \r
551                         if ( totalRowSpan >= mapHeight )\r
552                                 firstCell.removeAttribute( 'colSpan' );\r
553                         else\r
554                                 firstCell.$.colSpan = totalColSpan;\r
555 \r
556                         // Swip empty <tr> left at the end of table due to the merging.\r
557                         var trs = new CKEDITOR.dom.nodeList( table.$.rows ),\r
558                                 count = trs.count();\r
559 \r
560                         for ( i = count - 1; i >= 0; i-- )\r
561                         {\r
562                                 var tailTr = trs.getItem( i );\r
563                                 if ( !tailTr.$.cells.length )\r
564                                 {\r
565                                         tailTr.remove();\r
566                                         count++;\r
567                                         continue;\r
568                                 }\r
569                         }\r
570 \r
571                         return firstCell;\r
572                 }\r
573                 // Be able to merge cells only if actual dimension of selected\r
574                 // cells equals to the caculated rectangle.\r
575                 else\r
576                         return ( totalRowSpan * totalColSpan ) == dimension;\r
577         }\r
578 \r
579         function verticalSplitCell ( selection, isDetect )\r
580         {\r
581                 var cells = getSelectedCells( selection );\r
582                 if ( cells.length > 1 )\r
583                         return false;\r
584                 else if ( isDetect )\r
585                         return true;\r
586 \r
587                 var cell = cells[ 0 ],\r
588                         tr = cell.getParent(),\r
589                         table = tr.getAscendant( 'table' ),\r
590                         map = buildTableMap( table ),\r
591                         rowIndex = tr.$.rowIndex,\r
592                         colIndex = cellInRow( map, rowIndex, cell ),\r
593                         rowSpan = cell.$.rowSpan,\r
594                         newCell,\r
595                         newRowSpan,\r
596                         newCellRowSpan,\r
597                         newRowIndex;\r
598 \r
599                 if ( rowSpan > 1 )\r
600                 {\r
601                         newRowSpan = Math.ceil( rowSpan / 2 );\r
602                         newCellRowSpan = Math.floor( rowSpan / 2 );\r
603                         newRowIndex = rowIndex + newRowSpan;\r
604                         var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),\r
605                                 newCellRow = cellInRow( map, newRowIndex ),\r
606                                 candidateCell;\r
607 \r
608                         newCell = cell.clone();\r
609 \r
610                         // Figure out where to insert the new cell by checking the vitual row.\r
611                         for ( var c = 0; c < newCellRow.length; c++ )\r
612                         {\r
613                                 candidateCell = newCellRow[ c ];\r
614                                 // Catch first cell actually following the column.\r
615                                 if ( candidateCell.parentNode == newCellTr.$\r
616                                         && c > colIndex )\r
617                                 {\r
618                                         newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );\r
619                                         break;\r
620                                 }\r
621                                 else\r
622                                         candidateCell = null;\r
623                         }\r
624 \r
625                         // The destination row is empty, append at will.\r
626                         if ( !candidateCell )\r
627                                 newCellTr.append( newCell, true );\r
628                 }\r
629                 else\r
630                 {\r
631                         newCellRowSpan = newRowSpan = 1;\r
632 \r
633                         newCellTr = tr.clone();\r
634                         newCellTr.insertAfter( tr );\r
635                         newCellTr.append( newCell = cell.clone() );\r
636 \r
637                         var cellsInSameRow = cellInRow( map, rowIndex );\r
638                         for ( var i = 0; i < cellsInSameRow.length; i++ )\r
639                                 cellsInSameRow[ i ].rowSpan++;\r
640                 }\r
641 \r
642                 if ( !CKEDITOR.env.ie )\r
643                         newCell.appendBogus();\r
644 \r
645                 cell.$.rowSpan = newRowSpan;\r
646                 newCell.$.rowSpan = newCellRowSpan;\r
647                 if ( newRowSpan == 1 )\r
648                         cell.removeAttribute( 'rowSpan' );\r
649                 if ( newCellRowSpan == 1 )\r
650                         newCell.removeAttribute( 'rowSpan' );\r
651 \r
652                 return newCell;\r
653         }\r
654 \r
655         function horizontalSplitCell( selection, isDetect )\r
656         {\r
657                 var cells = getSelectedCells( selection );\r
658                 if ( cells.length > 1 )\r
659                         return false;\r
660                 else if ( isDetect )\r
661                         return true;\r
662 \r
663                 var cell = cells[ 0 ],\r
664                         tr = cell.getParent(),\r
665                         table = tr.getAscendant( 'table' ),\r
666                         map = buildTableMap( table ),\r
667                         rowIndex = tr.$.rowIndex,\r
668                         colIndex = cellInRow( map, rowIndex, cell ),\r
669                         colSpan = cell.$.colSpan,\r
670                         newCell,\r
671                         newColSpan,\r
672                         newCellColSpan;\r
673 \r
674                 if ( colSpan > 1 )\r
675                 {\r
676                         newColSpan = Math.ceil( colSpan / 2 );\r
677                         newCellColSpan = Math.floor( colSpan / 2 );\r
678                 }\r
679                 else\r
680                 {\r
681                         newCellColSpan = newColSpan = 1;\r
682                         var cellsInSameCol = cellInCol( map, colIndex );\r
683                         for ( var i = 0; i < cellsInSameCol.length; i++ )\r
684                                 cellsInSameCol[ i ].colSpan++;\r
685                 }\r
686                 newCell = cell.clone();\r
687                 newCell.insertAfter( cell );\r
688                 if ( !CKEDITOR.env.ie )\r
689                         newCell.appendBogus();\r
690 \r
691                 cell.$.colSpan = newColSpan;\r
692                 newCell.$.colSpan = newCellColSpan;\r
693                 if ( newColSpan == 1 )\r
694                         cell.removeAttribute( 'colSpan' );\r
695                 if ( newCellColSpan == 1 )\r
696                         newCell.removeAttribute( 'colSpan' );\r
697 \r
698                 return newCell;\r
699         }\r
700         // Context menu on table caption incorrect (#3834)\r
701         var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };\r
702 \r
703         CKEDITOR.plugins.tabletools =\r
704         {\r
705                 init : function( editor )\r
706                 {\r
707                         var lang = editor.lang.table;\r
708 \r
709                         editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );\r
710                         CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );\r
711 \r
712                         editor.addCommand( 'tableDelete',\r
713                                 {\r
714                                         exec : function( editor )\r
715                                         {\r
716                                                 var selection = editor.getSelection();\r
717                                                 var startElement = selection && selection.getStartElement();\r
718                                                 var table = startElement && startElement.getAscendant( 'table', true );\r
719 \r
720                                                 if ( !table )\r
721                                                         return;\r
722 \r
723                                                 // Maintain the selection point at where the table was deleted.\r
724                                                 selection.selectElement( table );\r
725                                                 var range = selection.getRanges()[0];\r
726                                                 range.collapse();\r
727                                                 selection.selectRanges( [ range ] );\r
728 \r
729                                                 // If the table's parent has only one child, remove it as well.\r
730                                                 if ( table.getParent().getChildCount() == 1 )\r
731                                                         table.getParent().remove();\r
732                                                 else\r
733                                                         table.remove();\r
734                                         }\r
735                                 } );\r
736 \r
737                         editor.addCommand( 'rowDelete',\r
738                                 {\r
739                                         exec : function( editor )\r
740                                         {\r
741                                                 var selection = editor.getSelection();\r
742                                                 placeCursorInCell( deleteRows( selection ) );\r
743                                         }\r
744                                 } );\r
745 \r
746                         editor.addCommand( 'rowInsertBefore',\r
747                                 {\r
748                                         exec : function( editor )\r
749                                         {\r
750                                                 var selection = editor.getSelection();\r
751                                                 insertRow( selection, true );\r
752                                         }\r
753                                 } );\r
754 \r
755                         editor.addCommand( 'rowInsertAfter',\r
756                                 {\r
757                                         exec : function( editor )\r
758                                         {\r
759                                                 var selection = editor.getSelection();\r
760                                                 insertRow( selection );\r
761                                         }\r
762                                 } );\r
763 \r
764                         editor.addCommand( 'columnDelete',\r
765                                 {\r
766                                         exec : function( editor )\r
767                                         {\r
768                                                 var selection = editor.getSelection();\r
769                                                 deleteColumns( selection );\r
770                                         }\r
771                                 } );\r
772 \r
773                         editor.addCommand( 'columnInsertBefore',\r
774                                 {\r
775                                         exec : function( editor )\r
776                                         {\r
777                                                 var selection = editor.getSelection();\r
778                                                 insertColumn( selection, true );\r
779                                         }\r
780                                 } );\r
781 \r
782                         editor.addCommand( 'columnInsertAfter',\r
783                                 {\r
784                                         exec : function( editor )\r
785                                         {\r
786                                                 var selection = editor.getSelection();\r
787                                                 insertColumn( selection );\r
788                                         }\r
789                                 } );\r
790 \r
791                         editor.addCommand( 'cellDelete',\r
792                                 {\r
793                                         exec : function( editor )\r
794                                         {\r
795                                                 var selection = editor.getSelection();\r
796                                                 deleteCells( selection );\r
797                                         }\r
798                                 } );\r
799 \r
800                         editor.addCommand( 'cellMerge',\r
801                                 {\r
802                                         exec : function( editor )\r
803                                         {\r
804                                                 placeCursorInCell( mergeCells( editor.getSelection() ), true );\r
805                                         }\r
806                                 } );\r
807 \r
808                         editor.addCommand( 'cellMergeRight',\r
809                                 {\r
810                                         exec : function( editor )\r
811                                         {\r
812                                                 placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );\r
813                                         }\r
814                                 } );\r
815 \r
816                         editor.addCommand( 'cellMergeDown',\r
817                                 {\r
818                                         exec : function( editor )\r
819                                         {\r
820                                                 placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );\r
821                                         }\r
822                                 } );\r
823 \r
824                         editor.addCommand( 'cellVerticalSplit',\r
825                                 {\r
826                                         exec : function( editor )\r
827                                         {\r
828                                                 placeCursorInCell( verticalSplitCell( editor.getSelection() ) );\r
829                                         }\r
830                                 } );\r
831 \r
832                         editor.addCommand( 'cellHorizontalSplit',\r
833                                 {\r
834                                         exec : function( editor )\r
835                                         {\r
836                                                 placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );\r
837                                         }\r
838                                 } );\r
839 \r
840                         editor.addCommand( 'cellInsertBefore',\r
841                                 {\r
842                                         exec : function( editor )\r
843                                         {\r
844                                                 var selection = editor.getSelection();\r
845                                                 insertCell( selection, true );\r
846                                         }\r
847                                 } );\r
848 \r
849                         editor.addCommand( 'cellInsertAfter',\r
850                                 {\r
851                                         exec : function( editor )\r
852                                         {\r
853                                                 var selection = editor.getSelection();\r
854                                                 insertCell( selection );\r
855                                         }\r
856                                 } );\r
857 \r
858                         // If the "menu" plugin is loaded, register the menu items.\r
859                         if ( editor.addMenuItems )\r
860                         {\r
861                                 editor.addMenuItems(\r
862                                         {\r
863                                                 tablecell :\r
864                                                 {\r
865                                                         label : lang.cell.menu,\r
866                                                         group : 'tablecell',\r
867                                                         order : 1,\r
868                                                         getItems : function()\r
869                                                         {\r
870                                                                 var selection = editor.getSelection(),\r
871                                                                         cells = getSelectedCells( selection );\r
872                                                                 return {\r
873                                                                         tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,\r
874                                                                         tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,\r
875                                                                         tablecell_delete : CKEDITOR.TRISTATE_OFF,\r
876                                                                         tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
877                                                                         tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
878                                                                         tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
879                                                                         tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
880                                                                         tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
881                                                                         tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
882                                                                 };\r
883                                                         }\r
884                                                 },\r
885 \r
886                                                 tablecell_insertBefore :\r
887                                                 {\r
888                                                         label : lang.cell.insertBefore,\r
889                                                         group : 'tablecell',\r
890                                                         command : 'cellInsertBefore',\r
891                                                         order : 5\r
892                                                 },\r
893 \r
894                                                 tablecell_insertAfter :\r
895                                                 {\r
896                                                         label : lang.cell.insertAfter,\r
897                                                         group : 'tablecell',\r
898                                                         command : 'cellInsertAfter',\r
899                                                         order : 10\r
900                                                 },\r
901 \r
902                                                 tablecell_delete :\r
903                                                 {\r
904                                                         label : lang.cell.deleteCell,\r
905                                                         group : 'tablecell',\r
906                                                         command : 'cellDelete',\r
907                                                         order : 15\r
908                                                 },\r
909 \r
910                                                 tablecell_merge :\r
911                                                 {\r
912                                                         label : lang.cell.merge,\r
913                                                         group : 'tablecell',\r
914                                                         command : 'cellMerge',\r
915                                                         order : 16\r
916                                                 },\r
917 \r
918                                                 tablecell_merge_right :\r
919                                                 {\r
920                                                         label : lang.cell.mergeRight,\r
921                                                         group : 'tablecell',\r
922                                                         command : 'cellMergeRight',\r
923                                                         order : 17\r
924                                                 },\r
925 \r
926                                                 tablecell_merge_down :\r
927                                                 {\r
928                                                         label : lang.cell.mergeDown,\r
929                                                         group : 'tablecell',\r
930                                                         command : 'cellMergeDown',\r
931                                                         order : 18\r
932                                                 },\r
933 \r
934                                                 tablecell_split_horizontal :\r
935                                                 {\r
936                                                         label : lang.cell.splitHorizontal,\r
937                                                         group : 'tablecell',\r
938                                                         command : 'cellHorizontalSplit',\r
939                                                         order : 19\r
940                                                 },\r
941 \r
942                                                 tablecell_split_vertical :\r
943                                                 {\r
944                                                         label : lang.cell.splitVertical,\r
945                                                         group : 'tablecell',\r
946                                                         command : 'cellVerticalSplit',\r
947                                                         order : 20\r
948                                                 },\r
949 \r
950                                                 tablecell_properties :\r
951                                                 {\r
952                                                         label : lang.cell.title,\r
953                                                         group : 'tablecellproperties',\r
954                                                         command : 'cellProperties',\r
955                                                         order : 21\r
956                                                 },\r
957 \r
958                                                 tablerow :\r
959                                                 {\r
960                                                         label : lang.row.menu,\r
961                                                         group : 'tablerow',\r
962                                                         order : 1,\r
963                                                         getItems : function()\r
964                                                         {\r
965                                                                 return {\r
966                                                                         tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,\r
967                                                                         tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,\r
968                                                                         tablerow_delete : CKEDITOR.TRISTATE_OFF\r
969                                                                 };\r
970                                                         }\r
971                                                 },\r
972 \r
973                                                 tablerow_insertBefore :\r
974                                                 {\r
975                                                         label : lang.row.insertBefore,\r
976                                                         group : 'tablerow',\r
977                                                         command : 'rowInsertBefore',\r
978                                                         order : 5\r
979                                                 },\r
980 \r
981                                                 tablerow_insertAfter :\r
982                                                 {\r
983                                                         label : lang.row.insertAfter,\r
984                                                         group : 'tablerow',\r
985                                                         command : 'rowInsertAfter',\r
986                                                         order : 10\r
987                                                 },\r
988 \r
989                                                 tablerow_delete :\r
990                                                 {\r
991                                                         label : lang.row.deleteRow,\r
992                                                         group : 'tablerow',\r
993                                                         command : 'rowDelete',\r
994                                                         order : 15\r
995                                                 },\r
996 \r
997                                                 tablecolumn :\r
998                                                 {\r
999                                                         label : lang.column.menu,\r
1000                                                         group : 'tablecolumn',\r
1001                                                         order : 1,\r
1002                                                         getItems : function()\r
1003                                                         {\r
1004                                                                 return {\r
1005                                                                         tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,\r
1006                                                                         tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,\r
1007                                                                         tablecolumn_delete : CKEDITOR.TRISTATE_OFF\r
1008                                                                 };\r
1009                                                         }\r
1010                                                 },\r
1011 \r
1012                                                 tablecolumn_insertBefore :\r
1013                                                 {\r
1014                                                         label : lang.column.insertBefore,\r
1015                                                         group : 'tablecolumn',\r
1016                                                         command : 'columnInsertBefore',\r
1017                                                         order : 5\r
1018                                                 },\r
1019 \r
1020                                                 tablecolumn_insertAfter :\r
1021                                                 {\r
1022                                                         label : lang.column.insertAfter,\r
1023                                                         group : 'tablecolumn',\r
1024                                                         command : 'columnInsertAfter',\r
1025                                                         order : 10\r
1026                                                 },\r
1027 \r
1028                                                 tablecolumn_delete :\r
1029                                                 {\r
1030                                                         label : lang.column.deleteColumn,\r
1031                                                         group : 'tablecolumn',\r
1032                                                         command : 'columnDelete',\r
1033                                                         order : 15\r
1034                                                 }\r
1035                                         });\r
1036                         }\r
1037 \r
1038                         // If the "contextmenu" plugin is laoded, register the listeners.\r
1039                         if ( editor.contextMenu )\r
1040                         {\r
1041                                 editor.contextMenu.addListener( function( element, selection )\r
1042                                         {\r
1043                                                 if ( !element )\r
1044                                                         return null;\r
1045 \r
1046                                                 while ( element )\r
1047                                                 {\r
1048                                                         if ( element.getName() in contextMenuTags )\r
1049                                                         {\r
1050                                                                 return {\r
1051                                                                         tablecell : CKEDITOR.TRISTATE_OFF,\r
1052                                                                         tablerow : CKEDITOR.TRISTATE_OFF,\r
1053                                                                         tablecolumn : CKEDITOR.TRISTATE_OFF\r
1054                                                                 };\r
1055                                                         }\r
1056                                                         element = element.getParent();\r
1057                                                 }\r
1058 \r
1059                                                 return null;\r
1060                                         } );\r
1061                         }\r
1062                 },\r
1063 \r
1064                 getSelectedCells : getSelectedCells\r
1065 \r
1066         };\r
1067         CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );\r
1068 })();\r