JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
6a082d25236bdc5db6fef0253952c1f90607bb0f
[ckeditor.git] / _source / plugins / table / dialogs / table.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         var widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/,\r
9                 heightPattern = /^(\d+(?:\.\d+)?)px$/;\r
10 \r
11         var commitValue = function( data )\r
12         {\r
13                 var id = this.id;\r
14                 if ( !data.info )\r
15                         data.info = {};\r
16                 data.info[id] = this.getValue();\r
17         };\r
18 \r
19         function tableDialog( editor, command )\r
20         {\r
21                 var makeElement = function( name ){ return new CKEDITOR.dom.element( name, editor.document ); };\r
22 \r
23                 return {\r
24                         title : editor.lang.table.title,\r
25                         minWidth : 310,\r
26                         minHeight : CKEDITOR.env.ie ? 310 : 280,\r
27                         onShow : function()\r
28                         {\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
33 \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
38                                 {\r
39                                         if ( ( selectedTable = editor.getSelection().getSelectedElement() ) )\r
40                                         {\r
41                                                 if ( selectedTable.getName() != 'table' )\r
42                                                         selectedTable = null;\r
43                                         }\r
44                                         else if ( ranges.length > 0 )\r
45                                         {\r
46                                                 var rangeRoot = ranges[0].getCommonAncestor( true );\r
47                                                 selectedTable = rangeRoot.getAscendant( 'table', true );\r
48                                         }\r
49 \r
50                                         // Save a reference to the selected table, and push a new set of default values.\r
51                                         this._.selectedElement = selectedTable;\r
52                                 }\r
53 \r
54                                 // Enable, disable and select the row, cols, width fields.\r
55                                 if ( selectedTable )\r
56                                 {\r
57                                         this.setupContent( selectedTable );\r
58                                         rowsInput && rowsInput.disable();\r
59                                         colsInput && colsInput.disable();\r
60                                         widthInput && widthInput.select();\r
61                                 }\r
62                                 else\r
63                                 {\r
64                                         rowsInput && rowsInput.enable();\r
65                                         colsInput && colsInput.enable();\r
66                                         rowsInput && rowsInput.select();\r
67                                 }\r
68                         },\r
69                         onOk : function()\r
70                         {\r
71                                 var table = this._.selectedElement || makeElement( 'table' ),\r
72                                         me = this,\r
73                                         data = {};\r
74 \r
75                                 this.commitContent( data, table );\r
76 \r
77                                 if ( data.info )\r
78                                 {\r
79                                         var info = data.info;\r
80 \r
81                                         // Generate the rows and cols.\r
82                                         if ( !this._.selectedElement )\r
83                                         {\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
87 \r
88                                                 for ( var i = 0 ; i < rows ; i++ )\r
89                                                 {\r
90                                                         var row = tbody.append( makeElement( 'tr' ) );\r
91                                                         for ( var j = 0 ; j < cols ; j++ )\r
92                                                         {\r
93                                                                 var cell = row.append( makeElement( 'td' ) );\r
94                                                                 if ( !CKEDITOR.env.ie )\r
95                                                                         cell.append( makeElement( 'br' ) );\r
96                                                         }\r
97                                                 }\r
98                                         }\r
99 \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
102 \r
103                                         // Should we make a <thead>?\r
104                                         var headers = info.selHeaders;\r
105                                         if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )\r
106                                         {\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
110 \r
111                                                 // Change TD to TH:\r
112                                                 for ( i = 0 ; i < theRow.getChildCount() ; i++ )\r
113                                                 {\r
114                                                         var th = theRow.getChild( i );\r
115                                                         if ( th.type == CKEDITOR.NODE_ELEMENT )\r
116                                                         {\r
117                                                                 th.renameNode( 'th' );\r
118                                                                 th.setAttribute( 'scope', 'col' );\r
119                                                         }\r
120                                                 }\r
121                                                 thead.append( theRow.remove() );\r
122                                         }\r
123 \r
124                                         if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )\r
125                                         {\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
129 \r
130                                                 var previousFirstRow = tbody.getFirst();\r
131                                                 while ( thead.getChildCount() > 0 )\r
132                                                 {\r
133                                                         theRow = thead.getFirst();\r
134                                                         for ( i = 0; i < theRow.getChildCount() ; i++ )\r
135                                                         {\r
136                                                                 var newCell = theRow.getChild( i );\r
137                                                                 if ( newCell.type == CKEDITOR.NODE_ELEMENT )\r
138                                                                 {\r
139                                                                         newCell.renameNode( 'td' );\r
140                                                                         newCell.removeAttribute( 'scope' );\r
141                                                                 }\r
142                                                         }\r
143                                                         theRow.insertBefore( previousFirstRow );\r
144                                                 }\r
145                                                 thead.remove();\r
146                                         }\r
147 \r
148                                         // Should we make all first cells in a row TH?\r
149                                         if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )\r
150                                         {\r
151                                                 for( row = 0 ; row < table.$.rows.length ; row++ )\r
152                                                 {\r
153                                                         newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );\r
154                                                         newCell.renameNode( 'th' );\r
155                                                         newCell.setAttribute( 'scope', 'row' );\r
156                                                 }\r
157                                         }\r
158 \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
161                                         {\r
162                                                 for( i = 0 ; i < table.$.rows.length ; i++ )\r
163                                                 {\r
164                                                         row = new CKEDITOR.dom.element( table.$.rows[i] );\r
165                                                         if ( row.getParent().getName() == 'tbody' )\r
166                                                         {\r
167                                                                 newCell = new CKEDITOR.dom.element( row.$.cells[0] );\r
168                                                                 newCell.renameNode( 'td' );\r
169                                                                 newCell.removeAttribute( 'scope' );\r
170                                                         }\r
171                                                 }\r
172                                         }\r
173 \r
174                                         // Set the width and height.\r
175                                         var styles = [];\r
176                                         if ( info.txtHeight )\r
177                                                 table.setStyle( 'height', CKEDITOR.tools.cssLength( info.txtHeight ) );\r
178                                         else\r
179                                                 table.removeStyle( 'height' );\r
180 \r
181                                         if ( info.txtWidth )\r
182                                         {\r
183                                                 var type = info.cmbWidthType || 'pixels';\r
184                                                 table.setStyle( 'width', info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );\r
185                                         }\r
186                                         else\r
187                                                 table.removeStyle( 'width' );\r
188 \r
189                                         if( !table.getAttribute( 'style' ) )\r
190                                                 table.removeAttribute( 'style' );\r
191                                 }\r
192 \r
193                                 // Insert the table element if we're creating one.\r
194                                 if ( !this._.selectedElement )\r
195                                         editor.insertElement( table );\r
196 \r
197                                 return true;\r
198                         },\r
199                         contents : [\r
200                                 {\r
201                                         id : 'info',\r
202                                         label : editor.lang.table.title,\r
203                                         elements :\r
204                                         [\r
205                                                 {\r
206                                                         type : 'hbox',\r
207                                                         widths : [ null, null ],\r
208                                                         styles : [ 'vertical-align:top' ],\r
209                                                         children :\r
210                                                         [\r
211                                                                 {\r
212                                                                         type : 'vbox',\r
213                                                                         padding : 0,\r
214                                                                         children :\r
215                                                                         [\r
216                                                                                 {\r
217                                                                                         type : 'text',\r
218                                                                                         id : 'txtRows',\r
219                                                                                         'default' : 3,\r
220                                                                                         label : editor.lang.table.rows,\r
221                                                                                         style : 'width:5em',\r
222                                                                                         validate : function()\r
223                                                                                         {\r
224                                                                                                 var pass = true,\r
225                                                                                                         value = this.getValue();\r
226                                                                                                 pass = pass && CKEDITOR.dialog.validate.integer()( value )\r
227                                                                                                         && value > 0;\r
228                                                                                                 if ( !pass )\r
229                                                                                                 {\r
230                                                                                                         alert( editor.lang.table.invalidRows );\r
231                                                                                                         this.select();\r
232                                                                                                 }\r
233                                                                                                 return pass;\r
234                                                                                         },\r
235                                                                                         setup : function( selectedElement )\r
236                                                                                         {\r
237                                                                                                 this.setValue( selectedElement.$.rows.length );\r
238                                                                                         },\r
239                                                                                         commit : commitValue\r
240                                                                                 },\r
241                                                                                 {\r
242                                                                                         type : 'text',\r
243                                                                                         id : 'txtCols',\r
244                                                                                         'default' : 2,\r
245                                                                                         label : editor.lang.table.columns,\r
246                                                                                         style : 'width:5em',\r
247                                                                                         validate : function()\r
248                                                                                         {\r
249                                                                                                 var pass = true,\r
250                                                                                                         value = this.getValue();\r
251                                                                                                 pass = pass && CKEDITOR.dialog.validate.integer()( value )\r
252                                                                                                         && value > 0;\r
253                                                                                                 if ( !pass )\r
254                                                                                                 {\r
255                                                                                                         alert( editor.lang.table.invalidCols );\r
256                                                                                                         this.select();\r
257                                                                                                 }\r
258                                                                                                 return pass;\r
259                                                                                         },\r
260                                                                                         setup : function( selectedTable )\r
261                                                                                         {\r
262                                                                                                 this.setValue( selectedTable.$.rows[0].cells.length);\r
263                                                                                         },\r
264                                                                                         commit : commitValue\r
265                                                                                 },\r
266                                                                                 {\r
267                                                                                         type : 'html',\r
268                                                                                         html : '&nbsp;'\r
269                                                                                 },\r
270                                                                                 {\r
271                                                                                         type : 'select',\r
272                                                                                         id : 'selHeaders',\r
273                                                                                         'default' : '',\r
274                                                                                         label : editor.lang.table.headers,\r
275                                                                                         items :\r
276                                                                                         [\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
281                                                                                         ],\r
282                                                                                         setup : function( selectedTable )\r
283                                                                                         {\r
284                                                                                                 // Fill in the headers field.\r
285                                                                                                 var dialog = this.getDialog();\r
286                                                                                                 dialog.hasColumnHeaders = true;\r
287 \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
290                                                                                                 {\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
293                                                                                                         {\r
294                                                                                                                 dialog.hasColumnHeaders = false;\r
295                                                                                                                 break;\r
296                                                                                                         }\r
297                                                                                                 }\r
298 \r
299                                                                                                 // Check if the table contains <thead>.\r
300                                                                                                 if ( ( selectedTable.$.tHead !== null) )\r
301                                                                                                         this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );\r
302                                                                                                 else\r
303                                                                                                         this.setValue( dialog.hasColumnHeaders ? 'col' : '' );\r
304                                                                                         },\r
305                                                                                         commit : commitValue\r
306                                                                                 },\r
307                                                                                 {\r
308                                                                                         type : 'text',\r
309                                                                                         id : 'txtBorder',\r
310                                                                                         'default' : 1,\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
315                                                                                         {\r
316                                                                                                 this.setValue( selectedTable.getAttribute( 'border' ) || '' );\r
317                                                                                         },\r
318                                                                                         commit : function( data, selectedTable )\r
319                                                                                         {\r
320                                                                                                 if ( this.getValue() )\r
321                                                                                                         selectedTable.setAttribute( 'border', this.getValue() );\r
322                                                                                                 else\r
323                                                                                                         selectedTable.removeAttribute( 'border' );\r
324                                                                                         }\r
325                                                                                 },\r
326                                                                                 {\r
327                                                                                         id : 'cmbAlign',\r
328                                                                                         type : 'select',\r
329                                                                                         'default' : '',\r
330                                                                                         label : editor.lang.table.align,\r
331                                                                                         items :\r
332                                                                                         [\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
337                                                                                         ],\r
338                                                                                         setup : function( selectedTable )\r
339                                                                                         {\r
340                                                                                                 this.setValue( selectedTable.getAttribute( 'align' ) || '' );\r
341                                                                                         },\r
342                                                                                         commit : function( data, selectedTable )\r
343                                                                                         {\r
344                                                                                                 if ( this.getValue() )\r
345                                                                                                         selectedTable.setAttribute( 'align', this.getValue() );\r
346                                                                                                 else\r
347                                                                                                         selectedTable.removeAttribute( 'align' );\r
348                                                                                         }\r
349                                                                                 }\r
350                                                                         ]\r
351                                                                 },\r
352                                                                 {\r
353                                                                         type : 'vbox',\r
354                                                                         padding : 0,\r
355                                                                         children :\r
356                                                                         [\r
357                                                                                 {\r
358                                                                                         type : 'hbox',\r
359                                                                                         widths : [ '5em' ],\r
360                                                                                         children :\r
361                                                                                         [\r
362                                                                                                 {\r
363                                                                                                         type : 'text',\r
364                                                                                                         id : 'txtWidth',\r
365                                                                                                         style : 'width:5em',\r
366                                                                                                         label : editor.lang.table.width,\r
367                                                                                                         'default' : 200,\r
368                                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),\r
369                                                                                                         setup : function( selectedTable )\r
370                                                                                                         {\r
371                                                                                                                 var widthMatch = widthPattern.exec( selectedTable.$.style.width );\r
372                                                                                                                 if ( widthMatch )\r
373                                                                                                                         this.setValue( widthMatch[1] );\r
374                                                                                                         },\r
375                                                                                                         commit : commitValue\r
376                                                                                                 },\r
377                                                                                                 {\r
378                                                                                                         id : 'cmbWidthType',\r
379                                                                                                         type : 'select',\r
380                                                                                                         label : '&nbsp;',\r
381                                                                                                         'default' : 'pixels',\r
382                                                                                                         items :\r
383                                                                                                         [\r
384                                                                                                                 [ editor.lang.table.widthPx , 'pixels'],\r
385                                                                                                                 [ editor.lang.table.widthPc , 'percents']\r
386                                                                                                         ],\r
387                                                                                                         setup : function( selectedTable )\r
388                                                                                                         {\r
389                                                                                                                 var widthMatch = widthPattern.exec( selectedTable.$.style.width );\r
390                                                                                                                 if ( widthMatch )\r
391                                                                                                                         this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );\r
392                                                                                                         },\r
393                                                                                                         commit : commitValue\r
394                                                                                                 }\r
395                                                                                         ]\r
396                                                                                 },\r
397                                                                                 {\r
398                                                                                         type : 'hbox',\r
399                                                                                         widths : [ '5em' ],\r
400                                                                                         children :\r
401                                                                                         [\r
402                                                                                                 {\r
403                                                                                                         type : 'text',\r
404                                                                                                         id : 'txtHeight',\r
405                                                                                                         style : 'width:5em',\r
406                                                                                                         label : editor.lang.table.height,\r
407                                                                                                         'default' : '',\r
408                                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),\r
409                                                                                                         setup : function( selectedTable )\r
410                                                                                                         {\r
411                                                                                                                 var heightMatch = heightPattern.exec( selectedTable.$.style.height );\r
412                                                                                                                 if ( heightMatch )\r
413                                                                                                                         this.setValue( heightMatch[1] );\r
414                                                                                                         },\r
415                                                                                                         commit : commitValue\r
416                                                                                                 },\r
417                                                                                                 {\r
418                                                                                                         type : 'html',\r
419                                                                                                         html : '<br />' + editor.lang.table.widthPx\r
420                                                                                                 }\r
421                                                                                         ]\r
422                                                                                 },\r
423                                                                                 {\r
424                                                                                         type : 'html',\r
425                                                                                         html : '&nbsp;'\r
426                                                                                 },\r
427                                                                                 {\r
428                                                                                         type : 'text',\r
429                                                                                         id : 'txtCellSpace',\r
430                                                                                         style : 'width:3em',\r
431                                                                                         label : editor.lang.table.cellSpace,\r
432                                                                                         'default' : 1,\r
433                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),\r
434                                                                                         setup : function( selectedTable )\r
435                                                                                         {\r
436                                                                                                 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );\r
437                                                                                         },\r
438                                                                                         commit : function( data, selectedTable )\r
439                                                                                         {\r
440                                                                                                 if ( this.getValue() )\r
441                                                                                                         selectedTable.setAttribute( 'cellSpacing', this.getValue() );\r
442                                                                                                 else\r
443                                                                                                         selectedTable.removeAttribute( 'cellSpacing' );\r
444                                                                                         }\r
445                                                                                 },\r
446                                                                                 {\r
447                                                                                         type : 'text',\r
448                                                                                         id : 'txtCellPad',\r
449                                                                                         style : 'width:3em',\r
450                                                                                         label : editor.lang.table.cellPad,\r
451                                                                                         'default' : 1,\r
452                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),\r
453                                                                                         setup : function( selectedTable )\r
454                                                                                         {\r
455                                                                                                 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );\r
456                                                                                         },\r
457                                                                                         commit : function( data, selectedTable )\r
458                                                                                         {\r
459                                                                                                 if ( this.getValue() )\r
460                                                                                                         selectedTable.setAttribute( 'cellPadding', this.getValue() );\r
461                                                                                                 else\r
462                                                                                                         selectedTable.removeAttribute( 'cellPadding' );\r
463                                                                                         }\r
464                                                                                 }\r
465                                                                         ]\r
466                                                                 }\r
467                                                         ]\r
468                                                 },\r
469                                                 {\r
470                                                         type : 'html',\r
471                                                         align : 'right',\r
472                                                         html : ''\r
473                                                 },\r
474                                                 {\r
475                                                         type : 'vbox',\r
476                                                         padding : 0,\r
477                                                         children :\r
478                                                         [\r
479                                                                 {\r
480                                                                         type : 'text',\r
481                                                                         id : 'txtCaption',\r
482                                                                         label : editor.lang.table.caption,\r
483                                                                         setup : function( selectedTable )\r
484                                                                         {\r
485                                                                                 var nodeList = selectedTable.getElementsByTag( 'caption' );\r
486                                                                                 if ( nodeList.count() > 0 )\r
487                                                                                 {\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
492                                                                                 }\r
493                                                                         },\r
494                                                                         commit : function( data, table )\r
495                                                                         {\r
496                                                                                 var caption = this.getValue(),\r
497                                                                                         captionElement = table.getElementsByTag( 'caption' );\r
498                                                                                 if ( caption )\r
499                                                                                 {\r
500                                                                                         if ( captionElement.count() > 0 )\r
501                                                                                         {\r
502                                                                                                 captionElement = captionElement.getItem( 0 );\r
503                                                                                                 captionElement.setHtml( '' );\r
504                                                                                         }\r
505                                                                                         else\r
506                                                                                         {\r
507                                                                                                 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );\r
508                                                                                                 if ( table.getChildCount() )\r
509                                                                                                         captionElement.insertBefore( table.getFirst() );\r
510                                                                                                 else\r
511                                                                                                         captionElement.appendTo( table );\r
512                                                                                         }\r
513                                                                                         captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );\r
514                                                                                 }\r
515                                                                                 else if ( captionElement.count() > 0 )\r
516                                                                                 {\r
517                                                                                         for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )\r
518                                                                                                 captionElement.getItem( i ).remove();\r
519                                                                                 }\r
520                                                                         }\r
521                                                                 },\r
522                                                                 {\r
523                                                                         type : 'text',\r
524                                                                         id : 'txtSummary',\r
525                                                                         label : editor.lang.table.summary,\r
526                                                                         setup : function( selectedTable )\r
527                                                                         {\r
528                                                                                 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );\r
529                                                                         },\r
530                                                                         commit : function( data, selectedTable )\r
531                                                                         {\r
532                                                                                 if ( this.getValue() )\r
533                                                                                         selectedTable.setAttribute( 'summary', this.getValue() );\r
534                                                                         }\r
535                                                                 }\r
536                                                         ]\r
537                                                 }\r
538                                         ]\r
539                                 }\r
540                         ]\r
541                 };\r
542         }\r
543 \r
544         CKEDITOR.dialog.add( 'table', function( editor )\r
545                 {\r
546                         return tableDialog( editor, 'table' );\r
547                 } );\r
548         CKEDITOR.dialog.add( 'tableProperties', function( editor )\r
549                 {\r
550                         return tableDialog( editor, 'tableProperties' );\r
551                 } );\r
552 })();\r