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
37 var styles = dialog.getContentElement( 'advanced', 'advStyles' );
\r
41 styles.on( 'change', function( evt )
\r
43 // Synchronize width value.
\r
44 var width = this.getStyle( 'width', '' ),
\r
45 txtWidth = dialog.getContentElement( 'info', 'txtWidth' ),
\r
46 cmbWidthType = dialog.getContentElement( 'info', 'cmbWidthType' ),
\r
51 isPx = ( width.length < 3 || width.substr( width.length - 1 ) != '%' );
\r
52 width = parseInt( width, 10 );
\r
55 txtWidth && txtWidth.setValue( width, true );
\r
56 cmbWidthType && cmbWidthType.setValue( isPx ? 'pixels' : 'percents', true );
\r
58 // Synchronize height value.
\r
59 var height = this.getStyle( 'height', '' ),
\r
60 txtHeight = dialog.getContentElement( 'info', 'txtHeight' );
\r
62 height && ( height = parseInt( height, 10 ) );
\r
63 txtHeight && txtHeight.setValue( height, true );
\r
70 // Detect if there's a selected table.
\r
71 var selection = editor.getSelection(),
\r
72 ranges = selection.getRanges(),
\r
73 selectedTable = null;
\r
75 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
\r
76 colsInput = this.getContentElement( 'info', 'txtCols' ),
\r
77 widthInput = this.getContentElement( 'info', 'txtWidth' ),
\r
78 heightInput = this.getContentElement( 'info', 'txtHeight' );
\r
80 if ( command == 'tableProperties' )
\r
82 if ( ( selectedTable = editor.getSelection().getSelectedElement() ) )
\r
84 if ( selectedTable.getName() != 'table' )
\r
85 selectedTable = null;
\r
87 else if ( ranges.length > 0 )
\r
89 // Webkit could report the following range on cell selection (#4948):
\r
90 // <table><tr><td>[ </td></tr></table>]
\r
91 if ( CKEDITOR.env.webkit )
\r
92 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
\r
94 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
95 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
98 // Save a reference to the selected table, and push a new set of default values.
\r
99 this._.selectedElement = selectedTable;
\r
102 // Enable or disable the row, cols, width fields.
\r
103 if ( selectedTable )
\r
105 this.setupContent( selectedTable );
\r
106 rowsInput && rowsInput.disable();
\r
107 colsInput && colsInput.disable();
\r
111 rowsInput && rowsInput.enable();
\r
112 colsInput && colsInput.enable();
\r
115 // Call the onChange method for the widht and height fields so
\r
116 // they get reflected into the Advanced tab.
\r
117 widthInput && widthInput.onChange();
\r
118 heightInput && heightInput.onChange();
\r
122 if ( this._.selectedElement )
\r
124 var selection = editor.getSelection(),
\r
125 bms = editor.getSelection().createBookmarks();
\r
128 var table = this._.selectedElement || makeElement( 'table' ),
\r
132 this.commitContent( data, table );
\r
136 var info = data.info;
\r
138 // Generate the rows and cols.
\r
139 if ( !this._.selectedElement )
\r
141 var tbody = table.append( makeElement( 'tbody' ) ),
\r
142 rows = parseInt( info.txtRows, 10 ) || 0,
\r
143 cols = parseInt( info.txtCols, 10 ) || 0;
\r
145 for ( var i = 0 ; i < rows ; i++ )
\r
147 var row = tbody.append( makeElement( 'tr' ) );
\r
148 for ( var j = 0 ; j < cols ; j++ )
\r
150 var cell = row.append( makeElement( 'td' ) );
\r
151 if ( !CKEDITOR.env.ie )
\r
152 cell.append( makeElement( 'br' ) );
\r
157 // Modify the table headers. Depends on having rows and cols generated
\r
158 // correctly so it can't be done in commit functions.
\r
160 // Should we make a <thead>?
\r
161 var headers = info.selHeaders;
\r
162 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
164 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
165 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
166 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
168 // Change TD to TH:
\r
169 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
171 var th = theRow.getChild( i );
\r
172 if ( th.type == CKEDITOR.NODE_ELEMENT )
\r
174 th.renameNode( 'th' );
\r
175 th.setAttribute( 'scope', 'col' );
\r
178 thead.append( theRow.remove() );
\r
181 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
183 // Move the row out of the THead and put it in the TBody:
\r
184 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
185 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
187 var previousFirstRow = tbody.getFirst();
\r
188 while ( thead.getChildCount() > 0 )
\r
190 theRow = thead.getFirst();
\r
191 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
193 var newCell = theRow.getChild( i );
\r
194 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
196 newCell.renameNode( 'td' );
\r
197 newCell.removeAttribute( 'scope' );
\r
200 theRow.insertBefore( previousFirstRow );
\r
205 // Should we make all first cells in a row TH?
\r
206 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
208 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
210 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
211 newCell.renameNode( 'th' );
\r
212 newCell.setAttribute( 'scope', 'row' );
\r
216 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
217 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
219 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
221 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
222 if ( row.getParent().getName() == 'tbody' )
\r
224 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
225 newCell.renameNode( 'td' );
\r
226 newCell.removeAttribute( 'scope' );
\r
231 // Set the width and height.
\r
233 if ( info.txtHeight )
\r
234 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
236 table.removeStyle( 'height' );
\r
238 if ( info.txtWidth )
\r
240 var type = info.cmbWidthType || 'pixels';
\r
241 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
244 table.removeStyle( 'width' );
\r
246 if ( !table.getAttribute( 'style' ) )
\r
247 table.removeAttribute( 'style' );
\r
250 // Insert the table element if we're creating one.
\r
251 if ( !this._.selectedElement )
\r
252 editor.insertElement( table );
\r
253 // Properly restore the selection inside table. (#4822)
\r
255 selection.selectBookmarks( bms );
\r
262 label : editor.lang.table.title,
\r
267 widths : [ null, null ],
\r
268 styles : [ 'vertical-align:top' ],
\r
280 label : editor.lang.table.rows,
\r
282 style : 'width:5em',
\r
283 validate : function()
\r
286 value = this.getValue();
\r
287 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
291 alert( editor.lang.table.invalidRows );
\r
296 setup : function( selectedElement )
\r
298 this.setValue( selectedElement.$.rows.length );
\r
300 commit : commitValue
\r
306 label : editor.lang.table.columns,
\r
308 style : 'width:5em',
\r
309 validate : function()
\r
312 value = this.getValue();
\r
313 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
317 alert( editor.lang.table.invalidCols );
\r
322 setup : function( selectedTable )
\r
324 this.setValue( selectedTable.$.rows[0].cells.length);
\r
326 commit : commitValue
\r
336 label : editor.lang.table.headers,
\r
339 [ editor.lang.table.headersNone, '' ],
\r
340 [ editor.lang.table.headersRow, 'row' ],
\r
341 [ editor.lang.table.headersColumn, 'col' ],
\r
342 [ editor.lang.table.headersBoth, 'both' ]
\r
344 setup : function( selectedTable )
\r
346 // Fill in the headers field.
\r
347 var dialog = this.getDialog();
\r
348 dialog.hasColumnHeaders = true;
\r
350 // Check if all the first cells in every row are TH
\r
351 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
353 // If just one cell isn't a TH then it isn't a header column
\r
354 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
356 dialog.hasColumnHeaders = false;
\r
361 // Check if the table contains <thead>.
\r
362 if ( ( selectedTable.$.tHead !== null) )
\r
363 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
365 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
367 commit : commitValue
\r
373 label : editor.lang.table.border,
\r
374 style : 'width:3em',
\r
375 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
376 setup : function( selectedTable )
\r
378 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
380 commit : function( data, selectedTable )
\r
382 if ( this.getValue() )
\r
383 selectedTable.setAttribute( 'border', this.getValue() );
\r
385 selectedTable.removeAttribute( 'border' );
\r
392 label : editor.lang.table.align,
\r
395 [ editor.lang.common.notSet , ''],
\r
396 [ editor.lang.table.alignLeft , 'left'],
\r
397 [ editor.lang.table.alignCenter , 'center'],
\r
398 [ editor.lang.table.alignRight , 'right']
\r
400 setup : function( selectedTable )
\r
402 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
404 commit : function( data, selectedTable )
\r
406 if ( this.getValue() )
\r
407 selectedTable.setAttribute( 'align', this.getValue() );
\r
409 selectedTable.removeAttribute( 'align' );
\r
421 widths : [ '5em' ],
\r
427 style : 'width:5em',
\r
428 label : editor.lang.table.width,
\r
430 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
432 // Extra labelling of width unit type.
\r
433 onLoad : function()
\r
435 var widthType = this.getDialog().getContentElement( 'info', 'cmbWidthType' ),
\r
436 labelElement = widthType.getElement(),
\r
437 inputElement = this.getInputElement(),
\r
438 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
440 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
443 onChange : function()
\r
445 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
449 var value = this.getValue();
\r
452 value += this.getDialog().getContentElement( 'info', 'cmbWidthType' ).getValue() == 'percents' ? '%' : 'px';
\r
454 styles.updateStyle( 'width', value );
\r
458 setup : function( selectedTable )
\r
460 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
462 this.setValue( widthMatch[1] );
\r
464 this.setValue( '' );
\r
466 commit : commitValue
\r
469 id : 'cmbWidthType',
\r
471 label : editor.lang.table.widthUnit,
\r
472 labelStyle: 'visibility:hidden',
\r
473 'default' : 'pixels',
\r
476 [ editor.lang.table.widthPx , 'pixels'],
\r
477 [ editor.lang.table.widthPc , 'percents']
\r
479 setup : function( selectedTable )
\r
481 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
483 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
485 onChange : function()
\r
487 this.getDialog().getContentElement( 'info', 'txtWidth' ).onChange();
\r
489 commit : commitValue
\r
495 widths : [ '5em' ],
\r
501 style : 'width:5em',
\r
502 label : editor.lang.table.height,
\r
504 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
506 // Extra labelling of height unit type.
\r
507 onLoad : function()
\r
509 var heightType = this.getDialog().getContentElement( 'info', 'htmlHeightType' ),
\r
510 labelElement = heightType.getElement(),
\r
511 inputElement = this.getInputElement(),
\r
512 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
514 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
517 onChange : function()
\r
519 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
523 var value = this.getValue();
\r
524 styles.updateStyle( 'height', value && ( value + 'px' ) );
\r
528 setup : function( selectedTable )
\r
530 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
532 this.setValue( heightMatch[1] );
\r
534 commit : commitValue
\r
537 id : 'htmlHeightType',
\r
539 html : '<div><br />' + editor.lang.table.widthPx + '</div>'
\r
549 id : 'txtCellSpace',
\r
550 style : 'width:3em',
\r
551 label : editor.lang.table.cellSpace,
\r
553 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
554 setup : function( selectedTable )
\r
556 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
558 commit : function( data, selectedTable )
\r
560 if ( this.getValue() )
\r
561 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
563 selectedTable.removeAttribute( 'cellSpacing' );
\r
569 style : 'width:3em',
\r
570 label : editor.lang.table.cellPad,
\r
572 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
573 setup : function( selectedTable )
\r
575 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
577 commit : function( data, selectedTable )
\r
579 if ( this.getValue() )
\r
580 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
582 selectedTable.removeAttribute( 'cellPadding' );
\r
602 label : editor.lang.table.caption,
\r
603 setup : function( selectedTable )
\r
605 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
606 if ( nodeList.count() > 0 )
\r
608 var caption = nodeList.getItem( 0 );
\r
609 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
610 caption = CKEDITOR.tools.trim( caption );
\r
611 this.setValue( caption );
\r
614 commit : function( data, table )
\r
616 var caption = this.getValue(),
\r
617 captionElement = table.getElementsByTag( 'caption' );
\r
620 if ( captionElement.count() > 0 )
\r
622 captionElement = captionElement.getItem( 0 );
\r
623 captionElement.setHtml( '' );
\r
627 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
628 if ( table.getChildCount() )
\r
629 captionElement.insertBefore( table.getFirst() );
\r
631 captionElement.appendTo( table );
\r
633 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
635 else if ( captionElement.count() > 0 )
\r
637 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
638 captionElement.getItem( i ).remove();
\r
645 label : editor.lang.table.summary,
\r
646 setup : function( selectedTable )
\r
648 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
650 commit : function( data, selectedTable )
\r
652 if ( this.getValue() )
\r
653 selectedTable.setAttribute( 'summary', this.getValue() );
\r
655 selectedTable.removeAttribute( 'summary' );
\r
662 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
\r
667 CKEDITOR.dialog.add( 'table', function( editor )
\r
669 return tableDialog( editor, 'table' );
\r
671 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
673 return tableDialog( editor, 'tableProperties' );
\r