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 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 = selection.getSelectedElement() ) )
\r
83 selectedTable = selectedTable.getAscendant( 'table', true );
\r
84 else if ( ranges.length > 0 )
\r
86 // Webkit could report the following range on cell selection (#4948):
\r
87 // <table><tr><td>[ </td></tr></table>]
\r
88 if ( CKEDITOR.env.webkit )
\r
89 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
\r
91 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
92 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
95 // Save a reference to the selected table, and push a new set of default values.
\r
96 this._.selectedElement = selectedTable;
\r
99 // Enable or disable the row, cols, width fields.
\r
100 if ( selectedTable )
\r
102 this.setupContent( selectedTable );
\r
103 rowsInput && rowsInput.disable();
\r
104 colsInput && colsInput.disable();
\r
108 rowsInput && rowsInput.enable();
\r
109 colsInput && colsInput.enable();
\r
112 // Call the onChange method for the widht and height fields so
\r
113 // they get reflected into the Advanced tab.
\r
114 widthInput && widthInput.onChange();
\r
115 heightInput && heightInput.onChange();
\r
119 if ( this._.selectedElement )
\r
121 var selection = editor.getSelection(),
\r
122 bms = selection.createBookmarks();
\r
125 var table = this._.selectedElement || makeElement( 'table' ),
\r
129 this.commitContent( data, table );
\r
133 var info = data.info;
\r
135 // Generate the rows and cols.
\r
136 if ( !this._.selectedElement )
\r
138 var tbody = table.append( makeElement( 'tbody' ) ),
\r
139 rows = parseInt( info.txtRows, 10 ) || 0,
\r
140 cols = parseInt( info.txtCols, 10 ) || 0;
\r
142 for ( var i = 0 ; i < rows ; i++ )
\r
144 var row = tbody.append( makeElement( 'tr' ) );
\r
145 for ( var j = 0 ; j < cols ; j++ )
\r
147 var cell = row.append( makeElement( 'td' ) );
\r
148 if ( !CKEDITOR.env.ie )
\r
149 cell.append( makeElement( 'br' ) );
\r
154 // Modify the table headers. Depends on having rows and cols generated
\r
155 // correctly so it can't be done in commit functions.
\r
157 // Should we make a <thead>?
\r
158 var headers = info.selHeaders;
\r
159 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
161 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
162 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
163 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
165 // Change TD to TH:
\r
166 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
168 var th = theRow.getChild( i );
\r
169 // Skip bookmark nodes. (#6155)
\r
170 if ( th.type == CKEDITOR.NODE_ELEMENT && !th.data( 'cke-bookmark' ) )
\r
172 th.renameNode( 'th' );
\r
173 th.setAttribute( 'scope', 'col' );
\r
176 thead.append( theRow.remove() );
\r
179 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
181 // Move the row out of the THead and put it in the TBody:
\r
182 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
183 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
185 var previousFirstRow = tbody.getFirst();
\r
186 while ( thead.getChildCount() > 0 )
\r
188 theRow = thead.getFirst();
\r
189 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
191 var newCell = theRow.getChild( i );
\r
192 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
194 newCell.renameNode( 'td' );
\r
195 newCell.removeAttribute( 'scope' );
\r
198 theRow.insertBefore( previousFirstRow );
\r
203 // Should we make all first cells in a row TH?
\r
204 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
206 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
208 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
209 newCell.renameNode( 'th' );
\r
210 newCell.setAttribute( 'scope', 'row' );
\r
214 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
215 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
217 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
219 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
220 if ( row.getParent().getName() == 'tbody' )
\r
222 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
223 newCell.renameNode( 'td' );
\r
224 newCell.removeAttribute( 'scope' );
\r
229 // Set the width and height.
\r
231 if ( info.txtHeight )
\r
232 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
234 table.removeStyle( 'height' );
\r
236 if ( info.txtWidth )
\r
238 var type = info.cmbWidthType || 'pixels';
\r
239 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
242 table.removeStyle( 'width' );
\r
244 if ( !table.getAttribute( 'style' ) )
\r
245 table.removeAttribute( 'style' );
\r
248 // Insert the table element if we're creating one.
\r
249 if ( !this._.selectedElement )
\r
250 editor.insertElement( table );
\r
251 // Properly restore the selection inside table. (#4822)
\r
253 selection.selectBookmarks( bms );
\r
260 label : editor.lang.table.title,
\r
265 widths : [ null, null ],
\r
266 styles : [ 'vertical-align:top' ],
\r
278 label : editor.lang.table.rows,
\r
280 style : 'width:5em',
\r
281 validate : function()
\r
284 value = this.getValue();
\r
285 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
289 alert( editor.lang.table.invalidRows );
\r
294 setup : function( selectedElement )
\r
296 this.setValue( selectedElement.$.rows.length );
\r
298 commit : commitValue
\r
304 label : editor.lang.table.columns,
\r
306 style : 'width:5em',
\r
307 validate : function()
\r
310 value = this.getValue();
\r
311 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
315 alert( editor.lang.table.invalidCols );
\r
320 setup : function( selectedTable )
\r
322 this.setValue( selectedTable.$.rows[0].cells.length);
\r
324 commit : commitValue
\r
334 label : editor.lang.table.headers,
\r
337 [ editor.lang.table.headersNone, '' ],
\r
338 [ editor.lang.table.headersRow, 'row' ],
\r
339 [ editor.lang.table.headersColumn, 'col' ],
\r
340 [ editor.lang.table.headersBoth, 'both' ]
\r
342 setup : function( selectedTable )
\r
344 // Fill in the headers field.
\r
345 var dialog = this.getDialog();
\r
346 dialog.hasColumnHeaders = true;
\r
348 // Check if all the first cells in every row are TH
\r
349 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
351 // If just one cell isn't a TH then it isn't a header column
\r
352 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
354 dialog.hasColumnHeaders = false;
\r
359 // Check if the table contains <thead>.
\r
360 if ( ( selectedTable.$.tHead !== null) )
\r
361 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
363 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
365 commit : commitValue
\r
371 label : editor.lang.table.border,
\r
372 style : 'width:3em',
\r
373 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
374 setup : function( selectedTable )
\r
376 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
378 commit : function( data, selectedTable )
\r
380 if ( this.getValue() )
\r
381 selectedTable.setAttribute( 'border', this.getValue() );
\r
383 selectedTable.removeAttribute( 'border' );
\r
390 label : editor.lang.common.align,
\r
393 [ editor.lang.common.notSet , ''],
\r
394 [ editor.lang.common.alignLeft , 'left'],
\r
395 [ editor.lang.common.alignCenter , 'center'],
\r
396 [ editor.lang.common.alignRight , 'right']
\r
398 setup : function( selectedTable )
\r
400 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
402 commit : function( data, selectedTable )
\r
404 if ( this.getValue() )
\r
405 selectedTable.setAttribute( 'align', this.getValue() );
\r
407 selectedTable.removeAttribute( 'align' );
\r
419 widths : [ '5em' ],
\r
425 style : 'width:5em',
\r
426 label : editor.lang.common.width,
\r
428 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
430 // Extra labelling of width unit type.
\r
431 onLoad : function()
\r
433 var widthType = this.getDialog().getContentElement( 'info', 'cmbWidthType' ),
\r
434 labelElement = widthType.getElement(),
\r
435 inputElement = this.getInputElement(),
\r
436 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
438 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
441 onChange : function()
\r
443 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
447 var value = this.getValue();
\r
450 value += this.getDialog().getContentElement( 'info', 'cmbWidthType' ).getValue() == 'percents' ? '%' : 'px';
\r
452 styles.updateStyle( 'width', value );
\r
456 setup : function( selectedTable )
\r
458 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
460 this.setValue( widthMatch[1] );
\r
462 this.setValue( '' );
\r
464 commit : commitValue
\r
467 id : 'cmbWidthType',
\r
469 label : editor.lang.table.widthUnit,
\r
470 labelStyle: 'visibility:hidden',
\r
471 'default' : 'pixels',
\r
474 [ editor.lang.table.widthPx , 'pixels'],
\r
475 [ editor.lang.table.widthPc , 'percents']
\r
477 setup : function( selectedTable )
\r
479 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
481 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
483 onChange : function()
\r
485 this.getDialog().getContentElement( 'info', 'txtWidth' ).onChange();
\r
487 commit : commitValue
\r
493 widths : [ '5em' ],
\r
499 style : 'width:5em',
\r
500 label : editor.lang.common.height,
\r
502 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
504 // Extra labelling of height unit type.
\r
505 onLoad : function()
\r
507 var heightType = this.getDialog().getContentElement( 'info', 'htmlHeightType' ),
\r
508 labelElement = heightType.getElement(),
\r
509 inputElement = this.getInputElement(),
\r
510 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
512 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
515 onChange : function()
\r
517 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
\r
521 var value = this.getValue();
\r
522 styles.updateStyle( 'height', value && ( value + 'px' ) );
\r
526 setup : function( selectedTable )
\r
528 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
530 this.setValue( heightMatch[1] );
\r
532 commit : commitValue
\r
535 id : 'htmlHeightType',
\r
537 html : '<div><br />' + editor.lang.table.widthPx + '</div>'
\r
547 id : 'txtCellSpace',
\r
548 style : 'width:3em',
\r
549 label : editor.lang.table.cellSpace,
\r
551 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
552 setup : function( selectedTable )
\r
554 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
556 commit : function( data, selectedTable )
\r
558 if ( this.getValue() )
\r
559 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
561 selectedTable.removeAttribute( 'cellSpacing' );
\r
567 style : 'width:3em',
\r
568 label : editor.lang.table.cellPad,
\r
570 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
571 setup : function( selectedTable )
\r
573 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
575 commit : function( data, selectedTable )
\r
577 if ( this.getValue() )
\r
578 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
580 selectedTable.removeAttribute( 'cellPadding' );
\r
600 label : editor.lang.table.caption,
\r
601 setup : function( selectedTable )
\r
603 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
604 if ( nodeList.count() > 0 )
\r
606 var caption = nodeList.getItem( 0 );
\r
607 caption = CKEDITOR.tools.trim( caption.getText() );
\r
608 this.setValue( caption );
\r
611 commit : function( data, table )
\r
613 var caption = this.getValue(),
\r
614 captionElement = table.getElementsByTag( 'caption' );
\r
617 if ( captionElement.count() > 0 )
\r
619 captionElement = captionElement.getItem( 0 );
\r
620 captionElement.setHtml( '' );
\r
624 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
625 if ( table.getChildCount() )
\r
626 captionElement.insertBefore( table.getFirst() );
\r
628 captionElement.appendTo( table );
\r
630 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
632 else if ( captionElement.count() > 0 )
\r
634 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
635 captionElement.getItem( i ).remove();
\r
642 label : editor.lang.table.summary,
\r
643 setup : function( selectedTable )
\r
645 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
647 commit : function( data, selectedTable )
\r
649 if ( this.getValue() )
\r
650 selectedTable.setAttribute( 'summary', this.getValue() );
\r
652 selectedTable.removeAttribute( 'summary' );
\r
659 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
\r
664 CKEDITOR.dialog.add( 'table', function( editor )
\r
666 return tableDialog( editor, 'table' );
\r
668 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
670 return tableDialog( editor, 'tableProperties' );
\r