JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
1491a24c8b1efe3901628c6147267bc1609b1103
[ckeditor.git] / _source / plugins / tabletools / dialogs / tableCell.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 CKEDITOR.dialog.add( 'cellProperties', function( editor )\r
7         {\r
8                 var langTable = editor.lang.table;\r
9                 var langCell = langTable.cell;\r
10                 var langCommon = editor.lang.common;\r
11                 var validate = CKEDITOR.dialog.validate;\r
12                 var widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/,\r
13                         heightPattern = /^(\d+(?:\.\d+)?)px$/;\r
14                 var bind = CKEDITOR.tools.bind;\r
15 \r
16                 function spacer()\r
17                 {\r
18                         return { type : 'html', html : ' ' };\r
19                 }\r
20 \r
21                 /**\r
22                  *\r
23                  * @param dialogName\r
24                  * @param callback [ childDialog ]\r
25                  */\r
26                 function getDialogValue( dialogName, callback )\r
27                 {\r
28                         var onOk = function()\r
29                         {\r
30                                 releaseHandlers( this );\r
31                                 callback( this );\r
32                         };\r
33                         var onCancel = function()\r
34                         {\r
35                                 releaseHandlers( this );\r
36                         };\r
37                         var bindToDialog = function( dialog )\r
38                         {\r
39                                 dialog.on( 'ok', onOk );\r
40                                 dialog.on( 'cancel', onCancel );\r
41                         };\r
42                         var releaseHandlers = function( dialog )\r
43                         {\r
44                                 dialog.removeListener( 'ok', onOk );\r
45                                 dialog.removeListener( 'cancel', onCancel );\r
46                         };\r
47                         editor.execCommand( dialogName );\r
48                         if ( editor._.storedDialogs.colordialog )\r
49                                 bindToDialog( editor._.storedDialogs.colordialog );\r
50                         else\r
51                         {\r
52                                 CKEDITOR.on( 'dialogDefinition', function( e )\r
53                                 {\r
54                                         if ( e.data.name != dialogName )\r
55                                                 return;\r
56 \r
57                                         var definition = e.data.definition;\r
58 \r
59                                         e.removeListener();\r
60                                         definition.onLoad = CKEDITOR.tools.override( definition.onLoad, function( orginal )\r
61                                         {\r
62                                                 return function()\r
63                                                 {\r
64                                                         bindToDialog( this );\r
65                                                         definition.onLoad = orginal;\r
66                                                         if ( typeof orginal == 'function' )\r
67                                                                 orginal.call( this );\r
68                                                 };\r
69                                         } );\r
70                                 });\r
71                         }\r
72                 }\r
73 \r
74                 return {\r
75                         title : langCell.title,\r
76                         minWidth : CKEDITOR.env.ie && CKEDITOR.env.quirks ? 550 : 480,\r
77                         minHeight : CKEDITOR.env.ie ? ( CKEDITOR.env.quirks ? 180 : 150 ) : 140,\r
78                         contents : [\r
79                                 {\r
80                                         id : 'info',\r
81                                         label : langCell.title,\r
82                                         accessKey : 'I',\r
83                                         elements :\r
84                                         [\r
85                                                 {\r
86                                                         type : 'hbox',\r
87                                                         widths : [ '40%', '5%', '40%' ],\r
88                                                         children :\r
89                                                         [\r
90                                                                 {\r
91                                                                         type : 'vbox',\r
92                                                                         padding : 0,\r
93                                                                         children :\r
94                                                                         [\r
95                                                                                 {\r
96                                                                                         type : 'hbox',\r
97                                                                                         widths : [ '70%', '30%' ],\r
98                                                                                         children :\r
99                                                                                         [\r
100                                                                                                 {\r
101                                                                                                         type : 'text',\r
102                                                                                                         id : 'width',\r
103                                                                                                         label : langTable.width,\r
104                                                                                                         widths : [ '71%', '29%' ],\r
105                                                                                                         labelLayout : 'horizontal',\r
106                                                                                                         validate : validate[ 'number' ]( langCell.invalidWidth ),\r
107                                                                                                         setup : function( selectedCell )\r
108                                                                                                         {\r
109                                                                                                                 var widthMatch = widthPattern.exec( selectedCell.$.style.width );\r
110                                                                                                                 if ( widthMatch )\r
111                                                                                                                         this.setValue( widthMatch[1] );\r
112                                                                                                         },\r
113                                                                                                         commit : function( selectedCell )\r
114                                                                                                         {\r
115                                                                                                                 var unit = this.getDialog().getValueOf( 'info', 'widthType' );\r
116                                                                                                                 if ( this.getValue() !== '' )\r
117                                                                                                                         selectedCell.$.style.width = this.getValue() + unit;\r
118                                                                                                                 else\r
119                                                                                                                         selectedCell.$.style.width = '';\r
120                                                                                                         },\r
121                                                                                                         'default' : ''\r
122                                                                                                 },\r
123                                                                                                 {\r
124                                                                                                         type : 'select',\r
125                                                                                                         id : 'widthType',\r
126                                                                                                         labelLayout : 'horizontal',\r
127                                                                                                         widths : [ '0%', '100%' ],\r
128                                                                                                         label : '',\r
129                                                                                                         'default' : 'px',\r
130                                                                                                         items :\r
131                                                                                                         [\r
132                                                                                                                 [ langTable.widthPx, 'px' ],\r
133                                                                                                                 [ langTable.widthPc, '%' ]\r
134                                                                                                         ],\r
135                                                                                                         setup : function( selectedCell )\r
136                                                                                                         {\r
137                                                                                                                 var widthMatch = widthPattern.exec( selectedCell.$.style.width );\r
138                                                                                                                 if ( widthMatch )\r
139                                                                                                                         this.setValue( widthMatch[2] );\r
140                                                                                                         }\r
141                                                                                                 }\r
142                                                                                         ]\r
143                                                                                 },\r
144                                                                                 {\r
145                                                                                         type : 'hbox',\r
146                                                                                         widths : [ '70%', '30%' ],\r
147                                                                                         children :\r
148                                                                                         [\r
149                                                                                                 {\r
150                                                                                                         type : 'text',\r
151                                                                                                         id : 'height',\r
152                                                                                                         label : langTable.height,\r
153                                                                                                         'default' : '',\r
154                                                                                                         widths : [ '71%', '29%' ],\r
155                                                                                                         labelLayout : 'horizontal',\r
156                                                                                                         validate : validate[ 'number' ]( langCell.invalidHeight ),\r
157                                                                                                         setup : function( selectedCell )\r
158                                                                                                         {\r
159                                                                                                                 var heightMatch = heightPattern.exec( selectedCell.$.style.height );\r
160                                                                                                                 if ( heightMatch )\r
161                                                                                                                         this.setValue( heightMatch[1] );\r
162                                                                                                         },\r
163                                                                                                         commit : function( selectedCell )\r
164                                                                                                         {\r
165                                                                                                                 if ( this.getValue() !== '' )\r
166                                                                                                                         selectedCell.$.style.height = this.getValue() + 'px';\r
167                                                                                                                 else\r
168                                                                                                                         selectedCell.$.style.height = '';\r
169                                                                                                         }\r
170                                                                                                 },\r
171                                                                                                 {\r
172                                                                                                         type : 'html',\r
173                                                                                                         html : langTable.widthPx\r
174                                                                                                 }\r
175                                                                                         ]\r
176                                                                                 },\r
177                                                                                 spacer(),\r
178                                                                                 {\r
179                                                                                         type : 'select',\r
180                                                                                         id : 'wordWrap',\r
181                                                                                         labelLayout : 'horizontal',\r
182                                                                                         label : langCell.wordWrap,\r
183                                                                                         widths : [ '50%', '50%' ],\r
184                                                                                         'default' : 'yes',\r
185                                                                                         items :\r
186                                                                                         [\r
187                                                                                                 [ langCell.yes, 'yes' ],\r
188                                                                                                 [ langCell.no, 'no' ]\r
189                                                                                         ],\r
190                                                                                         commit : function( selectedCell )\r
191                                                                                         {\r
192                                                                                                 if ( this.getValue() == 'no' )\r
193                                                                                                         selectedCell.setAttribute( 'noWrap', 'nowrap' );\r
194                                                                                                 else\r
195                                                                                                         selectedCell.removeAttribute( 'noWrap' );\r
196                                                                                         }\r
197                                                                                 },\r
198                                                                                 spacer(),\r
199                                                                                 {\r
200                                                                                         type : 'select',\r
201                                                                                         id : 'hAlign',\r
202                                                                                         labelLayout : 'horizontal',\r
203                                                                                         label : langCell.hAlign,\r
204                                                                                         widths : [ '50%', '50%' ],\r
205                                                                                         'default' : '',\r
206                                                                                         items :\r
207                                                                                         [\r
208                                                                                                 [ langCommon.notSet, '' ],\r
209                                                                                                 [ langTable.alignLeft, 'left' ],\r
210                                                                                                 [ langTable.alignCenter, 'center' ],\r
211                                                                                                 [ langTable.alignRight, 'right' ]\r
212                                                                                         ],\r
213                                                                                         setup : function( selectedCell )\r
214                                                                                         {\r
215                                                                                                 this.setValue( selectedCell.getAttribute( 'align' ) || '' );\r
216                                                                                         },\r
217                                                                                         commit : function( selectedCell )\r
218                                                                                         {\r
219                                                                                                 if ( this.getValue() )\r
220                                                                                                         selectedCell.setAttribute( 'align', this.getValue() );\r
221                                                                                                 else\r
222                                                                                                         selectedCell.removeAttribute( 'align' );\r
223                                                                                         }\r
224                                                                                 },\r
225                                                                                 {\r
226                                                                                         type : 'select',\r
227                                                                                         id : 'vAlign',\r
228                                                                                         labelLayout : 'horizontal',\r
229                                                                                         label : langCell.vAlign,\r
230                                                                                         widths : [ '50%', '50%' ],\r
231                                                                                         'default' : '',\r
232                                                                                         items :\r
233                                                                                         [\r
234                                                                                                 [ langCommon.notSet, '' ],\r
235                                                                                                 [ langCell.alignTop, 'top' ],\r
236                                                                                                 [ langCell.alignMiddle, 'middle' ],\r
237                                                                                                 [ langCell.alignBottom, 'bottom' ],\r
238                                                                                                 [ langCell.alignBaseline, 'baseline' ]\r
239                                                                                         ],\r
240                                                                                         setup : function( selectedCell )\r
241                                                                                         {\r
242                                                                                                 this.setValue( selectedCell.getAttribute( 'vAlign' ) || '' );\r
243                                                                                         },\r
244                                                                                         commit : function( selectedCell )\r
245                                                                                         {\r
246                                                                                                 if ( this.getValue() )\r
247                                                                                                         selectedCell.setAttribute( 'vAlign', this.getValue() );\r
248                                                                                                 else\r
249                                                                                                         selectedCell.removeAttribute( 'vAlign' );\r
250                                                                                         }\r
251                                                                                 }\r
252                                                                         ]\r
253                                                                 },\r
254                                                                 spacer(),\r
255                                                                 {\r
256                                                                         type : 'vbox',\r
257                                                                         padding : 0,\r
258                                                                         children :\r
259                                                                         [\r
260                                                                                 {\r
261                                                                                         type : 'select',\r
262                                                                                         id : 'cellType',\r
263                                                                                         label : langCell.cellType,\r
264                                                                                         labelLayout : 'horizontal',\r
265                                                                                         widths : [ '50%', '50%' ],\r
266                                                                                         'default' : 'td',\r
267                                                                                         items :\r
268                                                                                         [\r
269                                                                                                 [ langCell.data, 'td' ],\r
270                                                                                                 [ langCell.header, 'th' ]\r
271                                                                                         ],\r
272                                                                                         setup : function( selectedCell )\r
273                                                                                         {\r
274                                                                                                 this.setValue( selectedCell.getName() );\r
275                                                                                         },\r
276                                                                                         commit : function( selectedCell )\r
277                                                                                         {\r
278                                                                                                 selectedCell.renameNode( this.getValue() );\r
279                                                                                         }\r
280                                                                                 },\r
281                                                                                 spacer(),\r
282                                                                                 {\r
283                                                                                         type : 'text',\r
284                                                                                         id : 'rowSpan',\r
285                                                                                         label : langCell.rowSpan,\r
286                                                                                         labelLayout : 'horizontal',\r
287                                                                                         widths : [ '50%', '50%' ],\r
288                                                                                         'default' : '',\r
289                                                                                         validate : validate.integer( langCell.invalidRowSpan ),\r
290                                                                                         setup : function( selectedCell )\r
291                                                                                         {\r
292                                                                                                 this.setValue( selectedCell.getAttribute( 'rowSpan' ) || '' );\r
293                                                                                         },\r
294                                                                                         commit : function( selectedCell )\r
295                                                                                         {\r
296                                                                                                 if ( this.getValue() )\r
297                                                                                                         selectedCell.setAttribute( 'rowSpan', this.getValue() );\r
298                                                                                                 else\r
299                                                                                                         selectedCell.removeAttribute( 'rowSpan' );\r
300                                                                                         }\r
301                                                                                 },\r
302                                                                                 {\r
303                                                                                         type : 'text',\r
304                                                                                         id : 'colSpan',\r
305                                                                                         label : langCell.colSpan,\r
306                                                                                         labelLayout : 'horizontal',\r
307                                                                                         widths : [ '50%', '50%' ],\r
308                                                                                         'default' : '',\r
309                                                                                         validate : validate.integer( langCell.invalidColSpan ),\r
310                                                                                         setup : function( selectedCell )\r
311                                                                                         {\r
312                                                                                                 this.setValue( selectedCell.getAttribute( 'colSpan' ) || '' );\r
313                                                                                         },\r
314                                                                                         commit : function( selectedCell )\r
315                                                                                         {\r
316                                                                                                 if ( this.getValue() )\r
317                                                                                                         selectedCell.setAttribute( 'colSpan', this.getValue() );\r
318                                                                                                 else\r
319                                                                                                         selectedCell.removeAttribute( 'colSpan' );\r
320                                                                                         }\r
321                                                                                 },\r
322                                                                                 spacer(),\r
323                                                                                 {\r
324                                                                                         type : 'hbox',\r
325                                                                                         padding : 0,\r
326                                                                                         widths : [ '80%', '20%' ],\r
327                                                                                         children :\r
328                                                                                         [\r
329                                                                                                 {\r
330                                                                                                         type : 'text',\r
331                                                                                                         id : 'bgColor',\r
332                                                                                                         label : langCell.bgColor,\r
333                                                                                                         labelLayout : 'horizontal',\r
334                                                                                                         widths : [ '70%', '30%' ],\r
335                                                                                                         'default' : '',\r
336                                                                                                         setup : function( selectedCell )\r
337                                                                                                         {\r
338                                                                                                                 this.setValue( selectedCell.getAttribute( 'bgColor' ) || '' );\r
339                                                                                                         },\r
340                                                                                                         commit : function( selectedCell )\r
341                                                                                                         {\r
342                                                                                                                 if ( this.getValue() )\r
343                                                                                                                         selectedCell.setAttribute( 'bgColor', this.getValue() );\r
344                                                                                                                 else\r
345                                                                                                                         selectedCell.removeAttribute( 'bgColor' );\r
346                                                                                                         }\r
347                                                                                                 },\r
348                                                                                                 {\r
349                                                                                                         type : 'button',\r
350                                                                                                         id : 'bgColorChoose',\r
351                                                                                                         label : langCell.chooseColor,\r
352                                                                                                         style : 'margin-left: 10px',\r
353                                                                                                         onClick : function()\r
354                                                                                                         {\r
355                                                                                                                 var self = this;\r
356                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
357                                                                                                                 {\r
358                                                                                                                         self.getDialog().getContentElement( 'info', 'bgColor' ).setValue(\r
359                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
360                                                                                                                         );\r
361                                                                                                                 } );\r
362                                                                                                         }\r
363                                                                                                 }\r
364                                                                                         ]\r
365                                                                                 },\r
366                                                                                 spacer(),\r
367                                                                                 {\r
368                                                                                         type : 'hbox',\r
369                                                                                         padding : 0,\r
370                                                                                         widths : [ '80%', '20%' ],\r
371                                                                                         children :\r
372                                                                                         [\r
373                                                                                                 {\r
374                                                                                                         type : 'text',\r
375                                                                                                         id : 'borderColor',\r
376                                                                                                         label : langCell.borderColor,\r
377                                                                                                         labelLayout : 'horizontal',\r
378                                                                                                         widths : [ '70%', '30%' ],\r
379                                                                                                         'default' : '',\r
380                                                                                                         setup : function( selectedCell )\r
381                                                                                                         {\r
382                                                                                                                 this.setValue( selectedCell.getStyle( 'border-color' ) || '' );\r
383                                                                                                         },\r
384                                                                                                         commit : function( selectedCell )\r
385                                                                                                         {\r
386                                                                                                                 if ( this.getValue() )\r
387                                                                                                                         selectedCell.setStyle( 'border-color', this.getValue() );\r
388                                                                                                                 else\r
389                                                                                                                         selectedCell.removeStyle( 'border-color' );\r
390                                                                                                         }\r
391                                                                                                 },\r
392                                                                                                 {\r
393                                                                                                         type : 'button',\r
394                                                                                                         id : 'borderColorChoose',\r
395                                                                                                         label : langCell.chooseColor,\r
396                                                                                                         style : 'margin-left: 10px',\r
397                                                                                                         onClick : function()\r
398                                                                                                         {\r
399                                                                                                                 var self = this;\r
400                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
401                                                                                                                 {\r
402                                                                                                                         self.getDialog().getContentElement( 'info', 'borderColor' ).setValue(\r
403                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
404                                                                                                                         );\r
405                                                                                                                 } );\r
406                                                                                                         }\r
407                                                                                                 }\r
408                                                                                         ]\r
409                                                                                 }\r
410                                                                         ]\r
411                                                                 }\r
412                                                         ]\r
413                                                 }\r
414                                         ]\r
415                                 }\r
416                         ],\r
417                         onShow : function()\r
418                         {\r
419                                 this.cells = CKEDITOR.plugins.tabletools.getSelectedCells(\r
420                                         this._.editor.getSelection() );\r
421                                 this.setupContent( this.cells[ 0 ] );\r
422                         },\r
423                         onOk : function()\r
424                         {\r
425                                 var cells = this.cells;\r
426                                 for ( var i = 0 ; i < cells.length ; i++ )\r
427                                         this.commitContent( cells[ i ] );\r
428                         }\r
429                 };\r
430         } );\r