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