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