2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 var widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/,
\r
9 heightPattern = /^(\d+(?:\.\d+)?)px$/;
\r
11 var commitValue = function( data )
\r
16 data.info[id] = this.getValue();
\r
19 function tableDialog( editor, command )
\r
21 var makeElement = function( name )
\r
23 return new CKEDITOR.dom.element( name, editor.document );
\r
26 var dialogadvtab = editor.plugins.dialogadvtab;
\r
29 title : editor.lang.table.title,
\r
31 minHeight : CKEDITOR.env.ie ? 310 : 280,
\r
38 var styles = dialog.getContentElement( 'advanced', 'advStyles' );
\r
42 styles.on( 'change', function( evt )
\r
47 // Flag to avoid recursion.
\r
50 // Synchronize width value.
\r
51 var width = this.getStyle( 'width', '' ),
\r
52 txtWidth = dialog.getContentElement( 'info', 'txtWidth' ),
\r
53 cmbWidthType = dialog.getContentElement( 'info', 'cmbWidthType' ),
\r
58 isPx = ( width.length < 3 || width.substr( width.length - 1 ) != '%' );
\r
59 width = parseInt( width, 10 );
\r
62 txtWidth && txtWidth.setValue( width );
\r
63 cmbWidthType && cmbWidthType.setValue( isPx ? 'pixels' : 'percents' );
\r
65 // Synchronize height value.
\r
66 var height = this.getStyle( 'height', '' ),
\r
67 txtHeight = dialog.getContentElement( 'info', 'txtHeight' );
\r
69 height && ( height = parseInt( height, 10 ) );
\r
70 txtHeight && txtHeight.setValue( height );
\r
79 // Detect if there's a selected table.
\r
80 var selection = editor.getSelection(),
\r
81 ranges = selection.getRanges(),
\r
82 selectedTable = null;
\r
84 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
\r
85 colsInput = this.getContentElement( 'info', 'txtCols' ),
\r
86 widthInput = this.getContentElement( 'info', 'txtWidth' ),
\r
87 heightInput = this.getContentElement( 'info', 'txtHeight' );
\r
89 if ( command == 'tableProperties' )
\r
91 if ( ( selectedTable = editor.getSelection().getSelectedElement() ) )
\r
93 if ( selectedTable.getName() != 'table' )
\r
94 selectedTable = null;
\r
96 else if ( ranges.length > 0 )
\r
98 // Webkit could report the following range on cell selection (#4948):
\r
99 // <table><tr><td>[ </td></tr></table>]
\r
100 if ( CKEDITOR.env.webkit )
\r
101 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
\r
103 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
104 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
107 // Save a reference to the selected table, and push a new set of default values.
\r
108 this._.selectedElement = selectedTable;
\r
111 // Enable, disable and select the row, cols, width fields.
\r
112 if ( selectedTable )
\r
114 this.setupContent( selectedTable );
\r
115 rowsInput && rowsInput.disable();
\r
116 colsInput && colsInput.disable();
\r
117 widthInput && widthInput.select();
\r
121 rowsInput && rowsInput.enable();
\r
122 colsInput && colsInput.enable();
\r
123 rowsInput && rowsInput.select();
\r
126 // Call the onChange method for the widht and height fields so
\r
127 // they get reflected into the Advanced tab.
\r
128 widthInput && widthInput.onChange();
\r
129 heightInput && heightInput.onChange();
\r
133 if ( this._.selectedElement )
\r
135 var selection = editor.getSelection(),
\r
136 bms = editor.getSelection().createBookmarks();
\r
139 var table = this._.selectedElement || makeElement( 'table' ),
\r
143 this.commitContent( data, table );
\r
147 var info = data.info;
\r
149 // Generate the rows and cols.
\r
150 if ( !this._.selectedElement )
\r
152 var tbody = table.append( makeElement( 'tbody' ) ),
\r
153 rows = parseInt( info.txtRows, 10 ) || 0,
\r
154 cols = parseInt( info.txtCols, 10 ) || 0;
\r
156 for ( var i = 0 ; i < rows ; i++ )
\r
158 var row = tbody.append( makeElement( 'tr' ) );
\r
159 for ( var j = 0 ; j < cols ; j++ )
\r
161 var cell = row.append( makeElement( 'td' ) );
\r
162 if ( !CKEDITOR.env.ie )
\r
163 cell.append( makeElement( 'br' ) );
\r
168 // Modify the table headers. Depends on having rows and cols generated
\r
169 // correctly so it can't be done in commit functions.
\r
171 // Should we make a <thead>?
\r
172 var headers = info.selHeaders;
\r
173 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
175 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
176 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
177 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
179 // Change TD to TH:
\r
180 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
182 var th = theRow.getChild( i );
\r
183 if ( th.type == CKEDITOR.NODE_ELEMENT )
\r
185 th.renameNode( 'th' );
\r
186 th.setAttribute( 'scope', 'col' );
\r
189 thead.append( theRow.remove() );
\r
192 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
194 // Move the row out of the THead and put it in the TBody:
\r
195 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
196 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
198 var previousFirstRow = tbody.getFirst();
\r
199 while ( thead.getChildCount() > 0 )
\r
201 theRow = thead.getFirst();
\r
202 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
204 var newCell = theRow.getChild( i );
\r
205 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
207 newCell.renameNode( 'td' );
\r
208 newCell.removeAttribute( 'scope' );
\r
211 theRow.insertBefore( previousFirstRow );
\r
216 // Should we make all first cells in a row TH?
\r
217 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
219 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
221 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
222 newCell.renameNode( 'th' );
\r
223 newCell.setAttribute( 'scope', 'row' );
\r
227 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
228 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
230 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
232 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
233 if ( row.getParent().getName() == 'tbody' )
\r
235 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
236 newCell.renameNode( 'td' );
\r
237 newCell.removeAttribute( 'scope' );
\r
242 // Set the width and height.
\r
244 if ( info.txtHeight )
\r
245 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
247 table.removeStyle( 'height' );
\r
249 if ( info.txtWidth )
\r
251 var type = info.cmbWidthType || 'pixels';
\r
252 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
255 table.removeStyle( 'width' );
\r
257 if ( !table.getAttribute( 'style' ) )
\r
258 table.removeAttribute( 'style' );
\r
261 // Insert the table element if we're creating one.
\r
262 if ( !this._.selectedElement )
\r
263 editor.insertElement( table );
\r
264 // Properly restore the selection inside table. (#4822)
\r
266 selection.selectBookmarks( bms );
\r
273 label : editor.lang.table.title,
\r
278 widths : [ null, null ],
\r
279 styles : [ 'vertical-align:top' ],
\r
291 label : editor.lang.table.rows,
\r
293 style : 'width:5em',
\r
294 validate : function()
\r
297 value = this.getValue();
\r
298 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
302 alert( editor.lang.table.invalidRows );
\r
307 setup : function( selectedElement )
\r
309 this.setValue( selectedElement.$.rows.length );
\r
311 commit : commitValue
\r
317 label : editor.lang.table.columns,
\r
319 style : 'width:5em',
\r
320 validate : function()
\r
323 value = this.getValue();
\r
324 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
328 alert( editor.lang.table.invalidCols );
\r
333 setup : function( selectedTable )
\r
335 this.setValue( selectedTable.$.rows[0].cells.length);
\r
337 commit : commitValue
\r
347 label : editor.lang.table.headers,
\r
350 [ editor.lang.table.headersNone, '' ],
\r
351 [ editor.lang.table.headersRow, 'row' ],
\r
352 [ editor.lang.table.headersColumn, 'col' ],
\r
353 [ editor.lang.table.headersBoth, 'both' ]
\r
355 setup : function( selectedTable )
\r
357 // Fill in the headers field.
\r
358 var dialog = this.getDialog();
\r
359 dialog.hasColumnHeaders = true;
\r
361 // Check if all the first cells in every row are TH
\r
362 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
364 // If just one cell isn't a TH then it isn't a header column
\r
365 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
367 dialog.hasColumnHeaders = false;
\r
372 // Check if the table contains <thead>.
\r
373 if ( ( selectedTable.$.tHead !== null) )
\r
374 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
376 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
378 commit : commitValue
\r
384 label : editor.lang.table.border,
\r
385 style : 'width:3em',
\r
386 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
387 setup : function( selectedTable )
\r
389 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
391 commit : function( data, selectedTable )
\r
393 if ( this.getValue() )
\r
394 selectedTable.setAttribute( 'border', this.getValue() );
\r
396 selectedTable.removeAttribute( 'border' );
\r
403 label : editor.lang.table.align,
\r
406 [ editor.lang.common.notSet , ''],
\r
407 [ editor.lang.table.alignLeft , 'left'],
\r
408 [ editor.lang.table.alignCenter , 'center'],
\r
409 [ editor.lang.table.alignRight , 'right']
\r
411 setup : function( selectedTable )
\r
413 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
415 commit : function( data, selectedTable )
\r
417 if ( this.getValue() )
\r
418 selectedTable.setAttribute( 'align', this.getValue() );
\r
420 selectedTable.removeAttribute( 'align' );
\r
432 widths : [ '5em' ],
\r
438 style : 'width:5em',
\r
439 label : editor.lang.table.width,
\r
441 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
443 // Extra labelling of width unit type.
\r
444 onLoad : function()
\r
446 var widthType = this.getDialog().getContentElement( 'info', 'cmbWidthType' ),
\r
447 labelElement = widthType.getElement(),
\r
448 inputElement = this.getInputElement(),
\r
449 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
451 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
454 onChange : function()
\r
456 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
460 var value = this.getValue();
\r
463 value += this.getDialog().getContentElement( 'info', 'cmbWidthType' ).getValue() == 'percents' ? '%' : 'px';
\r
465 styles.updateStyle( 'width', value );
\r
469 setup : function( selectedTable )
\r
471 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
473 this.setValue( widthMatch[1] );
\r
475 this.setValue( '' );
\r
477 commit : commitValue
\r
480 id : 'cmbWidthType',
\r
482 label : editor.lang.table.widthUnit,
\r
483 labelStyle: 'visibility:hidden',
\r
484 'default' : 'pixels',
\r
487 [ editor.lang.table.widthPx , 'pixels'],
\r
488 [ editor.lang.table.widthPc , 'percents']
\r
490 setup : function( selectedTable )
\r
492 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
494 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
496 onChange : function()
\r
498 this.getDialog().getContentElement( 'info', 'txtWidth' ).onChange();
\r
500 commit : commitValue
\r
506 widths : [ '5em' ],
\r
512 style : 'width:5em',
\r
513 label : editor.lang.table.height,
\r
515 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
517 // Extra labelling of height unit type.
\r
518 onLoad : function()
\r
520 var heightType = this.getDialog().getContentElement( 'info', 'htmlHeightType' ),
\r
521 labelElement = heightType.getElement(),
\r
522 inputElement = this.getInputElement(),
\r
523 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
525 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
528 onChange : function()
\r
530 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
534 var value = this.getValue();
\r
535 styles.updateStyle( 'height', value && ( value + 'px' ) );
\r
539 setup : function( selectedTable )
\r
541 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
543 this.setValue( heightMatch[1] );
\r
545 commit : commitValue
\r
548 id : 'htmlHeightType',
\r
550 html : '<div><br />' + editor.lang.table.widthPx + '</div>'
\r
560 id : 'txtCellSpace',
\r
561 style : 'width:3em',
\r
562 label : editor.lang.table.cellSpace,
\r
564 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
565 setup : function( selectedTable )
\r
567 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
569 commit : function( data, selectedTable )
\r
571 if ( this.getValue() )
\r
572 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
574 selectedTable.removeAttribute( 'cellSpacing' );
\r
580 style : 'width:3em',
\r
581 label : editor.lang.table.cellPad,
\r
583 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
584 setup : function( selectedTable )
\r
586 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
588 commit : function( data, selectedTable )
\r
590 if ( this.getValue() )
\r
591 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
593 selectedTable.removeAttribute( 'cellPadding' );
\r
613 label : editor.lang.table.caption,
\r
614 setup : function( selectedTable )
\r
616 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
617 if ( nodeList.count() > 0 )
\r
619 var caption = nodeList.getItem( 0 );
\r
620 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
621 caption = CKEDITOR.tools.trim( caption );
\r
622 this.setValue( caption );
\r
625 commit : function( data, table )
\r
627 var caption = this.getValue(),
\r
628 captionElement = table.getElementsByTag( 'caption' );
\r
631 if ( captionElement.count() > 0 )
\r
633 captionElement = captionElement.getItem( 0 );
\r
634 captionElement.setHtml( '' );
\r
638 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
639 if ( table.getChildCount() )
\r
640 captionElement.insertBefore( table.getFirst() );
\r
642 captionElement.appendTo( table );
\r
644 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
646 else if ( captionElement.count() > 0 )
\r
648 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
649 captionElement.getItem( i ).remove();
\r
656 label : editor.lang.table.summary,
\r
657 setup : function( selectedTable )
\r
659 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
661 commit : function( data, selectedTable )
\r
663 if ( this.getValue() )
\r
664 selectedTable.setAttribute( 'summary', this.getValue() );
\r
666 selectedTable.removeAttribute( 'summary' );
\r
673 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
\r
678 CKEDITOR.dialog.add( 'table', function( editor )
\r
680 return tableDialog( editor, 'table' );
\r
682 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
684 return tableDialog( editor, 'tableProperties' );
\r