2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
5 CKEDITOR.dialog.add( 'select', function( editor )
\r
7 // Add a new option to a SELECT object (combo or list).
\r
8 function addOption( combo, optionText, optionValue, documentObject, index )
\r
10 combo = getSelect( combo );
\r
12 if ( documentObject )
\r
13 oOption = documentObject.createElement( "OPTION" );
\r
15 oOption = document.createElement( "OPTION" );
\r
17 if ( combo && oOption && oOption.getName() == 'option' )
\r
19 if ( CKEDITOR.env.ie ) {
\r
20 if ( !isNaN( parseInt( index, 10) ) )
\r
21 combo.$.options.add( oOption.$, index );
\r
23 combo.$.options.add( oOption.$ );
\r
25 oOption.$.innerHTML = optionText.length > 0 ? optionText : '';
\r
26 oOption.$.value = optionValue;
\r
30 if ( index !== null && index < combo.getChildCount() )
\r
31 combo.getChild( index < 0 ? 0 : index ).insertBeforeMe( oOption );
\r
33 combo.append( oOption );
\r
35 oOption.setText( optionText.length > 0 ? optionText : '' );
\r
36 oOption.setValue( optionValue );
\r
44 // Remove all selected options from a SELECT object.
\r
45 function removeSelectedOptions( combo )
\r
47 combo = getSelect( combo );
\r
49 // Save the selected index
\r
50 var iSelectedIndex = getSelectedIndex( combo );
\r
52 // Remove all selected options.
\r
53 for ( var i = combo.getChildren().count() - 1 ; i >= 0 ; i-- )
\r
55 if ( combo.getChild( i ).$.selected )
\r
56 combo.getChild( i ).remove();
\r
59 // Reset the selection based on the original selected index.
\r
60 setSelectedIndex( combo, iSelectedIndex );
\r
62 //Modify option from a SELECT object.
\r
63 function modifyOption( combo, index, title, value )
\r
65 combo = getSelect( combo );
\r
68 var child = combo.getChild( index );
\r
69 child.setText( title );
\r
70 child.setValue( value );
\r
73 function removeAllOptions( combo )
\r
75 combo = getSelect( combo );
\r
76 while( combo.getChild( 0 ) && combo.getChild( 0 ).remove() )
\r
79 // Moves the selected option by a number of steps (also negative).
\r
80 function changeOptionPosition( combo, steps, documentObject )
\r
82 combo = getSelect( combo );
\r
83 var iActualIndex = getSelectedIndex( combo );
\r
84 if ( iActualIndex < 0 )
\r
87 var iFinalIndex = iActualIndex + steps;
\r
88 iFinalIndex = ( iFinalIndex < 0 ) ? 0 : iFinalIndex;
\r
89 iFinalIndex = ( iFinalIndex >= combo.getChildCount() ) ? combo.getChildCount() - 1 : iFinalIndex;
\r
91 if ( iActualIndex == iFinalIndex )
\r
94 var oOption = combo.getChild( iActualIndex ),
\r
95 sText = oOption.getText(),
\r
96 sValue = oOption.getValue();
\r
100 oOption = addOption( combo, sText, sValue, ( !documentObject ) ? null : documentObject, iFinalIndex );
\r
101 setSelectedIndex( combo, iFinalIndex );
\r
104 function getSelectedIndex( combo )
\r
106 combo = getSelect( combo );
\r
107 return combo ? combo.$.selectedIndex : -1;
\r
109 function setSelectedIndex( combo, index )
\r
111 combo = getSelect( combo );
\r
114 var count = combo.getChildren().count();
\r
115 combo.$.selectedIndex = ( index >= count ) ? ( count - 1 ) : index;
\r
118 function getOptions( combo )
\r
120 combo = getSelect( combo );
\r
121 return combo ? combo.getChildren() : false;
\r
123 function getSelect( obj )
\r
125 if ( obj && obj.domId && obj.getInputElement().$ ) // Dialog element.
\r
126 return obj.getInputElement();
\r
127 else if ( obj && obj.$ )
\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
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
143 this.selectBox = element;
\r
144 this.setupContent( element.getName(), element );
\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
154 var editor = this.getParentEditor(),
\r
155 element = this.selectBox,
\r
156 isInsertMode = !element;
\r
158 if ( isInsertMode )
\r
159 element = editor.document.createElement( 'select' );
\r
160 this.commitContent( element );
\r
162 if ( isInsertMode )
\r
163 editor.insertElement( element );
\r
168 label : editor.lang.select.selectInfo,
\r
169 title : editor.lang.select.selectInfo,
\r
175 widths : [ '25%','75%' ],
\r
176 labelLayout : 'horizontal',
\r
177 label : editor.lang.common.name,
\r
181 style : 'width:350px',
\r
182 setup : function( name, element )
\r
184 if ( name == 'clear' )
\r
185 this.setValue( '' );
\r
186 else if ( name == 'select' )
\r
189 element.getAttribute( '_cke_saved_name' ) ||
\r
190 element.getAttribute( 'name' ) ||
\r
194 commit : function( element )
\r
196 if ( this.getValue() )
\r
197 element.setAttribute( '_cke_saved_name', this.getValue() );
\r
200 element.removeAttribute( '_cke_saved_name' ) ;
\r
201 element.removeAttribute( 'name' );
\r
208 widths : [ '25%','75%' ],
\r
209 labelLayout : 'horizontal',
\r
210 label : editor.lang.select.value,
\r
211 style : 'width:350px',
\r
213 className : 'cke_disabled',
\r
214 onLoad : function()
\r
216 this.getInputElement().setAttribute( 'readOnly', true );
\r
218 setup : function( name, element )
\r
220 if ( name == 'clear' )
\r
221 this.setValue( '' );
\r
222 else if ( name == 'option' && element.getAttribute( 'selected' ) )
\r
223 this.setValue( element.$.value );
\r
228 widths : [ '175px', '170px' ],
\r
236 labelLayout : 'horizontal',
\r
237 label : editor.lang.select.size,
\r
240 style : 'width:175px',
\r
241 validate: function()
\r
243 var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed );
\r
244 return ( ( this.getValue() === '' ) || func.apply( this ) );
\r
246 setup : function( name, element )
\r
248 if ( name == 'select' )
\r
249 this.setValue( element.getAttribute( 'size' ) || '' );
\r
251 commit : function( element )
\r
253 if ( this.getValue() )
\r
254 element.setAttribute( 'size', this.getValue() );
\r
256 element.removeAttribute( 'size' );
\r
261 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.lines ) + '</span>'
\r
267 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.opAvail ) + '</span>'
\r
271 widths : [ '115px', '115px' ,'100px' ],
\r
282 label : editor.lang.select.opText,
\r
283 style : 'width:115px',
\r
284 setup : function( name, element )
\r
286 if ( name == 'clear' )
\r
287 this.setValue( "" );
\r
296 style : 'width:115px;height:75px',
\r
298 onChange : function()
\r
300 var dialog = this.getDialog(),
\r
301 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
302 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
303 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
304 iIndex = getSelectedIndex( this );
\r
306 setSelectedIndex( values, iIndex );
\r
307 optName.setValue( this.getValue() );
\r
308 optValue.setValue( values.getValue() );
\r
310 setup : function( name, element )
\r
312 if ( name == 'clear' )
\r
313 removeAllOptions( this );
\r
314 else if ( name == 'option' )
\r
315 addOption( this, element.getText(), element.getText(),
\r
316 this.getDialog().getParentEditor().document );
\r
318 commit : function( element )
\r
320 var dialog = this.getDialog(),
\r
321 optionsNames = getOptions( this ),
\r
322 optionsValues = getOptions( dialog.getContentElement( 'info', 'cmbValue' ) ),
\r
323 selectValue = dialog.getContentElement( 'info', 'txtValue' ).getValue();
\r
325 removeAllOptions( element );
\r
327 for ( var i = 0 ; i < optionsNames.count() ; i++ )
\r
329 var oOption = addOption( element, optionsNames.getItem( i ).getValue(),
\r
330 optionsValues.getItem( i ).getValue(), dialog.getParentEditor().document );
\r
331 if ( optionsValues.getItem( i ).getValue() == selectValue )
\r
333 oOption.setAttribute( 'selected', 'selected' );
\r
334 oOption.selected = true;
\r
346 id : 'txtOptValue',
\r
348 label : editor.lang.select.opValue,
\r
349 style : 'width:115px',
\r
350 setup : function( name, element )
\r
352 if ( name == 'clear' )
\r
353 this.setValue( "" );
\r
361 style : 'width:115px;height:75px',
\r
363 onChange : function()
\r
365 var dialog = this.getDialog(),
\r
366 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
367 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
368 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
369 iIndex = getSelectedIndex( this );
\r
371 setSelectedIndex( names, iIndex );
\r
372 optName.setValue( names.getValue() );
\r
373 optValue.setValue( this.getValue() );
\r
375 setup : function( name, element )
\r
377 if ( name == 'clear' )
\r
378 removeAllOptions( this );
\r
379 else if ( name == 'option' )
\r
381 var oValue = element.getValue();
\r
382 addOption( this, oValue, oValue,
\r
383 this.getDialog().getParentEditor().document );
\r
384 if ( element.getAttribute( 'selected' ) == 'selected' )
\r
385 this.getDialog().getContentElement( 'info', 'txtValue' ).setValue( oValue );
\r
399 label : editor.lang.select.btnAdd,
\r
400 title : editor.lang.select.btnAdd,
\r
401 style : 'width:100%;',
\r
402 onClick : function()
\r
405 var dialog = this.getDialog(),
\r
406 parentEditor = dialog.getParentEditor(),
\r
407 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
408 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
409 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
410 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
412 addOption(names, optName.getValue(), optName.getValue(), dialog.getParentEditor().document );
\r
413 addOption(values, optValue.getValue(), optValue.getValue(), dialog.getParentEditor().document );
\r
415 optName.setValue( "" );
\r
416 optValue.setValue( "" );
\r
421 label : editor.lang.select.btnModify,
\r
422 title : editor.lang.select.btnModify,
\r
423 style : 'width:100%;',
\r
424 onClick : function()
\r
426 //Modify selected option.
\r
427 var dialog = this.getDialog(),
\r
428 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
429 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
430 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
431 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
432 iIndex = getSelectedIndex( names );
\r
436 modifyOption( names, iIndex, optName.getValue(), optName.getValue() );
\r
437 modifyOption( values, iIndex, optValue.getValue(), optValue.getValue() );
\r
443 style : 'width:100%;',
\r
444 label : editor.lang.select.btnUp,
\r
445 title : editor.lang.select.btnUp,
\r
446 onClick : function()
\r
449 var dialog = this.getDialog(),
\r
450 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
451 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
453 changeOptionPosition( names, -1, dialog.getParentEditor().document );
\r
454 changeOptionPosition( values, -1, dialog.getParentEditor().document );
\r
459 style : 'width:100%;',
\r
460 label : editor.lang.select.btnDown,
\r
461 title : editor.lang.select.btnDown,
\r
462 onClick : function()
\r
465 var dialog = this.getDialog(),
\r
466 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
467 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
469 changeOptionPosition( names, 1, dialog.getParentEditor().document );
\r
470 changeOptionPosition( values, 1, dialog.getParentEditor().document );
\r
479 widths : [ '40%', '20%', '40%' ],
\r
484 label : editor.lang.select.btnSetValue,
\r
485 title : editor.lang.select.btnSetValue,
\r
486 onClick : function()
\r
488 //Set as default value.
\r
489 var dialog = this.getDialog(),
\r
490 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
491 txtValue = dialog.getContentElement( 'info', 'txtValue' );
\r
492 txtValue.setValue( values.getValue() );
\r
497 label : editor.lang.select.btnDelete,
\r
498 title : editor.lang.select.btnDelete,
\r
499 onClick : function()
\r
502 var dialog = this.getDialog(),
\r
503 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
504 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
505 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
506 optValue = dialog.getContentElement( 'info', 'txtOptValue' );
\r
508 removeSelectedOptions( names );
\r
509 removeSelectedOptions( values );
\r
511 optName.setValue( "" );
\r
512 optValue.setValue( "" );
\r
518 label : editor.lang.select.chkMulti,
\r
522 setup : function( name, element )
\r
524 if ( name == 'select' )
\r
525 this.setValue( element.getAttribute( 'multiple' ) );
\r
527 commit : function( element )
\r
529 if ( this.getValue() )
\r
530 element.setAttribute( 'multiple', this.getValue() );
\r
532 element.removeAttribute( 'multiple' );
\r