2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
8 var pxUnit = CKEDITOR.tools.cssLength,
9 needsIEHacks = CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.quirks || CKEDITOR.env.version < 7 );
11 function getWidth( el )
13 return CKEDITOR.env.ie ? el.$.clientWidth : parseInt( el.getComputedStyle( 'width' ), 10 );
16 function getBorderWidth( element, side )
18 var computed = element.getComputedStyle( 'border-' + side + '-width' ),
26 if ( computed.indexOf( 'px' ) < 0 )
29 if ( computed in borderMap && element.getComputedStyle( 'border-style' ) != 'none' )
30 computed = borderMap[ computed ];
35 return parseInt( computed, 10 );
38 // Gets the table row that contains the most columns.
39 function getMasterPillarRow( table )
41 var $rows = table.$.rows,
42 maxCells = 0, cellsCount,
45 for ( var i = 0, len = $rows.length ; i < len; i++ )
48 cellsCount = $tr.cells.length;
50 if ( cellsCount > maxCells )
52 maxCells = cellsCount;
60 function buildTableColumnPillars( table )
64 rtl = ( table.getComputedStyle( 'direction' ) == 'rtl' );
66 // Get the raw row element that cointains the most columns.
67 var $tr = getMasterPillarRow( table );
69 // Get the tbody element and position, which will be used to set the
70 // top and bottom boundaries.
71 var tbody = new CKEDITOR.dom.element( table.$.tBodies[ 0 ] ),
72 tbodyPosition = tbody.getDocumentPosition();
74 // Loop thorugh all cells, building pillars after each one of them.
75 for ( var i = 0, len = $tr.cells.length ; i < len ; i++ )
77 // Both the current cell and the successive one will be used in the
78 // pillar size calculation.
79 var td = new CKEDITOR.dom.element( $tr.cells[ i ] ),
80 nextTd = $tr.cells[ i + 1 ] && new CKEDITOR.dom.element( $tr.cells[ i + 1 ] );
82 pillarIndex += td.$.colSpan || 1;
84 // Calculate the pillar boundary positions.
85 var pillarLeft, pillarRight, pillarWidth, pillarPadding;
87 var x = td.getDocumentPosition().x;
89 // Calculate positions based on the current cell.
91 pillarRight = x + getBorderWidth( td, 'left' ) :
92 pillarLeft = x + td.$.offsetWidth - getBorderWidth( td, 'right' );
94 // Calculate positions based on the next cell, if available.
97 x = nextTd.getDocumentPosition().x;
100 pillarLeft = x + nextTd.$.offsetWidth - getBorderWidth( nextTd, 'right' ) :
101 pillarRight = x + getBorderWidth( nextTd, 'left' );
103 // Otherwise calculate positions based on the table (for last cell).
106 x = table.getDocumentPosition().x;
110 pillarRight = x + table.$.offsetWidth;
113 pillarWidth = Math.max( pillarRight - pillarLeft, 3 );
115 // Make the pillar touch area at least 14 pixels wide, for easy to use.
116 pillarPadding = Math.max( Math.round( 7 - ( pillarWidth / 2 ) ), 0 );
118 // The pillar should reflects exactly the shape of the hovered
119 // column border line.
126 height: tbody.$.offsetHeight,
127 padding : pillarPadding,
134 function getPillarAtPosition( pillars, positionX )
136 for ( var i = 0, len = pillars.length ; i < len ; i++ )
138 var pillar = pillars[ i ],
139 pad = pillar.padding;
141 if ( positionX >= pillar.x - pad && positionX <= ( pillar.x + pillar.width + pad ) )
148 function cancel( evt )
150 ( evt.data || evt ).preventDefault();
153 function columnResizer( editor )
162 var leftSideCells, rightSideCells, leftShiftBoundary, rightShiftBoundary;
170 document.removeListener( 'mouseup', onMouseUp );
171 resizer.removeListener( 'mousedown', onMouseDown );
172 resizer.removeListener( 'mousemove', onMouseMove );
174 document.getBody().setStyle( 'cursor', 'auto' );
176 // Hide the resizer (remove it on IE7 - #5890).
177 needsIEHacks ? resizer.remove() : resizer.hide();
180 function resizeStart()
182 // Before starting to resize, figure out which cells to change
183 // and the boundaries of this resizing shift.
185 var columnIndex = pillar.index,
186 map = CKEDITOR.tools.buildTableMap( pillar.table ),
187 leftColumnCells = [],
188 rightColumnCells = [],
189 leftMinSize = Number.MAX_VALUE,
190 rightMinSize = leftMinSize,
193 for ( var i = 0, len = map.length ; i < len ; i++ )
196 leftCell = row[ columnIndex + ( rtl ? 1 : 0 ) ],
197 rightCell = row[ columnIndex + ( rtl ? 0 : 1 ) ];
199 leftCell = leftCell && new CKEDITOR.dom.element( leftCell );
200 rightCell = rightCell && new CKEDITOR.dom.element( rightCell );
202 if ( !leftCell || !rightCell || !leftCell.equals( rightCell ) )
204 leftCell && ( leftMinSize = Math.min( leftMinSize, getWidth( leftCell ) ) );
205 rightCell && ( rightMinSize = Math.min( rightMinSize, getWidth( rightCell ) ) );
207 leftColumnCells.push( leftCell );
208 rightColumnCells.push( rightCell );
212 // Cache the list of cells to be resized.
213 leftSideCells = leftColumnCells;
214 rightSideCells = rightColumnCells;
216 // Cache the resize limit boundaries.
217 leftShiftBoundary = pillar.x - leftMinSize;
218 rightShiftBoundary = pillar.x + rightMinSize;
220 resizer.setOpacity( 0.5 );
221 startOffset = parseInt( resizer.getStyle( 'left' ), 10 );
225 resizer.on( 'mousemove', onMouseMove );
227 // Prevent the native drag behavior otherwise 'mousemove' won't fire.
228 document.on( 'dragstart', cancel );
235 resizer.setOpacity( 0 );
237 currentShift && resizeColumn();
239 var table = pillar.table;
240 setTimeout( function () { table.removeCustomData( '_cke_table_pillars' ); }, 0 );
242 document.removeListener( 'dragstart', cancel );
245 function resizeColumn()
247 var rtl = pillar.rtl,
248 cellsCount = rtl ? rightSideCells.length : leftSideCells.length;
250 // Perform the actual resize to table cells, only for those by side of the pillar.
251 for ( var i = 0 ; i < cellsCount ; i++ )
253 var leftCell = leftSideCells[ i ],
254 rightCell = rightSideCells[ i ],
255 table = pillar.table;
257 // Defer the resizing to avoid any interference among cells.
258 CKEDITOR.tools.setTimeout(
259 function( leftCell, leftOldWidth, rightCell, rightOldWidth, tableWidth, sizeShift )
261 leftCell && leftCell.setStyle( 'width', pxUnit( Math.max( leftOldWidth + sizeShift, 0 ) ) );
262 rightCell && rightCell.setStyle( 'width', pxUnit( Math.max( rightOldWidth - sizeShift, 0 ) ) );
264 // If we're in the last cell, we need to resize the table as well
266 table.setStyle( 'width', pxUnit( tableWidth + sizeShift * ( rtl ? -1 : 1 ) ) );
270 leftCell, leftCell && getWidth( leftCell ),
271 rightCell, rightCell && getWidth( rightCell ),
272 ( !leftCell || !rightCell ) && ( getWidth( table ) + getBorderWidth( table, 'left' ) + getBorderWidth( table, 'right' ) ),
277 function onMouseDown( evt )
283 document.on( 'mouseup', onMouseUp, this );
286 function onMouseUp( evt )
288 evt.removeListener();
293 function onMouseMove( evt )
295 move( evt.data.$.clientX );
298 document = editor.document;
300 resizer = CKEDITOR.dom.element.createFromHtml(
301 '<div data-cke-temp=1 contenteditable=false unselectable=on '+
302 'style="position:absolute;cursor:col-resize;filter:alpha(opacity=0);opacity:0;' +
303 'padding:0;background-color:#004;background-image:none;border:0px none;z-index:10"></div>', document );
305 // Except on IE6/7 (#5890), place the resizer after body to prevent it
306 // from being editable.
308 document.getDocumentElement().append( resizer );
310 this.attachTo = function( targetPillar )
312 // Accept only one pillar at a time.
316 // On IE6/7, we append the resizer everytime we need it. (#5890)
319 document.getBody().append( resizer );
323 pillar = targetPillar;
327 width: pxUnit( targetPillar.width ),
328 height : pxUnit( targetPillar.height ),
329 left : pxUnit( targetPillar.x ),
330 top : pxUnit( targetPillar.y )
333 // In IE6/7, it's not possible to have custom cursors for floating
334 // elements in an editable document. Show the resizer in that case,
335 // to give the user a visual clue.
336 needsIEHacks && resizer.setOpacity( 0.25 );
338 resizer.on( 'mousedown', onMouseDown, this );
340 document.getBody().setStyle( 'cursor', 'col-resize' );
342 // Display the resizer to receive events but don't show it,
343 // only change the cursor to resizable shape.
347 var move = this.move = function( posX )
352 var pad = pillar.padding;
354 if ( !isResizing && ( posX < pillar.x - pad || posX > ( pillar.x + pillar.width + pad ) ) )
360 var resizerNewPosition = posX - Math.round( resizer.$.offsetWidth / 2 );
364 if ( resizerNewPosition == leftShiftBoundary || resizerNewPosition == rightShiftBoundary )
367 resizerNewPosition = Math.max( resizerNewPosition, leftShiftBoundary );
368 resizerNewPosition = Math.min( resizerNewPosition, rightShiftBoundary );
370 currentShift = resizerNewPosition - startOffset;
373 resizer.setStyle( 'left', pxUnit( resizerNewPosition ) );
379 function clearPillarsCache( evt )
381 var target = evt.data.getTarget();
383 if ( evt.name == 'mouseout' )
385 // Bypass interal mouse move.
386 if ( !target.is ( 'table' ) )
389 var dest = new CKEDITOR.dom.element( evt.data.$.relatedTarget || evt.data.$.toElement );
390 while( dest && dest.$ && !dest.equals( target ) && !dest.is( 'body' ) )
391 dest = dest.getParent();
392 if ( !dest || dest.equals( target ) )
396 target.getAscendant( 'table', 1 ).removeCustomData( '_cke_table_pillars' );
397 evt.removeListener();
400 CKEDITOR.plugins.add( 'tableresize',
402 requires : [ 'tabletools' ],
403 init : function( editor )
405 editor.on( 'contentDom', function()
409 editor.document.getBody().on( 'mousemove', function( evt )
413 // If we're already attached to a pillar, simply move the
415 if ( resizer && resizer.move( evt.$.clientX ) )
421 // Considering table, tr, td, tbody but nothing else.
422 var target = evt.getTarget(),
426 if ( !target.is( 'table' ) && !target.getAscendant( 'tbody', 1 ) )
429 table = target.getAscendant( 'table', 1 );
431 if ( !( pillars = table.getCustomData( '_cke_table_pillars' ) ) )
433 // Cache table pillars calculation result.
434 table.setCustomData( '_cke_table_pillars', ( pillars = buildTableColumnPillars( table ) ) );
435 table.on( 'mouseout', clearPillarsCache );
436 table.on( 'mousedown', clearPillarsCache );
439 var pillar = getPillarAtPosition( pillars, evt.$.clientX );
442 !resizer && ( resizer = new columnResizer( editor ) );
443 resizer.attachTo( pillar );