1 | /** |
---|
2 | * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved. |
---|
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license |
---|
4 | */ |
---|
5 | |
---|
6 | ( function() { |
---|
7 | var pxUnit = CKEDITOR.tools.cssLength, |
---|
8 | needsIEHacks = CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.quirks ); |
---|
9 | |
---|
10 | function getWidth( el ) { |
---|
11 | return CKEDITOR.env.ie ? el.$.clientWidth : parseInt( el.getComputedStyle( 'width' ), 10 ); |
---|
12 | } |
---|
13 | |
---|
14 | function getBorderWidth( element, side ) { |
---|
15 | var computed = element.getComputedStyle( 'border-' + side + '-width' ), |
---|
16 | borderMap = { |
---|
17 | thin: '0px', |
---|
18 | medium: '1px', |
---|
19 | thick: '2px' |
---|
20 | }; |
---|
21 | |
---|
22 | if ( computed.indexOf( 'px' ) < 0 ) { |
---|
23 | // look up keywords |
---|
24 | if ( computed in borderMap && element.getComputedStyle( 'border-style' ) != 'none' ) |
---|
25 | computed = borderMap[ computed ]; |
---|
26 | else |
---|
27 | computed = 0; |
---|
28 | } |
---|
29 | |
---|
30 | return parseInt( computed, 10 ); |
---|
31 | } |
---|
32 | |
---|
33 | // Gets the table row that contains the most columns. |
---|
34 | function getMasterPillarRow( table ) { |
---|
35 | var $rows = table.$.rows, |
---|
36 | maxCells = 0, |
---|
37 | cellsCount, $elected, $tr; |
---|
38 | |
---|
39 | for ( var i = 0, len = $rows.length; i < len; i++ ) { |
---|
40 | $tr = $rows[ i ]; |
---|
41 | cellsCount = $tr.cells.length; |
---|
42 | |
---|
43 | if ( cellsCount > maxCells ) { |
---|
44 | maxCells = cellsCount; |
---|
45 | $elected = $tr; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return $elected; |
---|
50 | } |
---|
51 | |
---|
52 | function buildTableColumnPillars( table ) { |
---|
53 | var pillars = [], |
---|
54 | pillarIndex = -1, |
---|
55 | rtl = ( table.getComputedStyle( 'direction' ) == 'rtl' ); |
---|
56 | |
---|
57 | // Get the raw row element that cointains the most columns. |
---|
58 | var $tr = getMasterPillarRow( table ); |
---|
59 | |
---|
60 | // Get the tbody element and position, which will be used to set the |
---|
61 | // top and bottom boundaries. |
---|
62 | var tbody = new CKEDITOR.dom.element( table.$.tBodies[ 0 ] ), |
---|
63 | tbodyPosition = tbody.getDocumentPosition(); |
---|
64 | |
---|
65 | // Loop thorugh all cells, building pillars after each one of them. |
---|
66 | for ( var i = 0, len = $tr.cells.length; i < len; i++ ) { |
---|
67 | // Both the current cell and the successive one will be used in the |
---|
68 | // pillar size calculation. |
---|
69 | var td = new CKEDITOR.dom.element( $tr.cells[ i ] ), |
---|
70 | nextTd = $tr.cells[ i + 1 ] && new CKEDITOR.dom.element( $tr.cells[ i + 1 ] ); |
---|
71 | |
---|
72 | pillarIndex += td.$.colSpan || 1; |
---|
73 | |
---|
74 | // Calculate the pillar boundary positions. |
---|
75 | var pillarLeft, pillarRight, pillarWidth; |
---|
76 | |
---|
77 | var x = td.getDocumentPosition().x; |
---|
78 | |
---|
79 | // Calculate positions based on the current cell. |
---|
80 | rtl ? pillarRight = x + getBorderWidth( td, 'left' ) : pillarLeft = x + td.$.offsetWidth - getBorderWidth( td, 'right' ); |
---|
81 | |
---|
82 | // Calculate positions based on the next cell, if available. |
---|
83 | if ( nextTd ) { |
---|
84 | x = nextTd.getDocumentPosition().x; |
---|
85 | |
---|
86 | rtl ? pillarLeft = x + nextTd.$.offsetWidth - getBorderWidth( nextTd, 'right' ) : pillarRight = x + getBorderWidth( nextTd, 'left' ); |
---|
87 | } |
---|
88 | // Otherwise calculate positions based on the table (for last cell). |
---|
89 | else { |
---|
90 | x = table.getDocumentPosition().x; |
---|
91 | |
---|
92 | rtl ? pillarLeft = x : pillarRight = x + table.$.offsetWidth; |
---|
93 | } |
---|
94 | |
---|
95 | pillarWidth = Math.max( pillarRight - pillarLeft, 3 ); |
---|
96 | |
---|
97 | // The pillar should reflects exactly the shape of the hovered |
---|
98 | // column border line. |
---|
99 | pillars.push( { |
---|
100 | table: table, |
---|
101 | index: pillarIndex, |
---|
102 | x: pillarLeft, |
---|
103 | y: tbodyPosition.y, |
---|
104 | width: pillarWidth, |
---|
105 | height: tbody.$.offsetHeight, |
---|
106 | rtl: rtl |
---|
107 | } ); |
---|
108 | } |
---|
109 | |
---|
110 | return pillars; |
---|
111 | } |
---|
112 | |
---|
113 | function getPillarAtPosition( pillars, positionX ) { |
---|
114 | for ( var i = 0, len = pillars.length; i < len; i++ ) { |
---|
115 | var pillar = pillars[ i ]; |
---|
116 | |
---|
117 | if ( positionX >= pillar.x && positionX <= ( pillar.x + pillar.width ) ) |
---|
118 | return pillar; |
---|
119 | } |
---|
120 | |
---|
121 | return null; |
---|
122 | } |
---|
123 | |
---|
124 | function cancel( evt ) { |
---|
125 | ( evt.data || evt ).preventDefault(); |
---|
126 | } |
---|
127 | |
---|
128 | function columnResizer( editor ) { |
---|
129 | var pillar, document, resizer, isResizing, startOffset, currentShift; |
---|
130 | |
---|
131 | var leftSideCells, rightSideCells, leftShiftBoundary, rightShiftBoundary; |
---|
132 | |
---|
133 | function detach() { |
---|
134 | pillar = null; |
---|
135 | currentShift = 0; |
---|
136 | isResizing = 0; |
---|
137 | |
---|
138 | document.removeListener( 'mouseup', onMouseUp ); |
---|
139 | resizer.removeListener( 'mousedown', onMouseDown ); |
---|
140 | resizer.removeListener( 'mousemove', onMouseMove ); |
---|
141 | |
---|
142 | document.getBody().setStyle( 'cursor', 'auto' ); |
---|
143 | |
---|
144 | // Hide the resizer (remove it on IE7 - #5890). |
---|
145 | needsIEHacks ? resizer.remove() : resizer.hide(); |
---|
146 | } |
---|
147 | |
---|
148 | function resizeStart() { |
---|
149 | // Before starting to resize, figure out which cells to change |
---|
150 | // and the boundaries of this resizing shift. |
---|
151 | |
---|
152 | var columnIndex = pillar.index, |
---|
153 | map = CKEDITOR.tools.buildTableMap( pillar.table ), |
---|
154 | leftColumnCells = [], |
---|
155 | rightColumnCells = [], |
---|
156 | leftMinSize = Number.MAX_VALUE, |
---|
157 | rightMinSize = leftMinSize, |
---|
158 | rtl = pillar.rtl; |
---|
159 | |
---|
160 | for ( var i = 0, len = map.length; i < len; i++ ) { |
---|
161 | var row = map[ i ], |
---|
162 | leftCell = row[ columnIndex + ( rtl ? 1 : 0 ) ], |
---|
163 | rightCell = row[ columnIndex + ( rtl ? 0 : 1 ) ]; |
---|
164 | |
---|
165 | leftCell = leftCell && new CKEDITOR.dom.element( leftCell ); |
---|
166 | rightCell = rightCell && new CKEDITOR.dom.element( rightCell ); |
---|
167 | |
---|
168 | if ( !leftCell || !rightCell || !leftCell.equals( rightCell ) ) { |
---|
169 | leftCell && ( leftMinSize = Math.min( leftMinSize, getWidth( leftCell ) ) ); |
---|
170 | rightCell && ( rightMinSize = Math.min( rightMinSize, getWidth( rightCell ) ) ); |
---|
171 | |
---|
172 | leftColumnCells.push( leftCell ); |
---|
173 | rightColumnCells.push( rightCell ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | // Cache the list of cells to be resized. |
---|
178 | leftSideCells = leftColumnCells; |
---|
179 | rightSideCells = rightColumnCells; |
---|
180 | |
---|
181 | // Cache the resize limit boundaries. |
---|
182 | leftShiftBoundary = pillar.x - leftMinSize; |
---|
183 | rightShiftBoundary = pillar.x + rightMinSize; |
---|
184 | |
---|
185 | resizer.setOpacity( 0.5 ); |
---|
186 | startOffset = parseInt( resizer.getStyle( 'left' ), 10 ); |
---|
187 | currentShift = 0; |
---|
188 | isResizing = 1; |
---|
189 | |
---|
190 | resizer.on( 'mousemove', onMouseMove ); |
---|
191 | |
---|
192 | // Prevent the native drag behavior otherwise 'mousemove' won't fire. |
---|
193 | document.on( 'dragstart', cancel ); |
---|
194 | } |
---|
195 | |
---|
196 | function resizeEnd() { |
---|
197 | isResizing = 0; |
---|
198 | |
---|
199 | resizer.setOpacity( 0 ); |
---|
200 | |
---|
201 | currentShift && resizeColumn(); |
---|
202 | |
---|
203 | var table = pillar.table; |
---|
204 | setTimeout( function() { |
---|
205 | table.removeCustomData( '_cke_table_pillars' ); |
---|
206 | }, 0 ); |
---|
207 | |
---|
208 | document.removeListener( 'dragstart', cancel ); |
---|
209 | } |
---|
210 | |
---|
211 | function resizeColumn() { |
---|
212 | var rtl = pillar.rtl, |
---|
213 | cellsCount = rtl ? rightSideCells.length : leftSideCells.length, |
---|
214 | cellsSaved = 0; |
---|
215 | |
---|
216 | // Perform the actual resize to table cells, only for those by side of the pillar. |
---|
217 | for ( var i = 0; i < cellsCount; i++ ) { |
---|
218 | var leftCell = leftSideCells[ i ], |
---|
219 | rightCell = rightSideCells[ i ], |
---|
220 | table = pillar.table; |
---|
221 | |
---|
222 | // Defer the resizing to avoid any interference among cells. |
---|
223 | CKEDITOR.tools.setTimeout( function( leftCell, leftOldWidth, rightCell, rightOldWidth, tableWidth, sizeShift ) { |
---|
224 | // 1px is the minimum valid width (#11626). |
---|
225 | leftCell && leftCell.setStyle( 'width', pxUnit( Math.max( leftOldWidth + sizeShift, 1 ) ) ); |
---|
226 | rightCell && rightCell.setStyle( 'width', pxUnit( Math.max( rightOldWidth - sizeShift, 1 ) ) ); |
---|
227 | |
---|
228 | // If we're in the last cell, we need to resize the table as well |
---|
229 | if ( tableWidth ) |
---|
230 | table.setStyle( 'width', pxUnit( tableWidth + sizeShift * ( rtl ? -1 : 1 ) ) ); |
---|
231 | |
---|
232 | // Cells resizing is asynchronous-y, so we have to use syncing |
---|
233 | // to save snapshot only after all cells are resized. (#13388) |
---|
234 | if ( ++cellsSaved == cellsCount ) { |
---|
235 | editor.fire( 'saveSnapshot' ); |
---|
236 | } |
---|
237 | }, 0, this, [ |
---|
238 | leftCell, leftCell && getWidth( leftCell ), |
---|
239 | rightCell, rightCell && getWidth( rightCell ), |
---|
240 | ( !leftCell || !rightCell ) && ( getWidth( table ) + getBorderWidth( table, 'left' ) + getBorderWidth( table, 'right' ) ), |
---|
241 | currentShift |
---|
242 | ] ); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | function onMouseDown( evt ) { |
---|
247 | cancel( evt ); |
---|
248 | |
---|
249 | // Save editor's state before we do any magic with cells. (#13388) |
---|
250 | editor.fire( 'saveSnapshot' ); |
---|
251 | resizeStart(); |
---|
252 | |
---|
253 | document.on( 'mouseup', onMouseUp, this ); |
---|
254 | } |
---|
255 | |
---|
256 | function onMouseUp( evt ) { |
---|
257 | evt.removeListener(); |
---|
258 | |
---|
259 | resizeEnd(); |
---|
260 | } |
---|
261 | |
---|
262 | function onMouseMove( evt ) { |
---|
263 | move( evt.data.getPageOffset().x ); |
---|
264 | } |
---|
265 | |
---|
266 | document = editor.document; |
---|
267 | |
---|
268 | resizer = CKEDITOR.dom.element.createFromHtml( '<div data-cke-temp=1 contenteditable=false unselectable=on ' + |
---|
269 | 'style="position:absolute;cursor:col-resize;filter:alpha(opacity=0);opacity:0;' + |
---|
270 | 'padding:0;background-color:#004;background-image:none;border:0px none;z-index:10"></div>', document ); |
---|
271 | |
---|
272 | // Clean DOM when editor is destroyed. |
---|
273 | editor.on( 'destroy', function() { |
---|
274 | resizer.remove(); |
---|
275 | } ); |
---|
276 | |
---|
277 | // Except on IE6/7 (#5890), place the resizer after body to prevent it |
---|
278 | // from being editable. |
---|
279 | if ( !needsIEHacks ) |
---|
280 | document.getDocumentElement().append( resizer ); |
---|
281 | |
---|
282 | this.attachTo = function( targetPillar ) { |
---|
283 | // Accept only one pillar at a time. |
---|
284 | if ( isResizing ) |
---|
285 | return; |
---|
286 | |
---|
287 | // On IE6/7, we append the resizer everytime we need it. (#5890) |
---|
288 | if ( needsIEHacks ) { |
---|
289 | document.getBody().append( resizer ); |
---|
290 | currentShift = 0; |
---|
291 | } |
---|
292 | |
---|
293 | pillar = targetPillar; |
---|
294 | |
---|
295 | resizer.setStyles( { |
---|
296 | width: pxUnit( targetPillar.width ), |
---|
297 | height: pxUnit( targetPillar.height ), |
---|
298 | left: pxUnit( targetPillar.x ), |
---|
299 | top: pxUnit( targetPillar.y ) |
---|
300 | } ); |
---|
301 | |
---|
302 | // In IE6/7, it's not possible to have custom cursors for floating |
---|
303 | // elements in an editable document. Show the resizer in that case, |
---|
304 | // to give the user a visual clue. |
---|
305 | needsIEHacks && resizer.setOpacity( 0.25 ); |
---|
306 | |
---|
307 | resizer.on( 'mousedown', onMouseDown, this ); |
---|
308 | |
---|
309 | document.getBody().setStyle( 'cursor', 'col-resize' ); |
---|
310 | |
---|
311 | // Display the resizer to receive events but don't show it, |
---|
312 | // only change the cursor to resizable shape. |
---|
313 | resizer.show(); |
---|
314 | }; |
---|
315 | |
---|
316 | var move = this.move = function( posX ) { |
---|
317 | if ( !pillar ) |
---|
318 | return 0; |
---|
319 | |
---|
320 | if ( !isResizing && ( posX < pillar.x || posX > ( pillar.x + pillar.width ) ) ) { |
---|
321 | detach(); |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | var resizerNewPosition = posX - Math.round( resizer.$.offsetWidth / 2 ); |
---|
326 | |
---|
327 | if ( isResizing ) { |
---|
328 | if ( resizerNewPosition == leftShiftBoundary || resizerNewPosition == rightShiftBoundary ) |
---|
329 | return 1; |
---|
330 | |
---|
331 | resizerNewPosition = Math.max( resizerNewPosition, leftShiftBoundary ); |
---|
332 | resizerNewPosition = Math.min( resizerNewPosition, rightShiftBoundary ); |
---|
333 | |
---|
334 | currentShift = resizerNewPosition - startOffset; |
---|
335 | } |
---|
336 | |
---|
337 | resizer.setStyle( 'left', pxUnit( resizerNewPosition ) ); |
---|
338 | |
---|
339 | return 1; |
---|
340 | }; |
---|
341 | } |
---|
342 | |
---|
343 | function clearPillarsCache( evt ) { |
---|
344 | var target = evt.data.getTarget(); |
---|
345 | |
---|
346 | if ( evt.name == 'mouseout' ) { |
---|
347 | // Bypass interal mouse move. |
---|
348 | if ( !target.is( 'table' ) ) |
---|
349 | return; |
---|
350 | |
---|
351 | var dest = new CKEDITOR.dom.element( evt.data.$.relatedTarget || evt.data.$.toElement ); |
---|
352 | while ( dest && dest.$ && !dest.equals( target ) && !dest.is( 'body' ) ) |
---|
353 | dest = dest.getParent(); |
---|
354 | if ( !dest || dest.equals( target ) ) |
---|
355 | return; |
---|
356 | } |
---|
357 | |
---|
358 | target.getAscendant( 'table', 1 ).removeCustomData( '_cke_table_pillars' ); |
---|
359 | evt.removeListener(); |
---|
360 | } |
---|
361 | |
---|
362 | CKEDITOR.plugins.add( 'tableresize', { |
---|
363 | requires: 'tabletools', |
---|
364 | |
---|
365 | init: function( editor ) { |
---|
366 | editor.on( 'contentDom', function() { |
---|
367 | var resizer, |
---|
368 | editable = editor.editable(); |
---|
369 | |
---|
370 | // In Classic editor it is better to use document |
---|
371 | // instead of editable so event will work below body. |
---|
372 | editable.attachListener( editable.isInline() ? editable : editor.document, 'mousemove', function( evt ) { |
---|
373 | evt = evt.data; |
---|
374 | |
---|
375 | var target = evt.getTarget(); |
---|
376 | |
---|
377 | // FF may return document and IE8 some UFO (object with no nodeType property...) |
---|
378 | // instead of an element (#11823). |
---|
379 | if ( target.type != CKEDITOR.NODE_ELEMENT ) |
---|
380 | return; |
---|
381 | |
---|
382 | var pageX = evt.getPageOffset().x; |
---|
383 | |
---|
384 | // If we're already attached to a pillar, simply move the |
---|
385 | // resizer. |
---|
386 | if ( resizer && resizer.move( pageX ) ) { |
---|
387 | cancel( evt ); |
---|
388 | return; |
---|
389 | } |
---|
390 | |
---|
391 | // Considering table, tr, td, tbody but nothing else. |
---|
392 | var table, pillars; |
---|
393 | |
---|
394 | if ( !target.is( 'table' ) && !target.getAscendant( 'tbody', 1 ) ) |
---|
395 | return; |
---|
396 | |
---|
397 | table = target.getAscendant( 'table', 1 ); |
---|
398 | |
---|
399 | // Make sure the table we found is inside the container |
---|
400 | // (eg. we should not use tables the editor is embedded within) |
---|
401 | if ( !editor.editable().contains( table ) ) { |
---|
402 | return; |
---|
403 | } |
---|
404 | |
---|
405 | if ( !( pillars = table.getCustomData( '_cke_table_pillars' ) ) ) { |
---|
406 | // Cache table pillars calculation result. |
---|
407 | table.setCustomData( '_cke_table_pillars', ( pillars = buildTableColumnPillars( table ) ) ); |
---|
408 | table.on( 'mouseout', clearPillarsCache ); |
---|
409 | table.on( 'mousedown', clearPillarsCache ); |
---|
410 | } |
---|
411 | |
---|
412 | var pillar = getPillarAtPosition( pillars, pageX ); |
---|
413 | if ( pillar ) { |
---|
414 | !resizer && ( resizer = new columnResizer( editor ) ); |
---|
415 | resizer.attachTo( pillar ); |
---|
416 | } |
---|
417 | } ); |
---|
418 | } ); |
---|
419 | } |
---|
420 | } ); |
---|
421 | |
---|
422 | } )(); |
---|