JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1.1
[ckeditor.git] / _source / plugins / tabletools / dialogs / tableCell.js
1 /*\r
2 Copyright (c) 2003-2010, 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( element )\r
108                                                                                                         {\r
109                                                                                                                 var widthAttr = parseInt( element.getAttribute( 'width' ), 10 ),\r
110                                                                                                                                 widthStyle = parseInt( element.getStyle( 'width' ), 10 );\r
111 \r
112                                                                                                                 !isNaN( widthAttr ) && this.setValue( widthAttr );\r
113                                                                                                                 !isNaN( widthStyle ) && this.setValue( widthStyle );\r
114                                                                                                         },\r
115                                                                                                         commit : function( element )\r
116                                                                                                         {\r
117                                                                                                                 var value = parseInt( this.getValue(), 10 ),\r
118                                                                                                                                 unit = this.getDialog().getValueOf( 'info', 'widthType' );\r
119 \r
120                                                                                                                 if ( !isNaN( value ) )\r
121                                                                                                                         element.setStyle( 'width', value + unit );\r
122                                                                                                                 else\r
123                                                                                                                         element.removeStyle( 'width' );\r
124 \r
125                                                                                                                 element.removeAttribute( 'width' );\r
126                                                                                                         },\r
127                                                                                                         'default' : ''\r
128                                                                                                 },\r
129                                                                                                 {\r
130                                                                                                         type : 'select',\r
131                                                                                                         id : 'widthType',\r
132                                                                                                         labelLayout : 'horizontal',\r
133                                                                                                         widths : [ '0%', '100%' ],\r
134                                                                                                         label : '',\r
135                                                                                                         'default' : 'px',\r
136                                                                                                         items :\r
137                                                                                                         [\r
138                                                                                                                 [ langTable.widthPx, 'px' ],\r
139                                                                                                                 [ langTable.widthPc, '%' ]\r
140                                                                                                         ],\r
141                                                                                                         setup : function( selectedCell )\r
142                                                                                                         {\r
143                                                                                                                 var widthMatch = widthPattern.exec( selectedCell.$.style.width );\r
144                                                                                                                 if ( widthMatch )\r
145                                                                                                                         this.setValue( widthMatch[2] );\r
146                                                                                                         }\r
147                                                                                                 }\r
148                                                                                         ]\r
149                                                                                 },\r
150                                                                                 {\r
151                                                                                         type : 'hbox',\r
152                                                                                         widths : [ '70%', '30%' ],\r
153                                                                                         children :\r
154                                                                                         [\r
155                                                                                                 {\r
156                                                                                                         type : 'text',\r
157                                                                                                         id : 'height',\r
158                                                                                                         label : langTable.height,\r
159                                                                                                         'default' : '',\r
160                                                                                                         widths : [ '71%', '29%' ],\r
161                                                                                                         labelLayout : 'horizontal',\r
162                                                                                                         validate : validate[ 'number' ]( langCell.invalidHeight ),\r
163                                                                                                         setup : function( element )\r
164                                                                                                         {\r
165                                                                                                                 var heightAttr = parseInt( element.getAttribute( 'height' ), 10 ),\r
166                                                                                                                                 heightStyle = parseInt( element.getStyle( 'height' ), 10 );\r
167 \r
168                                                                                                                 !isNaN( heightAttr ) && this.setValue( heightAttr );\r
169                                                                                                                 !isNaN( heightStyle ) && this.setValue( heightStyle );\r
170                                                                                                         },\r
171                                                                                                         commit : function( element )\r
172                                                                                                         {\r
173                                                                                                                 var value = parseInt( this.getValue(), 10 );\r
174 \r
175                                                                                                                 if ( !isNaN( value ) )\r
176                                                                                                                         element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );\r
177                                                                                                                 else\r
178                                                                                                                         element.removeStyle( 'height' );\r
179 \r
180                                                                                                                 element.removeAttribute( 'height' );\r
181                                                                                                         }\r
182                                                                                                 },\r
183                                                                                                 {\r
184                                                                                                         type : 'html',\r
185                                                                                                         html : langTable.widthPx\r
186                                                                                                 }\r
187                                                                                         ]\r
188                                                                                 },\r
189                                                                                 spacer(),\r
190                                                                                 {\r
191                                                                                         type : 'select',\r
192                                                                                         id : 'wordWrap',\r
193                                                                                         labelLayout : 'horizontal',\r
194                                                                                         label : langCell.wordWrap,\r
195                                                                                         widths : [ '50%', '50%' ],\r
196                                                                                         'default' : 'yes',\r
197                                                                                         items :\r
198                                                                                         [\r
199                                                                                                 [ langCell.yes, 'yes' ],\r
200                                                                                                 [ langCell.no, 'no' ]\r
201                                                                                         ],\r
202                                                                                         setup : function( element )\r
203                                                                                         {\r
204                                                                                                 var wordWrapAttr = element.getAttribute( 'noWrap' ),\r
205                                                                                                                 wordWrapStyle = element.getStyle( 'white-space' );\r
206 \r
207                                                                                                 if ( wordWrapStyle == 'nowrap' || wordWrapAttr )\r
208                                                                                                         this.setValue( 'no' );\r
209                                                                                         },\r
210                                                                                         commit : function( element )\r
211                                                                                         {\r
212                                                                                                 if ( this.getValue() == 'no' )\r
213                                                                                                         element.setStyle( 'white-space', 'nowrap' );\r
214                                                                                                 else\r
215                                                                                                         element.removeStyle( 'white-space' );\r
216 \r
217                                                                                                 element.removeAttribute( 'noWrap' );\r
218                                                                                         }\r
219                                                                                 },\r
220                                                                                 spacer(),\r
221                                                                                 {\r
222                                                                                         type : 'select',\r
223                                                                                         id : 'hAlign',\r
224                                                                                         labelLayout : 'horizontal',\r
225                                                                                         label : langCell.hAlign,\r
226                                                                                         widths : [ '50%', '50%' ],\r
227                                                                                         'default' : '',\r
228                                                                                         items :\r
229                                                                                         [\r
230                                                                                                 [ langCommon.notSet, '' ],\r
231                                                                                                 [ langTable.alignLeft, 'left' ],\r
232                                                                                                 [ langTable.alignCenter, 'center' ],\r
233                                                                                                 [ langTable.alignRight, 'right' ]\r
234                                                                                         ],\r
235                                                                                         setup : function( element )\r
236                                                                                         {\r
237                                                                                                 var alignAttr = element.getAttribute( 'align' ),\r
238                                                                                                                 textAlignStyle = element.getStyle( 'text-align');\r
239 \r
240                                                                                                 this.setValue(  textAlignStyle || alignAttr || '' );\r
241                                                                                         },\r
242                                                                                         commit : function( selectedCell )\r
243                                                                                         {\r
244                                                                                                 var value = this.getValue();\r
245 \r
246                                                                                                 if ( value )\r
247                                                                                                         selectedCell.setStyle( 'text-align', value );\r
248                                                                                                 else\r
249                                                                                                         selectedCell.removeStyle( 'text-align' );\r
250 \r
251                                                                                                 selectedCell.removeAttribute( 'align' );\r
252                                                                                         }\r
253                                                                                 },\r
254                                                                                 {\r
255                                                                                         type : 'select',\r
256                                                                                         id : 'vAlign',\r
257                                                                                         labelLayout : 'horizontal',\r
258                                                                                         label : langCell.vAlign,\r
259                                                                                         widths : [ '50%', '50%' ],\r
260                                                                                         'default' : '',\r
261                                                                                         items :\r
262                                                                                         [\r
263                                                                                                 [ langCommon.notSet, '' ],\r
264                                                                                                 [ langCell.alignTop, 'top' ],\r
265                                                                                                 [ langCell.alignMiddle, 'middle' ],\r
266                                                                                                 [ langCell.alignBottom, 'bottom' ],\r
267                                                                                                 [ langCell.alignBaseline, 'baseline' ]\r
268                                                                                         ],\r
269                                                                                         setup : function( element )\r
270                                                                                         {\r
271                                                                                                 var vAlignAttr = element.getAttribute( 'vAlign' ),\r
272                                                                                                                 vAlignStyle = element.getStyle( 'vertical-align' );\r
273 \r
274                                                                                                 switch( vAlignStyle )\r
275                                                                                                 {\r
276                                                                                                         // Ignore all other unrelated style values..\r
277                                                                                                         case 'top':\r
278                                                                                                         case 'middle':\r
279                                                                                                         case 'bottom':\r
280                                                                                                         case 'baseline':\r
281                                                                                                                 break;\r
282                                                                                                         default:\r
283                                                                                                                 vAlignStyle = '';\r
284                                                                                                 }\r
285 \r
286                                                                                                 this.setValue( vAlignStyle || vAlignAttr || '' );\r
287                                                                                         },\r
288                                                                                         commit : function( element )\r
289                                                                                         {\r
290                                                                                                 var value = this.getValue();\r
291 \r
292                                                                                                 if ( value )\r
293                                                                                                         element.setStyle( 'vertical-align', value );\r
294                                                                                                 else\r
295                                                                                                         element.removeStyle( 'vertical-align' );\r
296 \r
297                                                                                                 element.removeAttribute( 'vAlign' );\r
298                                                                                         }\r
299                                                                                 }\r
300                                                                         ]\r
301                                                                 },\r
302                                                                 spacer(),\r
303                                                                 {\r
304                                                                         type : 'vbox',\r
305                                                                         padding : 0,\r
306                                                                         children :\r
307                                                                         [\r
308                                                                                 {\r
309                                                                                         type : 'select',\r
310                                                                                         id : 'cellType',\r
311                                                                                         label : langCell.cellType,\r
312                                                                                         labelLayout : 'horizontal',\r
313                                                                                         widths : [ '50%', '50%' ],\r
314                                                                                         'default' : 'td',\r
315                                                                                         items :\r
316                                                                                         [\r
317                                                                                                 [ langCell.data, 'td' ],\r
318                                                                                                 [ langCell.header, 'th' ]\r
319                                                                                         ],\r
320                                                                                         setup : function( selectedCell )\r
321                                                                                         {\r
322                                                                                                 this.setValue( selectedCell.getName() );\r
323                                                                                         },\r
324                                                                                         commit : function( selectedCell )\r
325                                                                                         {\r
326                                                                                                 selectedCell.renameNode( this.getValue() );\r
327                                                                                         }\r
328                                                                                 },\r
329                                                                                 spacer(),\r
330                                                                                 {\r
331                                                                                         type : 'text',\r
332                                                                                         id : 'rowSpan',\r
333                                                                                         label : langCell.rowSpan,\r
334                                                                                         labelLayout : 'horizontal',\r
335                                                                                         widths : [ '50%', '50%' ],\r
336                                                                                         'default' : '',\r
337                                                                                         validate : validate.integer( langCell.invalidRowSpan ),\r
338                                                                                         setup : function( selectedCell )\r
339                                                                                         {\r
340                                                                                                 var attrVal = parseInt( selectedCell.getAttribute( 'rowSpan' ), 10 );\r
341                                                                                                 if ( attrVal && attrVal  != 1 )\r
342                                                                                                         this.setValue(  attrVal );\r
343                                                                                         },\r
344                                                                                         commit : function( selectedCell )\r
345                                                                                         {\r
346                                                                                                 var value = parseInt( this.getValue(), 10 );\r
347                                                                                                 if ( value && value != 1 )\r
348                                                                                                         selectedCell.setAttribute( 'rowSpan', this.getValue() );\r
349                                                                                                 else\r
350                                                                                                         selectedCell.removeAttribute( 'rowSpan' );\r
351                                                                                         }\r
352                                                                                 },\r
353                                                                                 {\r
354                                                                                         type : 'text',\r
355                                                                                         id : 'colSpan',\r
356                                                                                         label : langCell.colSpan,\r
357                                                                                         labelLayout : 'horizontal',\r
358                                                                                         widths : [ '50%', '50%' ],\r
359                                                                                         'default' : '',\r
360                                                                                         validate : validate.integer( langCell.invalidColSpan ),\r
361                                                                                         setup : function( element )\r
362                                                                                         {\r
363                                                                                                 var attrVal = parseInt( element.getAttribute( 'colSpan' ), 10 );\r
364                                                                                                 if ( attrVal && attrVal  != 1 )\r
365                                                                                                         this.setValue(  attrVal );\r
366                                                                                         },\r
367                                                                                         commit : function( selectedCell )\r
368                                                                                         {\r
369                                                                                                 var value = parseInt( this.getValue(), 10 );\r
370                                                                                                 if ( value && value != 1 )\r
371                                                                                                         selectedCell.setAttribute( 'colSpan', this.getValue() );\r
372                                                                                                 else\r
373                                                                                                         selectedCell.removeAttribute( 'colSpan' );\r
374                                                                                         }\r
375                                                                                 },\r
376                                                                                 spacer(),\r
377                                                                                 {\r
378                                                                                         type : 'hbox',\r
379                                                                                         padding : 0,\r
380                                                                                         widths : [ '80%', '20%' ],\r
381                                                                                         children :\r
382                                                                                         [\r
383                                                                                                 {\r
384                                                                                                         type : 'text',\r
385                                                                                                         id : 'bgColor',\r
386                                                                                                         label : langCell.bgColor,\r
387                                                                                                         labelLayout : 'horizontal',\r
388                                                                                                         widths : [ '70%', '30%' ],\r
389                                                                                                         'default' : '',\r
390                                                                                                         setup : function( element )\r
391                                                                                                         {\r
392                                                                                                                 var bgColorAttr = element.getAttribute( 'bgColor' ),\r
393                                                                                                                                 bgColorStyle = element.getStyle( 'background-color' );\r
394 \r
395                                                                                                                 this.setValue( bgColorStyle || bgColorAttr );\r
396                                                                                                         },\r
397                                                                                                         commit : function( selectedCell )\r
398                                                                                                         {\r
399                                                                                                                 var value = this.getValue();\r
400 \r
401                                                                                                                 if ( value )\r
402                                                                                                                         selectedCell.setStyle( 'background-color', this.getValue() );\r
403                                                                                                                 else\r
404                                                                                                                         selectedCell.removeStyle( 'background-color' );\r
405 \r
406                                                                                                                 selectedCell.removeAttribute( 'bgColor');\r
407                                                                                                         }\r
408                                                                                                 },\r
409                                                                                                 {\r
410                                                                                                         type : 'button',\r
411                                                                                                         id : 'bgColorChoose',\r
412                                                                                                         label : langCell.chooseColor,\r
413                                                                                                         style : 'margin-left: 10px',\r
414                                                                                                         onClick : function()\r
415                                                                                                         {\r
416                                                                                                                 var self = this;\r
417                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
418                                                                                                                 {\r
419                                                                                                                         self.getDialog().getContentElement( 'info', 'bgColor' ).setValue(\r
420                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
421                                                                                                                         );\r
422                                                                                                                 } );\r
423                                                                                                         }\r
424                                                                                                 }\r
425                                                                                         ]\r
426                                                                                 },\r
427                                                                                 spacer(),\r
428                                                                                 {\r
429                                                                                         type : 'hbox',\r
430                                                                                         padding : 0,\r
431                                                                                         widths : [ '80%', '20%' ],\r
432                                                                                         children :\r
433                                                                                         [\r
434                                                                                                 {\r
435                                                                                                         type : 'text',\r
436                                                                                                         id : 'borderColor',\r
437                                                                                                         label : langCell.borderColor,\r
438                                                                                                         labelLayout : 'horizontal',\r
439                                                                                                         widths : [ '70%', '30%' ],\r
440                                                                                                         'default' : '',\r
441                                                                                                         setup : function( element )\r
442                                                                                                         {\r
443                                                                                                                 var borderColorAttr = element.getAttribute( 'borderColor' ),\r
444                                                                                                                                 borderColorStyle = element.getStyle( 'border-color' );\r
445 \r
446                                                                                                                 this.setValue( borderColorStyle || borderColorAttr );\r
447                                                                                                         },\r
448                                                                                                         commit : function( selectedCell )\r
449                                                                                                         {\r
450                                                                                                                 var value = this.getValue();\r
451                                                                                                                 if ( value )\r
452                                                                                                                         selectedCell.setStyle( 'border-color', this.getValue() );\r
453                                                                                                                 else\r
454                                                                                                                         selectedCell.removeStyle( 'border-color' );\r
455 \r
456                                                                                                                 selectedCell.removeAttribute( 'borderColor');\r
457                                                                                                         }\r
458                                                                                                 },\r
459                                                                                                 {\r
460                                                                                                         type : 'button',\r
461                                                                                                         id : 'borderColorChoose',\r
462                                                                                                         label : langCell.chooseColor,\r
463                                                                                                         style : 'margin-left: 10px',\r
464                                                                                                         onClick : function()\r
465                                                                                                         {\r
466                                                                                                                 var self = this;\r
467                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
468                                                                                                                 {\r
469                                                                                                                         self.getDialog().getContentElement( 'info', 'borderColor' ).setValue(\r
470                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
471                                                                                                                         );\r
472                                                                                                                 } );\r
473                                                                                                         }\r
474                                                                                                 }\r
475                                                                                         ]\r
476                                                                                 }\r
477                                                                         ]\r
478                                                                 }\r
479                                                         ]\r
480                                                 }\r
481                                         ]\r
482                                 }\r
483                         ],\r
484                         onShow : function()\r
485                         {\r
486                                 this.cells = CKEDITOR.plugins.tabletools.getSelectedCells(\r
487                                         this._.editor.getSelection() );\r
488                                 this.setupContent( this.cells[ 0 ] );\r
489                         },\r
490                         onOk : function()\r
491                         {\r
492                                 var cells = this.cells;\r
493                                 for ( var i = 0 ; i < cells.length ; i++ )\r
494                                         this.commitContent( cells[ i ] );\r
495                         }\r
496                 };\r
497         } );\r