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 tableColumns( table )
20 var cols = 0, maxCols = 0;
21 for ( var i = 0, row, rows = table.$.rows.length; i < rows; i++ )
23 row = table.$.rows[ i ], cols = 0;
24 for ( var j = 0, cell, cells = row.cells.length; j < cells; j++ )
26 cell = row.cells[ j ];
30 cols > maxCols && ( maxCols = cols );
36 function tableDialog( editor, command )
38 var makeElement = function( name )
40 return new CKEDITOR.dom.element( name, editor.document );
43 var dialogadvtab = editor.plugins.dialogadvtab;
46 title : editor.lang.table.title,
48 minHeight : CKEDITOR.env.ie ? 310 : 280,
54 var styles = dialog.getContentElement( 'advanced', 'advStyles' );
58 styles.on( 'change', function( evt )
60 // Synchronize width value.
61 var width = this.getStyle( 'width', '' ),
62 txtWidth = dialog.getContentElement( 'info', 'txtWidth' );
64 txtWidth && txtWidth.setValue( width, true );
66 // Synchronize height value.
67 var height = this.getStyle( 'height', '' ),
68 txtHeight = dialog.getContentElement( 'info', 'txtHeight' );
70 txtHeight && txtHeight.setValue( height, true );
77 // Detect if there's a selected table.
78 var selection = editor.getSelection(),
79 ranges = selection.getRanges(),
82 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
83 colsInput = this.getContentElement( 'info', 'txtCols' ),
84 widthInput = this.getContentElement( 'info', 'txtWidth' ),
85 heightInput = this.getContentElement( 'info', 'txtHeight' );
87 if ( command == 'tableProperties' )
89 if ( ( selectedTable = selection.getSelectedElement() ) )
90 selectedTable = selectedTable.getAscendant( 'table', true );
91 else if ( ranges.length > 0 )
93 // Webkit could report the following range on cell selection (#4948):
94 // <table><tr><td>[ </td></tr></table>]
95 if ( CKEDITOR.env.webkit )
96 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
98 var rangeRoot = ranges[0].getCommonAncestor( true );
99 selectedTable = rangeRoot.getAscendant( 'table', true );
102 // Save a reference to the selected table, and push a new set of default values.
103 this._.selectedElement = selectedTable;
106 // Enable or disable the row, cols, width fields.
109 this.setupContent( selectedTable );
110 rowsInput && rowsInput.disable();
111 colsInput && colsInput.disable();
115 rowsInput && rowsInput.enable();
116 colsInput && colsInput.enable();
119 // Call the onChange method for the widht and height fields so
120 // they get reflected into the Advanced tab.
121 widthInput && widthInput.onChange();
122 heightInput && heightInput.onChange();
126 var selection = editor.getSelection(),
127 bms = this._.selectedElement && selection.createBookmarks();
129 var table = this._.selectedElement || makeElement( 'table' ),
133 this.commitContent( data, table );
137 var info = data.info;
139 // Generate the rows and cols.
140 if ( !this._.selectedElement )
142 var tbody = table.append( makeElement( 'tbody' ) ),
143 rows = parseInt( info.txtRows, 10 ) || 0,
144 cols = parseInt( info.txtCols, 10 ) || 0;
146 for ( var i = 0 ; i < rows ; i++ )
148 var row = tbody.append( makeElement( 'tr' ) );
149 for ( var j = 0 ; j < cols ; j++ )
151 var cell = row.append( makeElement( 'td' ) );
152 if ( !CKEDITOR.env.ie )
153 cell.append( makeElement( 'br' ) );
158 // Modify the table headers. Depends on having rows and cols generated
159 // correctly so it can't be done in commit functions.
161 // Should we make a <thead>?
162 var headers = info.selHeaders;
163 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
165 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
166 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
167 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
170 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
172 var th = theRow.getChild( i );
173 // Skip bookmark nodes. (#6155)
174 if ( th.type == CKEDITOR.NODE_ELEMENT && !th.data( 'cke-bookmark' ) )
176 th.renameNode( 'th' );
177 th.setAttribute( 'scope', 'col' );
180 thead.append( theRow.remove() );
183 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
185 // Move the row out of the THead and put it in the TBody:
186 thead = new CKEDITOR.dom.element( table.$.tHead );
187 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
189 var previousFirstRow = tbody.getFirst();
190 while ( thead.getChildCount() > 0 )
192 theRow = thead.getFirst();
193 for ( i = 0; i < theRow.getChildCount() ; i++ )
195 var newCell = theRow.getChild( i );
196 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
198 newCell.renameNode( 'td' );
199 newCell.removeAttribute( 'scope' );
202 theRow.insertBefore( previousFirstRow );
207 // Should we make all first cells in a row TH?
208 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
210 for ( row = 0 ; row < table.$.rows.length ; row++ )
212 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
213 newCell.renameNode( 'th' );
214 newCell.setAttribute( 'scope', 'row' );
218 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
219 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
221 for ( i = 0 ; i < table.$.rows.length ; i++ )
223 row = new CKEDITOR.dom.element( table.$.rows[i] );
224 if ( row.getParent().getName() == 'tbody' )
226 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
227 newCell.renameNode( 'td' );
228 newCell.removeAttribute( 'scope' );
233 // Set the width and height.
234 info.txtHeight ? table.setStyle( 'height', info.txtHeight ) : table.removeStyle( 'height' );
235 info.txtWidth ? table.setStyle( 'width', info.txtWidth ) : table.removeStyle( 'width' );
237 if ( !table.getAttribute( 'style' ) )
238 table.removeAttribute( 'style' );
241 // Insert the table element if we're creating one.
242 if ( !this._.selectedElement )
244 editor.insertElement( table );
245 // Override the default cursor position after insertElement to place
246 // cursor inside the first cell (#7959), IE needs a while.
247 setTimeout( function()
249 var firstCell = new CKEDITOR.dom.element( table.$.rows[ 0 ].cells[ 0 ] );
250 var range = new CKEDITOR.dom.range( editor.document );
251 range.moveToPosition( firstCell, CKEDITOR.POSITION_AFTER_START );
255 // Properly restore the selection, (#4822) but don't break
256 // because of this, e.g. updated table caption.
258 try { selection.selectBookmarks( bms ); } catch( er ){}
263 label : editor.lang.table.title,
268 widths : [ null, null ],
269 styles : [ 'vertical-align:top' ],
281 label : editor.lang.table.rows,
283 controlStyle : 'width:5em',
284 validate : function()
287 value = this.getValue();
288 pass = pass && CKEDITOR.dialog.validate.integer()( value )
292 alert( editor.lang.table.invalidRows );
297 setup : function( selectedElement )
299 this.setValue( selectedElement.$.rows.length );
307 label : editor.lang.table.columns,
309 controlStyle : 'width:5em',
310 validate : function()
313 value = this.getValue();
314 pass = pass && CKEDITOR.dialog.validate.integer()( value )
318 alert( editor.lang.table.invalidCols );
323 setup : function( selectedTable )
325 this.setValue( tableColumns( selectedTable ) );
337 label : editor.lang.table.headers,
340 [ editor.lang.table.headersNone, '' ],
341 [ editor.lang.table.headersRow, 'row' ],
342 [ editor.lang.table.headersColumn, 'col' ],
343 [ editor.lang.table.headersBoth, 'both' ]
345 setup : function( selectedTable )
347 // Fill in the headers field.
348 var dialog = this.getDialog();
349 dialog.hasColumnHeaders = true;
351 // Check if all the first cells in every row are TH
352 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
354 // If just one cell isn't a TH then it isn't a header column
355 var headCell = selectedTable.$.rows[row].cells[0];
356 if ( headCell && headCell.nodeName.toLowerCase() != 'th' )
358 dialog.hasColumnHeaders = false;
363 // Check if the table contains <thead>.
364 if ( ( selectedTable.$.tHead !== null) )
365 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
367 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
375 label : editor.lang.table.border,
376 controlStyle : 'width:3em',
377 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
378 setup : function( selectedTable )
380 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
382 commit : function( data, selectedTable )
384 if ( this.getValue() )
385 selectedTable.setAttribute( 'border', this.getValue() );
387 selectedTable.removeAttribute( 'border' );
394 label : editor.lang.common.align,
397 [ editor.lang.common.notSet , ''],
398 [ editor.lang.common.alignLeft , 'left'],
399 [ editor.lang.common.alignCenter , 'center'],
400 [ editor.lang.common.alignRight , 'right']
402 setup : function( selectedTable )
404 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
406 commit : function( data, selectedTable )
408 if ( this.getValue() )
409 selectedTable.setAttribute( 'align', this.getValue() );
411 selectedTable.removeAttribute( 'align' );
429 controlStyle : 'width:5em',
430 label : editor.lang.common.width,
431 title : editor.lang.common.cssLengthTooltip,
433 getValue : defaultToPixel,
434 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.width ) ),
435 onChange : function()
437 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
438 styles && styles.updateStyle( 'width', this.getValue() );
440 setup : function( selectedTable )
442 var val = selectedTable.getStyle( 'width' );
443 val && this.setValue( val );
457 controlStyle : 'width:5em',
458 label : editor.lang.common.height,
459 title : editor.lang.common.cssLengthTooltip,
461 getValue : defaultToPixel,
462 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.height ) ),
463 onChange : function()
465 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
466 styles && styles.updateStyle( 'height', this.getValue() );
469 setup : function( selectedTable )
471 var val = selectedTable.getStyle( 'width' );
472 val && this.setValue( val );
485 controlStyle : 'width:3em',
486 label : editor.lang.table.cellSpace,
488 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellSpacing ),
489 setup : function( selectedTable )
491 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
493 commit : function( data, selectedTable )
495 if ( this.getValue() )
496 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
498 selectedTable.removeAttribute( 'cellSpacing' );
504 controlStyle : 'width:3em',
505 label : editor.lang.table.cellPad,
507 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellPadding ),
508 setup : function( selectedTable )
510 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
512 commit : function( data, selectedTable )
514 if ( this.getValue() )
515 selectedTable.setAttribute( 'cellPadding', this.getValue() );
517 selectedTable.removeAttribute( 'cellPadding' );
537 label : editor.lang.table.caption,
538 setup : function( selectedTable )
542 var nodeList = selectedTable.getElementsByTag( 'caption' );
543 if ( nodeList.count() > 0 )
545 var caption = nodeList.getItem( 0 );
546 var firstElementChild = caption.getFirst( CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT ) );
548 if ( firstElementChild && !firstElementChild.equals( caption.getBogus() ) )
551 this.setValue( caption.getText() );
555 caption = CKEDITOR.tools.trim( caption.getText() );
556 this.setValue( caption );
559 commit : function( data, table )
561 if ( !this.isEnabled() )
564 var caption = this.getValue(),
565 captionElement = table.getElementsByTag( 'caption' );
568 if ( captionElement.count() > 0 )
570 captionElement = captionElement.getItem( 0 );
571 captionElement.setHtml( '' );
575 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
576 if ( table.getChildCount() )
577 captionElement.insertBefore( table.getFirst() );
579 captionElement.appendTo( table );
581 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
583 else if ( captionElement.count() > 0 )
585 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
586 captionElement.getItem( i ).remove();
593 label : editor.lang.table.summary,
594 setup : function( selectedTable )
596 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
598 commit : function( data, selectedTable )
600 if ( this.getValue() )
601 selectedTable.setAttribute( 'summary', this.getValue() );
603 selectedTable.removeAttribute( 'summary' );
610 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
615 CKEDITOR.dialog.add( 'table', function( editor )
617 return tableDialog( editor, 'table' );
619 CKEDITOR.dialog.add( 'tableProperties', function( editor )
621 return tableDialog( editor, 'tableProperties' );