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 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 if ( this._.selectedElement )
\r
73 var selection = editor.getSelection(),
\r
74 bms = editor.getSelection().createBookmarks();
\r
77 var table = this._.selectedElement || makeElement( 'table' ),
\r
81 this.commitContent( data, table );
\r
85 var info = data.info;
\r
87 // Generate the rows and cols.
\r
88 if ( !this._.selectedElement )
\r
90 var tbody = table.append( makeElement( 'tbody' ) ),
\r
91 rows = parseInt( info.txtRows, 10 ) || 0,
\r
92 cols = parseInt( info.txtCols, 10 ) || 0;
\r
94 for ( var i = 0 ; i < rows ; i++ )
\r
96 var row = tbody.append( makeElement( 'tr' ) );
\r
97 for ( var j = 0 ; j < cols ; j++ )
\r
99 var cell = row.append( makeElement( 'td' ) );
\r
100 if ( !CKEDITOR.env.ie )
\r
101 cell.append( makeElement( 'br' ) );
\r
106 // Modify the table headers. Depends on having rows and cols generated
\r
107 // correctly so it can't be done in commit functions.
\r
109 // Should we make a <thead>?
\r
110 var headers = info.selHeaders;
\r
111 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
\r
113 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
\r
114 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
115 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
\r
117 // Change TD to TH:
\r
118 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
\r
120 var th = theRow.getChild( i );
\r
121 if ( th.type == CKEDITOR.NODE_ELEMENT )
\r
123 th.renameNode( 'th' );
\r
124 th.setAttribute( 'scope', 'col' );
\r
127 thead.append( theRow.remove() );
\r
130 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
\r
132 // Move the row out of the THead and put it in the TBody:
\r
133 thead = new CKEDITOR.dom.element( table.$.tHead );
\r
134 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
\r
136 var previousFirstRow = tbody.getFirst();
\r
137 while ( thead.getChildCount() > 0 )
\r
139 theRow = thead.getFirst();
\r
140 for ( i = 0; i < theRow.getChildCount() ; i++ )
\r
142 var newCell = theRow.getChild( i );
\r
143 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
\r
145 newCell.renameNode( 'td' );
\r
146 newCell.removeAttribute( 'scope' );
\r
149 theRow.insertBefore( previousFirstRow );
\r
154 // Should we make all first cells in a row TH?
\r
155 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
\r
157 for ( row = 0 ; row < table.$.rows.length ; row++ )
\r
159 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
\r
160 newCell.renameNode( 'th' );
\r
161 newCell.setAttribute( 'scope', 'row' );
\r
165 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
\r
166 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
\r
168 for ( i = 0 ; i < table.$.rows.length ; i++ )
\r
170 row = new CKEDITOR.dom.element( table.$.rows[i] );
\r
171 if ( row.getParent().getName() == 'tbody' )
\r
173 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
\r
174 newCell.renameNode( 'td' );
\r
175 newCell.removeAttribute( 'scope' );
\r
180 // Set the width and height.
\r
182 if ( info.txtHeight )
\r
183 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );
\r
185 table.removeStyle( 'height' );
\r
187 if ( info.txtWidth )
\r
189 var type = info.cmbWidthType || 'pixels';
\r
190 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );
\r
193 table.removeStyle( 'width' );
\r
195 if ( !table.getAttribute( 'style' ) )
\r
196 table.removeAttribute( 'style' );
\r
199 // Insert the table element if we're creating one.
\r
200 if ( !this._.selectedElement )
\r
201 editor.insertElement( table );
\r
202 // Properly restore the selection inside table. (#4822)
\r
204 selection.selectBookmarks( bms );
\r
211 label : editor.lang.table.title,
\r
216 widths : [ null, null ],
\r
217 styles : [ 'vertical-align:top' ],
\r
229 label : editor.lang.table.rows,
\r
230 style : 'width:5em',
\r
231 validate : function()
\r
234 value = this.getValue();
\r
235 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
239 alert( editor.lang.table.invalidRows );
\r
244 setup : function( selectedElement )
\r
246 this.setValue( selectedElement.$.rows.length );
\r
248 commit : commitValue
\r
254 label : editor.lang.table.columns,
\r
255 style : 'width:5em',
\r
256 validate : function()
\r
259 value = this.getValue();
\r
260 pass = pass && CKEDITOR.dialog.validate.integer()( value )
\r
264 alert( editor.lang.table.invalidCols );
\r
269 setup : function( selectedTable )
\r
271 this.setValue( selectedTable.$.rows[0].cells.length);
\r
273 commit : commitValue
\r
283 label : editor.lang.table.headers,
\r
286 [ editor.lang.table.headersNone, '' ],
\r
287 [ editor.lang.table.headersRow, 'row' ],
\r
288 [ editor.lang.table.headersColumn, 'col' ],
\r
289 [ editor.lang.table.headersBoth, 'both' ]
\r
291 setup : function( selectedTable )
\r
293 // Fill in the headers field.
\r
294 var dialog = this.getDialog();
\r
295 dialog.hasColumnHeaders = true;
\r
297 // Check if all the first cells in every row are TH
\r
298 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
\r
300 // If just one cell isn't a TH then it isn't a header column
\r
301 if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' )
\r
303 dialog.hasColumnHeaders = false;
\r
308 // Check if the table contains <thead>.
\r
309 if ( ( selectedTable.$.tHead !== null) )
\r
310 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
\r
312 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
\r
314 commit : commitValue
\r
320 label : editor.lang.table.border,
\r
321 style : 'width:3em',
\r
322 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
\r
323 setup : function( selectedTable )
\r
325 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
\r
327 commit : function( data, selectedTable )
\r
329 if ( this.getValue() )
\r
330 selectedTable.setAttribute( 'border', this.getValue() );
\r
332 selectedTable.removeAttribute( 'border' );
\r
339 label : editor.lang.table.align,
\r
342 [ editor.lang.table.alignNotSet , ''],
\r
343 [ editor.lang.table.alignLeft , 'left'],
\r
344 [ editor.lang.table.alignCenter , 'center'],
\r
345 [ editor.lang.table.alignRight , 'right']
\r
347 setup : function( selectedTable )
\r
349 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
\r
351 commit : function( data, selectedTable )
\r
353 if ( this.getValue() )
\r
354 selectedTable.setAttribute( 'align', this.getValue() );
\r
356 selectedTable.removeAttribute( 'align' );
\r
368 widths : [ '5em' ],
\r
374 style : 'width:5em',
\r
375 label : editor.lang.table.width,
\r
377 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),
\r
378 setup : function( selectedTable )
\r
380 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
382 this.setValue( widthMatch[1] );
\r
384 this.setValue( '' );
\r
386 commit : commitValue
\r
389 id : 'cmbWidthType',
\r
392 'default' : 'pixels',
\r
395 [ editor.lang.table.widthPx , 'pixels'],
\r
396 [ editor.lang.table.widthPc , 'percents']
\r
398 setup : function( selectedTable )
\r
400 var widthMatch = widthPattern.exec( selectedTable.$.style.width );
\r
402 this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );
\r
404 commit : commitValue
\r
410 widths : [ '5em' ],
\r
416 style : 'width:5em',
\r
417 label : editor.lang.table.height,
\r
419 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),
\r
420 setup : function( selectedTable )
\r
422 var heightMatch = heightPattern.exec( selectedTable.$.style.height );
\r
424 this.setValue( heightMatch[1] );
\r
426 commit : commitValue
\r
430 html : '<br />' + editor.lang.table.widthPx
\r
440 id : 'txtCellSpace',
\r
441 style : 'width:3em',
\r
442 label : editor.lang.table.cellSpace,
\r
444 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),
\r
445 setup : function( selectedTable )
\r
447 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
\r
449 commit : function( data, selectedTable )
\r
451 if ( this.getValue() )
\r
452 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
\r
454 selectedTable.removeAttribute( 'cellSpacing' );
\r
460 style : 'width:3em',
\r
461 label : editor.lang.table.cellPad,
\r
463 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),
\r
464 setup : function( selectedTable )
\r
466 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
\r
468 commit : function( data, selectedTable )
\r
470 if ( this.getValue() )
\r
471 selectedTable.setAttribute( 'cellPadding', this.getValue() );
\r
473 selectedTable.removeAttribute( 'cellPadding' );
\r
493 label : editor.lang.table.caption,
\r
494 setup : function( selectedTable )
\r
496 var nodeList = selectedTable.getElementsByTag( 'caption' );
\r
497 if ( nodeList.count() > 0 )
\r
499 var caption = nodeList.getItem( 0 );
\r
500 caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || '';
\r
501 caption = CKEDITOR.tools.trim( caption );
\r
502 this.setValue( caption );
\r
505 commit : function( data, table )
\r
507 var caption = this.getValue(),
\r
508 captionElement = table.getElementsByTag( 'caption' );
\r
511 if ( captionElement.count() > 0 )
\r
513 captionElement = captionElement.getItem( 0 );
\r
514 captionElement.setHtml( '' );
\r
518 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
\r
519 if ( table.getChildCount() )
\r
520 captionElement.insertBefore( table.getFirst() );
\r
522 captionElement.appendTo( table );
\r
524 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
\r
526 else if ( captionElement.count() > 0 )
\r
528 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
\r
529 captionElement.getItem( i ).remove();
\r
536 label : editor.lang.table.summary,
\r
537 setup : function( selectedTable )
\r
539 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
\r
541 commit : function( data, selectedTable )
\r
543 if ( this.getValue() )
\r
544 selectedTable.setAttribute( 'summary', this.getValue() );
\r
555 CKEDITOR.dialog.add( 'table', function( editor )
\r
557 return tableDialog( editor, 'table' );
\r
559 CKEDITOR.dialog.add( 'tableProperties', function( editor )
\r
561 return tableDialog( editor, 'tableProperties' );
\r