2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 /** @fileoverview The "dialogui" plugin. */
8 CKEDITOR.plugins.add( 'dialogui' );
12 var initPrivateObject = function( elementDefinition )
14 this._ || ( this._ = {} );
15 this._['default'] = this._.initValue = elementDefinition['default'] || '';
16 this._.required = elementDefinition[ 'required' ] || false;
17 var args = [ this._ ];
18 for ( var i = 1 ; i < arguments.length ; i++ )
19 args.push( arguments[i] );
21 CKEDITOR.tools.extend.apply( CKEDITOR.tools, args );
26 build : function( dialog, elementDefinition, output )
28 return new CKEDITOR.ui.dialog.textInput( dialog, elementDefinition, output );
33 build : function( dialog, elementDefinition, output )
35 return new CKEDITOR.ui.dialog[elementDefinition.type]( dialog, elementDefinition, output );
40 build : function( dialog, elementDefinition, output )
42 var children = elementDefinition.children,
46 for ( var i = 0 ; ( i < children.length && ( child = children[i] ) ) ; i++ )
49 childHtmlList.push( childHtml );
50 childObjList.push( CKEDITOR.dialog._.uiElementBuilders[ child.type ].build( dialog, child, childHtml ) );
52 return new CKEDITOR.ui.dialog[ elementDefinition.type ]( dialog, childObjList, childHtmlList, output, elementDefinition );
57 isChanged : function()
59 return this.getValue() != this.getInitValue();
62 reset : function( noChangeEvent )
64 this.setValue( this.getInitValue(), noChangeEvent );
67 setInitValue : function()
69 this._.initValue = this.getValue();
72 resetInitValue : function()
74 this._.initValue = this._['default'];
77 getInitValue : function()
79 return this._.initValue;
82 commonEventProcessors = CKEDITOR.tools.extend( {}, CKEDITOR.ui.dialog.uiElement.prototype.eventProcessors,
84 onChange : function( dialog, func )
86 if ( !this._.domOnChangeRegistered )
88 dialog.on( 'load', function()
90 this.getInputElement().on( 'change', function()
92 // Make sure 'onchange' doesn't get fired after dialog closed. (#5719)
93 if ( !dialog.parts.dialog.isVisible() )
96 this.fire( 'change', { value : this.getValue() } );
99 this._.domOnChangeRegistered = true;
102 this.on( 'change', func );
105 eventRegex = /^on([A-Z]\w+)/,
106 cleanInnerDefinition = function( def )
108 // An inner UI element should not have the parent's type, title or events.
111 if ( eventRegex.test( i ) || i == 'title' || i == 'type' )
117 CKEDITOR.tools.extend( CKEDITOR.ui.dialog,
118 /** @lends CKEDITOR.ui.dialog */
121 * Base class for all dialog elements with a textual label on the left.
124 * @extends CKEDITOR.ui.dialog.uiElement
125 * @param {CKEDITOR.dialog} dialog
126 * Parent dialog object.
127 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
128 * The element definition. Accepted fields:
130 * <li><strong>label</strong> (Required) The label string.</li>
131 * <li><strong>labelLayout</strong> (Optional) Put 'horizontal' here if the
132 * label element is to be layed out horizontally. Otherwise a vertical
133 * layout will be used.</li>
134 * <li><strong>widths</strong> (Optional) This applies only for horizontal
135 * layouts - an 2-element array of lengths to specify the widths of the
136 * label and the content element.</li>
138 * @param {Array} htmlList
139 * List of HTML code to output to.
140 * @param {Function} contentHtml
141 * A function returning the HTML code string to be added inside the content
144 labeledElement : function( dialog, elementDefinition, htmlList, contentHtml )
146 if ( arguments.length < 4 )
149 var _ = initPrivateObject.call( this, elementDefinition );
150 _.labelId = CKEDITOR.tools.getNextId() + '_label';
151 var children = this._.children = [];
153 var innerHTML = function()
156 requiredClass = elementDefinition.required ? ' cke_required' : '' ;
157 if ( elementDefinition.labelLayout != 'horizontal' )
158 html.push( '<label class="cke_dialog_ui_labeled_label' + requiredClass + '" ',
159 ' id="'+ _.labelId + '"',
160 ' for="' + _.inputId + '"',
161 ' style="' + elementDefinition.labelStyle + '">',
162 elementDefinition.label,
164 '<div class="cke_dialog_ui_labeled_content" role="presentation">',
165 contentHtml.call( this, dialog, elementDefinition ),
169 var hboxDefinition = {
171 widths : elementDefinition.widths,
177 html : '<label class="cke_dialog_ui_labeled_label' + requiredClass + '"' +
178 ' id="' + _.labelId + '"' +
179 ' for="' + _.inputId + '"' +
180 ' style="' + elementDefinition.labelStyle + '">' +
181 CKEDITOR.tools.htmlEncode( elementDefinition.label ) +
186 html : '<span class="cke_dialog_ui_labeled_content">' +
187 contentHtml.call( this, dialog, elementDefinition ) +
192 CKEDITOR.dialog._.uiElementBuilders.hbox.build( dialog, hboxDefinition, html );
194 return html.join( '' );
196 CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition, htmlList, 'div', null, { role : 'presentation' }, innerHTML );
200 * A text input with a label. This UI element class represents both the
201 * single-line text inputs and password inputs in dialog boxes.
204 * @extends CKEDITOR.ui.dialog.labeledElement
205 * @param {CKEDITOR.dialog} dialog
206 * Parent dialog object.
207 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
208 * The element definition. Accepted fields:
210 * <li><strong>default</strong> (Optional) The default value.</li>
211 * <li><strong>validate</strong> (Optional) The validation function. </li>
212 * <li><strong>maxLength</strong> (Optional) The maximum length of text box
214 * <li><strong>size</strong> (Optional) The size of the text box. This is
215 * usually overridden by the size defined by the skin, however.</li>
217 * @param {Array} htmlList
218 * List of HTML code to output to.
220 textInput : function( dialog, elementDefinition, htmlList )
222 if ( arguments.length < 3 )
225 initPrivateObject.call( this, elementDefinition );
226 var domId = this._.inputId = CKEDITOR.tools.getNextId() + '_textInput',
227 attributes = { 'class' : 'cke_dialog_ui_input_' + elementDefinition.type, id : domId, type : 'text' },
230 // Set the validator, if any.
231 if ( elementDefinition.validate )
232 this.validate = elementDefinition.validate;
234 // Set the max length and size.
235 if ( elementDefinition.maxLength )
236 attributes.maxlength = elementDefinition.maxLength;
237 if ( elementDefinition.size )
238 attributes.size = elementDefinition.size;
240 if ( elementDefinition.controlStyle )
241 attributes.style = elementDefinition.controlStyle;
243 // If user presses Enter in a text box, it implies clicking OK for the dialog.
244 var me = this, keyPressedOnMe = false;
245 dialog.on( 'load', function()
247 me.getInputElement().on( 'keydown', function( evt )
249 if ( evt.data.getKeystroke() == 13 )
250 keyPressedOnMe = true;
253 // Lower the priority this 'keyup' since 'ok' will close the dialog.(#3749)
254 me.getInputElement().on( 'keyup', function( evt )
256 if ( evt.data.getKeystroke() == 13 && keyPressedOnMe )
258 dialog.getButton( 'ok' ) && setTimeout( function ()
260 dialog.getButton( 'ok' ).click();
262 keyPressedOnMe = false;
264 }, null, null, 1000 );
268 var innerHTML = function()
270 // IE BUG: Text input fields in IE at 100% would exceed a <td> or inline
271 // container's width, so need to wrap it inside a <div>.
272 var html = [ '<div class="cke_dialog_ui_input_', elementDefinition.type, '" role="presentation"' ];
274 if ( elementDefinition.width )
275 html.push( 'style="width:'+ elementDefinition.width +'" ' );
277 html.push( '><input ' );
279 attributes[ 'aria-labelledby' ] = this._.labelId;
280 this._.required && ( attributes[ 'aria-required' ] = this._.required );
281 for ( var i in attributes )
282 html.push( i + '="' + attributes[i] + '" ' );
283 html.push( ' /></div>' );
284 return html.join( '' );
286 CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
290 * A text area with a label on the top or left.
292 * @extends CKEDITOR.ui.dialog.labeledElement
294 * @param {CKEDITOR.dialog} dialog
295 * Parent dialog object.
296 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
297 * The element definition. Accepted fields:
299 * <li><strong>rows</strong> (Optional) The number of rows displayed.
300 * Defaults to 5 if not defined.</li>
301 * <li><strong>cols</strong> (Optional) The number of cols displayed.
302 * Defaults to 20 if not defined. Usually overridden by skins.</li>
303 * <li><strong>default</strong> (Optional) The default value.</li>
304 * <li><strong>validate</strong> (Optional) The validation function. </li>
306 * @param {Array} htmlList
307 * List of HTML code to output to.
309 textarea : function( dialog, elementDefinition, htmlList )
311 if ( arguments.length < 3 )
314 initPrivateObject.call( this, elementDefinition );
316 domId = this._.inputId = CKEDITOR.tools.getNextId() + '_textarea',
319 if ( elementDefinition.validate )
320 this.validate = elementDefinition.validate;
322 // Generates the essential attributes for the textarea tag.
323 attributes.rows = elementDefinition.rows || 5;
324 attributes.cols = elementDefinition.cols || 20;
327 var innerHTML = function()
329 attributes[ 'aria-labelledby' ] = this._.labelId;
330 this._.required && ( attributes[ 'aria-required' ] = this._.required );
331 var html = [ '<div class="cke_dialog_ui_input_textarea" role="presentation"><textarea class="cke_dialog_ui_input_textarea" id="', domId, '" ' ];
332 for ( var i in attributes )
333 html.push( i + '="' + CKEDITOR.tools.htmlEncode( attributes[i] ) + '" ' );
334 html.push( '>', CKEDITOR.tools.htmlEncode( me._['default'] ), '</textarea></div>' );
335 return html.join( '' );
337 CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
341 * A single checkbox with a label on the right.
343 * @extends CKEDITOR.ui.dialog.uiElement
345 * @param {CKEDITOR.dialog} dialog
346 * Parent dialog object.
347 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
348 * The element definition. Accepted fields:
350 * <li><strong>checked</strong> (Optional) Whether the checkbox is checked
351 * on instantiation. Defaults to false.</li>
352 * <li><strong>validate</strong> (Optional) The validation function.</li>
353 * <li><strong>label</strong> (Optional) The checkbox label.</li>
355 * @param {Array} htmlList
356 * List of HTML code to output to.
358 checkbox : function( dialog, elementDefinition, htmlList )
360 if ( arguments.length < 3 )
363 var _ = initPrivateObject.call( this, elementDefinition, { 'default' : !!elementDefinition[ 'default' ] } );
365 if ( elementDefinition.validate )
366 this.validate = elementDefinition.validate;
369 var innerHTML = function()
371 var myDefinition = CKEDITOR.tools.extend( {}, elementDefinition,
373 id : elementDefinition.id ? elementDefinition.id + '_checkbox' : CKEDITOR.tools.getNextId() + '_checkbox'
377 var labelId = CKEDITOR.tools.getNextId() + '_label';
378 var attributes = { 'class' : 'cke_dialog_ui_checkbox_input', type : 'checkbox', 'aria-labelledby' : labelId };
379 cleanInnerDefinition( myDefinition );
380 if ( elementDefinition[ 'default' ] )
381 attributes.checked = 'checked';
383 if ( typeof myDefinition.controlStyle != 'undefined' )
384 myDefinition.style = myDefinition.controlStyle;
386 _.checkbox = new CKEDITOR.ui.dialog.uiElement( dialog, myDefinition, html, 'input', null, attributes );
387 html.push( ' <label id="', labelId, '" for="', attributes.id, '">',
388 CKEDITOR.tools.htmlEncode( elementDefinition.label ),
390 return html.join( '' );
393 CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition, htmlList, 'span', null, null, innerHTML );
397 * A group of radio buttons.
400 * @extends CKEDITOR.ui.dialog.labeledElement
401 * @param {CKEDITOR.dialog} dialog
402 * Parent dialog object.
403 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
404 * The element definition. Accepted fields:
406 * <li><strong>default</strong> (Required) The default value.</li>
407 * <li><strong>validate</strong> (Optional) The validation function.</li>
408 * <li><strong>items</strong> (Required) An array of options. Each option
409 * is a 1- or 2-item array of format [ 'Description', 'Value' ]. If 'Value'
410 * is missing, then the value would be assumed to be the same as the
413 * @param {Array} htmlList
414 * List of HTML code to output to.
416 radio : function( dialog, elementDefinition, htmlList )
418 if ( arguments.length < 3)
421 initPrivateObject.call( this, elementDefinition );
422 if ( !this._['default'] )
423 this._['default'] = this._.initValue = elementDefinition.items[0][1];
424 if ( elementDefinition.validate )
425 this.validate = elementDefinition.valdiate;
426 var children = [], me = this;
429 var innerHTML = function()
431 var inputHtmlList = [], html = [],
432 commonAttributes = { 'class' : 'cke_dialog_ui_radio_item', 'aria-labelledby' : this._.labelId },
433 commonName = elementDefinition.id ? elementDefinition.id + '_radio' : CKEDITOR.tools.getNextId() + '_radio';
434 for ( var i = 0 ; i < elementDefinition.items.length ; i++ )
436 var item = elementDefinition.items[i],
437 title = item[2] !== undefined ? item[2] : item[0],
438 value = item[1] !== undefined ? item[1] : item[0],
439 inputId = CKEDITOR.tools.getNextId() + '_radio_input',
440 labelId = inputId + '_label',
441 inputDefinition = CKEDITOR.tools.extend( {}, elementDefinition,
447 labelDefinition = CKEDITOR.tools.extend( {}, inputDefinition,
454 'class' : 'cke_dialog_ui_radio_input',
457 'aria-labelledby' : labelId
460 if ( me._['default'] == value )
461 inputAttributes.checked = 'checked';
462 cleanInnerDefinition( inputDefinition );
463 cleanInnerDefinition( labelDefinition );
465 if ( typeof inputDefinition.controlStyle != 'undefined' )
466 inputDefinition.style = inputDefinition.controlStyle;
468 children.push( new CKEDITOR.ui.dialog.uiElement( dialog, inputDefinition, inputHtml, 'input', null, inputAttributes ) );
469 inputHtml.push( ' ' );
470 new CKEDITOR.ui.dialog.uiElement( dialog, labelDefinition, inputHtml, 'label', null, { id : labelId, 'for' : inputAttributes.id },
472 inputHtmlList.push( inputHtml.join( '' ) );
474 new CKEDITOR.ui.dialog.hbox( dialog, [], inputHtmlList, html );
475 return html.join( '' );
478 CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
479 this._.children = children;
483 * A button with a label inside.
486 * @extends CKEDITOR.ui.dialog.uiElement
487 * @param {CKEDITOR.dialog} dialog
488 * Parent dialog object.
489 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
490 * The element definition. Accepted fields:
492 * <li><strong>label</strong> (Required) The button label.</li>
493 * <li><strong>disabled</strong> (Optional) Set to true if you want the
494 * button to appear in disabled state.</li>
496 * @param {Array} htmlList
497 * List of HTML code to output to.
499 button : function( dialog, elementDefinition, htmlList )
501 if ( !arguments.length )
504 if ( typeof elementDefinition == 'function' )
505 elementDefinition = elementDefinition( dialog.getParentEditor() );
507 initPrivateObject.call( this, elementDefinition, { disabled : elementDefinition.disabled || false } );
509 // Add OnClick event to this input.
510 CKEDITOR.event.implementOn( this );
514 // Register an event handler for processing button clicks.
515 dialog.on( 'load', function( eventInfo )
517 var element = this.getElement();
521 element.on( 'click', function( evt )
523 me.fire( 'click', { dialog : me.getDialog() } );
524 evt.data.preventDefault();
527 element.on( 'keydown', function( evt )
529 if ( evt.data.getKeystroke() in { 32:1 } )
532 evt.data.preventDefault();
537 element.unselectable();
540 var outerDefinition = CKEDITOR.tools.extend( {}, elementDefinition );
541 delete outerDefinition.style;
543 var labelId = CKEDITOR.tools.getNextId() + '_label';
544 CKEDITOR.ui.dialog.uiElement.call(
552 style : elementDefinition.style,
553 href : 'javascript:void(0)',
554 title : elementDefinition.label,
556 'class' : elementDefinition['class'],
558 'aria-labelledby' : labelId
560 '<span id="' + labelId + '" class="cke_dialog_ui_button">' +
561 CKEDITOR.tools.htmlEncode( elementDefinition.label ) +
567 * @extends CKEDITOR.ui.dialog.uiElement
570 * @param {CKEDITOR.dialog} dialog
571 * Parent dialog object.
572 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
573 * The element definition. Accepted fields:
575 * <li><strong>default</strong> (Required) The default value.</li>
576 * <li><strong>validate</strong> (Optional) The validation function.</li>
577 * <li><strong>items</strong> (Required) An array of options. Each option
578 * is a 1- or 2-item array of format [ 'Description', 'Value' ]. If 'Value'
579 * is missing, then the value would be assumed to be the same as the
581 * <li><strong>multiple</strong> (Optional) Set this to true if you'd like
582 * to have a multiple-choice select box.</li>
583 * <li><strong>size</strong> (Optional) The number of items to display in
584 * the select box.</li>
586 * @param {Array} htmlList
587 * List of HTML code to output to.
589 select : function( dialog, elementDefinition, htmlList )
591 if ( arguments.length < 3 )
594 var _ = initPrivateObject.call( this, elementDefinition );
596 if ( elementDefinition.validate )
597 this.validate = elementDefinition.validate;
599 _.inputId = CKEDITOR.tools.getNextId() + '_select';
601 var innerHTML = function()
603 var myDefinition = CKEDITOR.tools.extend( {}, elementDefinition,
605 id : elementDefinition.id ? elementDefinition.id + '_select' : CKEDITOR.tools.getNextId() + '_select'
609 attributes = { 'id' : _.inputId, 'class' : 'cke_dialog_ui_input_select', 'aria-labelledby' : this._.labelId };
611 // Add multiple and size attributes from element definition.
612 if ( elementDefinition.size != undefined )
613 attributes.size = elementDefinition.size;
614 if ( elementDefinition.multiple != undefined )
615 attributes.multiple = elementDefinition.multiple;
617 cleanInnerDefinition( myDefinition );
618 for ( var i = 0, item ; i < elementDefinition.items.length && ( item = elementDefinition.items[i] ) ; i++ )
620 innerHTML.push( '<option value="',
621 CKEDITOR.tools.htmlEncode( item[1] !== undefined ? item[1] : item[0] ), '" /> ',
622 CKEDITOR.tools.htmlEncode( item[0] ) );
625 if ( typeof myDefinition.controlStyle != 'undefined' )
626 myDefinition.style = myDefinition.controlStyle;
628 _.select = new CKEDITOR.ui.dialog.uiElement( dialog, myDefinition, html, 'select', null, attributes, innerHTML.join( '' ) );
629 return html.join( '' );
632 CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
636 * A file upload input.
637 * @extends CKEDITOR.ui.dialog.labeledElement
640 * @param {CKEDITOR.dialog} dialog
641 * Parent dialog object.
642 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
643 * The element definition. Accepted fields:
645 * <li><strong>validate</strong> (Optional) The validation function.</li>
647 * @param {Array} htmlList
648 * List of HTML code to output to.
650 file : function( dialog, elementDefinition, htmlList )
652 if ( arguments.length < 3 )
655 if ( elementDefinition['default'] === undefined )
656 elementDefinition['default'] = '';
658 var _ = CKEDITOR.tools.extend( initPrivateObject.call( this, elementDefinition ), { definition : elementDefinition, buttons : [] } );
660 if ( elementDefinition.validate )
661 this.validate = elementDefinition.validate;
664 var innerHTML = function()
666 _.frameId = CKEDITOR.tools.getNextId() + '_fileInput';
668 // Support for custom document.domain in IE.
669 var isCustomDomain = CKEDITOR.env.isCustomDomain();
674 ' allowtransparency="0"' +
675 ' class="cke_dialog_ui_input_file"' +
676 ' id="', _.frameId, '"' +
677 ' title="', elementDefinition.label, '"' +
678 ' src="javascript:void(' ];
684 'document.domain=\'' + document.domain + '\';' +
685 'document.close();' +
694 return html.join( '' );
697 // IE BUG: Parent container does not resize to contain the iframe automatically.
698 dialog.on( 'load', function()
700 var iframe = CKEDITOR.document.getById( _.frameId ),
701 contentDiv = iframe.getParent();
702 contentDiv.addClass( 'cke_dialog_ui_input_file' );
705 CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
709 * A button for submitting the file in a file upload input.
710 * @extends CKEDITOR.ui.dialog.button
713 * @param {CKEDITOR.dialog} dialog
714 * Parent dialog object.
715 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
716 * The element definition. Accepted fields:
718 * <li><strong>for</strong> (Required) The file input's page and element Id
719 * to associate to, in a 2-item array format: [ 'page_id', 'element_id' ].
721 * <li><strong>validate</strong> (Optional) The validation function.</li>
723 * @param {Array} htmlList
724 * List of HTML code to output to.
726 fileButton : function( dialog, elementDefinition, htmlList )
728 if ( arguments.length < 3 )
731 var _ = initPrivateObject.call( this, elementDefinition ),
734 if ( elementDefinition.validate )
735 this.validate = elementDefinition.validate;
737 var myDefinition = CKEDITOR.tools.extend( {}, elementDefinition );
738 var onClick = myDefinition.onClick;
739 myDefinition.className = ( myDefinition.className ? myDefinition.className + ' ' : '' ) + 'cke_dialog_ui_button';
740 myDefinition.onClick = function( evt )
742 var target = elementDefinition[ 'for' ]; // [ pageId, elementId ]
743 if ( !onClick || onClick.call( this, evt ) !== false )
745 dialog.getContentElement( target[0], target[1] ).submit();
750 dialog.on( 'load', function()
752 dialog.getContentElement( elementDefinition[ 'for' ][0], elementDefinition[ 'for' ][1] )._.buttons.push( me );
755 CKEDITOR.ui.dialog.button.call( this, dialog, myDefinition, htmlList );
760 var myHtmlRe = /^\s*<[\w:]+\s+([^>]*)?>/,
761 theirHtmlRe = /^(\s*<[\w:]+(?:\s+[^>]*)?)((?:.|\r|\n)+)$/,
764 * A dialog element made from raw HTML code.
765 * @extends CKEDITOR.ui.dialog.uiElement
766 * @name CKEDITOR.ui.dialog.html
767 * @param {CKEDITOR.dialog} dialog Parent dialog object.
768 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition Element definition.
771 * <li><strong>html</strong> (Required) HTML code of this element.</li>
773 * @param {Array} htmlList List of HTML code to be added to the dialog's content area.
777 return function( dialog, elementDefinition, htmlList )
779 if ( arguments.length < 3 )
784 theirHtml = elementDefinition.html,
787 // If the HTML input doesn't contain any tags at the beginning, add a <span> tag around it.
788 if ( theirHtml.charAt( 0 ) != '<' )
789 theirHtml = '<span>' + theirHtml + '</span>';
791 // Look for focus function in definition.
792 var focus = elementDefinition.focus;
795 var oldFocus = this.focus;
796 this.focus = function()
798 oldFocus.call( this );
799 typeof focus == 'function' && focus.call( this );
800 this.fire( 'focus' );
802 if ( elementDefinition.isFocusable )
804 var oldIsFocusable = this.isFocusable;
805 this.isFocusable = oldIsFocusable;
807 this.keyboardFocusable = true;
810 CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition, myHtmlList, 'span', null, null, '' );
812 // Append the attributes created by the uiElement call to the real HTML.
813 myHtml = myHtmlList.join( '' );
814 myMatch = myHtml.match( myHtmlRe );
815 theirMatch = theirHtml.match( theirHtmlRe ) || [ '', '', '' ];
817 if ( emptyTagRe.test( theirMatch[1] ) )
819 theirMatch[1] = theirMatch[1].slice( 0, -1 );
820 theirMatch[2] = '/' + theirMatch[2];
823 htmlList.push( [ theirMatch[1], ' ', myMatch[1] || '', theirMatch[2] ].join( '' ) );
828 * Form fieldset for grouping dialog UI elements.
830 * @extends CKEDITOR.ui.dialog.uiElement
831 * @param {CKEDITOR.dialog} dialog Parent dialog object.
832 * @param {Array} childObjList
833 * Array of {@link CKEDITOR.ui.dialog.uiElement} objects inside this
835 * @param {Array} childHtmlList
836 * Array of HTML code that correspond to the HTML output of all the
837 * objects in childObjList.
838 * @param {Array} htmlList
839 * Array of HTML code that this element will output to.
840 * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
841 * The element definition. Accepted fields:
843 * <li><strong>label</strong> (Optional) The legend of the this fieldset.</li>
844 * <li><strong>children</strong> (Required) An array of dialog field definitions which will be grouped inside this fieldset. </li>
847 fieldset : function( dialog, childObjList, childHtmlList, htmlList, elementDefinition )
849 var legendLabel = elementDefinition.label;
851 var innerHTML = function()
854 legendLabel && html.push( '<legend>' + legendLabel + '</legend>' );
855 for ( var i = 0; i < childHtmlList.length; i++ )
856 html.push( childHtmlList[ i ] );
857 return html.join( '' );
860 this._ = { children : childObjList };
861 CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition, htmlList, 'fieldset', null, null, innerHTML );
866 CKEDITOR.ui.dialog.html.prototype = new CKEDITOR.ui.dialog.uiElement;
868 CKEDITOR.ui.dialog.labeledElement.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement,
869 /** @lends CKEDITOR.ui.dialog.labeledElement.prototype */
872 * Sets the label text of the element.
873 * @param {String} label The new label text.
874 * @returns {CKEDITOR.ui.dialog.labeledElement} The current labeled element.
877 setLabel : function( label )
879 var node = CKEDITOR.document.getById( this._.labelId );
880 if ( node.getChildCount() < 1 )
881 ( new CKEDITOR.dom.text( label, CKEDITOR.document ) ).appendTo( node );
883 node.getChild( 0 ).$.nodeValue = label;
888 * Retrieves the current label text of the elment.
889 * @returns {String} The current label text.
892 getLabel : function()
894 var node = CKEDITOR.document.getById( this._.labelId );
895 if ( !node || node.getChildCount() < 1 )
898 return node.getChild( 0 ).getText();
902 * Defines the onChange event for UI element definitions.
907 eventProcessors : commonEventProcessors
910 CKEDITOR.ui.dialog.button.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement,
911 /** @lends CKEDITOR.ui.dialog.button.prototype */
914 * Simulates a click to the button.
916 * @returns {Object} Return value of the 'click' event.
920 if ( !this._.disabled )
921 return this.fire( 'click', { dialog : this._.dialog } );
922 this.getElement().$.blur();
927 * Enables the button.
932 this._.disabled = false;
933 var element = this.getElement();
934 element && element.removeClass( 'cke_disabled' );
938 * Disables the button.
943 this._.disabled = true;
944 this.getElement().addClass( 'cke_disabled' );
947 isVisible : function()
949 return this.getElement().getFirst().isVisible();
952 isEnabled : function()
954 return !this._.disabled;
958 * Defines the onChange event and onClick for button element definitions.
963 eventProcessors : CKEDITOR.tools.extend( {}, CKEDITOR.ui.dialog.uiElement.prototype.eventProcessors,
966 onClick : function( dialog, func )
968 this.on( 'click', func );
973 * Handler for the element's access key up event. Simulates a click to
977 accessKeyUp : function()
983 * Handler for the element's access key down event. Simulates a mouse
984 * down to the button.
987 accessKeyDown : function()
992 keyboardFocusable : true
995 CKEDITOR.ui.dialog.textInput.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.labeledElement,
996 /** @lends CKEDITOR.ui.dialog.textInput.prototype */
999 * Gets the text input DOM element under this UI object.
1001 * @returns {CKEDITOR.dom.element} The DOM element of the text input.
1003 getInputElement : function()
1005 return CKEDITOR.document.getById( this._.inputId );
1009 * Puts focus into the text input.
1014 var me = this.selectParentTab();
1016 // GECKO BUG: setTimeout() is needed to workaround invisible selections.
1017 setTimeout( function()
1019 var element = me.getInputElement();
1020 element && element.$.focus();
1025 * Selects all the text in the text input.
1030 var me = this.selectParentTab();
1032 // GECKO BUG: setTimeout() is needed to workaround invisible selections.
1033 setTimeout( function()
1035 var e = me.getInputElement();
1045 * Handler for the text input's access key up event. Makes a select()
1046 * call to the text input.
1049 accessKeyUp : function()
1055 * Sets the value of this text input object.
1056 * @param {Object} value The new value.
1057 * @returns {CKEDITOR.ui.dialog.textInput} The current UI element.
1059 * uiElement.setValue( 'Blamo' );
1061 setValue : function( value )
1063 !value && ( value = '' );
1064 return CKEDITOR.ui.dialog.uiElement.prototype.setValue.apply( this, arguments );
1067 keyboardFocusable : true
1068 }, commonPrototype, true );
1070 CKEDITOR.ui.dialog.textarea.prototype = new CKEDITOR.ui.dialog.textInput();
1072 CKEDITOR.ui.dialog.select.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.labeledElement,
1073 /** @lends CKEDITOR.ui.dialog.select.prototype */
1076 * Gets the DOM element of the select box.
1077 * @returns {CKEDITOR.dom.element} The <select> element of this UI
1081 getInputElement : function()
1083 return this._.select.getElement();
1087 * Adds an option to the select box.
1088 * @param {String} label Option label.
1089 * @param {String} value (Optional) Option value, if not defined it'll be
1090 * assumed to be the same as the label.
1091 * @param {Number} index (Optional) Position of the option to be inserted
1092 * to. If not defined the new option will be inserted to the end of list.
1094 * @returns {CKEDITOR.ui.dialog.select} The current select UI element.
1096 add : function( label, value, index )
1098 var option = new CKEDITOR.dom.element( 'option', this.getDialog().getParentEditor().document ),
1099 selectElement = this.getInputElement().$;
1100 option.$.text = label;
1101 option.$.value = ( value === undefined || value === null ) ? label : value;
1102 if ( index === undefined || index === null )
1104 if ( CKEDITOR.env.ie )
1105 selectElement.add( option.$ );
1107 selectElement.add( option.$, null );
1110 selectElement.add( option.$, index );
1115 * Removes an option from the selection list.
1116 * @param {Number} index Index of the option to be removed.
1118 * @returns {CKEDITOR.ui.dialog.select} The current select UI element.
1120 remove : function( index )
1122 var selectElement = this.getInputElement().$;
1123 selectElement.remove( index );
1128 * Clears all options out of the selection list.
1129 * @returns {CKEDITOR.ui.dialog.select} The current select UI element.
1133 var selectElement = this.getInputElement().$;
1134 while ( selectElement.length > 0 )
1135 selectElement.remove( 0 );
1139 keyboardFocusable : true
1140 }, commonPrototype, true );
1142 CKEDITOR.ui.dialog.checkbox.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement,
1143 /** @lends CKEDITOR.ui.dialog.checkbox.prototype */
1146 * Gets the checkbox DOM element.
1148 * @returns {CKEDITOR.dom.element} The DOM element of the checkbox.
1150 getInputElement : function()
1152 return this._.checkbox.getElement();
1156 * Sets the state of the checkbox.
1158 * @param {Boolean} true to tick the checkbox, false to untick it.
1159 * @param {Boolean} noChangeEvent Internal commit, to supress 'change' event on this element.
1161 setValue : function( checked, noChangeEvent )
1163 this.getInputElement().$.checked = checked;
1164 !noChangeEvent && this.fire( 'change', { value : checked } );
1168 * Gets the state of the checkbox.
1170 * @returns {Boolean} true means the checkbox is ticked, false means it's not ticked.
1172 getValue : function()
1174 return this.getInputElement().$.checked;
1178 * Handler for the access key up event. Toggles the checkbox.
1181 accessKeyUp : function()
1183 this.setValue( !this.getValue() );
1187 * Defines the onChange event for UI element definitions.
1194 onChange : function( dialog, func )
1196 if ( !CKEDITOR.env.ie )
1197 return commonEventProcessors.onChange.apply( this, arguments );
1200 dialog.on( 'load', function()
1202 var element = this._.checkbox.getElement();
1203 element.on( 'propertychange', function( evt )
1206 if ( evt.propertyName == 'checked' )
1207 this.fire( 'change', { value : element.$.checked } );
1210 this.on( 'change', func );
1216 keyboardFocusable : true
1217 }, commonPrototype, true );
1219 CKEDITOR.ui.dialog.radio.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement,
1220 /** @lends CKEDITOR.ui.dialog.radio.prototype */
1223 * Checks one of the radio buttons in this button group.
1225 * @param {String} value The value of the button to be chcked.
1226 * @param {Boolean} noChangeEvent Internal commit, to supress 'change' event on this element.
1228 setValue : function( value, noChangeEvent )
1230 var children = this._.children,
1232 for ( var i = 0 ; ( i < children.length ) && ( item = children[i] ) ; i++ )
1233 item.getElement().$.checked = ( item.getValue() == value );
1234 !noChangeEvent && this.fire( 'change', { value : value } );
1238 * Gets the value of the currently checked radio button.
1240 * @returns {String} The currently checked button's value.
1242 getValue : function()
1244 var children = this._.children;
1245 for ( var i = 0 ; i < children.length ; i++ )
1247 if ( children[i].getElement().$.checked )
1248 return children[i].getValue();
1254 * Handler for the access key up event. Focuses the currently
1255 * selected radio button, or the first radio button if none is
1259 accessKeyUp : function()
1261 var children = this._.children, i;
1262 for ( i = 0 ; i < children.length ; i++ )
1264 if ( children[i].getElement().$.checked )
1266 children[i].getElement().focus();
1270 children[0].getElement().focus();
1274 * Defines the onChange event for UI element definitions.
1281 onChange : function( dialog, func )
1283 if ( !CKEDITOR.env.ie )
1284 return commonEventProcessors.onChange.apply( this, arguments );
1287 dialog.on( 'load', function()
1289 var children = this._.children, me = this;
1290 for ( var i = 0 ; i < children.length ; i++ )
1292 var element = children[i].getElement();
1293 element.on( 'propertychange', function( evt )
1296 if ( evt.propertyName == 'checked' && this.$.checked )
1297 me.fire( 'change', { value : this.getAttribute( 'value' ) } );
1301 this.on( 'change', func );
1307 keyboardFocusable : true
1308 }, commonPrototype, true );
1310 CKEDITOR.ui.dialog.file.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.labeledElement,
1312 /** @lends CKEDITOR.ui.dialog.file.prototype */
1315 * Gets the <input> element of this file input.
1316 * @returns {CKEDITOR.dom.element} The file input element.
1319 getInputElement : function()
1321 var frameDocument = CKEDITOR.document.getById( this._.frameId ).getFrameDocument();
1322 return frameDocument.$.forms.length > 0 ?
1323 new CKEDITOR.dom.element( frameDocument.$.forms[0].elements[0] ) :
1328 * Uploads the file in the file input.
1329 * @returns {CKEDITOR.ui.dialog.file} This object.
1334 this.getInputElement().getParent().$.submit();
1339 * Get the action assigned to the form.
1340 * @returns {String} The value of the action.
1343 getAction : function()
1345 return this.getInputElement().getParent().$.action;
1349 * The events must be applied on the inner input element, and
1350 * that must be done when the iframe & form has been loaded
1352 registerEvents : function( definition )
1354 var regex = /^on([A-Z]\w+)/,
1357 var registerDomEvent = function( uiElement, dialog, eventName, func )
1359 uiElement.on( 'formLoaded', function()
1361 uiElement.getInputElement().on( eventName, func, uiElement );
1365 for ( var i in definition )
1367 if ( !( match = i.match( regex ) ) )
1370 if ( this.eventProcessors[i] )
1371 this.eventProcessors[i].call( this, this._.dialog, definition[i] );
1373 registerDomEvent( this, this._.dialog, match[1].toLowerCase(), definition[i] );
1380 * Redraws the file input and resets the file path in the file input.
1381 * The redraw logic is necessary because non-IE browsers tend to clear
1382 * the <iframe> containing the file input after closing the dialog.
1388 frameElement = CKEDITOR.document.getById( _.frameId ),
1389 frameDocument = frameElement.getFrameDocument(),
1390 elementDefinition = _.definition,
1391 buttons = _.buttons,
1392 callNumber = this.formLoadedNumber,
1393 unloadNumber = this.formUnloadNumber,
1394 langDir = _.dialog._.editor.lang.dir,
1395 langCode = _.dialog._.editor.langCode;
1397 // The callback function for the iframe, but we must call tools.addFunction only once
1398 // so we store the function number in this.formLoadedNumber
1401 callNumber = this.formLoadedNumber = CKEDITOR.tools.addFunction(
1404 // Now we can apply the events to the input type=file
1405 this.fire( 'formLoaded' ) ;
1408 // Remove listeners attached to the content of the iframe (the file input)
1409 unloadNumber = this.formUnloadNumber = CKEDITOR.tools.addFunction(
1412 this.getInputElement().clearCustomData();
1415 this.getDialog()._.editor.on( 'destroy', function()
1417 CKEDITOR.tools.removeFunction( callNumber );
1418 CKEDITOR.tools.removeFunction( unloadNumber );
1422 function generateFormField()
1424 frameDocument.$.open();
1426 // Support for custom document.domain in IE.
1427 if ( CKEDITOR.env.isCustomDomain() )
1428 frameDocument.$.domain = document.domain;
1431 if ( elementDefinition.size )
1432 size = elementDefinition.size - ( CKEDITOR.env.ie ? 7 : 0 ); // "Browse" button is bigger in IE.
1434 frameDocument.$.write( [ '<html dir="' + langDir + '" lang="' + langCode + '"><head><title></title></head><body style="margin: 0; overflow: hidden; background: transparent;">',
1435 '<form enctype="multipart/form-data" method="POST" dir="' + langDir + '" lang="' + langCode + '" action="',
1436 CKEDITOR.tools.htmlEncode( elementDefinition.action ),
1438 '<input type="file" name="',
1439 CKEDITOR.tools.htmlEncode( elementDefinition.id || 'cke_upload' ),
1441 CKEDITOR.tools.htmlEncode( size > 0 ? size : "" ),
1445 '<script>window.parent.CKEDITOR.tools.callFunction(' + callNumber + ');',
1446 'window.onbeforeunload = function() {window.parent.CKEDITOR.tools.callFunction(' + unloadNumber + ')}</script>' ].join( '' ) );
1448 frameDocument.$.close();
1450 for ( var i = 0 ; i < buttons.length ; i++ )
1451 buttons[i].enable();
1454 // #3465: Wait for the browser to finish rendering the dialog first.
1455 if ( CKEDITOR.env.gecko )
1456 setTimeout( generateFormField, 500 );
1458 generateFormField();
1461 getValue : function()
1463 return this.getInputElement().$.value || '';
1467 * The default value of input type="file" is an empty string, but during initialization
1468 * of this UI element, the iframe still isn't ready so it can't be read from that object
1469 * Setting it manually prevents later issues about the current value ("") being different
1470 * of the initial value (undefined as it asked for .value of a div)
1472 setInitValue : function()
1474 this._.initValue = '';
1478 * Defines the onChange event for UI element definitions.
1485 onChange : function( dialog, func )
1487 // If this method is called several times (I'm not sure about how this can happen but the default
1488 // onChange processor includes this protection)
1489 // In order to reapply to the new element, the property is deleted at the beggining of the registerEvents method
1490 if ( !this._.domOnChangeRegistered )
1492 // By listening for the formLoaded event, this handler will get reapplied when a new
1494 this.on( 'formLoaded', function()
1496 this.getInputElement().on( 'change', function(){ this.fire( 'change', { value : this.getValue() } ); }, this );
1498 this._.domOnChangeRegistered = true;
1501 this.on( 'change', func );
1505 keyboardFocusable : true
1508 CKEDITOR.ui.dialog.fileButton.prototype = new CKEDITOR.ui.dialog.button;
1510 CKEDITOR.ui.dialog.fieldset.prototype = CKEDITOR.tools.clone( CKEDITOR.ui.dialog.hbox.prototype );
1512 CKEDITOR.dialog.addUIElement( 'text', textBuilder );
1513 CKEDITOR.dialog.addUIElement( 'password', textBuilder );
1514 CKEDITOR.dialog.addUIElement( 'textarea', commonBuilder );
1515 CKEDITOR.dialog.addUIElement( 'checkbox', commonBuilder );
1516 CKEDITOR.dialog.addUIElement( 'radio', commonBuilder );
1517 CKEDITOR.dialog.addUIElement( 'button', commonBuilder );
1518 CKEDITOR.dialog.addUIElement( 'select', commonBuilder );
1519 CKEDITOR.dialog.addUIElement( 'file', commonBuilder );
1520 CKEDITOR.dialog.addUIElement( 'fileButton', commonBuilder );
1521 CKEDITOR.dialog.addUIElement( 'html', commonBuilder );
1522 CKEDITOR.dialog.addUIElement( 'fieldset', containerBuilder );
1526 * Fired when the value of the uiElement is changed
1527 * @name CKEDITOR.ui.dialog.uiElement#change
1532 * Fired when the inner frame created by the element is ready.
1533 * Each time the button is used or the dialog is loaded a new
1534 * form might be created.
1535 * @name CKEDITOR.ui.dialog.fileButton#formLoaded