JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
ee5dbbd9de8c28edaa21d05f2374688947b5eca3
[ckeditor.git] / _source / plugins / forms / dialogs / select.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 CKEDITOR.dialog.add( 'select', function( editor )\r
6 {\r
7         // Add a new option to a SELECT object (combo or list).\r
8         function addOption( combo, optionText, optionValue, documentObject, index )\r
9         {\r
10                 combo = getSelect( combo );\r
11                 var oOption;\r
12                 if ( documentObject )\r
13                         oOption = documentObject.createElement( "OPTION" );\r
14                 else\r
15                         oOption = document.createElement( "OPTION" );\r
16 \r
17                 if ( combo && oOption && oOption.getName() == 'option' )\r
18                 {\r
19                         if ( CKEDITOR.env.ie ) {\r
20                                 if ( !isNaN( parseInt( index, 10) ) )\r
21                                         combo.$.options.add( oOption.$, index );\r
22                                 else\r
23                                         combo.$.options.add( oOption.$ );\r
24 \r
25                                 oOption.$.innerHTML = optionText.length > 0 ? optionText : '';\r
26                                 oOption.$.value     = optionValue;\r
27                         }\r
28                         else\r
29                         {\r
30                                 if ( index !== null && index < combo.getChildCount() )\r
31                                         combo.getChild( index < 0 ? 0 : index ).insertBeforeMe( oOption );\r
32                                 else\r
33                                         combo.append( oOption );\r
34 \r
35                                 oOption.setText( optionText.length > 0 ? optionText : '' );\r
36                                 oOption.setValue( optionValue );\r
37                         }\r
38                 }\r
39                 else\r
40                         return false;\r
41 \r
42                 return oOption;\r
43         }\r
44         // Remove all selected options from a SELECT object.\r
45         function removeSelectedOptions( combo )\r
46         {\r
47                 combo = getSelect( combo );\r
48 \r
49                 // Save the selected index\r
50                 var iSelectedIndex = getSelectedIndex( combo );\r
51 \r
52                 // Remove all selected options.\r
53                 for ( var i = combo.getChildren().count() - 1 ; i >= 0 ; i-- )\r
54                 {\r
55                         if ( combo.getChild( i ).$.selected )\r
56                                 combo.getChild( i ).remove();\r
57                 }\r
58 \r
59                 // Reset the selection based on the original selected index.\r
60                 setSelectedIndex( combo, iSelectedIndex );\r
61         }\r
62         //Modify option  from a SELECT object.\r
63         function modifyOption( combo, index, title, value )\r
64         {\r
65                 combo = getSelect( combo );\r
66                 if ( index < 0 )\r
67                         return false;\r
68                 var child = combo.getChild( index );\r
69                 child.setText( title );\r
70                 child.setValue( value );\r
71                 return child;\r
72         }\r
73         function removeAllOptions( combo )\r
74         {\r
75                 combo = getSelect( combo );\r
76                 while( combo.getChild( 0 ) && combo.getChild( 0 ).remove() )\r
77                 { /*jsl:pass*/ }\r
78         }\r
79         // Moves the selected option by a number of steps (also negative).\r
80         function changeOptionPosition( combo, steps, documentObject )\r
81         {\r
82                 combo = getSelect( combo );\r
83                 var iActualIndex = getSelectedIndex( combo );\r
84                 if ( iActualIndex < 0 )\r
85                         return false;\r
86 \r
87                 var iFinalIndex = iActualIndex + steps;\r
88                 iFinalIndex = ( iFinalIndex < 0 ) ? 0 : iFinalIndex;\r
89                 iFinalIndex = ( iFinalIndex >= combo.getChildCount() ) ? combo.getChildCount() - 1 : iFinalIndex;\r
90 \r
91                 if ( iActualIndex == iFinalIndex )\r
92                         return false;\r
93 \r
94                 var oOption = combo.getChild( iActualIndex ),\r
95                         sText   = oOption.getText(),\r
96                         sValue  = oOption.getValue();\r
97 \r
98                 oOption.remove();\r
99 \r
100                 oOption = addOption( combo, sText, sValue, ( !documentObject ) ? null : documentObject, iFinalIndex );\r
101                 setSelectedIndex( combo, iFinalIndex );\r
102                 return oOption;\r
103         }\r
104         function getSelectedIndex( combo )\r
105         {\r
106                 combo = getSelect( combo );\r
107                 return combo ? combo.$.selectedIndex : -1;\r
108         }\r
109         function setSelectedIndex( combo, index )\r
110         {\r
111                 combo = getSelect( combo );\r
112                 if ( index < 0 )\r
113                         return null;\r
114                 var count = combo.getChildren().count();\r
115                 combo.$.selectedIndex = ( index >= count ) ? ( count - 1 ) : index;\r
116                 return combo;\r
117         }\r
118         function getOptions( combo )\r
119         {\r
120                 combo = getSelect( combo );\r
121                 return combo ? combo.getChildren() : false;\r
122         }\r
123         function getSelect( obj )\r
124         {\r
125                 if ( obj && obj.domId && obj.getInputElement().$ )                              // Dialog element.\r
126                         return  obj.getInputElement();\r
127                 else if ( obj && obj.$ )\r
128                         return obj;\r
129                 return false;\r
130         }\r
131 \r
132         return {\r
133                 title : editor.lang.select.title,\r
134                 minWidth : CKEDITOR.env.ie ? 460 : 395,\r
135                 minHeight : CKEDITOR.env.ie ? 320 : 300,\r
136                 onShow : function()\r
137                 {\r
138                         delete this.selectBox;\r
139                         this.setupContent( 'clear' );\r
140                         var element = this.getParentEditor().getSelection().getSelectedElement();\r
141                         if ( element && element.getName() == "select" )\r
142                         {\r
143                                 this.selectBox = element;\r
144                                 this.setupContent( element.getName(), element );\r
145 \r
146                                 // Load Options into dialog.\r
147                                 var objOptions = getOptions( element );\r
148                                 for ( var i = 0 ; i < objOptions.count() ; i++ )\r
149                                         this.setupContent( 'option', objOptions.getItem( i ) );\r
150                         }\r
151                 },\r
152                 onOk : function()\r
153                 {\r
154                         var editor = this.getParentEditor(),\r
155                                 element = this.selectBox,\r
156                                 isInsertMode = !element;\r
157 \r
158                         if ( isInsertMode )\r
159                                 element = editor.document.createElement( 'select' );\r
160                         this.commitContent( element );\r
161 \r
162                         if ( isInsertMode )\r
163                         {\r
164                                 editor.insertElement(element);\r
165                                 if( CKEDITOR.env.ie )\r
166                                 {\r
167                                         var sel = editor.getSelection(),\r
168                                                 bms = sel.createBookmarks();\r
169                                         setTimeout(function ()\r
170                                         {\r
171                                                 sel.selectBookmarks( bms );\r
172                                         }, 0 );\r
173                                 }\r
174                         }\r
175                 },\r
176                 contents : [\r
177                         {\r
178                                 id : 'info',\r
179                                 label : editor.lang.select.selectInfo,\r
180                                 title : editor.lang.select.selectInfo,\r
181                                 accessKey : '',\r
182                                 elements : [\r
183                                         {\r
184                                                 id : 'txtName',\r
185                                                 type : 'text',\r
186                                                 widths : [ '25%','75%' ],\r
187                                                 labelLayout : 'horizontal',\r
188                                                 label : editor.lang.common.name,\r
189                                                 'default' : '',\r
190                                                 accessKey : 'N',\r
191                                                 align : 'center',\r
192                                                 style : 'width:350px',\r
193                                                 setup : function( name, element )\r
194                                                 {\r
195                                                         if ( name == 'clear' )\r
196                                                                 this.setValue( '' );\r
197                                                         else if ( name == 'select' )\r
198                                                         {\r
199                                                                 this.setValue(\r
200                                                                                 element.getAttribute( '_cke_saved_name' ) ||\r
201                                                                                 element.getAttribute( 'name' ) ||\r
202                                                                                 '' );\r
203                                                         }\r
204                                                 },\r
205                                                 commit : function( element )\r
206                                                 {\r
207                                                         if ( this.getValue() )\r
208                                                                 element.setAttribute( '_cke_saved_name', this.getValue() );\r
209                                                         else\r
210                                                         {\r
211                                                                 element.removeAttribute( '_cke_saved_name' ) ;\r
212                                                                 element.removeAttribute( 'name' );\r
213                                                         }\r
214                                                 }\r
215                                         },\r
216                                         {\r
217                                                 id : 'txtValue',\r
218                                                 type : 'text',\r
219                                                 widths : [ '25%','75%' ],\r
220                                                 labelLayout : 'horizontal',\r
221                                                 label : editor.lang.select.value,\r
222                                                 style : 'width:350px',\r
223                                                 'default' : '',\r
224                                                 className : 'cke_disabled',\r
225                                                 onLoad : function()\r
226                                                 {\r
227                                                         this.getInputElement().setAttribute( 'readOnly', true );\r
228                                                 },\r
229                                                 setup : function( name, element )\r
230                                                 {\r
231                                                         if ( name == 'clear' )\r
232                                                                 this.setValue( '' );\r
233                                                         else if ( name == 'option' && element.getAttribute( 'selected' ) )\r
234                                                                 this.setValue( element.$.value );\r
235                                                 }\r
236                                         },\r
237                                         {\r
238                                                 type : 'hbox',\r
239                                                 widths : [ '175px', '170px' ],\r
240                                                 align : 'center',\r
241                                                 children :\r
242                                                 [\r
243                                                         {\r
244                                                                 id : 'txtSize',\r
245                                                                 type : 'text',\r
246                                                                 align : 'center',\r
247                                                                 labelLayout : 'horizontal',\r
248                                                                 label : editor.lang.select.size,\r
249                                                                 'default' : '',\r
250                                                                 accessKey : 'S',\r
251                                                                 style : 'width:175px',\r
252                                                                 validate: function()\r
253                                                                 {\r
254                                                                         var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed );\r
255                                                                         return ( ( this.getValue() === '' ) || func.apply( this ) );\r
256                                                                 },\r
257                                                                 setup : function( name, element )\r
258                                                                 {\r
259                                                                         if ( name == 'select' )\r
260                                                                                 this.setValue( element.getAttribute( 'size' ) || '' );\r
261                                                                         if ( CKEDITOR.env.webkit )\r
262                                                                                 this.getInputElement().setStyle( 'width', '86px' );\r
263                                                                 },\r
264                                                                 commit : function( element )\r
265                                                                 {\r
266                                                                         if ( this.getValue() )\r
267                                                                                 element.setAttribute( 'size', this.getValue() );\r
268                                                                         else\r
269                                                                                 element.removeAttribute( 'size' );\r
270                                                                 }\r
271                                                         },\r
272                                                         {\r
273                                                                 type : 'html',\r
274                                                                 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.lines ) + '</span>'\r
275                                                         }\r
276                                                 ]\r
277                                         },\r
278                                         {\r
279                                                 type : 'html',\r
280                                                 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.opAvail ) + '</span>'\r
281                                         },\r
282                                         {\r
283                                                 type : 'hbox',\r
284                                                 widths : [ '115px', '115px' ,'100px' ],\r
285                                                 align : 'top',\r
286                                                 children :\r
287                                                 [\r
288                                                         {\r
289                                                                 type : 'vbox',\r
290                                                                 children :\r
291                                                                 [\r
292                                                                         {\r
293                                                                                 id : 'txtOptName',\r
294                                                                                 type : 'text',\r
295                                                                                 label : editor.lang.select.opText,\r
296                                                                                 style : 'width:115px',\r
297                                                                                 setup : function( name, element )\r
298                                                                                 {\r
299                                                                                         if ( name == 'clear' )\r
300                                                                                                 this.setValue( "" );\r
301                                                                                 }\r
302                                                                         },\r
303                                                                         {\r
304                                                                                 type : 'select',\r
305                                                                                 id : 'cmbName',\r
306                                                                                 label : '',\r
307                                                                                 title : '',\r
308                                                                                 size : 5,\r
309                                                                                 style : 'width:115px;height:75px',\r
310                                                                                 items : [],\r
311                                                                                 onChange : function()\r
312                                                                                 {\r
313                                                                                         var dialog = this.getDialog(),\r
314                                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' ),\r
315                                                                                                 optName = dialog.getContentElement( 'info', 'txtOptName' ),\r
316                                                                                                 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),\r
317                                                                                                 iIndex = getSelectedIndex( this );\r
318 \r
319                                                                                         setSelectedIndex( values, iIndex );\r
320                                                                                         optName.setValue( this.getValue() );\r
321                                                                                         optValue.setValue( values.getValue() );\r
322                                                                                 },\r
323                                                                                 setup : function( name, element )\r
324                                                                                 {\r
325                                                                                         if ( name == 'clear' )\r
326                                                                                                 removeAllOptions( this );\r
327                                                                                         else if ( name == 'option' )\r
328                                                                                                 addOption( this, element.getText(), element.getText(),\r
329                                                                                                         this.getDialog().getParentEditor().document );\r
330                                                                                 },\r
331                                                                                 commit : function( element )\r
332                                                                                 {\r
333                                                                                         var dialog = this.getDialog(),\r
334                                                                                                 optionsNames = getOptions( this ),\r
335                                                                                                 optionsValues = getOptions( dialog.getContentElement( 'info', 'cmbValue' ) ),\r
336                                                                                                 selectValue = dialog.getContentElement( 'info', 'txtValue' ).getValue();\r
337 \r
338                                                                                         removeAllOptions( element );\r
339 \r
340                                                                                         for ( var i = 0 ; i < optionsNames.count() ; i++ )\r
341                                                                                         {\r
342                                                                                                 var oOption = addOption( element, optionsNames.getItem( i ).getValue(),\r
343                                                                                                         optionsValues.getItem( i ).getValue(), dialog.getParentEditor().document );\r
344                                                                                                 if ( optionsValues.getItem( i ).getValue() == selectValue )\r
345                                                                                                 {\r
346                                                                                                         oOption.setAttribute( 'selected', 'selected' );\r
347                                                                                                         oOption.selected = true;\r
348                                                                                                 }\r
349                                                                                         }\r
350                                                                                 }\r
351                                                                         }\r
352                                                                 ]\r
353                                                         },\r
354                                                         {\r
355                                                                 type : 'vbox',\r
356                                                                 children :\r
357                                                                 [\r
358                                                                         {\r
359                                                                                 id : 'txtOptValue',\r
360                                                                                 type : 'text',\r
361                                                                                 label : editor.lang.select.opValue,\r
362                                                                                 style : 'width:115px',\r
363                                                                                 setup : function( name, element )\r
364                                                                                 {\r
365                                                                                         if ( name == 'clear' )\r
366                                                                                                 this.setValue( "" );\r
367                                                                                 }\r
368                                                                         },\r
369                                                                         {\r
370                                                                                 type : 'select',\r
371                                                                                 id : 'cmbValue',\r
372                                                                                 label : '',\r
373                                                                                 size : 5,\r
374                                                                                 style : 'width:115px;height:75px',\r
375                                                                                 items : [],\r
376                                                                                 onChange : function()\r
377                                                                                 {\r
378                                                                                         var dialog = this.getDialog(),\r
379                                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
380                                                                                                 optName = dialog.getContentElement( 'info', 'txtOptName' ),\r
381                                                                                                 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),\r
382                                                                                                 iIndex = getSelectedIndex( this );\r
383 \r
384                                                                                         setSelectedIndex( names, iIndex );\r
385                                                                                         optName.setValue( names.getValue() );\r
386                                                                                         optValue.setValue( this.getValue() );\r
387                                                                                 },\r
388                                                                                 setup : function( name, element )\r
389                                                                                 {\r
390                                                                                         if ( name == 'clear' )\r
391                                                                                                 removeAllOptions( this );\r
392                                                                                         else if ( name == 'option' )\r
393                                                                                         {\r
394                                                                                                 var oValue      = element.getValue();\r
395                                                                                                 addOption( this, oValue, oValue,\r
396                                                                                                         this.getDialog().getParentEditor().document );\r
397                                                                                                 if ( element.getAttribute( 'selected' ) == 'selected' )\r
398                                                                                                         this.getDialog().getContentElement( 'info', 'txtValue' ).setValue( oValue );\r
399                                                                                         }\r
400                                                                                 }\r
401                                                                         }\r
402                                                                 ]\r
403                                                         },\r
404                                                         {\r
405                                                                 type : 'vbox',\r
406                                                                 padding : 5,\r
407                                                                 children :\r
408                                                                 [\r
409                                                                         {\r
410                                                                                 type : 'button',\r
411                                                                                 style : '',\r
412                                                                                 label : editor.lang.select.btnAdd,\r
413                                                                                 title : editor.lang.select.btnAdd,\r
414                                                                                 style : 'width:100%;',\r
415                                                                                 onClick : function()\r
416                                                                                 {\r
417                                                                                         //Add new option.\r
418                                                                                         var dialog = this.getDialog(),\r
419                                                                                                 parentEditor = dialog.getParentEditor(),\r
420                                                                                                 optName = dialog.getContentElement( 'info', 'txtOptName' ),\r
421                                                                                                 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),\r
422                                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
423                                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' );\r
424 \r
425                                                                                         addOption(names, optName.getValue(), optName.getValue(), dialog.getParentEditor().document );\r
426                                                                                         addOption(values, optValue.getValue(), optValue.getValue(), dialog.getParentEditor().document );\r
427 \r
428                                                                                         optName.setValue( "" );\r
429                                                                                         optValue.setValue( "" );\r
430                                                                                 }\r
431                                                                         },\r
432                                                                         {\r
433                                                                                 type : 'button',\r
434                                                                                 label : editor.lang.select.btnModify,\r
435                                                                                 title : editor.lang.select.btnModify,\r
436                                                                                 style : 'width:100%;',\r
437                                                                                 onClick : function()\r
438                                                                                 {\r
439                                                                                         //Modify selected option.\r
440                                                                                         var dialog = this.getDialog(),\r
441                                                                                                 optName = dialog.getContentElement( 'info', 'txtOptName' ),\r
442                                                                                                 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),\r
443                                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
444                                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' ),\r
445                                                                                                 iIndex = getSelectedIndex( names );\r
446 \r
447                                                                                         if ( iIndex >= 0 )\r
448                                                                                         {\r
449                                                                                                 modifyOption( names, iIndex, optName.getValue(), optName.getValue() );\r
450                                                                                                 modifyOption( values, iIndex, optValue.getValue(), optValue.getValue() );\r
451                                                                                         }\r
452                                                                                 }\r
453                                                                         },\r
454                                                                         {\r
455                                                                                 type : 'button',\r
456                                                                                 style : 'width:100%;',\r
457                                                                                 label : editor.lang.select.btnUp,\r
458                                                                                 title : editor.lang.select.btnUp,\r
459                                                                                 onClick : function()\r
460                                                                                 {\r
461                                                                                         //Move up.\r
462                                                                                         var dialog = this.getDialog(),\r
463                                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
464                                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' );\r
465 \r
466                                                                                         changeOptionPosition( names, -1, dialog.getParentEditor().document );\r
467                                                                                         changeOptionPosition( values, -1, dialog.getParentEditor().document );\r
468                                                                                 }\r
469                                                                         },\r
470                                                                         {\r
471                                                                                 type : 'button',\r
472                                                                                 style : 'width:100%;',\r
473                                                                                 label : editor.lang.select.btnDown,\r
474                                                                                 title : editor.lang.select.btnDown,\r
475                                                                                 onClick : function()\r
476                                                                                 {\r
477                                                                                         //Move down.\r
478                                                                                         var dialog = this.getDialog(),\r
479                                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
480                                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' );\r
481 \r
482                                                                                         changeOptionPosition( names, 1, dialog.getParentEditor().document );\r
483                                                                                         changeOptionPosition( values, 1, dialog.getParentEditor().document );\r
484                                                                                 }\r
485                                                                         }\r
486                                                                 ]\r
487                                                         }\r
488                                                 ]\r
489                                         },\r
490                                         {\r
491                                                 type : 'hbox',\r
492                                                 widths : [ '40%', '20%', '40%' ],\r
493                                                 children :\r
494                                                 [\r
495                                                         {\r
496                                                                 type : 'button',\r
497                                                                 label : editor.lang.select.btnSetValue,\r
498                                                                 title : editor.lang.select.btnSetValue,\r
499                                                                 onClick : function()\r
500                                                                 {\r
501                                                                         //Set as default value.\r
502                                                                         var dialog = this.getDialog(),\r
503                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' ),\r
504                                                                                 txtValue = dialog.getContentElement( 'info', 'txtValue' );\r
505                                                                         txtValue.setValue( values.getValue() );\r
506                                                                 }\r
507                                                         },\r
508                                                         {\r
509                                                                 type : 'button',\r
510                                                                 label : editor.lang.select.btnDelete,\r
511                                                                 title : editor.lang.select.btnDelete,\r
512                                                                 onClick : function()\r
513                                                                 {\r
514                                                                         // Delete option.\r
515                                                                         var dialog = this.getDialog(),\r
516                                                                                 names = dialog.getContentElement( 'info', 'cmbName' ),\r
517                                                                                 values = dialog.getContentElement( 'info', 'cmbValue' ),\r
518                                                                                 optName = dialog.getContentElement( 'info', 'txtOptName' ),\r
519                                                                                 optValue = dialog.getContentElement( 'info', 'txtOptValue' );\r
520 \r
521                                                                         removeSelectedOptions( names );\r
522                                                                         removeSelectedOptions( values );\r
523 \r
524                                                                         optName.setValue( "" );\r
525                                                                         optValue.setValue( "" );\r
526                                                                 }\r
527                                                         },\r
528                                                         {\r
529                                                                 id : 'chkMulti',\r
530                                                                 type : 'checkbox',\r
531                                                                 label : editor.lang.select.chkMulti,\r
532                                                                 'default' : '',\r
533                                                                 accessKey : 'M',\r
534                                                                 value : "checked",\r
535                                                                 setup : function( name, element )\r
536                                                                 {\r
537                                                                         if ( name == 'select' )\r
538                                                                                 this.setValue( element.getAttribute( 'multiple' ) );\r
539                                                                         if ( CKEDITOR.env.webkit )\r
540                                                                                 this.getElement().getParent().setStyle( 'vertical-align', 'middle' );\r
541                                                                 },\r
542                                                                 commit : function( element )\r
543                                                                 {\r
544                                                                         if ( this.getValue() )\r
545                                                                                 element.setAttribute( 'multiple', this.getValue() );\r
546                                                                         else\r
547                                                                                 element.removeAttribute( 'multiple' );\r
548                                                                 }\r
549                                                         }\r
550                                                 ]\r
551                                         }\r
552                                 ]\r
553                         }\r
554                 ]\r
555         };\r
556 });\r