2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
8 var defaultToPixel
= CKEDITOR
.tools
.cssLength
;
10 var commitValue = function( data
)
15 data
.info
[id
] = this.getValue();
18 function tableDialog( editor
, command
)
20 var makeElement = function( name
)
22 return new CKEDITOR
.dom
.element( name
, editor
.document
);
25 var dialogadvtab
= editor
.plugins
.dialogadvtab
;
28 title
: editor
.lang
.table
.title
,
30 minHeight
: CKEDITOR
.env
.ie
? 310 : 280,
36 var styles
= dialog
.getContentElement( 'advanced', 'advStyles' );
40 styles
.on( 'change', function( evt
)
42 // Synchronize width value.
43 var width
= this.getStyle( 'width', '' ),
44 txtWidth
= dialog
.getContentElement( 'info', 'txtWidth' );
46 txtWidth
&& txtWidth
.setValue( width
, true );
48 // Synchronize height value.
49 var height
= this.getStyle( 'height', '' ),
50 txtHeight
= dialog
.getContentElement( 'info', 'txtHeight' );
52 txtHeight
&& txtHeight
.setValue( height
, true );
59 // Detect if there's a selected table.
60 var selection
= editor
.getSelection(),
61 ranges
= selection
.getRanges(),
64 var rowsInput
= this.getContentElement( 'info', 'txtRows' ),
65 colsInput
= this.getContentElement( 'info', 'txtCols' ),
66 widthInput
= this.getContentElement( 'info', 'txtWidth' ),
67 heightInput
= this.getContentElement( 'info', 'txtHeight' );
69 if ( command
== 'tableProperties' )
71 if ( ( selectedTable
= selection
.getSelectedElement() ) )
72 selectedTable
= selectedTable
.getAscendant( 'table', true );
73 else if ( ranges
.length
> 0 )
75 // Webkit could report the following range on cell selection (#4948):
76 // <table><tr><td>[ </td></tr></table>]
77 if ( CKEDITOR
.env
.webkit
)
78 ranges
[ 0 ].shrink( CKEDITOR
.NODE_ELEMENT
);
80 var rangeRoot
= ranges
[0].getCommonAncestor( true );
81 selectedTable
= rangeRoot
.getAscendant( 'table', true );
84 // Save a reference to the selected table, and push a new set of default values.
85 this._
.selectedElement
= selectedTable
;
88 // Enable or disable the row, cols, width fields.
91 this.setupContent( selectedTable
);
92 rowsInput
&& rowsInput
.disable();
93 colsInput
&& colsInput
.disable();
97 rowsInput
&& rowsInput
.enable();
98 colsInput
&& colsInput
.enable();
101 // Call the onChange method for the widht and height fields so
102 // they get reflected into the Advanced tab.
103 widthInput
&& widthInput
.onChange();
104 heightInput
&& heightInput
.onChange();
108 var selection
= editor
.getSelection(),
109 bms
= this._
.selectedElement
&& selection
.createBookmarks();
111 var table
= this._
.selectedElement
|| makeElement( 'table' ),
115 this.commitContent( data
, table
);
119 var info
= data
.info
;
121 // Generate the rows and cols.
122 if ( !this._
.selectedElement
)
124 var tbody
= table
.append( makeElement( 'tbody' ) ),
125 rows
= parseInt( info
.txtRows
, 10 ) || 0,
126 cols
= parseInt( info
.txtCols
, 10 ) || 0;
128 for ( var i
= 0 ; i
< rows
; i
++ )
130 var row
= tbody
.append( makeElement( 'tr' ) );
131 for ( var j
= 0 ; j
< cols
; j
++ )
133 var cell
= row
.append( makeElement( 'td' ) );
134 if ( !CKEDITOR
.env
.ie
)
135 cell
.append( makeElement( 'br' ) );
140 // Modify the table headers. Depends on having rows and cols generated
141 // correctly so it can't be done in commit functions.
143 // Should we make a <thead>?
144 var headers
= info
.selHeaders
;
145 if ( !table
.$.tHead
&& ( headers
== 'row' || headers
== 'both' ) )
147 var thead
= new CKEDITOR
.dom
.element( table
.$.createTHead() );
148 tbody
= table
.getElementsByTag( 'tbody' ).getItem( 0 );
149 var theRow
= tbody
.getElementsByTag( 'tr' ).getItem( 0 );
152 for ( i
= 0 ; i
< theRow
.getChildCount() ; i
++ )
154 var th
= theRow
.getChild( i
);
155 // Skip bookmark nodes. (#6155)
156 if ( th
.type
== CKEDITOR
.NODE_ELEMENT
&& !th
.data( 'cke-bookmark' ) )
158 th
.renameNode( 'th' );
159 th
.setAttribute( 'scope', 'col' );
162 thead
.append( theRow
.remove() );
165 if ( table
.$.tHead
!== null && !( headers
== 'row' || headers
== 'both' ) )
167 // Move the row out of the THead and put it in the TBody:
168 thead
= new CKEDITOR
.dom
.element( table
.$.tHead
);
169 tbody
= table
.getElementsByTag( 'tbody' ).getItem( 0 );
171 var previousFirstRow
= tbody
.getFirst();
172 while ( thead
.getChildCount() > 0 )
174 theRow
= thead
.getFirst();
175 for ( i
= 0; i
< theRow
.getChildCount() ; i
++ )
177 var newCell
= theRow
.getChild( i
);
178 if ( newCell
.type
== CKEDITOR
.NODE_ELEMENT
)
180 newCell
.renameNode( 'td' );
181 newCell
.removeAttribute( 'scope' );
184 theRow
.insertBefore( previousFirstRow
);
189 // Should we make all first cells in a row TH?
190 if ( !this.hasColumnHeaders
&& ( headers
== 'col' || headers
== 'both' ) )
192 for ( row
= 0 ; row
< table
.$.rows
.length
; row
++ )
194 newCell
= new CKEDITOR
.dom
.element( table
.$.rows
[ row
].cells
[ 0 ] );
195 newCell
.renameNode( 'th' );
196 newCell
.setAttribute( 'scope', 'row' );
200 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
201 if ( ( this.hasColumnHeaders
) && !( headers
== 'col' || headers
== 'both' ) )
203 for ( i
= 0 ; i
< table
.$.rows
.length
; i
++ )
205 row
= new CKEDITOR
.dom
.element( table
.$.rows
[i
] );
206 if ( row
.getParent().getName() == 'tbody' )
208 newCell
= new CKEDITOR
.dom
.element( row
.$.cells
[0] );
209 newCell
.renameNode( 'td' );
210 newCell
.removeAttribute( 'scope' );
215 // Set the width and height.
216 info
.txtHeight
? table
.setStyle( 'height', info
.txtHeight
) : table
.removeStyle( 'height' );
217 info
.txtWidth
? table
.setStyle( 'width', info
.txtWidth
) : table
.removeStyle( 'width' );
219 if ( !table
.getAttribute( 'style' ) )
220 table
.removeAttribute( 'style' );
223 // Insert the table element if we're creating one.
224 if ( !this._
.selectedElement
)
226 editor
.insertElement( table
);
227 // Override the default cursor position after insertElement to place
228 // cursor inside the first cell (#7959), IE needs a while.
229 setTimeout( function()
231 var firstCell
= new CKEDITOR
.dom
.element( table
.$.rows
[ 0 ].cells
[ 0 ] );
232 var range
= new CKEDITOR
.dom
.range( editor
.document
);
233 range
.moveToPosition( firstCell
, CKEDITOR
.POSITION_AFTER_START
);
237 // Properly restore the selection, (#4822) but don't break
238 // because of this, e.g. updated table caption.
240 try { selection
.selectBookmarks( bms
); } catch( er
){}
245 label
: editor
.lang
.table
.title
,
250 widths
: [ null, null ],
251 styles
: [ 'vertical-align:top' ],
263 label
: editor
.lang
.table
.rows
,
265 controlStyle
: 'width:5em',
266 validate : function()
269 value
= this.getValue();
270 pass
= pass
&& CKEDITOR
.dialog
.validate
.integer()( value
)
274 alert( editor
.lang
.table
.invalidRows
);
279 setup : function( selectedElement
)
281 this.setValue( selectedElement
.$.rows
.length
);
289 label
: editor
.lang
.table
.columns
,
291 controlStyle
: 'width:5em',
292 validate : function()
295 value
= this.getValue();
296 pass
= pass
&& CKEDITOR
.dialog
.validate
.integer()( value
)
300 alert( editor
.lang
.table
.invalidCols
);
305 setup : function( selectedTable
)
307 this.setValue( selectedTable
.$.rows
[0].cells
.length
);
319 label
: editor
.lang
.table
.headers
,
322 [ editor
.lang
.table
.headersNone
, '' ],
323 [ editor
.lang
.table
.headersRow
, 'row' ],
324 [ editor
.lang
.table
.headersColumn
, 'col' ],
325 [ editor
.lang
.table
.headersBoth
, 'both' ]
327 setup : function( selectedTable
)
329 // Fill in the headers field.
330 var dialog
= this.getDialog();
331 dialog
.hasColumnHeaders
= true;
333 // Check if all the first cells in every row are TH
334 for ( var row
= 0 ; row
< selectedTable
.$.rows
.length
; row
++ )
336 // If just one cell isn't a TH then it isn't a header column
337 if ( selectedTable
.$.rows
[row
].cells
[0].nodeName
.toLowerCase() != 'th' )
339 dialog
.hasColumnHeaders
= false;
344 // Check if the table contains <thead>.
345 if ( ( selectedTable
.$.tHead
!== null) )
346 this.setValue( dialog
.hasColumnHeaders
? 'both' : 'row' );
348 this.setValue( dialog
.hasColumnHeaders
? 'col' : '' );
356 label
: editor
.lang
.table
.border
,
357 controlStyle
: 'width:3em',
358 validate
: CKEDITOR
.dialog
.validate
['number']( editor
.lang
.table
.invalidBorder
),
359 setup : function( selectedTable
)
361 this.setValue( selectedTable
.getAttribute( 'border' ) || '' );
363 commit : function( data
, selectedTable
)
365 if ( this.getValue() )
366 selectedTable
.setAttribute( 'border', this.getValue() );
368 selectedTable
.removeAttribute( 'border' );
375 label
: editor
.lang
.common
.align
,
378 [ editor
.lang
.common
.notSet
, ''],
379 [ editor
.lang
.common
.alignLeft
, 'left'],
380 [ editor
.lang
.common
.alignCenter
, 'center'],
381 [ editor
.lang
.common
.alignRight
, 'right']
383 setup : function( selectedTable
)
385 this.setValue( selectedTable
.getAttribute( 'align' ) || '' );
387 commit : function( data
, selectedTable
)
389 if ( this.getValue() )
390 selectedTable
.setAttribute( 'align', this.getValue() );
392 selectedTable
.removeAttribute( 'align' );
410 controlStyle
: 'width:5em',
411 label
: editor
.lang
.common
.width
,
413 getValue
: defaultToPixel
,
414 validate
: CKEDITOR
.dialog
.validate
.cssLength( editor
.lang
.common
.invalidCssLength
.replace( '%1', editor
.lang
.common
.width
) ),
415 onChange : function()
417 var styles
= this.getDialog().getContentElement( 'advanced', 'advStyles' );
418 styles
&& styles
.updateStyle( 'width', this.getValue() );
420 setup : function( selectedTable
)
422 var val
= selectedTable
.getStyle( 'width' );
423 val
&& this.setValue( val
);
437 controlStyle
: 'width:5em',
438 label
: editor
.lang
.common
.height
,
440 getValue
: defaultToPixel
,
441 validate
: CKEDITOR
.dialog
.validate
.cssLength( editor
.lang
.common
.invalidCssLength
.replace( '%1', editor
.lang
.common
.height
) ),
442 onChange : function()
444 var styles
= this.getDialog().getContentElement( 'advanced', 'advStyles' );
445 styles
&& styles
.updateStyle( 'height', this.getValue() );
448 setup : function( selectedTable
)
450 var val
= selectedTable
.getStyle( 'width' );
451 val
&& this.setValue( val
);
464 controlStyle
: 'width:3em',
465 label
: editor
.lang
.table
.cellSpace
,
467 validate
: CKEDITOR
.dialog
.validate
.number( editor
.lang
.table
.invalidCellSpacing
),
468 setup : function( selectedTable
)
470 this.setValue( selectedTable
.getAttribute( 'cellSpacing' ) || '' );
472 commit : function( data
, selectedTable
)
474 if ( this.getValue() )
475 selectedTable
.setAttribute( 'cellSpacing', this.getValue() );
477 selectedTable
.removeAttribute( 'cellSpacing' );
483 controlStyle
: 'width:3em',
484 label
: editor
.lang
.table
.cellPad
,
486 validate
: CKEDITOR
.dialog
.validate
.number( editor
.lang
.table
.invalidCellPadding
),
487 setup : function( selectedTable
)
489 this.setValue( selectedTable
.getAttribute( 'cellPadding' ) || '' );
491 commit : function( data
, selectedTable
)
493 if ( this.getValue() )
494 selectedTable
.setAttribute( 'cellPadding', this.getValue() );
496 selectedTable
.removeAttribute( 'cellPadding' );
516 label
: editor
.lang
.table
.caption
,
517 setup : function( selectedTable
)
521 var nodeList
= selectedTable
.getElementsByTag( 'caption' );
522 if ( nodeList
.count() > 0 )
524 var caption
= nodeList
.getItem( 0 );
525 var firstElementChild
= caption
.getFirst( CKEDITOR
.dom
.walker
.nodeType( CKEDITOR
.NODE_ELEMENT
) );
527 if ( firstElementChild
&& !firstElementChild
.equals( caption
.getBogus() ) )
530 this.setValue( caption
.getText() );
534 caption
= CKEDITOR
.tools
.trim( caption
.getText() );
535 this.setValue( caption
);
538 commit : function( data
, table
)
540 if ( !this.isEnabled() )
543 var caption
= this.getValue(),
544 captionElement
= table
.getElementsByTag( 'caption' );
547 if ( captionElement
.count() > 0 )
549 captionElement
= captionElement
.getItem( 0 );
550 captionElement
.setHtml( '' );
554 captionElement
= new CKEDITOR
.dom
.element( 'caption', editor
.document
);
555 if ( table
.getChildCount() )
556 captionElement
.insertBefore( table
.getFirst() );
558 captionElement
.appendTo( table
);
560 captionElement
.append( new CKEDITOR
.dom
.text( caption
, editor
.document
) );
562 else if ( captionElement
.count() > 0 )
564 for ( var i
= captionElement
.count() - 1 ; i
>= 0 ; i
-- )
565 captionElement
.getItem( i
).remove();
572 label
: editor
.lang
.table
.summary
,
573 setup : function( selectedTable
)
575 this.setValue( selectedTable
.getAttribute( 'summary' ) || '' );
577 commit : function( data
, selectedTable
)
579 if ( this.getValue() )
580 selectedTable
.setAttribute( 'summary', this.getValue() );
582 selectedTable
.removeAttribute( 'summary' );
589 dialogadvtab
&& dialogadvtab
.createAdvancedTab( editor
)
594 CKEDITOR
.dialog
.add( 'table', function( editor
)
596 return tableDialog( editor
, 'table' );
598 CKEDITOR
.dialog
.add( 'tableProperties', function( editor
)
600 return tableDialog( editor
, 'tableProperties' );