JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.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                         langCell = langTable.cell,\r
10                         langCommon = editor.lang.common,\r
11                         validate = CKEDITOR.dialog.validate,\r
12                         widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/,\r
13                         heightPattern = /^(\d+(?:\.\d+)?)px$/,\r
14                         bind = CKEDITOR.tools.bind,\r
15                         spacer = { type : 'html', html : ' ' };\r
16 \r
17                 /**\r
18                  *\r
19                  * @param dialogName\r
20                  * @param callback [ childDialog ]\r
21                  */\r
22                 function getDialogValue( dialogName, callback )\r
23                 {\r
24                         var onOk = function()\r
25                         {\r
26                                 releaseHandlers( this );\r
27                                 callback( this, this._.parentDialog );\r
28                                 this._.parentDialog.changeFocus( true );\r
29                         };\r
30                         var onCancel = function()\r
31                         {\r
32                                 releaseHandlers( this );\r
33                                 this._.parentDialog.changeFocus();\r
34                         };\r
35                         var releaseHandlers = function( dialog )\r
36                         {\r
37                                 dialog.removeListener( 'ok', onOk );\r
38                                 dialog.removeListener( 'cancel', onCancel );\r
39                         };\r
40                         var bindToDialog = function( dialog )\r
41                         {\r
42                                 dialog.on( 'ok', onOk );\r
43                                 dialog.on( 'cancel', onCancel );\r
44                         };\r
45                         editor.execCommand( dialogName );\r
46                         if ( editor._.storedDialogs.colordialog )\r
47                                 bindToDialog( editor._.storedDialogs.colordialog );\r
48                         else\r
49                         {\r
50                                 CKEDITOR.on( 'dialogDefinition', function( e )\r
51                                 {\r
52                                         if ( e.data.name != dialogName )\r
53                                                 return;\r
54 \r
55                                         var definition = e.data.definition;\r
56 \r
57                                         e.removeListener();\r
58                                         definition.onLoad = CKEDITOR.tools.override( definition.onLoad, function( orginal )\r
59                                         {\r
60                                                 return function()\r
61                                                 {\r
62                                                         bindToDialog( this );\r
63                                                         definition.onLoad = orginal;\r
64                                                         if ( typeof orginal == 'function' )\r
65                                                                 orginal.call( this );\r
66                                                 };\r
67                                         } );\r
68                                 });\r
69                         }\r
70                 }\r
71 \r
72                 return {\r
73                         title : langCell.title,\r
74                         minWidth : CKEDITOR.env.ie && CKEDITOR.env.quirks ? 550 : 480,\r
75                         minHeight : CKEDITOR.env.ie ? ( CKEDITOR.env.quirks ? 180 : 150 ) : 140,\r
76                         contents : [\r
77                                 {\r
78                                         id : 'info',\r
79                                         label : langCell.title,\r
80                                         accessKey : 'I',\r
81                                         elements :\r
82                                         [\r
83                                                 {\r
84                                                         type : 'hbox',\r
85                                                         widths : [ '40%', '5%', '40%' ],\r
86                                                         children :\r
87                                                         [\r
88                                                                 {\r
89                                                                         type : 'vbox',\r
90                                                                         padding : 0,\r
91                                                                         children :\r
92                                                                         [\r
93                                                                                 {\r
94                                                                                         type : 'hbox',\r
95                                                                                         widths : [ '70%', '30%' ],\r
96                                                                                         children :\r
97                                                                                         [\r
98                                                                                                 {\r
99                                                                                                         type : 'text',\r
100                                                                                                         id : 'width',\r
101                                                                                                         label : langTable.width,\r
102                                                                                                         widths : [ '71%', '29%' ],\r
103                                                                                                         labelLayout : 'horizontal',\r
104                                                                                                         validate : validate[ 'number' ]( langCell.invalidWidth ),\r
105 \r
106                                                                                                         // Extra labelling of width unit type.\r
107                                                                                                         onLoad : function()\r
108                                                                                                         {\r
109                                                                                                                 var widthType = this.getDialog().getContentElement( 'info', 'widthType' ),\r
110                                                                                                                         labelElement = widthType.getElement(),\r
111                                                                                                                         inputElement = this.getInputElement(),\r
112                                                                                                                         ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );\r
113 \r
114                                                                                                                 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );\r
115                                                                                                         },\r
116 \r
117                                                                                                         setup : function( element )\r
118                                                                                                         {\r
119                                                                                                                 var widthAttr = parseInt( element.getAttribute( 'width' ), 10 ),\r
120                                                                                                                                 widthStyle = parseInt( element.getStyle( 'width' ), 10 );\r
121 \r
122                                                                                                                 !isNaN( widthAttr ) && this.setValue( widthAttr );\r
123                                                                                                                 !isNaN( widthStyle ) && this.setValue( widthStyle );\r
124                                                                                                         },\r
125                                                                                                         commit : function( element )\r
126                                                                                                         {\r
127                                                                                                                 var value = parseInt( this.getValue(), 10 ),\r
128                                                                                                                                 unit = this.getDialog().getValueOf( 'info', 'widthType' );\r
129 \r
130                                                                                                                 if ( !isNaN( value ) )\r
131                                                                                                                         element.setStyle( 'width', value + unit );\r
132                                                                                                                 else\r
133                                                                                                                         element.removeStyle( 'width' );\r
134 \r
135                                                                                                                 element.removeAttribute( 'width' );\r
136                                                                                                         },\r
137                                                                                                         'default' : ''\r
138                                                                                                 },\r
139                                                                                                 {\r
140                                                                                                         type : 'select',\r
141                                                                                                         id : 'widthType',\r
142                                                                                                         labelLayout : 'horizontal',\r
143                                                                                                         widths : [ '0%', '100%' ],\r
144                                                                                                         label : editor.lang.table.widthUnit,\r
145                                                                                                         labelStyle: 'display:none',\r
146                                                                                                         'default' : 'px',\r
147                                                                                                         items :\r
148                                                                                                         [\r
149                                                                                                                 [ langTable.widthPx, 'px' ],\r
150                                                                                                                 [ langTable.widthPc, '%' ]\r
151                                                                                                         ],\r
152                                                                                                         setup : function( selectedCell )\r
153                                                                                                         {\r
154                                                                                                                 var widthMatch = widthPattern.exec( selectedCell.getStyle( 'width' ) || selectedCell.getAttribute( 'width' ) );\r
155                                                                                                                 if ( widthMatch )\r
156                                                                                                                         this.setValue( widthMatch[2] );\r
157                                                                                                         }\r
158                                                                                                 }\r
159                                                                                         ]\r
160                                                                                 },\r
161                                                                                 {\r
162                                                                                         type : 'hbox',\r
163                                                                                         widths : [ '70%', '30%' ],\r
164                                                                                         children :\r
165                                                                                         [\r
166                                                                                                 {\r
167                                                                                                         type : 'text',\r
168                                                                                                         id : 'height',\r
169                                                                                                         label : langTable.height,\r
170                                                                                                         'default' : '',\r
171                                                                                                         widths : [ '71%', '29%' ],\r
172                                                                                                         labelLayout : 'horizontal',\r
173                                                                                                         validate : validate[ 'number' ]( langCell.invalidHeight ),\r
174 \r
175                                                                                                         // Extra labelling of height unit type.\r
176                                                                                                         onLoad : function()\r
177                                                                                                         {\r
178                                                                                                                 var heightType = this.getDialog().getContentElement( 'info', 'htmlHeightType' ),\r
179                                                                                                                         labelElement = heightType.getElement(),\r
180                                                                                                                         inputElement = this.getInputElement(),\r
181                                                                                                                         ariaLabelledByAttr = inputElement.getAttribute( 'aria-labelledby' );\r
182 \r
183                                                                                                                 inputElement.setAttribute( 'aria-labelledby', [ ariaLabelledByAttr, labelElement.$.id ].join( ' ' ) );\r
184                                                                                                         },\r
185 \r
186                                                                                                         setup : function( element )\r
187                                                                                                         {\r
188                                                                                                                 var heightAttr = parseInt( element.getAttribute( 'height' ), 10 ),\r
189                                                                                                                                 heightStyle = parseInt( element.getStyle( 'height' ), 10 );\r
190 \r
191                                                                                                                 !isNaN( heightAttr ) && this.setValue( heightAttr );\r
192                                                                                                                 !isNaN( heightStyle ) && this.setValue( heightStyle );\r
193                                                                                                         },\r
194                                                                                                         commit : function( element )\r
195                                                                                                         {\r
196                                                                                                                 var value = parseInt( this.getValue(), 10 );\r
197 \r
198                                                                                                                 if ( !isNaN( value ) )\r
199                                                                                                                         element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );\r
200                                                                                                                 else\r
201                                                                                                                         element.removeStyle( 'height' );\r
202 \r
203                                                                                                                 element.removeAttribute( 'height' );\r
204                                                                                                         }\r
205                                                                                                 },\r
206                                                                                                 {\r
207                                                                                                         id : 'htmlHeightType',\r
208                                                                                                         type : 'html',\r
209                                                                                                         html : langTable.widthPx\r
210                                                                                                 }\r
211                                                                                         ]\r
212                                                                                 },\r
213                                                                                 spacer,\r
214                                                                                 {\r
215                                                                                         type : 'select',\r
216                                                                                         id : 'wordWrap',\r
217                                                                                         labelLayout : 'horizontal',\r
218                                                                                         label : langCell.wordWrap,\r
219                                                                                         widths : [ '50%', '50%' ],\r
220                                                                                         'default' : 'yes',\r
221                                                                                         items :\r
222                                                                                         [\r
223                                                                                                 [ langCell.yes, 'yes' ],\r
224                                                                                                 [ langCell.no, 'no' ]\r
225                                                                                         ],\r
226                                                                                         setup : function( element )\r
227                                                                                         {\r
228                                                                                                 var wordWrapAttr = element.getAttribute( 'noWrap' ),\r
229                                                                                                                 wordWrapStyle = element.getStyle( 'white-space' );\r
230 \r
231                                                                                                 if ( wordWrapStyle == 'nowrap' || wordWrapAttr )\r
232                                                                                                         this.setValue( 'no' );\r
233                                                                                         },\r
234                                                                                         commit : function( element )\r
235                                                                                         {\r
236                                                                                                 if ( this.getValue() == 'no' )\r
237                                                                                                         element.setStyle( 'white-space', 'nowrap' );\r
238                                                                                                 else\r
239                                                                                                         element.removeStyle( 'white-space' );\r
240 \r
241                                                                                                 element.removeAttribute( 'noWrap' );\r
242                                                                                         }\r
243                                                                                 },\r
244                                                                                 spacer,\r
245                                                                                 {\r
246                                                                                         type : 'select',\r
247                                                                                         id : 'hAlign',\r
248                                                                                         labelLayout : 'horizontal',\r
249                                                                                         label : langCell.hAlign,\r
250                                                                                         widths : [ '50%', '50%' ],\r
251                                                                                         'default' : '',\r
252                                                                                         items :\r
253                                                                                         [\r
254                                                                                                 [ langCommon.notSet, '' ],\r
255                                                                                                 [ langTable.alignLeft, 'left' ],\r
256                                                                                                 [ langTable.alignCenter, 'center' ],\r
257                                                                                                 [ langTable.alignRight, 'right' ]\r
258                                                                                         ],\r
259                                                                                         setup : function( element )\r
260                                                                                         {\r
261                                                                                                 var alignAttr = element.getAttribute( 'align' ),\r
262                                                                                                                 textAlignStyle = element.getStyle( 'text-align');\r
263 \r
264                                                                                                 this.setValue(  textAlignStyle || alignAttr || '' );\r
265                                                                                         },\r
266                                                                                         commit : function( selectedCell )\r
267                                                                                         {\r
268                                                                                                 var value = this.getValue();\r
269 \r
270                                                                                                 if ( value )\r
271                                                                                                         selectedCell.setStyle( 'text-align', value );\r
272                                                                                                 else\r
273                                                                                                         selectedCell.removeStyle( 'text-align' );\r
274 \r
275                                                                                                 selectedCell.removeAttribute( 'align' );\r
276                                                                                         }\r
277                                                                                 },\r
278                                                                                 {\r
279                                                                                         type : 'select',\r
280                                                                                         id : 'vAlign',\r
281                                                                                         labelLayout : 'horizontal',\r
282                                                                                         label : langCell.vAlign,\r
283                                                                                         widths : [ '50%', '50%' ],\r
284                                                                                         'default' : '',\r
285                                                                                         items :\r
286                                                                                         [\r
287                                                                                                 [ langCommon.notSet, '' ],\r
288                                                                                                 [ langCell.alignTop, 'top' ],\r
289                                                                                                 [ langCell.alignMiddle, 'middle' ],\r
290                                                                                                 [ langCell.alignBottom, 'bottom' ],\r
291                                                                                                 [ langCell.alignBaseline, 'baseline' ]\r
292                                                                                         ],\r
293                                                                                         setup : function( element )\r
294                                                                                         {\r
295                                                                                                 var vAlignAttr = element.getAttribute( 'vAlign' ),\r
296                                                                                                                 vAlignStyle = element.getStyle( 'vertical-align' );\r
297 \r
298                                                                                                 switch( vAlignStyle )\r
299                                                                                                 {\r
300                                                                                                         // Ignore all other unrelated style values..\r
301                                                                                                         case 'top':\r
302                                                                                                         case 'middle':\r
303                                                                                                         case 'bottom':\r
304                                                                                                         case 'baseline':\r
305                                                                                                                 break;\r
306                                                                                                         default:\r
307                                                                                                                 vAlignStyle = '';\r
308                                                                                                 }\r
309 \r
310                                                                                                 this.setValue( vAlignStyle || vAlignAttr || '' );\r
311                                                                                         },\r
312                                                                                         commit : function( element )\r
313                                                                                         {\r
314                                                                                                 var value = this.getValue();\r
315 \r
316                                                                                                 if ( value )\r
317                                                                                                         element.setStyle( 'vertical-align', value );\r
318                                                                                                 else\r
319                                                                                                         element.removeStyle( 'vertical-align' );\r
320 \r
321                                                                                                 element.removeAttribute( 'vAlign' );\r
322                                                                                         }\r
323                                                                                 }\r
324                                                                         ]\r
325                                                                 },\r
326                                                                 spacer,\r
327                                                                 {\r
328                                                                         type : 'vbox',\r
329                                                                         padding : 0,\r
330                                                                         children :\r
331                                                                         [\r
332                                                                                 {\r
333                                                                                         type : 'select',\r
334                                                                                         id : 'cellType',\r
335                                                                                         label : langCell.cellType,\r
336                                                                                         labelLayout : 'horizontal',\r
337                                                                                         widths : [ '50%', '50%' ],\r
338                                                                                         'default' : 'td',\r
339                                                                                         items :\r
340                                                                                         [\r
341                                                                                                 [ langCell.data, 'td' ],\r
342                                                                                                 [ langCell.header, 'th' ]\r
343                                                                                         ],\r
344                                                                                         setup : function( selectedCell )\r
345                                                                                         {\r
346                                                                                                 this.setValue( selectedCell.getName() );\r
347                                                                                         },\r
348                                                                                         commit : function( selectedCell )\r
349                                                                                         {\r
350                                                                                                 selectedCell.renameNode( this.getValue() );\r
351                                                                                         }\r
352                                                                                 },\r
353                                                                                 spacer,\r
354                                                                                 {\r
355                                                                                         type : 'text',\r
356                                                                                         id : 'rowSpan',\r
357                                                                                         label : langCell.rowSpan,\r
358                                                                                         labelLayout : 'horizontal',\r
359                                                                                         widths : [ '50%', '50%' ],\r
360                                                                                         'default' : '',\r
361                                                                                         validate : validate.integer( langCell.invalidRowSpan ),\r
362                                                                                         setup : function( selectedCell )\r
363                                                                                         {\r
364                                                                                                 var attrVal = parseInt( selectedCell.getAttribute( 'rowSpan' ), 10 );\r
365                                                                                                 if ( attrVal && attrVal  != 1 )\r
366                                                                                                         this.setValue(  attrVal );\r
367                                                                                         },\r
368                                                                                         commit : function( selectedCell )\r
369                                                                                         {\r
370                                                                                                 var value = parseInt( this.getValue(), 10 );\r
371                                                                                                 if ( value && value != 1 )\r
372                                                                                                         selectedCell.setAttribute( 'rowSpan', this.getValue() );\r
373                                                                                                 else\r
374                                                                                                         selectedCell.removeAttribute( 'rowSpan' );\r
375                                                                                         }\r
376                                                                                 },\r
377                                                                                 {\r
378                                                                                         type : 'text',\r
379                                                                                         id : 'colSpan',\r
380                                                                                         label : langCell.colSpan,\r
381                                                                                         labelLayout : 'horizontal',\r
382                                                                                         widths : [ '50%', '50%' ],\r
383                                                                                         'default' : '',\r
384                                                                                         validate : validate.integer( langCell.invalidColSpan ),\r
385                                                                                         setup : function( element )\r
386                                                                                         {\r
387                                                                                                 var attrVal = parseInt( element.getAttribute( 'colSpan' ), 10 );\r
388                                                                                                 if ( attrVal && attrVal  != 1 )\r
389                                                                                                         this.setValue(  attrVal );\r
390                                                                                         },\r
391                                                                                         commit : function( selectedCell )\r
392                                                                                         {\r
393                                                                                                 var value = parseInt( this.getValue(), 10 );\r
394                                                                                                 if ( value && value != 1 )\r
395                                                                                                         selectedCell.setAttribute( 'colSpan', this.getValue() );\r
396                                                                                                 else\r
397                                                                                                         selectedCell.removeAttribute( 'colSpan' );\r
398                                                                                         }\r
399                                                                                 },\r
400                                                                                 spacer,\r
401                                                                                 {\r
402                                                                                         type : 'hbox',\r
403                                                                                         padding : 0,\r
404                                                                                         widths : [ '80%', '20%' ],\r
405                                                                                         children :\r
406                                                                                         [\r
407                                                                                                 {\r
408                                                                                                         type : 'text',\r
409                                                                                                         id : 'bgColor',\r
410                                                                                                         label : langCell.bgColor,\r
411                                                                                                         labelLayout : 'horizontal',\r
412                                                                                                         widths : [ '70%', '30%' ],\r
413                                                                                                         'default' : '',\r
414                                                                                                         setup : function( element )\r
415                                                                                                         {\r
416                                                                                                                 var bgColorAttr = element.getAttribute( 'bgColor' ),\r
417                                                                                                                                 bgColorStyle = element.getStyle( 'background-color' );\r
418 \r
419                                                                                                                 this.setValue( bgColorStyle || bgColorAttr );\r
420                                                                                                         },\r
421                                                                                                         commit : function( selectedCell )\r
422                                                                                                         {\r
423                                                                                                                 var value = this.getValue();\r
424 \r
425                                                                                                                 if ( value )\r
426                                                                                                                         selectedCell.setStyle( 'background-color', this.getValue() );\r
427                                                                                                                 else\r
428                                                                                                                         selectedCell.removeStyle( 'background-color' );\r
429 \r
430                                                                                                                 selectedCell.removeAttribute( 'bgColor');\r
431                                                                                                         }\r
432                                                                                                 },\r
433                                                                                                 {\r
434                                                                                                         type : 'button',\r
435                                                                                                         id : 'bgColorChoose',\r
436                                                                                                         label : langCell.chooseColor,\r
437                                                                                                         style : 'margin-left: 10px',\r
438                                                                                                         onClick : function()\r
439                                                                                                         {\r
440                                                                                                                 var self = this;\r
441                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
442                                                                                                                 {\r
443                                                                                                                         self.getDialog().getContentElement( 'info', 'bgColor' ).setValue(\r
444                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
445                                                                                                                         );\r
446                                                                                                                 } );\r
447                                                                                                         }\r
448                                                                                                 }\r
449                                                                                         ]\r
450                                                                                 },\r
451                                                                                 spacer,\r
452                                                                                 {\r
453                                                                                         type : 'hbox',\r
454                                                                                         padding : 0,\r
455                                                                                         widths : [ '80%', '20%' ],\r
456                                                                                         children :\r
457                                                                                         [\r
458                                                                                                 {\r
459                                                                                                         type : 'text',\r
460                                                                                                         id : 'borderColor',\r
461                                                                                                         label : langCell.borderColor,\r
462                                                                                                         labelLayout : 'horizontal',\r
463                                                                                                         widths : [ '70%', '30%' ],\r
464                                                                                                         'default' : '',\r
465                                                                                                         setup : function( element )\r
466                                                                                                         {\r
467                                                                                                                 var borderColorAttr = element.getAttribute( 'borderColor' ),\r
468                                                                                                                                 borderColorStyle = element.getStyle( 'border-color' );\r
469 \r
470                                                                                                                 this.setValue( borderColorStyle || borderColorAttr );\r
471                                                                                                         },\r
472                                                                                                         commit : function( selectedCell )\r
473                                                                                                         {\r
474                                                                                                                 var value = this.getValue();\r
475                                                                                                                 if ( value )\r
476                                                                                                                         selectedCell.setStyle( 'border-color', this.getValue() );\r
477                                                                                                                 else\r
478                                                                                                                         selectedCell.removeStyle( 'border-color' );\r
479 \r
480                                                                                                                 selectedCell.removeAttribute( 'borderColor');\r
481                                                                                                         }\r
482                                                                                                 },\r
483                                                                                                 {\r
484                                                                                                         type : 'button',\r
485                                                                                                         id : 'borderColorChoose',\r
486                                                                                                         label : langCell.chooseColor,\r
487                                                                                                         style : 'margin-left: 10px',\r
488                                                                                                         onClick : function()\r
489                                                                                                         {\r
490                                                                                                                 var self = this;\r
491                                                                                                                 getDialogValue( 'colordialog', function( colorDialog )\r
492                                                                                                                 {\r
493                                                                                                                         self.getDialog().getContentElement( 'info', 'borderColor' ).setValue(\r
494                                                                                                                                 colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue()\r
495                                                                                                                         );\r
496                                                                                                                 } );\r
497                                                                                                         }\r
498                                                                                                 }\r
499                                                                                         ]\r
500                                                                                 }\r
501                                                                         ]\r
502                                                                 }\r
503                                                         ]\r
504                                                 }\r
505                                         ]\r
506                                 }\r
507                         ],\r
508                         onShow : function()\r
509                         {\r
510                                 this.cells = CKEDITOR.plugins.tabletools.getSelectedCells(\r
511                                         this._.editor.getSelection() );\r
512                                 this.setupContent( this.cells[ 0 ] );\r
513                         },\r
514                         onOk : function()\r
515                         {\r
516                                 var selection = this._.editor.getSelection(),\r
517                                         bookmarks = selection.createBookmarks();\r
518 \r
519                                 var cells = this.cells;\r
520                                 for ( var i = 0 ; i < cells.length ; i++ )\r
521                                         this.commitContent( cells[ i ] );\r
522 \r
523                                 selection.selectBookmarks( bookmarks );\r
524 \r
525                                 // Force selectionChange event because of alignment style.\r
526                                 var firstElement = selection.getStartElement();\r
527                                 var currentPath = new CKEDITOR.dom.elementPath( firstElement );\r
528 \r
529                                 this._.editor._.selectionPreviousPath = currentPath;\r
530                                 this._.editor.fire( 'selectionChange', { selection : selection, path : currentPath, element : firstElement } );\r
531                         }\r
532                 };\r
533         } );\r