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