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