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 havint 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
119 th.setAttribute( 'scope', 'col' );
\r
122 thead.append( theRow.remove() );
\r
125 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
127 // Move the row out of the THead and put it in the TBody:
\r
128 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
129 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
131 var previousFirstRow = tbody.getFirst();
\r
132 while ( thead.getChildCount() > 0 )
\r
134 theRow = thead.getFirst();
\r
135 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
137 var newCell = theRow.getChild( i );
\r
138 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
140 newCell.renameNode( 'td' );
\r
141 newCell.removeAttribute( 'scope' );
\r
144 theRow.insertBefore( previousFirstRow );
\r
149 // Should we make all first cells in a row TH?
\r
150 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
152 for( row = 0 ; row < table.$.rows.length ; row++ )
\r
154 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
155 newCell.renameNode( 'th' );
\r
156 newCell.setAttribute( 'scope', 'col' );
\r
160 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
161 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
163 for( i = 0 ; i < table.$.rows.length ; i++ )
\r
165 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
166 if ( row.getParent().getName() == 'tbody' )
\r
168 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
169 newCell.renameNode( 'td');
\r
170 newCell.removeAttribute( 'scope' );
\r
175 // Set the width and height.
\r
177 if ( info.txtHeight )
\r
178 styles.push( 'height:' + info.txtHeight + 'px' );
\r
179 if ( info.txtWidth )
\r
181 var type = info.cmbWidthType || 'pixels';
\r
182 styles.push( 'width:' + info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
184 styles = styles.join( ';' );
\r
186 table.$.style.cssText = styles;
\r
188 table.removeAttribute( 'style' );
\r
191 // Insert the table element if we're creating one.
\r
192 if ( !this._.selectedElement )
\r
193 editor.insertElement( table );
\r
200 label : editor.lang.table.title,
\r
205 widths : [ null, null ],
\r
206 styles : [ 'vertical-align:top' ],
\r
218 label : editor.lang.table.rows,
\r
219 style : 'width:5em',
\r
220 validate : function()
\r
223 value = this.getValue();
\r
224 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
228 alert( editor.lang.table.invalidRows );
\r
233 setup : function( selectedElement )
\r
235 this.setValue( selectedElement.$.rows.length );
\r
237 commit : commitValue
\r
243 label : editor.lang.table.columns,
\r
244 style : 'width:5em',
\r
245 validate : function()
\r
248 value = this.getValue();
\r
249 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
253 alert( editor.lang.table.invalidCols );
\r
258 setup : function( selectedTable )
\r
260 this.setValue( selectedTable.$.rows[0].cells.length);
\r
262 commit : commitValue
\r
272 label : editor.lang.table.headers,
\r
275 [ editor.lang.table.headersNone, '' ],
\r
276 [ editor.lang.table.headersRow, 'row' ],
\r
277 [ editor.lang.table.headersColumn, 'col' ],
\r
278 [ editor.lang.table.headersBoth, 'both' ]
\r
280 setup : function( selectedTable )
\r
282 // Fill in the headers field.
\r
283 var dialog = this.getDialog();
\r
284 dialog.hasColumnHeaders = true;
\r
286 // Check if all the first cells in every row are TH
\r
287 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
289 // If just one cell isn't a TH then it isn't a header column
\r
290 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
292 dialog.hasColumnHeaders = false;
\r
297 // Check if the table contains <thead>.
\r
298 if ( ( selectedTable.$.tHead !== null) )
\r
299 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
301 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
303 commit : commitValue
\r
309 label : editor.lang.table.border,
\r
310 style : 'width:3em',
\r
311 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
312 setup : function( selectedTable )
\r
314 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
316 commit : function( data, selectedTable )
\r
318 if ( this.getValue() )
\r
319 selectedTable.setAttribute( 'border', this.getValue() );
\r
321 selectedTable.removeAttribute( 'border' );
\r
328 label : editor.lang.table.align,
\r
331 [ editor.lang.table.alignNotSet , ''],
\r
332 [ editor.lang.table.alignLeft , 'left'],
\r
333 [ editor.lang.table.alignCenter , 'center'],
\r
334 [ editor.lang.table.alignRight , 'right']
\r
336 setup : function( selectedTable )
\r
338 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
340 commit : function( data, selectedTable )
\r
342 if ( this.getValue() )
\r
343 selectedTable.setAttribute( 'align', this.getValue() );
\r
345 selectedTable.removeAttribute( 'align' );
\r
357 widths : [ '5em' ],
\r
363 style : 'width:5em',
\r
364 label : editor.lang.table.width,
\r
366 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
367 setup : function( selectedTable )
\r
369 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
371 this.setValue( widthMatch[1] );
\r
373 commit : commitValue
\r
376 id : 'cmbWidthType',
\r
379 'default' : 'pixels',
\r
382 [ editor.lang.table.widthPx , 'pixels'],
\r
383 [ editor.lang.table.widthPc , 'percents']
\r
385 setup : function( selectedTable )
\r
387 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
389 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
391 commit : commitValue
\r
397 widths : [ '5em' ],
\r
403 style : 'width:5em',
\r
404 label : editor.lang.table.height,
\r
406 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
407 setup : function( selectedTable )
\r
409 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
411 this.setValue( heightMatch[1] );
\r
413 commit : commitValue
\r
417 html : '<br />' + editor.lang.table.widthPx
\r
427 id : 'txtCellSpace',
\r
428 style : 'width:3em',
\r
429 label : editor.lang.table.cellSpace,
\r
431 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
432 setup : function( selectedTable )
\r
434 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
436 commit : function( data, selectedTable )
\r
438 if ( this.getValue() )
\r
439 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
441 selectedTable.removeAttribute( 'cellSpacing' );
\r
447 style : 'width:3em',
\r
448 label : editor.lang.table.cellPad,
\r
450 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
451 setup : function( selectedTable )
\r
453 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
455 commit : function( data, selectedTable )
\r
457 if ( this.getValue() )
\r
458 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
460 selectedTable.removeAttribute( 'cellPadding' );
\r
480 label : editor.lang.table.caption,
\r
481 setup : function( selectedTable )
\r
483 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
484 if ( nodeList.count() > 0 )
\r
486 var caption = nodeList.getItem( 0 );
\r
487 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
488 caption = CKEDITOR.tools.trim( caption );
\r
489 this.setValue( caption );
\r
492 commit : function( data, table )
\r
494 var caption = this.getValue(),
\r
495 captionElement = table.getElementsByTag( 'caption' );
\r
498 if ( captionElement.count() > 0 )
\r
500 captionElement = captionElement.getItem( 0 );
\r
501 captionElement.setHtml( '' );
\r
505 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
506 if ( table.getChildCount() )
\r
507 captionElement.insertBefore( table.getFirst() );
\r
509 captionElement.appendTo( table );
\r
511 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
513 else if ( captionElement.count() > 0 )
\r
515 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
516 captionElement.getItem( i ).remove();
\r
523 label : editor.lang.table.summary,
\r
524 setup : function( selectedTable )
\r
526 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
528 commit : function( data, selectedTable )
\r
530 if ( this.getValue() )
\r
531 selectedTable.setAttribute( 'summary', this.getValue() );
\r
542 CKEDITOR.dialog.add( 'table', function( editor )
\r
544 return tableDialog( editor, 'table' );
\r
546 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
548 return tableDialog( editor, 'tableProperties' );
\r