2 Copyright (c) 2003-2011, 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
191 style : 'width:350px',
\r
192 setup : function( name, element )
\r
194 if ( name == 'clear' )
\r
195 this.setValue( this[ 'default' ] || '' );
\r
196 else if ( name == 'select' )
\r
199 element.data( 'cke-saved-name' ) ||
\r
200 element.getAttribute( 'name' ) ||
\r
204 commit : function( element )
\r
206 if ( this.getValue() )
\r
207 element.data( 'cke-saved-name', this.getValue() );
\r
210 element.data( 'cke-saved-name', false );
\r
211 element.removeAttribute( 'name' );
\r
218 widths : [ '25%','75%' ],
\r
219 labelLayout : 'horizontal',
\r
220 label : editor.lang.select.value,
\r
221 style : 'width:350px',
\r
223 className : 'cke_disabled',
\r
224 onLoad : function()
\r
226 this.getInputElement().setAttribute( 'readOnly', true );
\r
228 setup : function( name, element )
\r
230 if ( name == 'clear' )
\r
231 this.setValue( '' );
\r
232 else if ( name == 'option' && element.getAttribute( 'selected' ) )
\r
233 this.setValue( element.$.value );
\r
238 widths : [ '175px', '170px' ],
\r
244 labelLayout : 'horizontal',
\r
245 label : editor.lang.select.size,
\r
248 style : 'width:175px',
\r
249 validate: function()
\r
251 var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed );
\r
252 return ( ( this.getValue() === '' ) || func.apply( this ) );
\r
254 setup : function( name, element )
\r
256 if ( name == 'select' )
\r
257 this.setValue( element.getAttribute( 'size' ) || '' );
\r
258 if ( CKEDITOR.env.webkit )
\r
259 this.getInputElement().setStyle( 'width', '86px' );
\r
261 commit : function( element )
\r
263 if ( this.getValue() )
\r
264 element.setAttribute( 'size', this.getValue() );
\r
266 element.removeAttribute( 'size' );
\r
271 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.lines ) + '</span>'
\r
277 html : '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.select.opAvail ) + '</span>'
\r
281 widths : [ '115px', '115px' ,'100px' ],
\r
291 label : editor.lang.select.opText,
\r
292 style : 'width:115px',
\r
293 setup : function( name, element )
\r
295 if ( name == 'clear' )
\r
296 this.setValue( "" );
\r
305 style : 'width:115px;height:75px',
\r
307 onChange : function()
\r
309 var dialog = this.getDialog(),
\r
310 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
311 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
312 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
313 iIndex = getSelectedIndex( this );
\r
315 setSelectedIndex( values, iIndex );
\r
316 optName.setValue( this.getValue() );
\r
317 optValue.setValue( values.getValue() );
\r
319 setup : function( name, element )
\r
321 if ( name == 'clear' )
\r
322 removeAllOptions( this );
\r
323 else if ( name == 'option' )
\r
324 addOption( this, element.getText(), element.getText(),
\r
325 this.getDialog().getParentEditor().document );
\r
327 commit : function( element )
\r
329 var dialog = this.getDialog(),
\r
330 optionsNames = getOptions( this ),
\r
331 optionsValues = getOptions( dialog.getContentElement( 'info', 'cmbValue' ) ),
\r
332 selectValue = dialog.getContentElement( 'info', 'txtValue' ).getValue();
\r
334 removeAllOptions( element );
\r
336 for ( var i = 0 ; i < optionsNames.count() ; i++ )
\r
338 var oOption = addOption( element, optionsNames.getItem( i ).getValue(),
\r
339 optionsValues.getItem( i ).getValue(), dialog.getParentEditor().document );
\r
340 if ( optionsValues.getItem( i ).getValue() == selectValue )
\r
342 oOption.setAttribute( 'selected', 'selected' );
\r
343 oOption.selected = true;
\r
355 id : 'txtOptValue',
\r
357 label : editor.lang.select.opValue,
\r
358 style : 'width:115px',
\r
359 setup : function( name, element )
\r
361 if ( name == 'clear' )
\r
362 this.setValue( "" );
\r
370 style : 'width:115px;height:75px',
\r
372 onChange : function()
\r
374 var dialog = this.getDialog(),
\r
375 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
376 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
377 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
378 iIndex = getSelectedIndex( this );
\r
380 setSelectedIndex( names, iIndex );
\r
381 optName.setValue( names.getValue() );
\r
382 optValue.setValue( this.getValue() );
\r
384 setup : function( name, element )
\r
386 if ( name == 'clear' )
\r
387 removeAllOptions( this );
\r
388 else if ( name == 'option' )
\r
390 var oValue = element.getValue();
\r
391 addOption( this, oValue, oValue,
\r
392 this.getDialog().getParentEditor().document );
\r
393 if ( element.getAttribute( 'selected' ) == 'selected' )
\r
394 this.getDialog().getContentElement( 'info', 'txtValue' ).setValue( oValue );
\r
409 label : editor.lang.select.btnAdd,
\r
410 title : editor.lang.select.btnAdd,
\r
411 style : 'width:100%;',
\r
412 onClick : function()
\r
415 var dialog = this.getDialog(),
\r
416 parentEditor = dialog.getParentEditor(),
\r
417 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
418 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
419 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
420 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
422 addOption(names, optName.getValue(), optName.getValue(), dialog.getParentEditor().document );
\r
423 addOption(values, optValue.getValue(), optValue.getValue(), dialog.getParentEditor().document );
\r
425 optName.setValue( "" );
\r
426 optValue.setValue( "" );
\r
432 label : editor.lang.select.btnModify,
\r
433 title : editor.lang.select.btnModify,
\r
434 style : 'width:100%;',
\r
435 onClick : function()
\r
437 //Modify selected option.
\r
438 var dialog = this.getDialog(),
\r
439 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
440 optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
\r
441 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
442 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
443 iIndex = getSelectedIndex( names );
\r
447 modifyOption( names, iIndex, optName.getValue(), optName.getValue() );
\r
448 modifyOption( values, iIndex, optValue.getValue(), optValue.getValue() );
\r
455 style : 'width:100%;',
\r
456 label : editor.lang.select.btnUp,
\r
457 title : editor.lang.select.btnUp,
\r
458 onClick : function()
\r
461 var dialog = this.getDialog(),
\r
462 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
463 values = dialog.getContentElement( 'info', 'cmbValue' );
\r
465 changeOptionPosition( names, -1, dialog.getParentEditor().document );
\r
466 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 id : 'btnSetValue',
\r
498 label : editor.lang.select.btnSetValue,
\r
499 title : editor.lang.select.btnSetValue,
\r
500 onClick : function()
\r
502 //Set as default value.
\r
503 var dialog = this.getDialog(),
\r
504 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
505 txtValue = dialog.getContentElement( 'info', 'txtValue' );
\r
506 txtValue.setValue( values.getValue() );
\r
512 label : editor.lang.select.btnDelete,
\r
513 title : editor.lang.select.btnDelete,
\r
514 onClick : function()
\r
517 var dialog = this.getDialog(),
\r
518 names = dialog.getContentElement( 'info', 'cmbName' ),
\r
519 values = dialog.getContentElement( 'info', 'cmbValue' ),
\r
520 optName = dialog.getContentElement( 'info', 'txtOptName' ),
\r
521 optValue = dialog.getContentElement( 'info', 'txtOptValue' );
\r
523 removeSelectedOptions( names );
\r
524 removeSelectedOptions( values );
\r
526 optName.setValue( "" );
\r
527 optValue.setValue( "" );
\r
533 label : editor.lang.select.chkMulti,
\r
537 setup : function( name, element )
\r
539 if ( name == 'select' )
\r
540 this.setValue( element.getAttribute( 'multiple' ) );
\r
541 if ( CKEDITOR.env.webkit )
\r
542 this.getElement().getParent().setStyle( 'vertical-align', 'middle' );
\r
544 commit : function( element )
\r
546 if ( this.getValue() )
\r
547 element.setAttribute( 'multiple', this.getValue() );
\r
549 element.removeAttribute( 'multiple' );
\r