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 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
179 table.removeStyle( 'height' );
\r
181 if ( info.txtWidth )
\r
183 var type = info.cmbWidthType || 'pixels';
\r
184 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
187 table.removeStyle( 'width' );
\r
189 if( !table.getAttribute( 'style' ) )
\r
190 table.removeAttribute( 'style' );
\r
193 // Insert the table element if we're creating one.
\r
194 if ( !this._.selectedElement )
\r
195 editor.insertElement( table );
\r
202 label : editor.lang.table.title,
\r
207 widths : [ null, null ],
\r
208 styles : [ 'vertical-align:top' ],
\r
220 label : editor.lang.table.rows,
\r
221 style : 'width:5em',
\r
222 validate : function()
\r
225 value = this.getValue();
\r
226 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
230 alert( editor.lang.table.invalidRows );
\r
235 setup : function( selectedElement )
\r
237 this.setValue( selectedElement.$.rows.length );
\r
239 commit : commitValue
\r
245 label : editor.lang.table.columns,
\r
246 style : 'width:5em',
\r
247 validate : function()
\r
250 value = this.getValue();
\r
251 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
255 alert( editor.lang.table.invalidCols );
\r
260 setup : function( selectedTable )
\r
262 this.setValue( selectedTable.$.rows[0].cells.length);
\r
264 commit : commitValue
\r
274 label : editor.lang.table.headers,
\r
277 [ editor.lang.table.headersNone, '' ],
\r
278 [ editor.lang.table.headersRow, 'row' ],
\r
279 [ editor.lang.table.headersColumn, 'col' ],
\r
280 [ editor.lang.table.headersBoth, 'both' ]
\r
282 setup : function( selectedTable )
\r
284 // Fill in the headers field.
\r
285 var dialog = this.getDialog();
\r
286 dialog.hasColumnHeaders = true;
\r
288 // Check if all the first cells in every row are TH
\r
289 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
291 // If just one cell isn't a TH then it isn't a header column
\r
292 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
294 dialog.hasColumnHeaders = false;
\r
299 // Check if the table contains <thead>.
\r
300 if ( ( selectedTable.$.tHead !== null) )
\r
301 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
303 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
305 commit : commitValue
\r
311 label : editor.lang.table.border,
\r
312 style : 'width:3em',
\r
313 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
314 setup : function( selectedTable )
\r
316 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
318 commit : function( data, selectedTable )
\r
320 if ( this.getValue() )
\r
321 selectedTable.setAttribute( 'border', this.getValue() );
\r
323 selectedTable.removeAttribute( 'border' );
\r
330 label : editor.lang.table.align,
\r
333 [ editor.lang.table.alignNotSet , ''],
\r
334 [ editor.lang.table.alignLeft , 'left'],
\r
335 [ editor.lang.table.alignCenter , 'center'],
\r
336 [ editor.lang.table.alignRight , 'right']
\r
338 setup : function( selectedTable )
\r
340 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
342 commit : function( data, selectedTable )
\r
344 if ( this.getValue() )
\r
345 selectedTable.setAttribute( 'align', this.getValue() );
\r
347 selectedTable.removeAttribute( 'align' );
\r
359 widths : [ '5em' ],
\r
365 style : 'width:5em',
\r
366 label : editor.lang.table.width,
\r
368 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
369 setup : function( selectedTable )
\r
371 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
373 this.setValue( widthMatch[1] );
\r
375 commit : commitValue
\r
378 id : 'cmbWidthType',
\r
381 'default' : 'pixels',
\r
384 [ editor.lang.table.widthPx , 'pixels'],
\r
385 [ editor.lang.table.widthPc , 'percents']
\r
387 setup : function( selectedTable )
\r
389 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
391 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
393 commit : commitValue
\r
399 widths : [ '5em' ],
\r
405 style : 'width:5em',
\r
406 label : editor.lang.table.height,
\r
408 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
409 setup : function( selectedTable )
\r
411 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
413 this.setValue( heightMatch[1] );
\r
415 commit : commitValue
\r
419 html : '<br />' + editor.lang.table.widthPx
\r
429 id : 'txtCellSpace',
\r
430 style : 'width:3em',
\r
431 label : editor.lang.table.cellSpace,
\r
433 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
434 setup : function( selectedTable )
\r
436 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
438 commit : function( data, selectedTable )
\r
440 if ( this.getValue() )
\r
441 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
443 selectedTable.removeAttribute( 'cellSpacing' );
\r
449 style : 'width:3em',
\r
450 label : editor.lang.table.cellPad,
\r
452 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
453 setup : function( selectedTable )
\r
455 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
457 commit : function( data, selectedTable )
\r
459 if ( this.getValue() )
\r
460 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
462 selectedTable.removeAttribute( 'cellPadding' );
\r
482 label : editor.lang.table.caption,
\r
483 setup : function( selectedTable )
\r
485 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
486 if ( nodeList.count() > 0 )
\r
488 var caption = nodeList.getItem( 0 );
\r
489 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
490 caption = CKEDITOR.tools.trim( caption );
\r
491 this.setValue( caption );
\r
494 commit : function( data, table )
\r
496 var caption = this.getValue(),
\r
497 captionElement = table.getElementsByTag( 'caption' );
\r
500 if ( captionElement.count() > 0 )
\r
502 captionElement = captionElement.getItem( 0 );
\r
503 captionElement.setHtml( '' );
\r
507 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
508 if ( table.getChildCount() )
\r
509 captionElement.insertBefore( table.getFirst() );
\r
511 captionElement.appendTo( table );
\r
513 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
515 else if ( captionElement.count() > 0 )
\r
517 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
518 captionElement.getItem( i ).remove();
\r
525 label : editor.lang.table.summary,
\r
526 setup : function( selectedTable )
\r
528 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
530 commit : function( data, selectedTable )
\r
532 if ( this.getValue() )
\r
533 selectedTable.setAttribute( 'summary', this.getValue() );
\r
544 CKEDITOR.dialog.add( 'table', function( editor )
\r
546 return tableDialog( editor, 'table' );
\r
548 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
550 return tableDialog( editor, 'tableProperties' );
\r