2 Copyright (c) 2003-2013, 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
37 // Whole-positive-integer validator.
\r
38 function validatorNum( msg )
\r
42 var value = this.getValue(),
\r
43 pass = !!( CKEDITOR.dialog.validate.integer()( value ) && value > 0 );
\r
55 function tableDialog( editor, command )
\r
57 var makeElement = function( name )
\r
59 return new CKEDITOR.dom.element( name, editor.document );
\r
62 var dialogadvtab = editor.plugins.dialogadvtab;
\r
65 title : editor.lang.table.title,
\r
67 minHeight : CKEDITOR.env.ie ? 310 : 280,
\r
73 var styles = dialog.getContentElement( 'advanced', 'advStyles' );
\r
77 styles.on( 'change', function( evt )
\r
79 // Synchronize width value.
\r
80 var width = this.getStyle( 'width', '' ),
\r
81 txtWidth = dialog.getContentElement( 'info', 'txtWidth' );
\r
83 txtWidth && txtWidth.setValue( width, true );
\r
85 // Synchronize height value.
\r
86 var height = this.getStyle( 'height', '' ),
\r
87 txtHeight = dialog.getContentElement( 'info', 'txtHeight' );
\r
89 txtHeight && txtHeight.setValue( height, true );
\r
96 // Detect if there's a selected table.
\r
97 var selection = editor.getSelection(),
\r
98 ranges = selection.getRanges(),
\r
99 selectedTable = null;
\r
101 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
\r
102 colsInput = this.getContentElement( 'info', 'txtCols' ),
\r
103 widthInput = this.getContentElement( 'info', 'txtWidth' ),
\r
104 heightInput = this.getContentElement( 'info', 'txtHeight' );
\r
106 if ( command == 'tableProperties' )
\r
108 if ( ( selectedTable = selection.getSelectedElement() ) )
\r
109 selectedTable = selectedTable.getAscendant( 'table', true );
\r
110 else if ( ranges.length > 0 )
\r
112 // Webkit could report the following range on cell selection (#4948):
\r
113 // <table><tr><td>[ </td></tr></table>]
\r
114 if ( CKEDITOR.env.webkit )
\r
115 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
\r
117 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
118 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
121 // Save a reference to the selected table, and push a new set of default values.
\r
122 this._.selectedElement = selectedTable;
\r
125 // Enable or disable the row, cols, width fields.
\r
126 if ( selectedTable )
\r
128 this.setupContent( selectedTable );
\r
129 rowsInput && rowsInput.disable();
\r
130 colsInput && colsInput.disable();
\r
134 rowsInput && rowsInput.enable();
\r
135 colsInput && colsInput.enable();
\r
138 // Call the onChange method for the widht and height fields so
\r
139 // they get reflected into the Advanced tab.
\r
140 widthInput && widthInput.onChange();
\r
141 heightInput && heightInput.onChange();
\r
145 var selection = editor.getSelection(),
\r
146 bms = this._.selectedElement && selection.createBookmarks();
\r
148 var table = this._.selectedElement || makeElement( 'table' ),
\r
152 this.commitContent( data, table );
\r
156 var info = data.info;
\r
158 // Generate the rows and cols.
\r
159 if ( !this._.selectedElement )
\r
161 var tbody = table.append( makeElement( 'tbody' ) ),
\r
162 rows = parseInt( info.txtRows, 10 ) || 0,
\r
163 cols = parseInt( info.txtCols, 10 ) || 0;
\r
165 for ( var i = 0 ; i < rows ; i++ )
\r
167 var row = tbody.append( makeElement( 'tr' ) );
\r
168 for ( var j = 0 ; j < cols ; j++ )
\r
170 var cell = row.append( makeElement( 'td' ) );
\r
171 if ( !CKEDITOR.env.ie )
\r
172 cell.append( makeElement( 'br' ) );
\r
177 // Modify the table headers. Depends on having rows and cols generated
\r
178 // correctly so it can't be done in commit functions.
\r
180 // Should we make a <thead>?
\r
181 var headers = info.selHeaders;
\r
182 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
184 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
185 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
186 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
188 // Change TD to TH:
\r
189 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
191 var th = theRow.getChild( i );
\r
192 // Skip bookmark nodes. (#6155)
\r
193 if ( th.type == CKEDITOR.NODE_ELEMENT && !th.data( 'cke-bookmark' ) )
\r
195 th.renameNode( 'th' );
\r
196 th.setAttribute( 'scope', 'col' );
\r
199 thead.append( theRow.remove() );
\r
202 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
204 // Move the row out of the THead and put it in the TBody:
\r
205 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
206 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
208 var previousFirstRow = tbody.getFirst();
\r
209 while ( thead.getChildCount() > 0 )
\r
211 theRow = thead.getFirst();
\r
212 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
214 var newCell = theRow.getChild( i );
\r
215 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
217 newCell.renameNode( 'td' );
\r
218 newCell.removeAttribute( 'scope' );
\r
221 theRow.insertBefore( previousFirstRow );
\r
226 // Should we make all first cells in a row TH?
\r
227 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
229 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
231 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
232 newCell.renameNode( 'th' );
\r
233 newCell.setAttribute( 'scope', 'row' );
\r
237 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
238 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
240 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
242 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
243 if ( row.getParent().getName() == 'tbody' )
\r
245 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
246 newCell.renameNode( 'td' );
\r
247 newCell.removeAttribute( 'scope' );
\r
252 // Set the width and height.
\r
253 info.txtHeight ? table.setStyle( 'height', info.txtHeight ) : table.removeStyle( 'height' );
\r
254 info.txtWidth ? table.setStyle( 'width', info.txtWidth ) : table.removeStyle( 'width' );
\r
256 if ( !table.getAttribute( 'style' ) )
\r
257 table.removeAttribute( 'style' );
\r
260 // Insert the table element if we're creating one.
\r
261 if ( !this._.selectedElement )
\r
263 editor.insertElement( table );
\r
264 // Override the default cursor position after insertElement to place
\r
265 // cursor inside the first cell (#7959), IE needs a while.
\r
266 setTimeout( function()
\r
268 var firstCell = new CKEDITOR.dom.element( table.$.rows[ 0 ].cells[ 0 ] );
\r
269 var range = new CKEDITOR.dom.range( editor.document );
\r
270 range.moveToPosition( firstCell, CKEDITOR.POSITION_AFTER_START );
\r
274 // Properly restore the selection, (#4822) but don't break
\r
275 // because of this, e.g. updated table caption.
\r
277 try { selection.selectBookmarks( bms ); } catch( er ){}
\r
282 label : editor.lang.table.title,
\r
287 widths : [ null, null ],
\r
288 styles : [ 'vertical-align:top' ],
\r
300 label : editor.lang.table.rows,
\r
302 controlStyle : 'width:5em',
\r
303 validate : validatorNum( editor.lang.table.invalidRows ),
\r
304 setup : function( selectedElement )
\r
306 this.setValue( selectedElement.$.rows.length );
\r
308 commit : commitValue
\r
314 label : editor.lang.table.columns,
\r
316 controlStyle : 'width:5em',
\r
317 validate : validatorNum( editor.lang.table.invalidCols ),
\r
318 setup : function( selectedTable )
\r
320 this.setValue( tableColumns( selectedTable ) );
\r
322 commit : commitValue
\r
332 label : editor.lang.table.headers,
\r
335 [ editor.lang.table.headersNone, '' ],
\r
336 [ editor.lang.table.headersRow, 'row' ],
\r
337 [ editor.lang.table.headersColumn, 'col' ],
\r
338 [ editor.lang.table.headersBoth, 'both' ]
\r
340 setup : function( selectedTable )
\r
342 // Fill in the headers field.
\r
343 var dialog = this.getDialog();
\r
344 dialog.hasColumnHeaders = true;
\r
346 // Check if all the first cells in every row are TH
\r
347 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
349 // If just one cell isn't a TH then it isn't a header column
\r
350 var headCell = selectedTable.$.rows[row].cells[0];
\r
351 if ( headCell && headCell.nodeName.toLowerCase() != 'th' )
\r
353 dialog.hasColumnHeaders = false;
\r
358 // Check if the table contains <thead>.
\r
359 if ( ( selectedTable.$.tHead !== null) )
\r
360 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
362 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
364 commit : commitValue
\r
370 label : editor.lang.table.border,
\r
371 controlStyle : 'width:3em',
\r
372 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
373 setup : function( selectedTable )
\r
375 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
377 commit : function( data, selectedTable )
\r
379 if ( this.getValue() )
\r
380 selectedTable.setAttribute( 'border', this.getValue() );
\r
382 selectedTable.removeAttribute( 'border' );
\r
389 label : editor.lang.common.align,
\r
392 [ editor.lang.common.notSet , ''],
\r
393 [ editor.lang.common.alignLeft , 'left'],
\r
394 [ editor.lang.common.alignCenter , 'center'],
\r
395 [ editor.lang.common.alignRight , 'right']
\r
397 setup : function( selectedTable )
\r
399 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
401 commit : function( data, selectedTable )
\r
403 if ( this.getValue() )
\r
404 selectedTable.setAttribute( 'align', this.getValue() );
\r
406 selectedTable.removeAttribute( 'align' );
\r
418 widths : [ '5em' ],
\r
424 controlStyle : 'width:5em',
\r
425 label : editor.lang.common.width,
\r
426 title : editor.lang.common.cssLengthTooltip,
\r
428 getValue : defaultToPixel,
\r
429 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.width ) ),
\r
430 onChange : function()
\r
432 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
433 styles && styles.updateStyle( 'width', this.getValue() );
\r
435 setup : function( selectedTable )
\r
437 var val = selectedTable.getStyle( 'width' );
\r
438 val && this.setValue( val );
\r
440 commit : commitValue
\r
446 widths : [ '5em' ],
\r
452 controlStyle : 'width:5em',
\r
453 label : editor.lang.common.height,
\r
454 title : editor.lang.common.cssLengthTooltip,
\r
456 getValue : defaultToPixel,
\r
457 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.height ) ),
\r
458 onChange : function()
\r
460 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
461 styles && styles.updateStyle( 'height', this.getValue() );
\r
464 setup : function( selectedTable )
\r
466 var val = selectedTable.getStyle( 'height' );
\r
467 val && this.setValue( val );
\r
469 commit : commitValue
\r
479 id : 'txtCellSpace',
\r
480 controlStyle : 'width:3em',
\r
481 label : editor.lang.table.cellSpace,
\r
483 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellSpacing ),
\r
484 setup : function( selectedTable )
\r
486 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
488 commit : function( data, selectedTable )
\r
490 if ( this.getValue() )
\r
491 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
493 selectedTable.removeAttribute( 'cellSpacing' );
\r
499 controlStyle : 'width:3em',
\r
500 label : editor.lang.table.cellPad,
\r
502 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellPadding ),
\r
503 setup : function( selectedTable )
\r
505 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
507 commit : function( data, selectedTable )
\r
509 if ( this.getValue() )
\r
510 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
512 selectedTable.removeAttribute( 'cellPadding' );
\r
532 label : editor.lang.table.caption,
\r
533 setup : function( selectedTable )
\r
537 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
538 if ( nodeList.count() > 0 )
\r
540 var caption = nodeList.getItem( 0 );
\r
541 var firstElementChild = caption.getFirst( CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT ) );
\r
543 if ( firstElementChild && !firstElementChild.equals( caption.getBogus() ) )
\r
546 this.setValue( caption.getText() );
\r
550 caption = CKEDITOR.tools.trim( caption.getText() );
\r
551 this.setValue( caption );
\r
554 commit : function( data, table )
\r
556 if ( !this.isEnabled() )
\r
559 var caption = this.getValue(),
\r
560 captionElement = table.getElementsByTag( 'caption' );
\r
563 if ( captionElement.count() > 0 )
\r
565 captionElement = captionElement.getItem( 0 );
\r
566 captionElement.setHtml( '' );
\r
570 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
571 if ( table.getChildCount() )
\r
572 captionElement.insertBefore( table.getFirst() );
\r
574 captionElement.appendTo( table );
\r
576 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
578 else if ( captionElement.count() > 0 )
\r
580 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
581 captionElement.getItem( i ).remove();
\r
588 label : editor.lang.table.summary,
\r
589 setup : function( selectedTable )
\r
591 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
593 commit : function( data, selectedTable )
\r
595 if ( this.getValue() )
\r
596 selectedTable.setAttribute( 'summary', this.getValue() );
\r
598 selectedTable.removeAttribute( 'summary' );
\r
605 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
\r
610 CKEDITOR.dialog.add( 'table', function( editor )
\r
612 return tableDialog( editor, 'table' );
\r
614 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
616 return tableDialog( editor, 'tableProperties' );
\r