2 Copyright (c) 2003-2009, 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 var rangeRoot = ranges[0].getCommonAncestor( true );
\r
47 selectedTable = rangeRoot.getAscendant( 'table', true );
\r
50 // Save a reference to the selected table, and push a new set of default values.
\r
51 this._.selectedElement = selectedTable;
\r
54 // Enable, disable and select the row, cols, width fields.
\r
55 if ( selectedTable )
\r
57 this.setupContent( selectedTable );
\r
58 rowsInput && rowsInput.disable();
\r
59 colsInput && colsInput.disable();
\r
60 widthInput && widthInput.select();
\r
64 rowsInput && rowsInput.enable();
\r
65 colsInput && colsInput.enable();
\r
66 rowsInput && rowsInput.select();
\r
71 var table = this._.selectedElement || makeElement( 'table' ),
\r
75 this.commitContent( data, table );
\r
79 var info = data.info;
\r
81 // Generate the rows and cols.
\r
82 if ( !this._.selectedElement )
\r
84 var tbody = table.append( makeElement( 'tbody' ) ),
\r
85 rows = parseInt( info.txtRows, 10 ) || 0,
\r
86 cols = parseInt( info.txtCols, 10 ) || 0;
\r
88 for ( var i = 0 ; i < rows ; i++ )
\r
90 var row = tbody.append( makeElement( 'tr' ) );
\r
91 for ( var j = 0 ; j < cols ; j++ )
\r
93 var cell = row.append( makeElement( 'td' ) );
\r
94 if ( !CKEDITOR.env.ie )
\r
95 cell.append( makeElement( 'br' ) );
\r
100 // Modify the table headers. Depends on having rows and cols generated
\r
101 // correctly so it can't be done in commit functions.
\r
103 // Should we make a <thead>?
\r
104 var headers = info.selHeaders;
\r
105 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
107 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
108 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
109 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
111 // Change TD to TH:
\r
112 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
114 var th = theRow.getChild( i );
\r
115 if ( th.type == CKEDITOR.NODE_ELEMENT )
\r
117 th.renameNode( 'th' );
\r
118 th.setAttribute( 'scope', 'col' );
\r
121 thead.append( theRow.remove() );
\r
124 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
126 // Move the row out of the THead and put it in the TBody:
\r
127 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
128 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
130 var previousFirstRow = tbody.getFirst();
\r
131 while ( thead.getChildCount() > 0 )
\r
133 theRow = thead.getFirst();
\r
134 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
136 var newCell = theRow.getChild( i );
\r
137 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
139 newCell.renameNode( 'td' );
\r
140 newCell.removeAttribute( 'scope' );
\r
143 theRow.insertBefore( previousFirstRow );
\r
148 // Should we make all first cells in a row TH?
\r
149 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
151 for( row = 0 ; row < table.$.rows.length ; row++ )
\r
153 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
154 newCell.renameNode( 'th' );
\r
155 newCell.setAttribute( 'scope', 'row' );
\r
159 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
160 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
162 for( i = 0 ; i < table.$.rows.length ; i++ )
\r
164 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
165 if ( row.getParent().getName() == 'tbody' )
\r
167 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
168 newCell.renameNode( 'td' );
\r
169 newCell.removeAttribute( 'scope' );
\r
174 // Set the width and height.
\r
176 if ( info.txtHeight )
\r
177 styles.push( 'height:' + info.txtHeight + 'px' );
\r
178 if ( info.txtWidth )
\r
180 var type = info.cmbWidthType || 'pixels';
\r
181 styles.push( 'width:' + info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
183 styles = styles.join( ';' );
\r
185 table.$.style.cssText = styles;
\r
187 table.removeAttribute( 'style' );
\r
190 // Insert the table element if we're creating one.
\r
191 if ( !this._.selectedElement )
\r
192 editor.insertElement( table );
\r
199 label : editor.lang.table.title,
\r
204 widths : [ null, null ],
\r
205 styles : [ 'vertical-align:top' ],
\r
217 label : editor.lang.table.rows,
\r
218 style : 'width:5em',
\r
219 validate : function()
\r
222 value = this.getValue();
\r
223 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
227 alert( editor.lang.table.invalidRows );
\r
232 setup : function( selectedElement )
\r
234 this.setValue( selectedElement.$.rows.length );
\r
236 commit : commitValue
\r
242 label : editor.lang.table.columns,
\r
243 style : 'width:5em',
\r
244 validate : function()
\r
247 value = this.getValue();
\r
248 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
252 alert( editor.lang.table.invalidCols );
\r
257 setup : function( selectedTable )
\r
259 this.setValue( selectedTable.$.rows[0].cells.length);
\r
261 commit : commitValue
\r
271 label : editor.lang.table.headers,
\r
274 [ editor.lang.table.headersNone, '' ],
\r
275 [ editor.lang.table.headersRow, 'row' ],
\r
276 [ editor.lang.table.headersColumn, 'col' ],
\r
277 [ editor.lang.table.headersBoth, 'both' ]
\r
279 setup : function( selectedTable )
\r
281 // Fill in the headers field.
\r
282 var dialog = this.getDialog();
\r
283 dialog.hasColumnHeaders = true;
\r
285 // Check if all the first cells in every row are TH
\r
286 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
288 // If just one cell isn't a TH then it isn't a header column
\r
289 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
291 dialog.hasColumnHeaders = false;
\r
296 // Check if the table contains <thead>.
\r
297 if ( ( selectedTable.$.tHead !== null) )
\r
298 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
300 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
302 commit : commitValue
\r
308 label : editor.lang.table.border,
\r
309 style : 'width:3em',
\r
310 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
311 setup : function( selectedTable )
\r
313 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
315 commit : function( data, selectedTable )
\r
317 if ( this.getValue() )
\r
318 selectedTable.setAttribute( 'border', this.getValue() );
\r
320 selectedTable.removeAttribute( 'border' );
\r
327 label : editor.lang.table.align,
\r
330 [ editor.lang.table.alignNotSet , ''],
\r
331 [ editor.lang.table.alignLeft , 'left'],
\r
332 [ editor.lang.table.alignCenter , 'center'],
\r
333 [ editor.lang.table.alignRight , 'right']
\r
335 setup : function( selectedTable )
\r
337 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
339 commit : function( data, selectedTable )
\r
341 if ( this.getValue() )
\r
342 selectedTable.setAttribute( 'align', this.getValue() );
\r
344 selectedTable.removeAttribute( 'align' );
\r
356 widths : [ '5em' ],
\r
362 style : 'width:5em',
\r
363 label : editor.lang.table.width,
\r
365 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
366 setup : function( selectedTable )
\r
368 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
370 this.setValue( widthMatch[1] );
\r
372 commit : commitValue
\r
375 id : 'cmbWidthType',
\r
378 'default' : 'pixels',
\r
381 [ editor.lang.table.widthPx , 'pixels'],
\r
382 [ editor.lang.table.widthPc , 'percents']
\r
384 setup : function( selectedTable )
\r
386 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
388 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
390 commit : commitValue
\r
396 widths : [ '5em' ],
\r
402 style : 'width:5em',
\r
403 label : editor.lang.table.height,
\r
405 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
406 setup : function( selectedTable )
\r
408 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
410 this.setValue( heightMatch[1] );
\r
412 commit : commitValue
\r
416 html : '<br />' + editor.lang.table.widthPx
\r
426 id : 'txtCellSpace',
\r
427 style : 'width:3em',
\r
428 label : editor.lang.table.cellSpace,
\r
430 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
431 setup : function( selectedTable )
\r
433 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
435 commit : function( data, selectedTable )
\r
437 if ( this.getValue() )
\r
438 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
440 selectedTable.removeAttribute( 'cellSpacing' );
\r
446 style : 'width:3em',
\r
447 label : editor.lang.table.cellPad,
\r
449 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
450 setup : function( selectedTable )
\r
452 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
454 commit : function( data, selectedTable )
\r
456 if ( this.getValue() )
\r
457 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
459 selectedTable.removeAttribute( 'cellPadding' );
\r
479 label : editor.lang.table.caption,
\r
480 setup : function( selectedTable )
\r
482 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
483 if ( nodeList.count() > 0 )
\r
485 var caption = nodeList.getItem( 0 );
\r
486 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
487 caption = CKEDITOR.tools.trim( caption );
\r
488 this.setValue( caption );
\r
491 commit : function( data, table )
\r
493 var caption = this.getValue(),
\r
494 captionElement = table.getElementsByTag( 'caption' );
\r
497 if ( captionElement.count() > 0 )
\r
499 captionElement = captionElement.getItem( 0 );
\r
500 captionElement.setHtml( '' );
\r
504 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
505 if ( table.getChildCount() )
\r
506 captionElement.insertBefore( table.getFirst() );
\r
508 captionElement.appendTo( table );
\r
510 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
512 else if ( captionElement.count() > 0 )
\r
514 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
515 captionElement.getItem( i ).remove();
\r
522 label : editor.lang.table.summary,
\r
523 setup : function( selectedTable )
\r
525 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
527 commit : function( data, selectedTable )
\r
529 if ( this.getValue() )
\r
530 selectedTable.setAttribute( 'summary', this.getValue() );
\r
541 CKEDITOR.dialog.add( 'table', function( editor )
\r
543 return tableDialog( editor, 'table' );
\r
545 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
547 return tableDialog( editor, 'tableProperties' );
\r