JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
2c02aa2dbba44d8e60c3d0395822adb8c539faff
[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 havint 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                                                                 if ( !i )\r
119                                                                         th.setAttribute( 'scope', 'col' );\r
120                                                         }\r
121                                                 }\r
122                                                 thead.append( theRow.remove() );\r
123                                         }\r
124 \r
125                                         if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )\r
126                                         {\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
130 \r
131                                                 var previousFirstRow = tbody.getFirst();\r
132                                                 while ( thead.getChildCount() > 0 )\r
133                                                 {\r
134                                                         theRow = thead.getFirst();\r
135                                                         for ( i = 0; i < theRow.getChildCount() ; i++ )\r
136                                                         {\r
137                                                                 var newCell = theRow.getChild( i );\r
138                                                                 if ( newCell.type == CKEDITOR.NODE_ELEMENT )\r
139                                                                 {\r
140                                                                         newCell.renameNode( 'td' );\r
141                                                                         newCell.removeAttribute( 'scope' );\r
142                                                                 }\r
143                                                         }\r
144                                                         theRow.insertBefore( previousFirstRow );\r
145                                                 }\r
146                                                 thead.remove();\r
147                                         }\r
148 \r
149                                         // Should we make all first cells in a row TH?\r
150                                         if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )\r
151                                         {\r
152                                                 for( row = 0 ; row < table.$.rows.length ; row++ )\r
153                                                 {\r
154                                                         newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );\r
155                                                         newCell.renameNode( 'th' );\r
156                                                         newCell.setAttribute( 'scope', 'col' );\r
157                                                 }\r
158                                         }\r
159 \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
162                                         {\r
163                                                 for( i = 0 ; i < table.$.rows.length ; i++ )\r
164                                                 {\r
165                                                         row = new CKEDITOR.dom.element( table.$.rows[i] );\r
166                                                         if ( row.getParent().getName() == 'tbody' )\r
167                                                         {\r
168                                                                 newCell = new CKEDITOR.dom.element( row.$.cells[0] );\r
169                                                                 newCell.renameNode( 'td');\r
170                                                                 newCell.removeAttribute( 'scope' );\r
171                                                         }\r
172                                                 }\r
173                                         }\r
174 \r
175                                         // Set the width and height.\r
176                                         var styles = [];\r
177                                         if ( info.txtHeight )\r
178                                                 styles.push( 'height:' + info.txtHeight + 'px' );\r
179                                         if ( info.txtWidth )\r
180                                         {\r
181                                                 var type = info.cmbWidthType || 'pixels';\r
182                                                 styles.push( 'width:' + info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) );\r
183                                         }\r
184                                         styles = styles.join( ';' );\r
185                                         if ( styles )\r
186                                                 table.$.style.cssText = styles;\r
187                                         else\r
188                                                 table.removeAttribute( 'style' );\r
189                                 }\r
190 \r
191                                 // Insert the table element if we're creating one.\r
192                                 if ( !this._.selectedElement )\r
193                                         editor.insertElement( table );\r
194 \r
195                                 return true;\r
196                         },\r
197                         contents : [\r
198                                 {\r
199                                         id : 'info',\r
200                                         label : editor.lang.table.title,\r
201                                         elements :\r
202                                         [\r
203                                                 {\r
204                                                         type : 'hbox',\r
205                                                         widths : [ null, null ],\r
206                                                         styles : [ 'vertical-align:top' ],\r
207                                                         children :\r
208                                                         [\r
209                                                                 {\r
210                                                                         type : 'vbox',\r
211                                                                         padding : 0,\r
212                                                                         children :\r
213                                                                         [\r
214                                                                                 {\r
215                                                                                         type : 'text',\r
216                                                                                         id : 'txtRows',\r
217                                                                                         'default' : 3,\r
218                                                                                         label : editor.lang.table.rows,\r
219                                                                                         style : 'width:5em',\r
220                                                                                         validate : function()\r
221                                                                                         {\r
222                                                                                                 var pass = true,\r
223                                                                                                         value = this.getValue();\r
224                                                                                                 pass = pass && CKEDITOR.dialog.validate.integer()( value )\r
225                                                                                                         && value > 0;\r
226                                                                                                 if ( !pass )\r
227                                                                                                 {\r
228                                                                                                         alert( editor.lang.table.invalidRows );\r
229                                                                                                         this.select();\r
230                                                                                                 }\r
231                                                                                                 return pass;\r
232                                                                                         },\r
233                                                                                         setup : function( selectedElement )\r
234                                                                                         {\r
235                                                                                                 this.setValue( selectedElement.$.rows.length );\r
236                                                                                         },\r
237                                                                                         commit : commitValue\r
238                                                                                 },\r
239                                                                                 {\r
240                                                                                         type : 'text',\r
241                                                                                         id : 'txtCols',\r
242                                                                                         'default' : 2,\r
243                                                                                         label : editor.lang.table.columns,\r
244                                                                                         style : 'width:5em',\r
245                                                                                         validate : function()\r
246                                                                                         {\r
247                                                                                                 var pass = true,\r
248                                                                                                         value = this.getValue();\r
249                                                                                                 pass = pass && CKEDITOR.dialog.validate.integer()( value )\r
250                                                                                                         && value > 0;\r
251                                                                                                 if ( !pass )\r
252                                                                                                 {\r
253                                                                                                         alert( editor.lang.table.invalidCols );\r
254                                                                                                         this.select();\r
255                                                                                                 }\r
256                                                                                                 return pass;\r
257                                                                                         },\r
258                                                                                         setup : function( selectedTable )\r
259                                                                                         {\r
260                                                                                                 this.setValue( selectedTable.$.rows[0].cells.length);\r
261                                                                                         },\r
262                                                                                         commit : commitValue\r
263                                                                                 },\r
264                                                                                 {\r
265                                                                                         type : 'html',\r
266                                                                                         html : '&nbsp;'\r
267                                                                                 },\r
268                                                                                 {\r
269                                                                                         type : 'select',\r
270                                                                                         id : 'selHeaders',\r
271                                                                                         'default' : '',\r
272                                                                                         label : editor.lang.table.headers,\r
273                                                                                         items :\r
274                                                                                         [\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
279                                                                                         ],\r
280                                                                                         setup : function( selectedTable )\r
281                                                                                         {\r
282                                                                                                 // Fill in the headers field.\r
283                                                                                                 var dialog = this.getDialog();\r
284                                                                                                 dialog.hasColumnHeaders = true;\r
285 \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
288                                                                                                 {\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
291                                                                                                         {\r
292                                                                                                                 dialog.hasColumnHeaders = false;\r
293                                                                                                                 break;\r
294                                                                                                         }\r
295                                                                                                 }\r
296 \r
297                                                                                                 // Check if the table contains <thead>.\r
298                                                                                                 if ( ( selectedTable.$.tHead !== null) )\r
299                                                                                                         this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );\r
300                                                                                                 else\r
301                                                                                                         this.setValue( dialog.hasColumnHeaders ? 'col' : '' );\r
302                                                                                         },\r
303                                                                                         commit : commitValue\r
304                                                                                 },\r
305                                                                                 {\r
306                                                                                         type : 'text',\r
307                                                                                         id : 'txtBorder',\r
308                                                                                         'default' : 1,\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
313                                                                                         {\r
314                                                                                                 this.setValue( selectedTable.getAttribute( 'border' ) || '' );\r
315                                                                                         },\r
316                                                                                         commit : function( data, selectedTable )\r
317                                                                                         {\r
318                                                                                                 if ( this.getValue() )\r
319                                                                                                         selectedTable.setAttribute( 'border', this.getValue() );\r
320                                                                                                 else\r
321                                                                                                         selectedTable.removeAttribute( 'border' );\r
322                                                                                         }\r
323                                                                                 },\r
324                                                                                 {\r
325                                                                                         id : 'cmbAlign',\r
326                                                                                         type : 'select',\r
327                                                                                         'default' : '',\r
328                                                                                         label : editor.lang.table.align,\r
329                                                                                         items :\r
330                                                                                         [\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
335                                                                                         ],\r
336                                                                                         setup : function( selectedTable )\r
337                                                                                         {\r
338                                                                                                 this.setValue( selectedTable.getAttribute( 'align' ) || '' );\r
339                                                                                         },\r
340                                                                                         commit : function( data, selectedTable )\r
341                                                                                         {\r
342                                                                                                 if ( this.getValue() )\r
343                                                                                                         selectedTable.setAttribute( 'align', this.getValue() );\r
344                                                                                                 else\r
345                                                                                                         selectedTable.removeAttribute( 'align' );\r
346                                                                                         }\r
347                                                                                 }\r
348                                                                         ]\r
349                                                                 },\r
350                                                                 {\r
351                                                                         type : 'vbox',\r
352                                                                         padding : 0,\r
353                                                                         children :\r
354                                                                         [\r
355                                                                                 {\r
356                                                                                         type : 'hbox',\r
357                                                                                         widths : [ '5em' ],\r
358                                                                                         children :\r
359                                                                                         [\r
360                                                                                                 {\r
361                                                                                                         type : 'text',\r
362                                                                                                         id : 'txtWidth',\r
363                                                                                                         style : 'width:5em',\r
364                                                                                                         label : editor.lang.table.width,\r
365                                                                                                         'default' : 200,\r
366                                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ),\r
367                                                                                                         setup : function( selectedTable )\r
368                                                                                                         {\r
369                                                                                                                 var widthMatch = widthPattern.exec( selectedTable.$.style.width );\r
370                                                                                                                 if ( widthMatch )\r
371                                                                                                                         this.setValue( widthMatch[1] );\r
372                                                                                                         },\r
373                                                                                                         commit : commitValue\r
374                                                                                                 },\r
375                                                                                                 {\r
376                                                                                                         id : 'cmbWidthType',\r
377                                                                                                         type : 'select',\r
378                                                                                                         label : '&nbsp;',\r
379                                                                                                         'default' : 'pixels',\r
380                                                                                                         items :\r
381                                                                                                         [\r
382                                                                                                                 [ editor.lang.table.widthPx , 'pixels'],\r
383                                                                                                                 [ editor.lang.table.widthPc , 'percents']\r
384                                                                                                         ],\r
385                                                                                                         setup : function( selectedTable )\r
386                                                                                                         {\r
387                                                                                                                 var widthMatch = widthPattern.exec( selectedTable.$.style.width );\r
388                                                                                                                 if ( widthMatch )\r
389                                                                                                                         this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' );\r
390                                                                                                         },\r
391                                                                                                         commit : commitValue\r
392                                                                                                 }\r
393                                                                                         ]\r
394                                                                                 },\r
395                                                                                 {\r
396                                                                                         type : 'hbox',\r
397                                                                                         widths : [ '5em' ],\r
398                                                                                         children :\r
399                                                                                         [\r
400                                                                                                 {\r
401                                                                                                         type : 'text',\r
402                                                                                                         id : 'txtHeight',\r
403                                                                                                         style : 'width:5em',\r
404                                                                                                         label : editor.lang.table.height,\r
405                                                                                                         'default' : '',\r
406                                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ),\r
407                                                                                                         setup : function( selectedTable )\r
408                                                                                                         {\r
409                                                                                                                 var heightMatch = heightPattern.exec( selectedTable.$.style.height );\r
410                                                                                                                 if ( heightMatch )\r
411                                                                                                                         this.setValue( heightMatch[1] );\r
412                                                                                                         },\r
413                                                                                                         commit : commitValue\r
414                                                                                                 },\r
415                                                                                                 {\r
416                                                                                                         type : 'html',\r
417                                                                                                         html : '<br />' + editor.lang.table.widthPx\r
418                                                                                                 }\r
419                                                                                         ]\r
420                                                                                 },\r
421                                                                                 {\r
422                                                                                         type : 'html',\r
423                                                                                         html : '&nbsp;'\r
424                                                                                 },\r
425                                                                                 {\r
426                                                                                         type : 'text',\r
427                                                                                         id : 'txtCellSpace',\r
428                                                                                         style : 'width:3em',\r
429                                                                                         label : editor.lang.table.cellSpace,\r
430                                                                                         'default' : 1,\r
431                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ),\r
432                                                                                         setup : function( selectedTable )\r
433                                                                                         {\r
434                                                                                                 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );\r
435                                                                                         },\r
436                                                                                         commit : function( data, selectedTable )\r
437                                                                                         {\r
438                                                                                                 if ( this.getValue() )\r
439                                                                                                         selectedTable.setAttribute( 'cellSpacing', this.getValue() );\r
440                                                                                                 else\r
441                                                                                                         selectedTable.removeAttribute( 'cellSpacing' );\r
442                                                                                         }\r
443                                                                                 },\r
444                                                                                 {\r
445                                                                                         type : 'text',\r
446                                                                                         id : 'txtCellPad',\r
447                                                                                         style : 'width:3em',\r
448                                                                                         label : editor.lang.table.cellPad,\r
449                                                                                         'default' : 1,\r
450                                                                                         validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ),\r
451                                                                                         setup : function( selectedTable )\r
452                                                                                         {\r
453                                                                                                 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );\r
454                                                                                         },\r
455                                                                                         commit : function( data, selectedTable )\r
456                                                                                         {\r
457                                                                                                 if ( this.getValue() )\r
458                                                                                                         selectedTable.setAttribute( 'cellPadding', this.getValue() );\r
459                                                                                                 else\r
460                                                                                                         selectedTable.removeAttribute( 'cellPadding' );\r
461                                                                                         }\r
462                                                                                 }\r
463                                                                         ]\r
464                                                                 }\r
465                                                         ]\r
466                                                 },\r
467                                                 {\r
468                                                         type : 'html',\r
469                                                         align : 'right',\r
470                                                         html : ''\r
471                                                 },\r
472                                                 {\r
473                                                         type : 'vbox',\r
474                                                         padding : 0,\r
475                                                         children :\r
476                                                         [\r
477                                                                 {\r
478                                                                         type : 'text',\r
479                                                                         id : 'txtCaption',\r
480                                                                         label : editor.lang.table.caption,\r
481                                                                         setup : function( selectedTable )\r
482                                                                         {\r
483                                                                                 var nodeList = selectedTable.getElementsByTag( 'caption' );\r
484                                                                                 if ( nodeList.count() > 0 )\r
485                                                                                 {\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
490                                                                                 }\r
491                                                                         },\r
492                                                                         commit : function( data, table )\r
493                                                                         {\r
494                                                                                 var caption = this.getValue(),\r
495                                                                                         captionElement = table.getElementsByTag( 'caption' );\r
496                                                                                 if ( caption )\r
497                                                                                 {\r
498                                                                                         if ( captionElement.count() > 0 )\r
499                                                                                         {\r
500                                                                                                 captionElement = captionElement.getItem( 0 );\r
501                                                                                                 captionElement.setHtml( '' );\r
502                                                                                         }\r
503                                                                                         else\r
504                                                                                         {\r
505                                                                                                 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );\r
506                                                                                                 if ( table.getChildCount() )\r
507                                                                                                         captionElement.insertBefore( table.getFirst() );\r
508                                                                                                 else\r
509                                                                                                         captionElement.appendTo( table );\r
510                                                                                         }\r
511                                                                                         captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );\r
512                                                                                 }\r
513                                                                                 else if ( captionElement.count() > 0 )\r
514                                                                                 {\r
515                                                                                         for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )\r
516                                                                                                 captionElement.getItem( i ).remove();\r
517                                                                                 }\r
518                                                                         }\r
519                                                                 },\r
520                                                                 {\r
521                                                                         type : 'text',\r
522                                                                         id : 'txtSummary',\r
523                                                                         label : editor.lang.table.summary,\r
524                                                                         setup : function( selectedTable )\r
525                                                                         {\r
526                                                                                 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );\r
527                                                                         },\r
528                                                                         commit : function( data, selectedTable )\r
529                                                                         {\r
530                                                                                 if ( this.getValue() )\r
531                                                                                         selectedTable.setAttribute( 'summary', this.getValue() );\r
532                                                                         }\r
533                                                                 }\r
534                                                         ]\r
535                                                 }\r
536                                         ]\r
537                                 }\r
538                         ]\r
539                 };\r
540         }\r
541 \r
542         CKEDITOR.dialog.add( 'table', function( editor )\r
543                 {\r
544                         return tableDialog( editor, 'table' );\r
545                 } );\r
546         CKEDITOR.dialog.add( 'tableProperties', function( editor )\r
547                 {\r
548                         return tableDialog( editor, 'tableProperties' );\r
549                 } );\r
550 })();\r