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
164 editor.insertElement(element);
\r
165 if( CKEDITOR.env.ie )
\r
167 var sel = editor.getSelection(),
\r
168 bms = sel.createBookmarks();
\r
169 setTimeout(function ()
\r
171 sel.selectBookmarks( bms );
\r
179 label : editor.lang.select.selectInfo,
\r
180 title : editor.lang.select.selectInfo,
\r
186 widths : [ '25%','75%' ],
\r
187 labelLayout : 'horizontal',
\r
188 label : editor.lang.common.name,
\r
192 style : 'width:350px',
\r
193 setup : function( name, element )
\r
195 if ( name == 'clear' )
\r
196 this.setValue( '' );
\r
197 else if ( name == 'select' )
\r
200 element.getAttribute( '_cke_saved_name' ) ||
\r
201 element.getAttribute( 'name' ) ||
\r
205 commit : function( element )
\r
207 if ( this.getValue() )
\r
208 element.setAttribute( '_cke_saved_name', this.getValue() );
\r
211 element.removeAttribute( '_cke_saved_name' ) ;
\r
212 element.removeAttribute( 'name' );
\r
219 widths : [ '25%','75%' ],
\r
220 labelLayout : 'horizontal',
\r
221 label : editor.lang.select.value,
\r
222 style : 'width:350px',
\r
224 className : 'cke_disabled',
\r
225 onLoad : function()
\r
227 this.getInputElement().setAttribute( 'readOnly', true );
\r
229 setup : function( name, element )
\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
239 widths : [ '175px', '170px' ],
\r
247 labelLayout : 'horizontal',
\r
248 label : editor.lang.select.size,
\r
251 style : 'width:175px',
\r
252 validate: function()
\r
254 var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed );
\r
255 return ( ( this.getValue() === '' ) || func.apply( this ) );
\r
257 setup : function( name, element )
\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
264 commit : function( element )
\r
266 if ( this.getValue() )
\r
267 element.setAttribute( 'size', this.getValue() );
\r
269 element.removeAttribute( 'size' );
\r
274 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.lines ) + '</span>'
\r
280 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.opAvail ) + '</span>'
\r
284 widths : [ '115px', '115px' ,'100px' ],
\r
295 label : editor.lang.select.opText,
\r
296 style : 'width:115px',
\r
297 setup : function( name, element )
\r
299 if ( name == 'clear' )
\r
300 this.setValue( "" );
\r
309 style : 'width:115px;height:75px',
\r
311 onChange : function()
\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
319 setSelectedIndex( values, iIndex );
\r
320 optName.setValue( this.getValue() );
\r
321 optValue.setValue( values.getValue() );
\r
323 setup : function( name, element )
\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
331 commit : function( element )
\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
338 removeAllOptions( element );
\r
340 for ( var i = 0 ; i < optionsNames.count() ; i++ )
\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
346 oOption.setAttribute( 'selected', 'selected' );
\r
347 oOption.selected = true;
\r
359 id : 'txtOptValue',
\r
361 label : editor.lang.select.opValue,
\r
362 style : 'width:115px',
\r
363 setup : function( name, element )
\r
365 if ( name == 'clear' )
\r
366 this.setValue( "" );
\r
374 style : 'width:115px;height:75px',
\r
376 onChange : function()
\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
384 setSelectedIndex( names, iIndex );
\r
385 optName.setValue( names.getValue() );
\r
386 optValue.setValue( this.getValue() );
\r
388 setup : function( name, element )
\r
390 if ( name == 'clear' )
\r
391 removeAllOptions( this );
\r
392 else if ( name == 'option' )
\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
412 label : editor.lang.select.btnAdd,
\r
413 title : editor.lang.select.btnAdd,
\r
414 style : 'width:100%;',
\r
415 onClick : function()
\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
425 addOption(names, optName.getValue(), optName.getValue(), dialog.getParentEditor().document );
\r
426 addOption(values, optValue.getValue(), optValue.getValue(), dialog.getParentEditor().document );
\r
428 optName.setValue( "" );
\r
429 optValue.setValue( "" );
\r
434 label : editor.lang.select.btnModify,
\r
435 title : editor.lang.select.btnModify,
\r
436 style : 'width:100%;',
\r
437 onClick : function()
\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
449 modifyOption( names, iIndex, optName.getValue(), optName.getValue() );
\r
450 modifyOption( values, iIndex, optValue.getValue(), optValue.getValue() );
\r
456 style : 'width:100%;',
\r
457 label : editor.lang.select.btnUp,
\r
458 title : editor.lang.select.btnUp,
\r
459 onClick : function()
\r
462 var dialog = this.getDialog(),
\r
463 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
464 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
466 changeOptionPosition( names, -1, dialog.getParentEditor().document );
\r
467 changeOptionPosition( values, -1, dialog.getParentEditor().document );
\r
472 style : 'width:100%;',
\r
473 label : editor.lang.select.btnDown,
\r
474 title : editor.lang.select.btnDown,
\r
475 onClick : function()
\r
478 var dialog = this.getDialog(),
\r
479 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
480 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
482 changeOptionPosition( names, 1, dialog.getParentEditor().document );
\r
483 changeOptionPosition( values, 1, dialog.getParentEditor().document );
\r
492 widths : [ '40%', '20%', '40%' ],
\r
497 label : editor.lang.select.btnSetValue,
\r
498 title : editor.lang.select.btnSetValue,
\r
499 onClick : function()
\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
510 label : editor.lang.select.btnDelete,
\r
511 title : editor.lang.select.btnDelete,
\r
512 onClick : function()
\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
521 removeSelectedOptions( names );
\r
522 removeSelectedOptions( values );
\r
524 optName.setValue( "" );
\r
525 optValue.setValue( "" );
\r
531 label : editor.lang.select.chkMulti,
\r
535 setup : function( name, element )
\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
542 commit : function( element )
\r
544 if ( this.getValue() )
\r
545 element.setAttribute( 'multiple', this.getValue() );
\r
547 element.removeAttribute( 'multiple' );
\r