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 ){ return new CKEDITOR.dom.element( name, editor.document ); };
\r
24 title : editor.lang.table.title,
\r
26 minHeight : CKEDITOR.env.ie ? 310 : 280,
\r
29 // Detect if there's a selected table.
\r
30 var selection = editor.getSelection(),
\r
31 ranges = selection.getRanges(),
\r
32 selectedTable = null;
\r
34 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
\r
35 colsInput = this.getContentElement( 'info', 'txtCols' ),
\r
36 widthInput = this.getContentElement( 'info', 'txtWidth' );
\r
37 if ( command == 'tableProperties' )
\r
39 if ( ( selectedTable = editor.getSelection().getSelectedElement() ) )
\r
41 if ( selectedTable.getName() != 'table' )
\r
42 selectedTable = null;
\r
44 else if ( ranges.length > 0 )
\r
46 // Webkit could report the following range on cell selection (#4948):
\r
47 // <table><tr><td>[ </td></tr></table>]
\r
48 if ( CKEDITOR.env.webkit )
\r
49 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
\r
51 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
52 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
55 // Save a reference to the selected table, and push a new set of default values.
\r
56 this._.selectedElement = selectedTable;
\r
59 // Enable, disable and select the row, cols, width fields.
\r
60 if ( selectedTable )
\r
62 this.setupContent( selectedTable );
\r
63 rowsInput && rowsInput.disable();
\r
64 colsInput && colsInput.disable();
\r
65 widthInput && widthInput.select();
\r
69 rowsInput && rowsInput.enable();
\r
70 colsInput && colsInput.enable();
\r
71 rowsInput && rowsInput.select();
\r
76 if ( this._.selectedElement )
\r
78 var selection = editor.getSelection(),
\r
79 bms = editor.getSelection().createBookmarks();
\r
82 var table = this._.selectedElement || makeElement( 'table' ),
\r
86 this.commitContent( data, table );
\r
90 var info = data.info;
\r
92 // Generate the rows and cols.
\r
93 if ( !this._.selectedElement )
\r
95 var tbody = table.append( makeElement( 'tbody' ) ),
\r
96 rows = parseInt( info.txtRows, 10 ) || 0,
\r
97 cols = parseInt( info.txtCols, 10 ) || 0;
\r
99 for ( var i = 0 ; i < rows ; i++ )
\r
101 var row = tbody.append( makeElement( 'tr' ) );
\r
102 for ( var j = 0 ; j < cols ; j++ )
\r
104 var cell = row.append( makeElement( 'td' ) );
\r
105 if ( !CKEDITOR.env.ie )
\r
106 cell.append( makeElement( 'br' ) );
\r
111 // Modify the table headers. Depends on having rows and cols generated
\r
112 // correctly so it can't be done in commit functions.
\r
114 // Should we make a <thead>?
\r
115 var headers = info.selHeaders;
\r
116 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
118 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
119 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
120 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
122 // Change TD to TH:
\r
123 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
125 var th = theRow.getChild( i );
\r
126 if ( th.type == CKEDITOR.NODE_ELEMENT )
\r
128 th.renameNode( 'th' );
\r
129 th.setAttribute( 'scope', 'col' );
\r
132 thead.append( theRow.remove() );
\r
135 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
137 // Move the row out of the THead and put it in the TBody:
\r
138 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
139 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
141 var previousFirstRow = tbody.getFirst();
\r
142 while ( thead.getChildCount() > 0 )
\r
144 theRow = thead.getFirst();
\r
145 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
147 var newCell = theRow.getChild( i );
\r
148 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
150 newCell.renameNode( 'td' );
\r
151 newCell.removeAttribute( 'scope' );
\r
154 theRow.insertBefore( previousFirstRow );
\r
159 // Should we make all first cells in a row TH?
\r
160 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
162 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
164 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
165 newCell.renameNode( 'th' );
\r
166 newCell.setAttribute( 'scope', 'row' );
\r
170 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
171 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
173 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
175 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
176 if ( row.getParent().getName() == 'tbody' )
\r
178 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
179 newCell.renameNode( 'td' );
\r
180 newCell.removeAttribute( 'scope' );
\r
185 // Set the width and height.
\r
187 if ( info.txtHeight )
\r
188 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
190 table.removeStyle( 'height' );
\r
192 if ( info.txtWidth )
\r
194 var type = info.cmbWidthType || 'pixels';
\r
195 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
198 table.removeStyle( 'width' );
\r
200 if ( !table.getAttribute( 'style' ) )
\r
201 table.removeAttribute( 'style' );
\r
204 // Insert the table element if we're creating one.
\r
205 if ( !this._.selectedElement )
\r
206 editor.insertElement( table );
\r
207 // Properly restore the selection inside table. (#4822)
\r
209 selection.selectBookmarks( bms );
\r
216 label : editor.lang.table.title,
\r
221 widths : [ null, null ],
\r
222 styles : [ 'vertical-align:top' ],
\r
234 label : editor.lang.table.rows,
\r
235 style : 'width:5em',
\r
236 validate : function()
\r
239 value = this.getValue();
\r
240 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
244 alert( editor.lang.table.invalidRows );
\r
249 setup : function( selectedElement )
\r
251 this.setValue( selectedElement.$.rows.length );
\r
253 commit : commitValue
\r
259 label : editor.lang.table.columns,
\r
260 style : 'width:5em',
\r
261 validate : function()
\r
264 value = this.getValue();
\r
265 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
269 alert( editor.lang.table.invalidCols );
\r
274 setup : function( selectedTable )
\r
276 this.setValue( selectedTable.$.rows[0].cells.length);
\r
278 commit : commitValue
\r
288 label : editor.lang.table.headers,
\r
291 [ editor.lang.table.headersNone, '' ],
\r
292 [ editor.lang.table.headersRow, 'row' ],
\r
293 [ editor.lang.table.headersColumn, 'col' ],
\r
294 [ editor.lang.table.headersBoth, 'both' ]
\r
296 setup : function( selectedTable )
\r
298 // Fill in the headers field.
\r
299 var dialog = this.getDialog();
\r
300 dialog.hasColumnHeaders = true;
\r
302 // Check if all the first cells in every row are TH
\r
303 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
305 // If just one cell isn't a TH then it isn't a header column
\r
306 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
308 dialog.hasColumnHeaders = false;
\r
313 // Check if the table contains <thead>.
\r
314 if ( ( selectedTable.$.tHead !== null) )
\r
315 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
317 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
319 commit : commitValue
\r
325 label : editor.lang.table.border,
\r
326 style : 'width:3em',
\r
327 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
328 setup : function( selectedTable )
\r
330 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
332 commit : function( data, selectedTable )
\r
334 if ( this.getValue() )
\r
335 selectedTable.setAttribute( 'border', this.getValue() );
\r
337 selectedTable.removeAttribute( 'border' );
\r
344 label : editor.lang.table.align,
\r
347 [ editor.lang.common.notSet , ''],
\r
348 [ editor.lang.table.alignLeft , 'left'],
\r
349 [ editor.lang.table.alignCenter , 'center'],
\r
350 [ editor.lang.table.alignRight , 'right']
\r
352 setup : function( selectedTable )
\r
354 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
356 commit : function( data, selectedTable )
\r
358 if ( this.getValue() )
\r
359 selectedTable.setAttribute( 'align', this.getValue() );
\r
361 selectedTable.removeAttribute( 'align' );
\r
373 widths : [ '5em' ],
\r
379 style : 'width:5em',
\r
380 label : editor.lang.table.width,
\r
382 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
384 // Extra labelling of width unit type.
\r
385 onLoad : function()
\r
387 var widthType = this.getDialog().getContentElement( 'info', 'cmbWidthType' ),
\r
388 labelElement = widthType.getElement(),
\r
389 inputElement = this.getInputElement(),
\r
390 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
392 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
395 setup : function( selectedTable )
\r
397 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
399 this.setValue( widthMatch[1] );
\r
401 this.setValue( '' );
\r
403 commit : commitValue
\r
406 id : 'cmbWidthType',
\r
408 label : editor.lang.table.widthUnit,
\r
409 labelStyle: 'visibility:hidden',
\r
410 'default' : 'pixels',
\r
413 [ editor.lang.table.widthPx , 'pixels'],
\r
414 [ editor.lang.table.widthPc , 'percents']
\r
416 setup : function( selectedTable )
\r
418 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
420 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
422 commit : commitValue
\r
428 widths : [ '5em' ],
\r
434 style : 'width:5em',
\r
435 label : editor.lang.table.height,
\r
437 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
439 // Extra labelling of height unit type.
\r
440 onLoad : function()
\r
442 var heightType = this.getDialog().getContentElement( 'info', 'htmlHeightType' ),
\r
443 labelElement = heightType.getElement(),
\r
444 inputElement = this.getInputElement(),
\r
445 ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );
\r
447 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );
\r
450 setup : function( selectedTable )
\r
452 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
454 this.setValue( heightMatch[1] );
\r
456 commit : commitValue
\r
459 id : 'htmlHeightType',
\r
461 html : '<div><br />' + editor.lang.table.widthPx + '</div>'
\r
471 id : 'txtCellSpace',
\r
472 style : 'width:3em',
\r
473 label : editor.lang.table.cellSpace,
\r
475 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
476 setup : function( selectedTable )
\r
478 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
480 commit : function( data, selectedTable )
\r
482 if ( this.getValue() )
\r
483 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
485 selectedTable.removeAttribute( 'cellSpacing' );
\r
491 style : 'width:3em',
\r
492 label : editor.lang.table.cellPad,
\r
494 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
495 setup : function( selectedTable )
\r
497 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
499 commit : function( data, selectedTable )
\r
501 if ( this.getValue() )
\r
502 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
504 selectedTable.removeAttribute( 'cellPadding' );
\r
524 label : editor.lang.table.caption,
\r
525 setup : function( selectedTable )
\r
527 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
528 if ( nodeList.count() > 0 )
\r
530 var caption = nodeList.getItem( 0 );
\r
531 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
532 caption = CKEDITOR.tools.trim( caption );
\r
533 this.setValue( caption );
\r
536 commit : function( data, table )
\r
538 var caption = this.getValue(),
\r
539 captionElement = table.getElementsByTag( 'caption' );
\r
542 if ( captionElement.count() > 0 )
\r
544 captionElement = captionElement.getItem( 0 );
\r
545 captionElement.setHtml( '' );
\r
549 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
550 if ( table.getChildCount() )
\r
551 captionElement.insertBefore( table.getFirst() );
\r
553 captionElement.appendTo( table );
\r
555 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
557 else if ( captionElement.count() > 0 )
\r
559 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
560 captionElement.getItem( i ).remove();
\r
567 label : editor.lang.table.summary,
\r
568 setup : function( selectedTable )
\r
570 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
572 commit : function( data, selectedTable )
\r
574 if ( this.getValue() )
\r
575 selectedTable.setAttribute( 'summary', this.getValue() );
\r
577 selectedTable.removeAttribute( 'summary' );
\r
588 CKEDITOR.dialog.add( 'table', function( editor )
\r
590 return tableDialog( editor, 'table' );
\r
592 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
594 return tableDialog( editor, 'tableProperties' );
\r