Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / plugins / dialog / plugin.js
bloba380c57933f693fbeace19ee1eefd11c98346374
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7  * @fileOverview The floating dialog plugin.
8  */
10 /**
11  * No resize for this dialog.
12  * @constant
13  */
14 CKEDITOR.DIALOG_RESIZE_NONE = 0;
16 /**
17  * Only allow horizontal resizing for this dialog, disable vertical resizing.
18  * @constant
19  */
20 CKEDITOR.DIALOG_RESIZE_WIDTH = 1;
22 /**
23  * Only allow vertical resizing for this dialog, disable horizontal resizing.
24  * @constant
25  */
26 CKEDITOR.DIALOG_RESIZE_HEIGHT = 2;
29  * Allow the dialog to be resized in both directions.
30  * @constant
31  */
32 CKEDITOR.DIALOG_RESIZE_BOTH = 3;
34 (function()
36         var cssLength = CKEDITOR.tools.cssLength;
37         function isTabVisible( tabId )
38         {
39                 return !!this._.tabs[ tabId ][ 0 ].$.offsetHeight;
40         }
42         function getPreviousVisibleTab()
43         {
44                 var tabId = this._.currentTabId,
45                         length = this._.tabIdList.length,
46                         tabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, tabId ) + length;
48                 for ( var i = tabIndex - 1 ; i > tabIndex - length ; i-- )
49                 {
50                         if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
51                                 return this._.tabIdList[ i % length ];
52                 }
54                 return null;
55         }
57         function getNextVisibleTab()
58         {
59                 var tabId = this._.currentTabId,
60                         length = this._.tabIdList.length,
61                         tabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, tabId );
63                 for ( var i = tabIndex + 1 ; i < tabIndex + length ; i++ )
64                 {
65                         if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
66                                 return this._.tabIdList[ i % length ];
67                 }
69                 return null;
70         }
73         function clearOrRecoverTextInputValue( container, isRecover )
74         {
75                 var inputs = container.$.getElementsByTagName( 'input' );
76                 for ( var i = 0, length = inputs.length; i < length ; i++ )
77                 {
78                         var item = new CKEDITOR.dom.element( inputs[ i ] );
80                         if ( item.getAttribute( 'type' ).toLowerCase() == 'text' )
81                         {
82                                 if ( isRecover )
83                                 {
84                                         item.setAttribute( 'value', item.getCustomData( 'fake_value' ) || '' );
85                                         item.removeCustomData( 'fake_value' );
86                                 }
87                                 else
88                                 {
89                                         item.setCustomData( 'fake_value', item.getAttribute( 'value' ) );
90                                         item.setAttribute( 'value', '' );
91                                 }
92                         }
93                 }
94         }
96         /**
97          * This is the base class for runtime dialog objects. An instance of this
98          * class represents a single named dialog for a single editor instance.
99          * @param {Object} editor The editor which created the dialog.
100          * @param {String} dialogName The dialog's registered name.
101          * @constructor
102          * @example
103          * var dialogObj = new CKEDITOR.dialog( editor, 'smiley' );
104          */
105         CKEDITOR.dialog = function( editor, dialogName )
106         {
107                 // Load the dialog definition.
108                 var definition = CKEDITOR.dialog._.dialogDefinitions[ dialogName ],
109                         defaultDefinition = CKEDITOR.tools.clone( defaultDialogDefinition ),
110                         buttonsOrder = editor.config.dialog_buttonsOrder || 'OS',
111                         dir = editor.lang.dir;
113                         if ( ( buttonsOrder == 'OS' && CKEDITOR.env.mac ) ||    // The buttons in MacOS Apps are in reverse order (#4750)
114                                 ( buttonsOrder == 'rtl' && dir == 'ltr' ) ||
115                                 ( buttonsOrder == 'ltr' && dir == 'rtl' ) )
116                                         defaultDefinition.buttons.reverse();
119                 // Completes the definition with the default values.
120                 definition = CKEDITOR.tools.extend( definition( editor ), defaultDefinition );
122                 // Clone a functionally independent copy for this dialog.
123                 definition = CKEDITOR.tools.clone( definition );
125                 // Create a complex definition object, extending it with the API
126                 // functions.
127                 definition = new definitionObject( this, definition );
129                 var doc = CKEDITOR.document;
131                 var themeBuilt = editor.theme.buildDialog( editor );
133                 // Initialize some basic parameters.
134                 this._ =
135                 {
136                         editor : editor,
137                         element : themeBuilt.element,
138                         name : dialogName,
139                         contentSize : { width : 0, height : 0 },
140                         size : { width : 0, height : 0 },
141                         contents : {},
142                         buttons : {},
143                         accessKeyMap : {},
145                         // Initialize the tab and page map.
146                         tabs : {},
147                         tabIdList : [],
148                         currentTabId : null,
149                         currentTabIndex : null,
150                         pageCount : 0,
151                         lastTab : null,
152                         tabBarMode : false,
154                         // Initialize the tab order array for input widgets.
155                         focusList : [],
156                         currentFocusIndex : 0,
157                         hasFocus : false
158                 };
160                 this.parts = themeBuilt.parts;
162                 CKEDITOR.tools.setTimeout( function()
163                         {
164                                 editor.fire( 'ariaWidget', this.parts.contents );
165                         },
166                         0, this );
168                 // Set the startup styles for the dialog, avoiding it enlarging the
169                 // page size on the dialog creation.
170                 this.parts.dialog.setStyles(
171                         {
172                                 position : CKEDITOR.env.ie6Compat ? 'absolute' : 'fixed',
173                                 top : 0,
174                                 left: 0,
175                                 visibility : 'hidden'
176                         });
178                 // Call the CKEDITOR.event constructor to initialize this instance.
179                 CKEDITOR.event.call( this );
181                 // Fire the "dialogDefinition" event, making it possible to customize
182                 // the dialog definition.
183                 this.definition = definition = CKEDITOR.fire( 'dialogDefinition',
184                         {
185                                 name : dialogName,
186                                 definition : definition
187                         }
188                         , editor ).definition;
190                 var tabsToRemove = {};
191                 // Cache tabs that should be removed.
192                 if ( !( 'removeDialogTabs' in editor._ ) && editor.config.removeDialogTabs )
193                 {
194                         var removeContents = editor.config.removeDialogTabs.split( ';' );
196                         for ( i = 0; i < removeContents.length; i++ )
197                         {
198                                 var parts = removeContents[ i ].split( ':' );
199                                 if ( parts.length == 2 )
200                                 {
201                                         var removeDialogName = parts[ 0 ];
202                                         if ( !tabsToRemove[ removeDialogName ] )
203                                                 tabsToRemove[ removeDialogName ] = [];
204                                         tabsToRemove[ removeDialogName ].push( parts[ 1 ] );
205                                 }
206                         }
207                         editor._.removeDialogTabs = tabsToRemove;
208                 }
210                 // Remove tabs of this dialog.
211                 if ( editor._.removeDialogTabs && ( tabsToRemove = editor._.removeDialogTabs[ dialogName ] ) )
212                 {
213                         for ( i = 0; i < tabsToRemove.length; i++ )
214                                 definition.removeContents( tabsToRemove[ i ] );
215                 }
217                 // Initialize load, show, hide, ok and cancel events.
218                 if ( definition.onLoad )
219                         this.on( 'load', definition.onLoad );
221                 if ( definition.onShow )
222                         this.on( 'show', definition.onShow );
224                 if ( definition.onHide )
225                         this.on( 'hide', definition.onHide );
227                 if ( definition.onOk )
228                 {
229                         this.on( 'ok', function( evt )
230                                 {
231                                         // Dialog confirm might probably introduce content changes (#5415).
232                                         editor.fire( 'saveSnapshot' );
233                                         setTimeout( function () { editor.fire( 'saveSnapshot' ); }, 0 );
234                                         if ( definition.onOk.call( this, evt ) === false )
235                                                 evt.data.hide = false;
236                                 });
237                 }
239                 if ( definition.onCancel )
240                 {
241                         this.on( 'cancel', function( evt )
242                                 {
243                                         if ( definition.onCancel.call( this, evt ) === false )
244                                                 evt.data.hide = false;
245                                 });
246                 }
248                 var me = this;
250                 // Iterates over all items inside all content in the dialog, calling a
251                 // function for each of them.
252                 var iterContents = function( func )
253                 {
254                         var contents = me._.contents,
255                                 stop = false;
257                         for ( var i in contents )
258                         {
259                                 for ( var j in contents[i] )
260                                 {
261                                         stop = func.call( this, contents[i][j] );
262                                         if ( stop )
263                                                 return;
264                                 }
265                         }
266                 };
268                 this.on( 'ok', function( evt )
269                         {
270                                 iterContents( function( item )
271                                         {
272                                                 if ( item.validate )
273                                                 {
274                                                         var isValid = item.validate( this );
276                                                         if ( typeof isValid == 'string' )
277                                                         {
278                                                                 alert( isValid );
279                                                                 isValid = false;
280                                                         }
282                                                         if ( isValid === false )
283                                                         {
284                                                                 if ( item.select )
285                                                                         item.select();
286                                                                 else
287                                                                         item.focus();
289                                                                 evt.data.hide = false;
290                                                                 evt.stop();
291                                                                 return true;
292                                                         }
293                                                 }
294                                         });
295                         }, this, null, 0 );
297                 this.on( 'cancel', function( evt )
298                         {
299                                 iterContents( function( item )
300                                         {
301                                                 if ( item.isChanged() )
302                                                 {
303                                                         if ( !confirm( editor.lang.common.confirmCancel ) )
304                                                                 evt.data.hide = false;
305                                                         return true;
306                                                 }
307                                         });
308                         }, this, null, 0 );
310                 this.parts.close.on( 'click', function( evt )
311                                 {
312                                         if ( this.fire( 'cancel', { hide : true } ).hide !== false )
313                                                 this.hide();
314                                         evt.data.preventDefault();
315                                 }, this );
317                 // Sort focus list according to tab order definitions.
318                 function setupFocus()
319                 {
320                         var focusList = me._.focusList;
321                         focusList.sort( function( a, b )
322                                 {
323                                         // Mimics browser tab order logics;
324                                         if ( a.tabIndex != b.tabIndex )
325                                                 return b.tabIndex - a.tabIndex;
326                                         //  Sort is not stable in some browsers,
327                                         // fall-back the comparator to 'focusIndex';
328                                         else
329                                                 return a.focusIndex - b.focusIndex;
330                                 });
332                         var size = focusList.length;
333                         for ( var i = 0; i < size; i++ )
334                                 focusList[ i ].focusIndex = i;
335                 }
337                 function changeFocus( forward )
338                 {
339                         var focusList = me._.focusList,
340                                 offset = forward ? 1 : -1;
341                         if ( focusList.length < 1 )
342                                 return;
344                         var current = me._.currentFocusIndex;
346                         // Trigger the 'blur' event of  any input element before anything,
347                         // since certain UI updates may depend on it.
348                         try
349                         {
350                                 focusList[ current ].getInputElement().$.blur();
351                         }
352                         catch( e ){}
354                         var startIndex = ( current + offset + focusList.length ) % focusList.length,
355                                 currentIndex = startIndex;
356                         while ( !focusList[ currentIndex ].isFocusable() )
357                         {
358                                 currentIndex = ( currentIndex + offset + focusList.length ) % focusList.length;
359                                 if ( currentIndex == startIndex )
360                                         break;
361                         }
362                         focusList[ currentIndex ].focus();
364                         // Select whole field content.
365                         if ( focusList[ currentIndex ].type == 'text' )
366                                 focusList[ currentIndex ].select();
367                 }
369                 this.changeFocus = changeFocus;
371                 var processed;
373                 function focusKeydownHandler( evt )
374                 {
375                         // If I'm not the top dialog, ignore.
376                         if ( me != CKEDITOR.dialog._.currentTop )
377                                 return;
379                         var keystroke = evt.data.getKeystroke(),
380                                 rtl = editor.lang.dir == 'rtl';
382                         processed = 0;
383                         if ( keystroke == 9 || keystroke == CKEDITOR.SHIFT + 9 )
384                         {
385                                 var shiftPressed = ( keystroke == CKEDITOR.SHIFT + 9 );
387                                 // Handling Tab and Shift-Tab.
388                                 if ( me._.tabBarMode )
389                                 {
390                                         // Change tabs.
391                                         var nextId = shiftPressed ? getPreviousVisibleTab.call( me ) : getNextVisibleTab.call( me );
392                                         me.selectPage( nextId );
393                                         me._.tabs[ nextId ][ 0 ].focus();
394                                 }
395                                 else
396                                 {
397                                         // Change the focus of inputs.
398                                         changeFocus( !shiftPressed );
399                                 }
401                                 processed = 1;
402                         }
403                         else if ( keystroke == CKEDITOR.ALT + 121 && !me._.tabBarMode && me.getPageCount() > 1 )
404                         {
405                                 // Alt-F10 puts focus into the current tab item in the tab bar.
406                                 me._.tabBarMode = true;
407                                 me._.tabs[ me._.currentTabId ][ 0 ].focus();
408                                 processed = 1;
409                         }
410                         else if ( ( keystroke == 37 || keystroke == 39 ) && me._.tabBarMode )
411                         {
412                                 // Arrow keys - used for changing tabs.
413                                 nextId = ( keystroke == ( rtl ? 39 : 37 ) ? getPreviousVisibleTab.call( me ) : getNextVisibleTab.call( me ) );
414                                 me.selectPage( nextId );
415                                 me._.tabs[ nextId ][ 0 ].focus();
416                                 processed = 1;
417                         }
418                         else if ( ( keystroke == 13 || keystroke == 32 ) && me._.tabBarMode )
419                         {
420                                 this.selectPage( this._.currentTabId );
421                                 this._.tabBarMode = false;
422                                 this._.currentFocusIndex = -1;
423                                 changeFocus( true );
424                                 processed = 1;
425                         }
427                         if ( processed )
428                         {
429                                 evt.stop();
430                                 evt.data.preventDefault();
431                         }
432                 }
434                 function focusKeyPressHandler( evt )
435                 {
436                         processed && evt.data.preventDefault();
437                 }
439                 var dialogElement = this._.element;
440                 // Add the dialog keyboard handlers.
441                 this.on( 'show', function()
442                         {
443                                 dialogElement.on( 'keydown', focusKeydownHandler, this, null, 0 );
444                                 // Some browsers instead, don't cancel key events in the keydown, but in the
445                                 // keypress. So we must do a longer trip in those cases. (#4531)
446                                 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
447                                         dialogElement.on( 'keypress', focusKeyPressHandler, this );
449                         } );
450                 this.on( 'hide', function()
451                         {
452                                 dialogElement.removeListener( 'keydown', focusKeydownHandler );
453                                 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
454                                         dialogElement.removeListener( 'keypress', focusKeyPressHandler );
455                         } );
456                 this.on( 'iframeAdded', function( evt )
457                         {
458                                 var doc = new CKEDITOR.dom.document( evt.data.iframe.$.contentWindow.document );
459                                 doc.on( 'keydown', focusKeydownHandler, this, null, 0 );
460                         } );
462                 // Auto-focus logic in dialog.
463                 this.on( 'show', function()
464                         {
465                                 // Setup tabIndex on showing the dialog instead of on loading
466                                 // to allow dynamic tab order happen in dialog definition.
467                                 setupFocus();
469                                 if ( editor.config.dialog_startupFocusTab
470                                         && me._.pageCount > 1 )
471                                 {
472                                         me._.tabBarMode = true;
473                                         me._.tabs[ me._.currentTabId ][ 0 ].focus();
474                                 }
475                                 else if ( !this._.hasFocus )
476                                 {
477                                         this._.currentFocusIndex = -1;
479                                         // Decide where to put the initial focus.
480                                         if ( definition.onFocus )
481                                         {
482                                                 var initialFocus = definition.onFocus.call( this );
483                                                 // Focus the field that the user specified.
484                                                 initialFocus && initialFocus.focus();
485                                         }
486                                         // Focus the first field in layout order.
487                                         else
488                                                 changeFocus( true );
490                                         /*
491                                          * IE BUG: If the initial focus went into a non-text element (e.g. button),
492                                          * then IE would still leave the caret inside the editing area.
493                                          */
494                                         if ( this._.editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
495                                         {
496                                                 var $selection = editor.document.$.selection,
497                                                         $range = $selection.createRange();
499                                                 if ( $range )
500                                                 {
501                                                         if ( $range.parentElement && $range.parentElement().ownerDocument == editor.document.$
502                                                           || $range.item && $range.item( 0 ).ownerDocument == editor.document.$ )
503                                                         {
504                                                                 var $myRange = document.body.createTextRange();
505                                                                 $myRange.moveToElementText( this.getElement().getFirst().$ );
506                                                                 $myRange.collapse( true );
507                                                                 $myRange.select();
508                                                         }
509                                                 }
510                                         }
511                                 }
512                         }, this, null, 0xffffffff );
514                 // IE6 BUG: Text fields and text areas are only half-rendered the first time the dialog appears in IE6 (#2661).
515                 // This is still needed after [2708] and [2709] because text fields in hidden TR tags are still broken.
516                 if ( CKEDITOR.env.ie6Compat )
517                 {
518                         this.on( 'load', function( evt )
519                                         {
520                                                 var outer = this.getElement(),
521                                                         inner = outer.getFirst();
522                                                 inner.remove();
523                                                 inner.appendTo( outer );
524                                         }, this );
525                 }
527                 initDragAndDrop( this );
528                 initResizeHandles( this );
530                 // Insert the title.
531                 ( new CKEDITOR.dom.text( definition.title, CKEDITOR.document ) ).appendTo( this.parts.title );
533                 // Insert the tabs and contents.
534                 for ( var i = 0 ; i < definition.contents.length ; i++ )
535                 {
536                         var page = definition.contents[i];
537                         page && this.addPage( page );
538                 }
540                 this.parts[ 'tabs' ].on( 'click', function( evt )
541                                 {
542                                         var target = evt.data.getTarget();
543                                         // If we aren't inside a tab, bail out.
544                                         if ( target.hasClass( 'cke_dialog_tab' ) )
545                                         {
546                                                 // Get the ID of the tab, without the 'cke_' prefix and the unique number suffix.
547                                                 var id = target.$.id;
548                                                 this.selectPage( id.substring( 4, id.lastIndexOf( '_' ) ) );
550                                                 if ( this._.tabBarMode )
551                                                 {
552                                                         this._.tabBarMode = false;
553                                                         this._.currentFocusIndex = -1;
554                                                         changeFocus( true );
555                                                 }
556                                                 evt.data.preventDefault();
557                                         }
558                                 }, this );
560                 // Insert buttons.
561                 var buttonsHtml = [],
562                         buttons = CKEDITOR.dialog._.uiElementBuilders.hbox.build( this,
563                                 {
564                                         type : 'hbox',
565                                         className : 'cke_dialog_footer_buttons',
566                                         widths : [],
567                                         children : definition.buttons
568                                 }, buttonsHtml ).getChild();
569                 this.parts.footer.setHtml( buttonsHtml.join( '' ) );
571                 for ( i = 0 ; i < buttons.length ; i++ )
572                         this._.buttons[ buttons[i].id ] = buttons[i];
573         };
575         // Focusable interface. Use it via dialog.addFocusable.
576         function Focusable( dialog, element, index )
577         {
578                 this.element = element;
579                 this.focusIndex = index;
580                 // TODO: support tabIndex for focusables.
581                 this.tabIndex = 0;
582                 this.isFocusable = function()
583                 {
584                         return !element.getAttribute( 'disabled' ) && element.isVisible();
585                 };
586                 this.focus = function()
587                 {
588                         dialog._.currentFocusIndex = this.focusIndex;
589                         this.element.focus();
590                 };
591                 // Bind events
592                 element.on( 'keydown', function( e )
593                         {
594                                 if ( e.data.getKeystroke() in { 32:1, 13:1 }  )
595                                         this.fire( 'click' );
596                         } );
597                 element.on( 'focus', function()
598                         {
599                                 this.fire( 'mouseover' );
600                         } );
601                 element.on( 'blur', function()
602                         {
603                                 this.fire( 'mouseout' );
604                         } );
605         }
607         CKEDITOR.dialog.prototype =
608         {
609                 destroy : function()
610                 {
611                         this.hide();
612                         this._.element.remove();
613                 },
615                 /**
616                  * Resizes the dialog.
617                  * @param {Number} width The width of the dialog in pixels.
618                  * @param {Number} height The height of the dialog in pixels.
619                  * @function
620                  * @example
621                  * dialogObj.resize( 800, 640 );
622                  */
623                 resize : (function()
624                 {
625                         return function( width, height )
626                         {
627                                 if ( this._.contentSize && this._.contentSize.width == width && this._.contentSize.height == height )
628                                         return;
630                                 CKEDITOR.dialog.fire( 'resize',
631                                         {
632                                                 dialog : this,
633                                                 skin : this._.editor.skinName,
634                                                 width : width,
635                                                 height : height
636                                         }, this._.editor );
638                                 this._.contentSize = { width : width, height : height };
639                         };
640                 })(),
642                 /**
643                  * Gets the current size of the dialog in pixels.
644                  * @returns {Object} An object with "width" and "height" properties.
645                  * @example
646                  * var width = dialogObj.getSize().width;
647                  */
648                 getSize : function()
649                 {
650                         var element = this._.element.getFirst();
651                         return { width : element.$.offsetWidth || 0, height : element.$.offsetHeight || 0};
652                 },
654                 /**
655                  * Moves the dialog to an (x, y) coordinate relative to the window.
656                  * @function
657                  * @param {Number} x The target x-coordinate.
658                  * @param {Number} y The target y-coordinate.
659                  * @param {Boolean} save Flag indicate whether the dialog position should be remembered on next open up.
660                  * @example
661                  * dialogObj.move( 10, 40 );
662                  */
663                 move : (function()
664                 {
665                         var isFixed;
666                         return function( x, y, save )
667                         {
668                                 // The dialog may be fixed positioned or absolute positioned. Ask the
669                                 // browser what is the current situation first.
670                                 var element = this._.element.getFirst();
671                                 if ( isFixed === undefined )
672                                         isFixed = element.getComputedStyle( 'position' ) == 'fixed';
674                                 if ( isFixed && this._.position && this._.position.x == x && this._.position.y == y )
675                                         return;
677                                 // Save the current position.
678                                 this._.position = { x : x, y : y };
680                                 // If not fixed positioned, add scroll position to the coordinates.
681                                 if ( !isFixed )
682                                 {
683                                         var scrollPosition = CKEDITOR.document.getWindow().getScrollPosition();
684                                         x += scrollPosition.x;
685                                         y += scrollPosition.y;
686                                 }
688                                 element.setStyles(
689                                                 {
690                                                         'left'  : ( x > 0 ? x : 0 ) + 'px',
691                                                         'top'   : ( y > 0 ? y : 0 ) + 'px'
692                                                 });
694                                 save && ( this._.moved = 1 );
695                         };
696                 })(),
698                 /**
699                  * Gets the dialog's position in the window.
700                  * @returns {Object} An object with "x" and "y" properties.
701                  * @example
702                  * var dialogX = dialogObj.getPosition().x;
703                  */
704                 getPosition : function(){ return CKEDITOR.tools.extend( {}, this._.position ); },
706                 /**
707                  * Shows the dialog box.
708                  * @example
709                  * dialogObj.show();
710                  */
711                 show : function()
712                 {
713                         // Insert the dialog's element to the root document.
714                         var element = this._.element;
715                         var definition = this.definition;
716                         if ( !( element.getParent() && element.getParent().equals( CKEDITOR.document.getBody() ) ) )
717                                 element.appendTo( CKEDITOR.document.getBody() );
718                         else
719                                 element.setStyle( 'display', 'block' );
721                         // FIREFOX BUG: Fix vanishing caret for Firefox 2 or Gecko 1.8.
722                         if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
723                         {
724                                 var dialogElement = this.parts.dialog;
725                                 dialogElement.setStyle( 'position', 'absolute' );
726                                 setTimeout( function()
727                                         {
728                                                 dialogElement.setStyle( 'position', 'fixed' );
729                                         }, 0 );
730                         }
733                         // First, set the dialog to an appropriate size.
734                         this.resize( this._.contentSize && this._.contentSize.width || definition.minWidth,
735                                         this._.contentSize && this._.contentSize.height || definition.minHeight );
737                         // Reset all inputs back to their default value.
738                         this.reset();
740                         // Select the first tab by default.
741                         this.selectPage( this.definition.contents[0].id );
743                         // Set z-index.
744                         if ( CKEDITOR.dialog._.currentZIndex === null )
745                                 CKEDITOR.dialog._.currentZIndex = this._.editor.config.baseFloatZIndex;
746                         this._.element.getFirst().setStyle( 'z-index', CKEDITOR.dialog._.currentZIndex += 10 );
748                         // Maintain the dialog ordering and dialog cover.
749                         // Also register key handlers if first dialog.
750                         if ( CKEDITOR.dialog._.currentTop === null )
751                         {
752                                 CKEDITOR.dialog._.currentTop = this;
753                                 this._.parentDialog = null;
754                                 showCover( this._.editor );
756                                 element.on( 'keydown', accessKeyDownHandler );
757                                 element.on( CKEDITOR.env.opera ? 'keypress' : 'keyup', accessKeyUpHandler );
759                                 // Prevent some keys from bubbling up. (#4269)
760                                 for ( var event in { keyup :1, keydown :1, keypress :1 } )
761                                         element.on( event, preventKeyBubbling );
762                         }
763                         else
764                         {
765                                 this._.parentDialog = CKEDITOR.dialog._.currentTop;
766                                 var parentElement = this._.parentDialog.getElement().getFirst();
767                                 parentElement.$.style.zIndex  -= Math.floor( this._.editor.config.baseFloatZIndex / 2 );
768                                 CKEDITOR.dialog._.currentTop = this;
769                         }
771                         // Register the Esc hotkeys.
772                         registerAccessKey( this, this, '\x1b', null, function()
773                                         {
774                                                 this.getButton( 'cancel' ) && this.getButton( 'cancel' ).click();
775                                         } );
777                         // Reset the hasFocus state.
778                         this._.hasFocus = false;
780                         CKEDITOR.tools.setTimeout( function()
781                                 {
782                                         this.layout();
783                                         this.parts.dialog.setStyle( 'visibility', '' );
785                                         // Execute onLoad for the first show.
786                                         this.fireOnce( 'load', {} );
787                                         CKEDITOR.ui.fire( 'ready', this );
789                                         this.fire( 'show', {} );
790                                         this._.editor.fire( 'dialogShow', this );
792                                         // Save the initial values of the dialog.
793                                         this.foreach( function( contentObj ) { contentObj.setInitValue && contentObj.setInitValue(); } );
795                                 },
796                                 100, this );
797                 },
799                 /**
800                  * Rearrange the dialog to its previous position or the middle of the window.
801                  * @since 3.5
802                  */
803                 layout : function()
804                 {
805                         var viewSize = CKEDITOR.document.getWindow().getViewPaneSize(),
806                                         dialogSize = this.getSize();
808                         this.move( this._.moved ? this._.position.x : ( viewSize.width - dialogSize.width ) / 2,
809                                         this._.moved ? this._.position.y : ( viewSize.height - dialogSize.height ) / 2 );
810                 },
812                 /**
813                  * Executes a function for each UI element.
814                  * @param {Function} fn Function to execute for each UI element.
815                  * @returns {CKEDITOR.dialog} The current dialog object.
816                  */
817                 foreach : function( fn )
818                 {
819                         for ( var i in this._.contents )
820                         {
821                                 for ( var j in this._.contents[i] )
822                                         fn( this._.contents[i][j] );
823                         }
824                         return this;
825                 },
827                 /**
828                  * Resets all input values in the dialog.
829                  * @example
830                  * dialogObj.reset();
831                  * @returns {CKEDITOR.dialog} The current dialog object.
832                  */
833                 reset : (function()
834                 {
835                         var fn = function( widget ){ if ( widget.reset ) widget.reset( 1 ); };
836                         return function(){ this.foreach( fn ); return this; };
837                 })(),
839                 setupContent : function()
840                 {
841                         var args = arguments;
842                         this.foreach( function( widget )
843                                 {
844                                         if ( widget.setup )
845                                                 widget.setup.apply( widget, args );
846                                 });
847                 },
849                 commitContent : function()
850                 {
851                         var args = arguments;
852                         this.foreach( function( widget )
853                                 {
854                                         if ( widget.commit )
855                                                 widget.commit.apply( widget, args );
856                                 });
857                 },
859                 /**
860                  * Hides the dialog box.
861                  * @example
862                  * dialogObj.hide();
863                  */
864                 hide : function()
865                 {
866                         if ( !this.parts.dialog.isVisible() )
867                                 return;
869                         this.fire( 'hide', {} );
870                         this._.editor.fire( 'dialogHide', this );
871                         var element = this._.element;
872                         element.setStyle( 'display', 'none' );
873                         this.parts.dialog.setStyle( 'visibility', 'hidden' );
874                         // Unregister all access keys associated with this dialog.
875                         unregisterAccessKey( this );
877                         // Close any child(top) dialogs first.
878                         while( CKEDITOR.dialog._.currentTop != this )
879                                 CKEDITOR.dialog._.currentTop.hide();
881                         // Maintain dialog ordering and remove cover if needed.
882                         if ( !this._.parentDialog )
883                                 hideCover();
884                         else
885                         {
886                                 var parentElement = this._.parentDialog.getElement().getFirst();
887                                 parentElement.setStyle( 'z-index', parseInt( parentElement.$.style.zIndex, 10 ) + Math.floor( this._.editor.config.baseFloatZIndex / 2 ) );
888                         }
889                         CKEDITOR.dialog._.currentTop = this._.parentDialog;
891                         // Deduct or clear the z-index.
892                         if ( !this._.parentDialog )
893                         {
894                                 CKEDITOR.dialog._.currentZIndex = null;
896                                 // Remove access key handlers.
897                                 element.removeListener( 'keydown', accessKeyDownHandler );
898                                 element.removeListener( CKEDITOR.env.opera ? 'keypress' : 'keyup', accessKeyUpHandler );
900                                 // Remove bubbling-prevention handler. (#4269)
901                                 for ( var event in { keyup :1, keydown :1, keypress :1 } )
902                                         element.removeListener( event, preventKeyBubbling );
904                                 var editor = this._.editor;
905                                 editor.focus();
907                                 if ( editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
908                                 {
909                                         var selection = editor.getSelection();
910                                         selection && selection.unlock( true );
911                                 }
912                         }
913                         else
914                                 CKEDITOR.dialog._.currentZIndex -= 10;
916                         delete this._.parentDialog;
917                         // Reset the initial values of the dialog.
918                         this.foreach( function( contentObj ) { contentObj.resetInitValue && contentObj.resetInitValue(); } );
919                 },
921                 /**
922                  * Adds a tabbed page into the dialog.
923                  * @param {Object} contents Content definition.
924                  * @example
925                  */
926                 addPage : function( contents )
927                 {
928                         var pageHtml = [],
929                                 titleHtml = contents.label ? ' title="' + CKEDITOR.tools.htmlEncode( contents.label ) + '"' : '',
930                                 elements = contents.elements,
931                                 vbox = CKEDITOR.dialog._.uiElementBuilders.vbox.build( this,
932                                                 {
933                                                         type : 'vbox',
934                                                         className : 'cke_dialog_page_contents',
935                                                         children : contents.elements,
936                                                         expand : !!contents.expand,
937                                                         padding : contents.padding,
938                                                         style : contents.style || 'width: 100%;'
939                                                 }, pageHtml );
941                         // Create the HTML for the tab and the content block.
942                         var page = CKEDITOR.dom.element.createFromHtml( pageHtml.join( '' ) );
943                         page.setAttribute( 'role', 'tabpanel' );
945                         var env = CKEDITOR.env;
946                         var tabId = 'cke_' + contents.id + '_' + CKEDITOR.tools.getNextNumber(),
947                                  tab = CKEDITOR.dom.element.createFromHtml( [
948                                         '<a class="cke_dialog_tab"',
949                                                 ( this._.pageCount > 0 ? ' cke_last' : 'cke_first' ),
950                                                 titleHtml,
951                                                 ( !!contents.hidden ? ' style="display:none"' : '' ),
952                                                 ' id="', tabId, '"',
953                                                 env.gecko && env.version >= 10900 && !env.hc ? '' : ' href="javascript:void(0)"',
954                                                 ' tabIndex="-1"',
955                                                 ' hidefocus="true"',
956                                                 ' role="tab">',
957                                                         contents.label,
958                                         '</a>'
959                                 ].join( '' ) );
961                         page.setAttribute( 'aria-labelledby', tabId );
963                         // Take records for the tabs and elements created.
964                         this._.tabs[ contents.id ] = [ tab, page ];
965                         this._.tabIdList.push( contents.id );
966                         !contents.hidden && this._.pageCount++;
967                         this._.lastTab = tab;
968                         this.updateStyle();
970                         var contentMap = this._.contents[ contents.id ] = {},
971                                 cursor,
972                                 children = vbox.getChild();
974                         while ( ( cursor = children.shift() ) )
975                         {
976                                 contentMap[ cursor.id ] = cursor;
977                                 if ( typeof( cursor.getChild ) == 'function' )
978                                         children.push.apply( children, cursor.getChild() );
979                         }
981                         // Attach the DOM nodes.
983                         page.setAttribute( 'name', contents.id );
984                         page.appendTo( this.parts.contents );
986                         tab.unselectable();
987                         this.parts.tabs.append( tab );
989                         // Add access key handlers if access key is defined.
990                         if ( contents.accessKey )
991                         {
992                                 registerAccessKey( this, this, 'CTRL+' + contents.accessKey,
993                                         tabAccessKeyDown, tabAccessKeyUp );
994                                 this._.accessKeyMap[ 'CTRL+' + contents.accessKey ] = contents.id;
995                         }
996                 },
998                 /**
999                  * Activates a tab page in the dialog by its id.
1000                  * @param {String} id The id of the dialog tab to be activated.
1001                  * @example
1002                  * dialogObj.selectPage( 'tab_1' );
1003                  */
1004                 selectPage : function( id )
1005                 {
1006                         if ( this._.currentTabId == id )
1007                                 return;
1009                         // Returning true means that the event has been canceled
1010                         if ( this.fire( 'selectPage', { page : id, currentPage : this._.currentTabId } ) === true )
1011                                 return;
1013                         // Hide the non-selected tabs and pages.
1014                         for ( var i in this._.tabs )
1015                         {
1016                                 var tab = this._.tabs[i][0],
1017                                         page = this._.tabs[i][1];
1018                                 if ( i != id )
1019                                 {
1020                                         tab.removeClass( 'cke_dialog_tab_selected' );
1021                                         page.hide();
1022                                 }
1023                                 page.setAttribute( 'aria-hidden', i != id );
1024                         }
1026                         var selected = this._.tabs[ id ];
1027                         selected[ 0 ].addClass( 'cke_dialog_tab_selected' );
1029                         // [IE] an invisible input[type='text'] will enlarge it's width
1030                         // if it's value is long when it shows, so we clear it's value
1031                         // before it shows and then recover it (#5649)
1032                         if ( CKEDITOR.env.ie6Compat || CKEDITOR.env.ie7Compat )
1033                         {
1034                                 clearOrRecoverTextInputValue( selected[ 1 ] );
1035                                 selected[ 1 ].show();
1036                                 setTimeout( function()
1037                                 {
1038                                         clearOrRecoverTextInputValue( selected[ 1 ], 1 );
1039                                 }, 0 );
1040                         }
1041                         else
1042                                 selected[ 1 ].show();
1044                         this._.currentTabId = id;
1045                         this._.currentTabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, id );
1046                 },
1048                 // Dialog state-specific style updates.
1049                 updateStyle : function()
1050                 {
1051                         // If only a single page shown, a different style is used in the central pane.
1052                         this.parts.dialog[ ( this._.pageCount === 1 ? 'add' : 'remove' ) + 'Class' ]( 'cke_single_page' );
1053                 },
1055                 /**
1056                  * Hides a page's tab away from the dialog.
1057                  * @param {String} id The page's Id.
1058                  * @example
1059                  * dialog.hidePage( 'tab_3' );
1060                  */
1061                 hidePage : function( id )
1062                 {
1063                         var tab = this._.tabs[id] && this._.tabs[id][0];
1064                         if ( !tab || this._.pageCount == 1 || !tab.isVisible() )
1065                                 return;
1066                         // Switch to other tab first when we're hiding the active tab.
1067                         else if ( id == this._.currentTabId )
1068                                 this.selectPage( getPreviousVisibleTab.call( this ) );
1070                         tab.hide();
1071                         this._.pageCount--;
1072                         this.updateStyle();
1073                 },
1075                 /**
1076                  * Unhides a page's tab.
1077                  * @param {String} id The page's Id.
1078                  * @example
1079                  * dialog.showPage( 'tab_2' );
1080                  */
1081                 showPage : function( id )
1082                 {
1083                         var tab = this._.tabs[id] && this._.tabs[id][0];
1084                         if ( !tab )
1085                                 return;
1086                         tab.show();
1087                         this._.pageCount++;
1088                         this.updateStyle();
1089                 },
1091                 /**
1092                  * Gets the root DOM element of the dialog.
1093                  * @returns {CKEDITOR.dom.element} The &lt;span&gt; element containing this dialog.
1094                  * @example
1095                  * var dialogElement = dialogObj.getElement().getFirst();
1096                  * dialogElement.setStyle( 'padding', '5px' );
1097                  */
1098                 getElement : function()
1099                 {
1100                         return this._.element;
1101                 },
1103                 /**
1104                  * Gets the name of the dialog.
1105                  * @returns {String} The name of this dialog.
1106                  * @example
1107                  * var dialogName = dialogObj.getName();
1108                  */
1109                 getName : function()
1110                 {
1111                         return this._.name;
1112                 },
1114                 /**
1115                  * Gets a dialog UI element object from a dialog page.
1116                  * @param {String} pageId id of dialog page.
1117                  * @param {String} elementId id of UI element.
1118                  * @example
1119                  * @returns {CKEDITOR.ui.dialog.uiElement} The dialog UI element.
1120                  */
1121                 getContentElement : function( pageId, elementId )
1122                 {
1123                         var page = this._.contents[ pageId ];
1124                         return page && page[ elementId ];
1125                 },
1127                 /**
1128                  * Gets the value of a dialog UI element.
1129                  * @param {String} pageId id of dialog page.
1130                  * @param {String} elementId id of UI element.
1131                  * @example
1132                  * @returns {Object} The value of the UI element.
1133                  */
1134                 getValueOf : function( pageId, elementId )
1135                 {
1136                         return this.getContentElement( pageId, elementId ).getValue();
1137                 },
1139                 /**
1140                  * Sets the value of a dialog UI element.
1141                  * @param {String} pageId id of the dialog page.
1142                  * @param {String} elementId id of the UI element.
1143                  * @param {Object} value The new value of the UI element.
1144                  * @example
1145                  */
1146                 setValueOf : function( pageId, elementId, value )
1147                 {
1148                         return this.getContentElement( pageId, elementId ).setValue( value );
1149                 },
1151                 /**
1152                  * Gets the UI element of a button in the dialog's button row.
1153                  * @param {String} id The id of the button.
1154                  * @example
1155                  * @returns {CKEDITOR.ui.dialog.button} The button object.
1156                  */
1157                 getButton : function( id )
1158                 {
1159                         return this._.buttons[ id ];
1160                 },
1162                 /**
1163                  * Simulates a click to a dialog button in the dialog's button row.
1164                  * @param {String} id The id of the button.
1165                  * @example
1166                  * @returns The return value of the dialog's "click" event.
1167                  */
1168                 click : function( id )
1169                 {
1170                         return this._.buttons[ id ].click();
1171                 },
1173                 /**
1174                  * Disables a dialog button.
1175                  * @param {String} id The id of the button.
1176                  * @example
1177                  */
1178                 disableButton : function( id )
1179                 {
1180                         return this._.buttons[ id ].disable();
1181                 },
1183                 /**
1184                  * Enables a dialog button.
1185                  * @param {String} id The id of the button.
1186                  * @example
1187                  */
1188                 enableButton : function( id )
1189                 {
1190                         return this._.buttons[ id ].enable();
1191                 },
1193                 /**
1194                  * Gets the number of pages in the dialog.
1195                  * @returns {Number} Page count.
1196                  */
1197                 getPageCount : function()
1198                 {
1199                         return this._.pageCount;
1200                 },
1202                 /**
1203                  * Gets the editor instance which opened this dialog.
1204                  * @returns {CKEDITOR.editor} Parent editor instances.
1205                  */
1206                 getParentEditor : function()
1207                 {
1208                         return this._.editor;
1209                 },
1211                 /**
1212                  * Gets the element that was selected when opening the dialog, if any.
1213                  * @returns {CKEDITOR.dom.element} The element that was selected, or null.
1214                  */
1215                 getSelectedElement : function()
1216                 {
1217                         return this.getParentEditor().getSelection().getSelectedElement();
1218                 },
1220                 /**
1221                  * Adds element to dialog's focusable list.
1222                  *
1223                  * @param {CKEDITOR.dom.element} element
1224                  * @param {Number} [index]
1225                  */
1226                 addFocusable: function( element, index ) {
1227                         if ( typeof index == 'undefined' )
1228                         {
1229                                 index = this._.focusList.length;
1230                                 this._.focusList.push( new Focusable( this, element, index ) );
1231                         }
1232                         else
1233                         {
1234                                 this._.focusList.splice( index, 0, new Focusable( this, element, index ) );
1235                                 for ( var i = index + 1 ; i < this._.focusList.length ; i++ )
1236                                         this._.focusList[ i ].focusIndex++;
1237                         }
1238                 }
1239         };
1241         CKEDITOR.tools.extend( CKEDITOR.dialog,
1242                 /**
1243                  * @lends CKEDITOR.dialog
1244                  */
1245                 {
1246                         /**
1247                          * Registers a dialog.
1248                          * @param {String} name The dialog's name.
1249                          * @param {Function|String} dialogDefinition
1250                          * A function returning the dialog's definition, or the URL to the .js file holding the function.
1251                          * The function should accept an argument "editor" which is the current editor instance, and
1252                          * return an object conforming to {@link CKEDITOR.dialog.dialogDefinition}.
1253                          * @example
1254                          * @see CKEDITOR.dialog.dialogDefinition
1255                          */
1256                         add : function( name, dialogDefinition )
1257                         {
1258                                 // Avoid path registration from multiple instances override definition.
1259                                 if ( !this._.dialogDefinitions[name]
1260                                         || typeof  dialogDefinition == 'function' )
1261                                         this._.dialogDefinitions[name] = dialogDefinition;
1262                         },
1264                         exists : function( name )
1265                         {
1266                                 return !!this._.dialogDefinitions[ name ];
1267                         },
1269                         getCurrent : function()
1270                         {
1271                                 return CKEDITOR.dialog._.currentTop;
1272                         },
1274                         /**
1275                          * The default OK button for dialogs. Fires the "ok" event and closes the dialog if the event succeeds.
1276                          * @static
1277                          * @field
1278                          * @example
1279                          * @type Function
1280                          */
1281                         okButton : (function()
1282                         {
1283                                 var retval = function( editor, override )
1284                                 {
1285                                         override = override || {};
1286                                         return CKEDITOR.tools.extend( {
1287                                                 id : 'ok',
1288                                                 type : 'button',
1289                                                 label : editor.lang.common.ok,
1290                                                 'class' : 'cke_dialog_ui_button_ok',
1291                                                 onClick : function( evt )
1292                                                 {
1293                                                         var dialog = evt.data.dialog;
1294                                                         if ( dialog.fire( 'ok', { hide : true } ).hide !== false )
1295                                                                 dialog.hide();
1296                                                 }
1297                                         }, override, true );
1298                                 };
1299                                 retval.type = 'button';
1300                                 retval.override = function( override )
1301                                 {
1302                                         return CKEDITOR.tools.extend( function( editor ){ return retval( editor, override ); },
1303                                                         { type : 'button' }, true );
1304                                 };
1305                                 return retval;
1306                         })(),
1308                         /**
1309                          * The default cancel button for dialogs. Fires the "cancel" event and closes the dialog if no UI element value changed.
1310                          * @static
1311                          * @field
1312                          * @example
1313                          * @type Function
1314                          */
1315                         cancelButton : (function()
1316                         {
1317                                 var retval = function( editor, override )
1318                                 {
1319                                         override = override || {};
1320                                         return CKEDITOR.tools.extend( {
1321                                                 id : 'cancel',
1322                                                 type : 'button',
1323                                                 label : editor.lang.common.cancel,
1324                                                 'class' : 'cke_dialog_ui_button_cancel',
1325                                                 onClick : function( evt )
1326                                                 {
1327                                                         var dialog = evt.data.dialog;
1328                                                         if ( dialog.fire( 'cancel', { hide : true } ).hide !== false )
1329                                                                 dialog.hide();
1330                                                 }
1331                                         }, override, true );
1332                                 };
1333                                 retval.type = 'button';
1334                                 retval.override = function( override )
1335                                 {
1336                                         return CKEDITOR.tools.extend( function( editor ){ return retval( editor, override ); },
1337                                                         { type : 'button' }, true );
1338                                 };
1339                                 return retval;
1340                         })(),
1342                         /**
1343                          * Registers a dialog UI element.
1344                          * @param {String} typeName The name of the UI element.
1345                          * @param {Function} builder The function to build the UI element.
1346                          * @example
1347                          */
1348                         addUIElement : function( typeName, builder )
1349                         {
1350                                 this._.uiElementBuilders[ typeName ] = builder;
1351                         }
1352                 });
1354         CKEDITOR.dialog._ =
1355         {
1356                 uiElementBuilders : {},
1358                 dialogDefinitions : {},
1360                 currentTop : null,
1362                 currentZIndex : null
1363         };
1365         // "Inherit" (copy actually) from CKEDITOR.event.
1366         CKEDITOR.event.implementOn( CKEDITOR.dialog );
1367         CKEDITOR.event.implementOn( CKEDITOR.dialog.prototype, true );
1369         var defaultDialogDefinition =
1370         {
1371                 resizable : CKEDITOR.DIALOG_RESIZE_BOTH,
1372                 minWidth : 600,
1373                 minHeight : 400,
1374                 buttons : [ CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton ]
1375         };
1377         // Tool function used to return an item from an array based on its id
1378         // property.
1379         var getById = function( array, id, recurse )
1380         {
1381                 for ( var i = 0, item ; ( item = array[ i ] ) ; i++ )
1382                 {
1383                         if ( item.id == id )
1384                                 return item;
1385                         if ( recurse && item[ recurse ] )
1386                         {
1387                                 var retval = getById( item[ recurse ], id, recurse ) ;
1388                                 if ( retval )
1389                                         return retval;
1390                         }
1391                 }
1392                 return null;
1393         };
1395         // Tool function used to add an item into an array.
1396         var addById = function( array, newItem, nextSiblingId, recurse, nullIfNotFound )
1397         {
1398                 if ( nextSiblingId )
1399                 {
1400                         for ( var i = 0, item ; ( item = array[ i ] ) ; i++ )
1401                         {
1402                                 if ( item.id == nextSiblingId )
1403                                 {
1404                                         array.splice( i, 0, newItem );
1405                                         return newItem;
1406                                 }
1408                                 if ( recurse && item[ recurse ] )
1409                                 {
1410                                         var retval = addById( item[ recurse ], newItem, nextSiblingId, recurse, true );
1411                                         if ( retval )
1412                                                 return retval;
1413                                 }
1414                         }
1416                         if ( nullIfNotFound )
1417                                 return null;
1418                 }
1420                 array.push( newItem );
1421                 return newItem;
1422         };
1424         // Tool function used to remove an item from an array based on its id.
1425         var removeById = function( array, id, recurse )
1426         {
1427                 for ( var i = 0, item ; ( item = array[ i ] ) ; i++ )
1428                 {
1429                         if ( item.id == id )
1430                                 return array.splice( i, 1 );
1431                         if ( recurse && item[ recurse ] )
1432                         {
1433                                 var retval = removeById( item[ recurse ], id, recurse );
1434                                 if ( retval )
1435                                         return retval;
1436                         }
1437                 }
1438                 return null;
1439         };
1441         /**
1442          * This class is not really part of the API. It is the "definition" property value
1443          * passed to "dialogDefinition" event handlers.
1444          * @constructor
1445          * @name CKEDITOR.dialog.dialogDefinitionObject
1446          * @extends CKEDITOR.dialog.dialogDefinition
1447          * @example
1448          * CKEDITOR.on( 'dialogDefinition', function( evt )
1449          *      {
1450          *              var definition = evt.data.definition;
1451          *              var content = definition.getContents( 'page1' );
1452          *              ...
1453          *      } );
1454          */
1455         var definitionObject = function( dialog, dialogDefinition )
1456         {
1457                 // TODO : Check if needed.
1458                 this.dialog = dialog;
1460                 // Transform the contents entries in contentObjects.
1461                 var contents = dialogDefinition.contents;
1462                 for ( var i = 0, content ; ( content = contents[i] ) ; i++ )
1463                         contents[ i ] = content && new contentObject( dialog, content );
1465                 CKEDITOR.tools.extend( this, dialogDefinition );
1466         };
1468         definitionObject.prototype =
1469         /** @lends CKEDITOR.dialog.dialogDefinitionObject.prototype */
1470         {
1471                 /**
1472                  * Gets a content definition.
1473                  * @param {String} id The id of the content definition.
1474                  * @returns {CKEDITOR.dialog.contentDefinition} The content definition
1475                  *              matching id.
1476                  */
1477                 getContents : function( id )
1478                 {
1479                         return getById( this.contents, id );
1480                 },
1482                 /**
1483                  * Gets a button definition.
1484                  * @param {String} id The id of the button definition.
1485                  * @returns {CKEDITOR.dialog.buttonDefinition} The button definition
1486                  *              matching id.
1487                  */
1488                 getButton : function( id )
1489                 {
1490                         return getById( this.buttons, id );
1491                 },
1493                 /**
1494                  * Adds a content definition object under this dialog definition.
1495                  * @param {CKEDITOR.dialog.contentDefinition} contentDefinition The
1496                  *              content definition.
1497                  * @param {String} [nextSiblingId] The id of an existing content
1498                  *              definition which the new content definition will be inserted
1499                  *              before. Omit if the new content definition is to be inserted as
1500                  *              the last item.
1501                  * @returns {CKEDITOR.dialog.contentDefinition} The inserted content
1502                  *              definition.
1503                  */
1504                 addContents : function( contentDefinition, nextSiblingId )
1505                 {
1506                         return addById( this.contents, contentDefinition, nextSiblingId );
1507                 },
1509                 /**
1510                  * Adds a button definition object under this dialog definition.
1511                  * @param {CKEDITOR.dialog.buttonDefinition} buttonDefinition The
1512                  *              button definition.
1513                  * @param {String} [nextSiblingId] The id of an existing button
1514                  *              definition which the new button definition will be inserted
1515                  *              before. Omit if the new button definition is to be inserted as
1516                  *              the last item.
1517                  * @returns {CKEDITOR.dialog.buttonDefinition} The inserted button
1518                  *              definition.
1519                  */
1520                 addButton : function( buttonDefinition, nextSiblingId )
1521                 {
1522                         return addById( this.buttons, buttonDefinition, nextSiblingId );
1523                 },
1525                 /**
1526                  * Removes a content definition from this dialog definition.
1527                  * @param {String} id The id of the content definition to be removed.
1528                  * @returns {CKEDITOR.dialog.contentDefinition} The removed content
1529                  *              definition.
1530                  */
1531                 removeContents : function( id )
1532                 {
1533                         removeById( this.contents, id );
1534                 },
1536                 /**
1537                  * Removes a button definition from the dialog definition.
1538                  * @param {String} id The id of the button definition to be removed.
1539                  * @returns {CKEDITOR.dialog.buttonDefinition} The removed button
1540                  *              definition.
1541                  */
1542                 removeButton : function( id )
1543                 {
1544                         removeById( this.buttons, id );
1545                 }
1546         };
1548         /**
1549          * This class is not really part of the API. It is the template of the
1550          * objects representing content pages inside the
1551          * CKEDITOR.dialog.dialogDefinitionObject.
1552          * @constructor
1553          * @name CKEDITOR.dialog.contentDefinitionObject
1554          * @example
1555          * CKEDITOR.on( 'dialogDefinition', function( evt )
1556          *      {
1557          *              var definition = evt.data.definition;
1558          *              var content = definition.getContents( 'page1' );
1559          *              content.remove( 'textInput1' );
1560          *              ...
1561          *      } );
1562          */
1563         function contentObject( dialog, contentDefinition )
1564         {
1565                 this._ =
1566                 {
1567                         dialog : dialog
1568                 };
1570                 CKEDITOR.tools.extend( this, contentDefinition );
1571         }
1573         contentObject.prototype =
1574         /** @lends CKEDITOR.dialog.contentDefinitionObject.prototype */
1575         {
1576                 /**
1577                  * Gets a UI element definition under the content definition.
1578                  * @param {String} id The id of the UI element definition.
1579                  * @returns {CKEDITOR.dialog.uiElementDefinition}
1580                  */
1581                 get : function( id )
1582                 {
1583                         return getById( this.elements, id, 'children' );
1584                 },
1586                 /**
1587                  * Adds a UI element definition to the content definition.
1588                  * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition The
1589                  *              UI elemnet definition to be added.
1590                  * @param {String} nextSiblingId The id of an existing UI element
1591                  *              definition which the new UI element definition will be inserted
1592                  *              before. Omit if the new button definition is to be inserted as
1593                  *              the last item.
1594                  * @returns {CKEDITOR.dialog.uiElementDefinition} The element
1595                  *              definition inserted.
1596                  */
1597                 add : function( elementDefinition, nextSiblingId )
1598                 {
1599                         return addById( this.elements, elementDefinition, nextSiblingId, 'children' );
1600                 },
1602                 /**
1603                  * Removes a UI element definition from the content definition.
1604                  * @param {String} id The id of the UI element definition to be
1605                  *              removed.
1606                  * @returns {CKEDITOR.dialog.uiElementDefinition} The element
1607                  *              definition removed.
1608                  * @example
1609                  */
1610                 remove : function( id )
1611                 {
1612                         removeById( this.elements, id, 'children' );
1613                 }
1614         };
1616         function initDragAndDrop( dialog )
1617         {
1618                 var lastCoords = null,
1619                         abstractDialogCoords = null,
1620                         element = dialog.getElement().getFirst(),
1621                         editor = dialog.getParentEditor(),
1622                         magnetDistance = editor.config.dialog_magnetDistance,
1623                         margins = editor.skin.margins || [ 0, 0, 0, 0 ];
1625                 if ( typeof magnetDistance == 'undefined' )
1626                         magnetDistance = 20;
1628                 function mouseMoveHandler( evt )
1629                 {
1630                         var dialogSize = dialog.getSize(),
1631                                 viewPaneSize = CKEDITOR.document.getWindow().getViewPaneSize(),
1632                                 x = evt.data.$.screenX,
1633                                 y = evt.data.$.screenY,
1634                                 dx = x - lastCoords.x,
1635                                 dy = y - lastCoords.y,
1636                                 realX, realY;
1638                         lastCoords = { x : x, y : y };
1639                         abstractDialogCoords.x += dx;
1640                         abstractDialogCoords.y += dy;
1642                         if ( abstractDialogCoords.x + margins[3] < magnetDistance )
1643                                 realX = - margins[3];
1644                         else if ( abstractDialogCoords.x - margins[1] > viewPaneSize.width - dialogSize.width - magnetDistance )
1645                                 realX = viewPaneSize.width - dialogSize.width + ( editor.lang.dir == 'rtl' ? 0 : margins[1] );
1646                         else
1647                                 realX = abstractDialogCoords.x;
1649                         if ( abstractDialogCoords.y + margins[0] < magnetDistance )
1650                                 realY = - margins[0];
1651                         else if ( abstractDialogCoords.y - margins[2] > viewPaneSize.height - dialogSize.height - magnetDistance )
1652                                 realY = viewPaneSize.height - dialogSize.height + margins[2];
1653                         else
1654                                 realY = abstractDialogCoords.y;
1656                         dialog.move( realX, realY, 1 );
1658                         evt.data.preventDefault();
1659                 }
1661                 function mouseUpHandler( evt )
1662                 {
1663                         CKEDITOR.document.removeListener( 'mousemove', mouseMoveHandler );
1664                         CKEDITOR.document.removeListener( 'mouseup', mouseUpHandler );
1666                         if ( CKEDITOR.env.ie6Compat )
1667                         {
1668                                 var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
1669                                 coverDoc.removeListener( 'mousemove', mouseMoveHandler );
1670                                 coverDoc.removeListener( 'mouseup', mouseUpHandler );
1671                         }
1672                 }
1674                 dialog.parts.title.on( 'mousedown', function( evt )
1675                         {
1676                                 lastCoords = { x : evt.data.$.screenX, y : evt.data.$.screenY };
1678                                 CKEDITOR.document.on( 'mousemove', mouseMoveHandler );
1679                                 CKEDITOR.document.on( 'mouseup', mouseUpHandler );
1680                                 abstractDialogCoords = dialog.getPosition();
1682                                 if ( CKEDITOR.env.ie6Compat )
1683                                 {
1684                                         var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
1685                                         coverDoc.on( 'mousemove', mouseMoveHandler );
1686                                         coverDoc.on( 'mouseup', mouseUpHandler );
1687                                 }
1689                                 evt.data.preventDefault();
1690                         }, dialog );
1691         }
1693         function initResizeHandles( dialog )
1694         {
1695                 var def = dialog.definition,
1696                         resizable = def.resizable;
1698                 if ( resizable == CKEDITOR.DIALOG_RESIZE_NONE )
1699                         return;
1701                 var editor = dialog.getParentEditor();
1702                 var wrapperWidth, wrapperHeight,
1703                                 viewSize, origin, startSize,
1704                                 dialogCover;
1706                 function positionDialog( right )
1707                 {
1708                         // Maintain righthand sizing in RTL.
1709                         if ( dialog._.moved && editor.lang.dir == 'rtl' )
1710                         {
1711                                 var element = dialog._.element.getFirst();
1712                                 element.setStyle( 'right', right + "px" );
1713                                 element.removeStyle( 'left' );
1714                         }
1715                         else if ( !dialog._.moved )
1716                                 dialog.layout();
1717                 }
1719                 var mouseDownFn = CKEDITOR.tools.addFunction( function( $event )
1720                 {
1721                         startSize = dialog.getSize();
1723                         var content = dialog.parts.contents,
1724                                 iframeDialog = content.$.getElementsByTagName( 'iframe' ).length;
1726                         // Shim to help capturing "mousemove" over iframe.
1727                         if ( iframeDialog )
1728                         {
1729                                 dialogCover = CKEDITOR.dom.element.createFromHtml( '<div class="cke_dialog_resize_cover" style="height: 100%; position: absolute; width: 100%;"></div>' );
1730                                 content.append( dialogCover );
1731                         }
1733                         // Calculate the offset between content and chrome size.
1734                         wrapperHeight = startSize.height - dialog.parts.contents.getSize( 'height',  ! ( CKEDITOR.env.gecko || CKEDITOR.env.opera || CKEDITOR.env.ie && CKEDITOR.env.quirks ) );
1735                         wrapperWidth = startSize.width - dialog.parts.contents.getSize( 'width', 1 );
1737                         origin = { x : $event.screenX, y : $event.screenY };
1739                         viewSize = CKEDITOR.document.getWindow().getViewPaneSize();
1741                         CKEDITOR.document.on( 'mousemove', mouseMoveHandler );
1742                         CKEDITOR.document.on( 'mouseup', mouseUpHandler );
1744                         if ( CKEDITOR.env.ie6Compat )
1745                         {
1746                                 var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
1747                                 coverDoc.on( 'mousemove', mouseMoveHandler );
1748                                 coverDoc.on( 'mouseup', mouseUpHandler );
1749                         }
1751                         $event.preventDefault && $event.preventDefault();
1752                 });
1754                 // Prepend the grip to the dialog.
1755                 dialog.on( 'load', function()
1756                 {
1757                         var direction = '';
1758                         if ( resizable == CKEDITOR.DIALOG_RESIZE_WIDTH )
1759                                 direction = ' cke_resizer_horizontal';
1760                         else if ( resizable == CKEDITOR.DIALOG_RESIZE_HEIGHT )
1761                                 direction = ' cke_resizer_vertical';
1762                         var resizer = CKEDITOR.dom.element.createFromHtml( '<div class="cke_resizer' + direction + '"' +
1763                                         ' title="' + CKEDITOR.tools.htmlEncode( editor.lang.resize ) + '"' +
1764                                         ' onmousedown="CKEDITOR.tools.callFunction(' + mouseDownFn + ', event )"></div>' );
1765                         dialog.parts.footer.append( resizer, 1 );
1766                 });
1767                 editor.on( 'destroy', function() { CKEDITOR.tools.removeFunction( mouseDownFn ); } );
1769                 function mouseMoveHandler( evt )
1770                 {
1771                         var rtl = editor.lang.dir == 'rtl',
1772                                 dx = ( evt.data.$.screenX - origin.x ) * ( rtl ? -1 : 1 ),
1773                                 dy = evt.data.$.screenY - origin.y,
1774                                 width = startSize.width,
1775                                 height = startSize.height,
1776                                 internalWidth = width + dx * ( dialog._.moved ? 1 : 2 ),
1777                                 internalHeight = height + dy * ( dialog._.moved ? 1 : 2 ),
1778                                 element = dialog._.element.getFirst(),
1779                                 right = rtl && element.getComputedStyle( 'right' ),
1780                                 position = dialog.getPosition();
1782                         // IE might return "auto", we need exact position.
1783                         if ( right )
1784                                 right = right == 'auto' ? viewSize.width - ( position.x || 0 ) - element.getSize( 'width' ) : parseInt( right, 10 );
1786                         if ( position.y + internalHeight > viewSize.height )
1787                                 internalHeight = viewSize.height - position.y;
1789                         if ( ( rtl ? right : position.x ) + internalWidth > viewSize.width )
1790                                 internalWidth = viewSize.width - ( rtl ? right : position.x );
1792                         // Make sure the dialog will not be resized to the wrong side when it's in the leftmost position for RTL.
1793                         if ( ( resizable == CKEDITOR.DIALOG_RESIZE_WIDTH || resizable == CKEDITOR.DIALOG_RESIZE_BOTH ) && !( rtl && dx > 0 && !position.x ) )
1794                                 width = Math.max( def.minWidth || 0, internalWidth - wrapperWidth );
1796                         if ( resizable == CKEDITOR.DIALOG_RESIZE_HEIGHT || resizable == CKEDITOR.DIALOG_RESIZE_BOTH )
1797                                 height = Math.max( def.minHeight || 0, internalHeight - wrapperHeight );
1799                         dialog.resize( width, height );
1800                         // The right property might get broken during resizing, so computing it before the resizing.
1801                         positionDialog( right );
1803                         evt.data.preventDefault();
1804                 }
1806                 function mouseUpHandler()
1807                 {
1808                         CKEDITOR.document.removeListener( 'mouseup', mouseUpHandler );
1809                         CKEDITOR.document.removeListener( 'mousemove', mouseMoveHandler );
1811                         if ( dialogCover )
1812                         {
1813                                 dialogCover.remove();
1814                                 dialogCover = null;
1815                         }
1817                         if ( CKEDITOR.env.ie6Compat )
1818                         {
1819                                 var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
1820                                 coverDoc.removeListener( 'mouseup', mouseUpHandler );
1821                                 coverDoc.removeListener( 'mousemove', mouseMoveHandler );
1822                         }
1824                         // Switch back to use the left property, if RTL is used.
1825                         if ( editor.lang.dir == 'rtl' )
1826                         {
1827                                 var element = dialog._.element.getFirst(),
1828                                         left = element.getComputedStyle( 'left' );
1830                                 // IE might return "auto", we need exact position.
1831                                 if ( left == 'auto' )
1832                                         left = viewSize.width - parseInt( element.getStyle( 'right' ), 10 ) - dialog.getSize().width;
1833                                 else
1834                                         left = parseInt( left, 10 );
1836                                 element.removeStyle( 'right' );
1837                                 // Make sure the left property gets applied, even if it is the same as previously.
1838                                 dialog._.position.x += 1;
1839                                 dialog.move( left, dialog._.position.y );
1840                         }
1841                 }
1842         }
1844         var resizeCover;
1845         // Caching resuable covers and allowing only one cover
1846         // on screen.
1847         var covers = {},
1848                 currentCover;
1850         function showCover( editor )
1851         {
1852                 var win = CKEDITOR.document.getWindow();
1853                 var config = editor.config,
1854                         backgroundColorStyle = config.dialog_backgroundCoverColor || 'white',
1855                         backgroundCoverOpacity = config.dialog_backgroundCoverOpacity,
1856                         baseFloatZIndex = config.baseFloatZIndex,
1857                         coverKey = CKEDITOR.tools.genKey(
1858                                         backgroundColorStyle,
1859                                         backgroundCoverOpacity,
1860                                         baseFloatZIndex ),
1861                         coverElement = covers[ coverKey ];
1863                 if ( !coverElement )
1864                 {
1865                         var html = [
1866                                         '<div tabIndex="-1" style="position: ', ( CKEDITOR.env.ie6Compat ? 'absolute' : 'fixed' ),
1867                                         '; z-index: ', baseFloatZIndex,
1868                                         '; top: 0px; left: 0px; ',
1869                                         ( !CKEDITOR.env.ie6Compat ? 'background-color: ' + backgroundColorStyle : '' ),
1870                                         '" class="cke_dialog_background_cover">'
1871                                 ];
1873                         if ( CKEDITOR.env.ie6Compat )
1874                         {
1875                                 // Support for custom document.domain in IE.
1876                                 var isCustomDomain = CKEDITOR.env.isCustomDomain(),
1877                                         iframeHtml = '<html><body style=\\\'background-color:' + backgroundColorStyle + ';\\\'></body></html>';
1879                                 html.push(
1880                                         '<iframe' +
1881                                                 ' hidefocus="true"' +
1882                                                 ' frameborder="0"' +
1883                                                 ' id="cke_dialog_background_iframe"' +
1884                                                 ' src="javascript:' );
1886                                 html.push( 'void((function(){' +
1887                                                                 'document.open();' +
1888                                                                 ( isCustomDomain ? 'document.domain=\'' + document.domain + '\';' : '' ) +
1889                                                                 'document.write( \'' + iframeHtml + '\' );' +
1890                                                                 'document.close();' +
1891                                                         '})())' );
1893                                 html.push(
1894                                                 '"' +
1895                                                 ' style="' +
1896                                                         'position:absolute;' +
1897                                                         'left:0;' +
1898                                                         'top:0;' +
1899                                                         'width:100%;' +
1900                                                         'height: 100%;' +
1901                                                         'progid:DXImageTransform.Microsoft.Alpha(opacity=0)">' +
1902                                         '</iframe>' );
1903                         }
1905                         html.push( '</div>' );
1907                         coverElement = CKEDITOR.dom.element.createFromHtml( html.join( '' ) );
1908                         coverElement.setOpacity( backgroundCoverOpacity != undefined ? backgroundCoverOpacity : 0.5 );
1910                         coverElement.appendTo( CKEDITOR.document.getBody() );
1911                         covers[ coverKey ] = coverElement;
1912                 }
1913                 else
1914                         coverElement.   show();
1916                 currentCover = coverElement;
1917                 var resizeFunc = function()
1918                 {
1919                         var size = win.getViewPaneSize();
1920                         coverElement.setStyles(
1921                                 {
1922                                         width : size.width + 'px',
1923                                         height : size.height + 'px'
1924                                 } );
1925                 };
1927                 var scrollFunc = function()
1928                 {
1929                         var pos = win.getScrollPosition(),
1930                                 cursor = CKEDITOR.dialog._.currentTop;
1931                         coverElement.setStyles(
1932                                         {
1933                                                 left : pos.x + 'px',
1934                                                 top : pos.y + 'px'
1935                                         });
1937                         if ( cursor )
1938                         {
1939                                 do
1940                                 {
1941                                         var dialogPos = cursor.getPosition();
1942                                         cursor.move( dialogPos.x, dialogPos.y );
1943                                 } while ( ( cursor = cursor._.parentDialog ) );
1944                         }
1945                 };
1947                 resizeCover = resizeFunc;
1948                 win.on( 'resize', resizeFunc );
1949                 resizeFunc();
1950                 // Using Safari/Mac, focus must be kept where it is (#7027)
1951                 if ( !( CKEDITOR.env.mac && CKEDITOR.env.webkit ) )
1952                         coverElement.focus();
1954                 if ( CKEDITOR.env.ie6Compat )
1955                 {
1956                         // IE BUG: win.$.onscroll assignment doesn't work.. it must be window.onscroll.
1957                         // So we need to invent a really funny way to make it work.
1958                         var myScrollHandler = function()
1959                                 {
1960                                         scrollFunc();
1961                                         arguments.callee.prevScrollHandler.apply( this, arguments );
1962                                 };
1963                         win.$.setTimeout( function()
1964                                 {
1965                                         myScrollHandler.prevScrollHandler = window.onscroll || function(){};
1966                                         window.onscroll = myScrollHandler;
1967                                 }, 0 );
1968                         scrollFunc();
1969                 }
1970         }
1972         function hideCover()
1973         {
1974                 if ( !currentCover )
1975                         return;
1977                 var win = CKEDITOR.document.getWindow();
1978                 currentCover.hide();
1979                 win.removeListener( 'resize', resizeCover );
1981                 if ( CKEDITOR.env.ie6Compat )
1982                 {
1983                         win.$.setTimeout( function()
1984                                 {
1985                                         var prevScrollHandler = window.onscroll && window.onscroll.prevScrollHandler;
1986                                         window.onscroll = prevScrollHandler || null;
1987                                 }, 0 );
1988                 }
1989                 resizeCover = null;
1990         }
1992         function removeCovers()
1993         {
1994                 for ( var coverId in covers )
1995                         covers[ coverId ].remove();
1996                 covers = {};
1997         }
1999         var accessKeyProcessors = {};
2001         var accessKeyDownHandler = function( evt )
2002         {
2003                 var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
2004                         alt = evt.data.$.altKey,
2005                         shift = evt.data.$.shiftKey,
2006                         key = String.fromCharCode( evt.data.$.keyCode ),
2007                         keyProcessor = accessKeyProcessors[( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '') + ( shift ? 'SHIFT+' : '' ) + key];
2009                 if ( !keyProcessor || !keyProcessor.length )
2010                         return;
2012                 keyProcessor = keyProcessor[keyProcessor.length - 1];
2013                 keyProcessor.keydown && keyProcessor.keydown.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
2014                 evt.data.preventDefault();
2015         };
2017         var accessKeyUpHandler = function( evt )
2018         {
2019                 var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
2020                         alt = evt.data.$.altKey,
2021                         shift = evt.data.$.shiftKey,
2022                         key = String.fromCharCode( evt.data.$.keyCode ),
2023                         keyProcessor = accessKeyProcessors[( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '') + ( shift ? 'SHIFT+' : '' ) + key];
2025                 if ( !keyProcessor || !keyProcessor.length )
2026                         return;
2028                 keyProcessor = keyProcessor[keyProcessor.length - 1];
2029                 if ( keyProcessor.keyup )
2030                 {
2031                         keyProcessor.keyup.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
2032                         evt.data.preventDefault();
2033                 }
2034         };
2036         var registerAccessKey = function( uiElement, dialog, key, downFunc, upFunc )
2037         {
2038                 var procList = accessKeyProcessors[key] || ( accessKeyProcessors[key] = [] );
2039                 procList.push( {
2040                                 uiElement : uiElement,
2041                                 dialog : dialog,
2042                                 key : key,
2043                                 keyup : upFunc || uiElement.accessKeyUp,
2044                                 keydown : downFunc || uiElement.accessKeyDown
2045                         } );
2046         };
2048         var unregisterAccessKey = function( obj )
2049         {
2050                 for ( var i in accessKeyProcessors )
2051                 {
2052                         var list = accessKeyProcessors[i];
2053                         for ( var j = list.length - 1 ; j >= 0 ; j-- )
2054                         {
2055                                 if ( list[j].dialog == obj || list[j].uiElement == obj )
2056                                         list.splice( j, 1 );
2057                         }
2058                         if ( list.length === 0 )
2059                                 delete accessKeyProcessors[i];
2060                 }
2061         };
2063         var tabAccessKeyUp = function( dialog, key )
2064         {
2065                 if ( dialog._.accessKeyMap[key] )
2066                         dialog.selectPage( dialog._.accessKeyMap[key] );
2067         };
2069         var tabAccessKeyDown = function( dialog, key )
2070         {
2071         };
2073         // ESC, ENTER
2074         var preventKeyBubblingKeys = { 27 :1, 13 :1 };
2075         var preventKeyBubbling = function( e )
2076         {
2077                 if ( e.data.getKeystroke() in preventKeyBubblingKeys )
2078                         e.data.stopPropagation();
2079         };
2081         (function()
2082         {
2083                 CKEDITOR.ui.dialog =
2084                 {
2085                         /**
2086                          * The base class of all dialog UI elements.
2087                          * @constructor
2088                          * @param {CKEDITOR.dialog} dialog Parent dialog object.
2089                          * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition Element
2090                          * definition. Accepted fields:
2091                          * <ul>
2092                          *      <li><strong>id</strong> (Required) The id of the UI element. See {@link
2093                          *      CKEDITOR.dialog#getContentElement}</li>
2094                          *      <li><strong>type</strong> (Required) The type of the UI element. The
2095                          *      value to this field specifies which UI element class will be used to
2096                          *      generate the final widget.</li>
2097                          *      <li><strong>title</strong> (Optional) The popup tooltip for the UI
2098                          *      element.</li>
2099                          *      <li><strong>hidden</strong> (Optional) A flag that tells if the element
2100                          *      should be initially visible.</li>
2101                          *      <li><strong>className</strong> (Optional) Additional CSS class names
2102                          *      to add to the UI element. Separated by space.</li>
2103                          *      <li><strong>style</strong> (Optional) Additional CSS inline styles
2104                          *      to add to the UI element. A semicolon (;) is required after the last
2105                          *      style declaration.</li>
2106                          *      <li><strong>accessKey</strong> (Optional) The alphanumeric access key
2107                          *      for this element. Access keys are automatically prefixed by CTRL.</li>
2108                          *      <li><strong>on*</strong> (Optional) Any UI element definition field that
2109                          *      starts with <em>on</em> followed immediately by a capital letter and
2110                          *      probably more letters is an event handler. Event handlers may be further
2111                          *      divided into registered event handlers and DOM event handlers. Please
2112                          *      refer to {@link CKEDITOR.ui.dialog.uiElement#registerEvents} and
2113                          *      {@link CKEDITOR.ui.dialog.uiElement#eventProcessors} for more
2114                          *      information.</li>
2115                          * </ul>
2116                          * @param {Array} htmlList
2117                          * List of HTML code to be added to the dialog's content area.
2118                          * @param {Function|String} nodeNameArg
2119                          * A function returning a string, or a simple string for the node name for
2120                          * the root DOM node. Default is 'div'.
2121                          * @param {Function|Object} stylesArg
2122                          * A function returning an object, or a simple object for CSS styles applied
2123                          * to the DOM node. Default is empty object.
2124                          * @param {Function|Object} attributesArg
2125                          * A fucntion returning an object, or a simple object for attributes applied
2126                          * to the DOM node. Default is empty object.
2127                          * @param {Function|String} contentsArg
2128                          * A function returning a string, or a simple string for the HTML code inside
2129                          * the root DOM node. Default is empty string.
2130                          * @example
2131                          */
2132                         uiElement : function( dialog, elementDefinition, htmlList, nodeNameArg, stylesArg, attributesArg, contentsArg )
2133                         {
2134                                 if ( arguments.length < 4 )
2135                                         return;
2137                                 var nodeName = ( nodeNameArg.call ? nodeNameArg( elementDefinition ) : nodeNameArg ) || 'div',
2138                                         html = [ '<', nodeName, ' ' ],
2139                                         styles = ( stylesArg && stylesArg.call ? stylesArg( elementDefinition ) : stylesArg ) || {},
2140                                         attributes = ( attributesArg && attributesArg.call ? attributesArg( elementDefinition ) : attributesArg ) || {},
2141                                         innerHTML = ( contentsArg && contentsArg.call ? contentsArg.call( this, dialog, elementDefinition ) : contentsArg ) || '',
2142                                         domId = this.domId = attributes.id || CKEDITOR.tools.getNextId() + '_uiElement',
2143                                         id = this.id = elementDefinition.id,
2144                                         i;
2146                                 // Set the id, a unique id is required for getElement() to work.
2147                                 attributes.id = domId;
2149                                 // Set the type and definition CSS class names.
2150                                 var classes = {};
2151                                 if ( elementDefinition.type )
2152                                         classes[ 'cke_dialog_ui_' + elementDefinition.type ] = 1;
2153                                 if ( elementDefinition.className )
2154                                         classes[ elementDefinition.className ] = 1;
2155                                 var attributeClasses = ( attributes['class'] && attributes['class'].split ) ? attributes['class'].split( ' ' ) : [];
2156                                 for ( i = 0 ; i < attributeClasses.length ; i++ )
2157                                 {
2158                                         if ( attributeClasses[i] )
2159                                                 classes[ attributeClasses[i] ] = 1;
2160                                 }
2161                                 var finalClasses = [];
2162                                 for ( i in classes )
2163                                         finalClasses.push( i );
2164                                 attributes['class'] = finalClasses.join( ' ' );
2166                                 // Set the popup tooltop.
2167                                 if ( elementDefinition.title )
2168                                         attributes.title = elementDefinition.title;
2170                                 // Write the inline CSS styles.
2171                                 var styleStr = ( elementDefinition.style || '' ).split( ';' );
2172                                 for ( i in styles )
2173                                         styleStr.push( i + ':' + styles[i] );
2174                                 if ( elementDefinition.hidden )
2175                                         styleStr.push( 'display:none' );
2176                                 for ( i = styleStr.length - 1 ; i >= 0 ; i-- )
2177                                 {
2178                                         if ( styleStr[i] === '' )
2179                                                 styleStr.splice( i, 1 );
2180                                 }
2181                                 if ( styleStr.length > 0 )
2182                                         attributes.style = ( attributes.style ? ( attributes.style + '; ' ) : '' ) + styleStr.join( '; ' );
2184                                 // Write the attributes.
2185                                 for ( i in attributes )
2186                                         html.push( i + '="' + CKEDITOR.tools.htmlEncode( attributes[i] ) + '" ');
2188                                 // Write the content HTML.
2189                                 html.push( '>', innerHTML, '</', nodeName, '>' );
2191                                 // Add contents to the parent HTML array.
2192                                 htmlList.push( html.join( '' ) );
2194                                 ( this._ || ( this._ = {} ) ).dialog = dialog;
2196                                 // Override isChanged if it is defined in element definition.
2197                                 if ( typeof( elementDefinition.isChanged ) == 'boolean' )
2198                                         this.isChanged = function(){ return elementDefinition.isChanged; };
2199                                 if ( typeof( elementDefinition.isChanged ) == 'function' )
2200                                         this.isChanged = elementDefinition.isChanged;
2202                                 // Add events.
2203                                 CKEDITOR.event.implementOn( this );
2205                                 this.registerEvents( elementDefinition );
2206                                 if ( this.accessKeyUp && this.accessKeyDown && elementDefinition.accessKey )
2207                                         registerAccessKey( this, dialog, 'CTRL+' + elementDefinition.accessKey );
2209                                 var me = this;
2210                                 dialog.on( 'load', function()
2211                                         {
2212                                                 if ( me.getInputElement() )
2213                                                 {
2214                                                         me.getInputElement().on( 'focus', function()
2215                                                                 {
2216                                                                         dialog._.tabBarMode = false;
2217                                                                         dialog._.hasFocus = true;
2218                                                                         me.fire( 'focus' );
2219                                                                 }, me );
2220                                                 }
2221                                         } );
2223                                 // Register the object as a tab focus if it can be included.
2224                                 if ( this.keyboardFocusable )
2225                                 {
2226                                         this.tabIndex = elementDefinition.tabIndex || 0;
2228                                         this.focusIndex = dialog._.focusList.push( this ) - 1;
2229                                         this.on( 'focus', function()
2230                                                 {
2231                                                         dialog._.currentFocusIndex = me.focusIndex;
2232                                                 } );
2233                                 }
2235                                 // Completes this object with everything we have in the
2236                                 // definition.
2237                                 CKEDITOR.tools.extend( this, elementDefinition );
2238                         },
2240                         /**
2241                          * Horizontal layout box for dialog UI elements, auto-expends to available width of container.
2242                          * @constructor
2243                          * @extends CKEDITOR.ui.dialog.uiElement
2244                          * @param {CKEDITOR.dialog} dialog
2245                          * Parent dialog object.
2246                          * @param {Array} childObjList
2247                          * Array of {@link CKEDITOR.ui.dialog.uiElement} objects inside this
2248                          * container.
2249                          * @param {Array} childHtmlList
2250                          * Array of HTML code that correspond to the HTML output of all the
2251                          * objects in childObjList.
2252                          * @param {Array} htmlList
2253                          * Array of HTML code that this element will output to.
2254                          * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
2255                          * The element definition. Accepted fields:
2256                          * <ul>
2257                          *      <li><strong>widths</strong> (Optional) The widths of child cells.</li>
2258                          *      <li><strong>height</strong> (Optional) The height of the layout.</li>
2259                          *      <li><strong>padding</strong> (Optional) The padding width inside child
2260                          *       cells.</li>
2261                          *      <li><strong>align</strong> (Optional) The alignment of the whole layout
2262                          *      </li>
2263                          * </ul>
2264                          * @example
2265                          */
2266                         hbox : function( dialog, childObjList, childHtmlList, htmlList, elementDefinition )
2267                         {
2268                                 if ( arguments.length < 4 )
2269                                         return;
2271                                 this._ || ( this._ = {} );
2273                                 var children = this._.children = childObjList,
2274                                         widths = elementDefinition && elementDefinition.widths || null,
2275                                         height = elementDefinition && elementDefinition.height || null,
2276                                         styles = {},
2277                                         i;
2278                                 /** @ignore */
2279                                 var innerHTML = function()
2280                                 {
2281                                         var html = [ '<tbody><tr class="cke_dialog_ui_hbox">' ];
2282                                         for ( i = 0 ; i < childHtmlList.length ; i++ )
2283                                         {
2284                                                 var className = 'cke_dialog_ui_hbox_child',
2285                                                         styles = [];
2286                                                 if ( i === 0 )
2287                                                         className = 'cke_dialog_ui_hbox_first';
2288                                                 if ( i == childHtmlList.length - 1 )
2289                                                         className = 'cke_dialog_ui_hbox_last';
2290                                                 html.push( '<td class="', className, '" role="presentation" ' );
2291                                                 if ( widths )
2292                                                 {
2293                                                         if ( widths[i] )
2294                                                                 styles.push( 'width:' + cssLength( widths[i] ) );
2295                                                 }
2296                                                 else
2297                                                         styles.push( 'width:' + Math.floor( 100 / childHtmlList.length ) + '%' );
2298                                                 if ( height )
2299                                                         styles.push( 'height:' + cssLength( height ) );
2300                                                 if ( elementDefinition && elementDefinition.padding != undefined )
2301                                                         styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
2302                                                 if ( styles.length > 0 )
2303                                                         html.push( 'style="' + styles.join('; ') + '" ' );
2304                                                 html.push( '>', childHtmlList[i], '</td>' );
2305                                         }
2306                                         html.push( '</tr></tbody>' );
2307                                         return html.join( '' );
2308                                 };
2310                                 var attribs = { role : 'presentation' };
2311                                 elementDefinition && elementDefinition.align && ( attribs.align = elementDefinition.align );
2313                                 CKEDITOR.ui.dialog.uiElement.call(
2314                                         this,
2315                                         dialog,
2316                                         elementDefinition || { type : 'hbox' },
2317                                         htmlList,
2318                                         'table',
2319                                         styles,
2320                                         attribs,
2321                                         innerHTML );
2322                         },
2324                         /**
2325                          * Vertical layout box for dialog UI elements.
2326                          * @constructor
2327                          * @extends CKEDITOR.ui.dialog.hbox
2328                          * @param {CKEDITOR.dialog} dialog
2329                          * Parent dialog object.
2330                          * @param {Array} childObjList
2331                          * Array of {@link CKEDITOR.ui.dialog.uiElement} objects inside this
2332                          * container.
2333                          * @param {Array} childHtmlList
2334                          * Array of HTML code that correspond to the HTML output of all the
2335                          * objects in childObjList.
2336                          * @param {Array} htmlList
2337                          * Array of HTML code that this element will output to.
2338                          * @param {CKEDITOR.dialog.uiElementDefinition} elementDefinition
2339                          * The element definition. Accepted fields:
2340                          * <ul>
2341                          *      <li><strong>width</strong> (Optional) The width of the layout.</li>
2342                          *      <li><strong>heights</strong> (Optional) The heights of individual cells.
2343                          *      </li>
2344                          *      <li><strong>align</strong> (Optional) The alignment of the layout.</li>
2345                          *      <li><strong>padding</strong> (Optional) The padding width inside child
2346                          *      cells.</li>
2347                          *      <li><strong>expand</strong> (Optional) Whether the layout should expand
2348                          *      vertically to fill its container.</li>
2349                          * </ul>
2350                          * @example
2351                          */
2352                         vbox : function( dialog, childObjList, childHtmlList, htmlList, elementDefinition )
2353                         {
2354                                 if ( arguments.length < 3 )
2355                                         return;
2357                                 this._ || ( this._ = {} );
2359                                 var children = this._.children = childObjList,
2360                                         width = elementDefinition && elementDefinition.width || null,
2361                                         heights = elementDefinition && elementDefinition.heights || null;
2362                                 /** @ignore */
2363                                 var innerHTML = function()
2364                                 {
2365                                         var html = [ '<table role="presentation" cellspacing="0" border="0" ' ];
2366                                         html.push( 'style="' );
2367                                         if ( elementDefinition && elementDefinition.expand )
2368                                                 html.push( 'height:100%;' );
2369                                         html.push( 'width:' + cssLength( width || '100%' ), ';' );
2370                                         html.push( '"' );
2371                                         html.push( 'align="', CKEDITOR.tools.htmlEncode(
2372                                                 ( elementDefinition && elementDefinition.align ) || ( dialog.getParentEditor().lang.dir == 'ltr' ? 'left' : 'right' ) ), '" ' );
2374                                         html.push( '><tbody>' );
2375                                         for ( var i = 0 ; i < childHtmlList.length ; i++ )
2376                                         {
2377                                                 var styles = [];
2378                                                 html.push( '<tr><td role="presentation" ' );
2379                                                 if ( width )
2380                                                         styles.push( 'width:' + cssLength( width || '100%' ) );
2381                                                 if ( heights )
2382                                                         styles.push( 'height:' + cssLength( heights[i] ) );
2383                                                 else if ( elementDefinition && elementDefinition.expand )
2384                                                         styles.push( 'height:' + Math.floor( 100 / childHtmlList.length ) + '%' );
2385                                                 if ( elementDefinition && elementDefinition.padding != undefined )
2386                                                         styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
2387                                                 if ( styles.length > 0 )
2388                                                         html.push( 'style="', styles.join( '; ' ), '" ' );
2389                                                 html.push( ' class="cke_dialog_ui_vbox_child">', childHtmlList[i], '</td></tr>' );
2390                                         }
2391                                         html.push( '</tbody></table>' );
2392                                         return html.join( '' );
2393                                 };
2394                                 CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition || { type : 'vbox' }, htmlList, 'div', null, { role : 'presentation' }, innerHTML );
2395                         }
2396                 };
2397         })();
2399         CKEDITOR.ui.dialog.uiElement.prototype =
2400         {
2401                 /**
2402                  * Gets the root DOM element of this dialog UI object.
2403                  * @returns {CKEDITOR.dom.element} Root DOM element of UI object.
2404                  * @example
2405                  * uiElement.getElement().hide();
2406                  */
2407                 getElement : function()
2408                 {
2409                         return CKEDITOR.document.getById( this.domId );
2410                 },
2412                 /**
2413                  * Gets the DOM element that the user inputs values.
2414                  * This function is used by setValue(), getValue() and focus(). It should
2415                  * be overrided in child classes where the input element isn't the root
2416                  * element.
2417                  * @returns {CKEDITOR.dom.element} The element where the user input values.
2418                  * @example
2419                  * var rawValue = textInput.getInputElement().$.value;
2420                  */
2421                 getInputElement : function()
2422                 {
2423                         return this.getElement();
2424                 },
2426                 /**
2427                  * Gets the parent dialog object containing this UI element.
2428                  * @returns {CKEDITOR.dialog} Parent dialog object.
2429                  * @example
2430                  * var dialog = uiElement.getDialog();
2431                  */
2432                 getDialog : function()
2433                 {
2434                         return this._.dialog;
2435                 },
2437                 /**
2438                  * Sets the value of this dialog UI object.
2439                  * @param {Object} value The new value.
2440                  * @param {Boolean} noChangeEvent Internal commit, to supress 'change' event on this element.
2441                  * @returns {CKEDITOR.dialog.uiElement} The current UI element.
2442                  * @example
2443                  * uiElement.setValue( 'Dingo' );
2444                  */
2445                 setValue : function( value, noChangeEvent )
2446                 {
2447                         this.getInputElement().setValue( value );
2448                         !noChangeEvent && this.fire( 'change', { value : value } );
2449                         return this;
2450                 },
2452                 /**
2453                  * Gets the current value of this dialog UI object.
2454                  * @returns {Object} The current value.
2455                  * @example
2456                  * var myValue = uiElement.getValue();
2457                  */
2458                 getValue : function()
2459                 {
2460                         return this.getInputElement().getValue();
2461                 },
2463                 /**
2464                  * Tells whether the UI object's value has changed.
2465                  * @returns {Boolean} true if changed, false if not changed.
2466                  * @example
2467                  * if ( uiElement.isChanged() )
2468                  * &nbsp;&nbsp;confirm( 'Value changed! Continue?' );
2469                  */
2470                 isChanged : function()
2471                 {
2472                         // Override in input classes.
2473                         return false;
2474                 },
2476                 /**
2477                  * Selects the parent tab of this element. Usually called by focus() or overridden focus() methods.
2478                  * @returns {CKEDITOR.dialog.uiElement} The current UI element.
2479                  * @example
2480                  * focus : function()
2481                  * {
2482                  *              this.selectParentTab();
2483                  *              // do something else.
2484                  * }
2485                  */
2486                 selectParentTab : function()
2487                 {
2488                         var element = this.getInputElement(),
2489                                 cursor = element,
2490                                 tabId;
2491                         while ( ( cursor = cursor.getParent() ) && cursor.$.className.search( 'cke_dialog_page_contents' ) == -1 )
2492                         { /*jsl:pass*/ }
2494                         // Some widgets don't have parent tabs (e.g. OK and Cancel buttons).
2495                         if ( !cursor )
2496                                 return this;
2498                         tabId = cursor.getAttribute( 'name' );
2499                         // Avoid duplicate select.
2500                         if ( this._.dialog._.currentTabId != tabId )
2501                                 this._.dialog.selectPage( tabId );
2502                         return this;
2503                 },
2505                 /**
2506                  * Puts the focus to the UI object. Switches tabs if the UI object isn't in the active tab page.
2507                  * @returns {CKEDITOR.dialog.uiElement} The current UI element.
2508                  * @example
2509                  * uiElement.focus();
2510                  */
2511                 focus : function()
2512                 {
2513                         this.selectParentTab().getInputElement().focus();
2514                         return this;
2515                 },
2517                 /**
2518                  * Registers the on* event handlers defined in the element definition.
2519                  * The default behavior of this function is:
2520                  * <ol>
2521                  *  <li>
2522                  *      If the on* event is defined in the class's eventProcesors list,
2523                  *      then the registration is delegated to the corresponding function
2524                  *      in the eventProcessors list.
2525                  *  </li>
2526                  *  <li>
2527                  *      If the on* event is not defined in the eventProcessors list, then
2528                  *      register the event handler under the corresponding DOM event of
2529                  *      the UI element's input DOM element (as defined by the return value
2530                  *      of {@link CKEDITOR.ui.dialog.uiElement#getInputElement}).
2531                  *  </li>
2532                  * </ol>
2533                  * This function is only called at UI element instantiation, but can
2534                  * be overridded in child classes if they require more flexibility.
2535                  * @param {CKEDITOR.dialog.uiElementDefinition} definition The UI element
2536                  * definition.
2537                  * @returns {CKEDITOR.dialog.uiElement} The current UI element.
2538                  * @example
2539                  */
2540                 registerEvents : function( definition )
2541                 {
2542                         var regex = /^on([A-Z]\w+)/,
2543                                 match;
2545                         var registerDomEvent = function( uiElement, dialog, eventName, func )
2546                         {
2547                                 dialog.on( 'load', function()
2548                                 {
2549                                         uiElement.getInputElement().on( eventName, func, uiElement );
2550                                 });
2551                         };
2553                         for ( var i in definition )
2554                         {
2555                                 if ( !( match = i.match( regex ) ) )
2556                                         continue;
2557                                 if ( this.eventProcessors[i] )
2558                                         this.eventProcessors[i].call( this, this._.dialog, definition[i] );
2559                                 else
2560                                         registerDomEvent( this, this._.dialog, match[1].toLowerCase(), definition[i] );
2561                         }
2563                         return this;
2564                 },
2566                 /**
2567                  * The event processor list used by
2568                  * {@link CKEDITOR.ui.dialog.uiElement#getInputElement} at UI element
2569                  * instantiation. The default list defines three on* events:
2570                  * <ol>
2571                  *  <li>onLoad - Called when the element's parent dialog opens for the
2572                  *  first time</li>
2573                  *  <li>onShow - Called whenever the element's parent dialog opens.</li>
2574                  *  <li>onHide - Called whenever the element's parent dialog closes.</li>
2575                  * </ol>
2576                  * @field
2577                  * @type Object
2578                  * @example
2579                  * // This connects the 'click' event in CKEDITOR.ui.dialog.button to onClick
2580                  * // handlers in the UI element's definitions.
2581                  * CKEDITOR.ui.dialog.button.eventProcessors = CKEDITOR.tools.extend( {},
2582                  * &nbsp;&nbsp;CKEDITOR.ui.dialog.uiElement.prototype.eventProcessors,
2583                  * &nbsp;&nbsp;{ onClick : function( dialog, func ) { this.on( 'click', func ); } },
2584                  * &nbsp;&nbsp;true );
2585                  */
2586                 eventProcessors :
2587                 {
2588                         onLoad : function( dialog, func )
2589                         {
2590                                 dialog.on( 'load', func, this );
2591                         },
2593                         onShow : function( dialog, func )
2594                         {
2595                                 dialog.on( 'show', func, this );
2596                         },
2598                         onHide : function( dialog, func )
2599                         {
2600                                 dialog.on( 'hide', func, this );
2601                         }
2602                 },
2604                 /**
2605                  * The default handler for a UI element's access key down event, which
2606                  * tries to put focus to the UI element.<br />
2607                  * Can be overridded in child classes for more sophisticaed behavior.
2608                  * @param {CKEDITOR.dialog} dialog The parent dialog object.
2609                  * @param {String} key The key combination pressed. Since access keys
2610                  * are defined to always include the CTRL key, its value should always
2611                  * include a 'CTRL+' prefix.
2612                  * @example
2613                  */
2614                 accessKeyDown : function( dialog, key )
2615                 {
2616                         this.focus();
2617                 },
2619                 /**
2620                  * The default handler for a UI element's access key up event, which
2621                  * does nothing.<br />
2622                  * Can be overridded in child classes for more sophisticated behavior.
2623                  * @param {CKEDITOR.dialog} dialog The parent dialog object.
2624                  * @param {String} key The key combination pressed. Since access keys
2625                  * are defined to always include the CTRL key, its value should always
2626                  * include a 'CTRL+' prefix.
2627                  * @example
2628                  */
2629                 accessKeyUp : function( dialog, key )
2630                 {
2631                 },
2633                 /**
2634                  * Disables a UI element.
2635                  * @example
2636                  */
2637                 disable : function()
2638                 {
2639                         var element = this.getInputElement();
2640                         element.setAttribute( 'disabled', 'true' );
2641                         element.addClass( 'cke_disabled' );
2642                 },
2644                 /**
2645                  * Enables a UI element.
2646                  * @example
2647                  */
2648                 enable : function()
2649                 {
2650                         var element = this.getInputElement();
2651                         element.removeAttribute( 'disabled' );
2652                         element.removeClass( 'cke_disabled' );
2653                 },
2655                 /**
2656                  * Determines whether an UI element is enabled or not.
2657                  * @returns {Boolean} Whether the UI element is enabled.
2658                  * @example
2659                  */
2660                 isEnabled : function()
2661                 {
2662                         return !this.getInputElement().getAttribute( 'disabled' );
2663                 },
2665                 /**
2666                  * Determines whether an UI element is visible or not.
2667                  * @returns {Boolean} Whether the UI element is visible.
2668                  * @example
2669                  */
2670                 isVisible : function()
2671                 {
2672                         return this.getInputElement().isVisible();
2673                 },
2675                 /**
2676                  * Determines whether an UI element is focus-able or not.
2677                  * Focus-able is defined as being both visible and enabled.
2678                  * @returns {Boolean} Whether the UI element can be focused.
2679                  * @example
2680                  */
2681                 isFocusable : function()
2682                 {
2683                         if ( !this.isEnabled() || !this.isVisible() )
2684                                 return false;
2685                         return true;
2686                 }
2687         };
2689         CKEDITOR.ui.dialog.hbox.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement,
2690                 /**
2691                  * @lends CKEDITOR.ui.dialog.hbox.prototype
2692                  */
2693                 {
2694                         /**
2695                          * Gets a child UI element inside this container.
2696                          * @param {Array|Number} indices An array or a single number to indicate the child's
2697                          * position in the container's descendant tree. Omit to get all the children in an array.
2698                          * @returns {Array|CKEDITOR.ui.dialog.uiElement} Array of all UI elements in the container
2699                          * if no argument given, or the specified UI element if indices is given.
2700                          * @example
2701                          * var checkbox = hbox.getChild( [0,1] );
2702                          * checkbox.setValue( true );
2703                          */
2704                         getChild : function( indices )
2705                         {
2706                                 // If no arguments, return a clone of the children array.
2707                                 if ( arguments.length < 1 )
2708                                         return this._.children.concat();
2710                                 // If indices isn't array, make it one.
2711                                 if ( !indices.splice )
2712                                         indices = [ indices ];
2714                                 // Retrieve the child element according to tree position.
2715                                 if ( indices.length < 2 )
2716                                         return this._.children[ indices[0] ];
2717                                 else
2718                                         return ( this._.children[ indices[0] ] && this._.children[ indices[0] ].getChild ) ?
2719                                                 this._.children[ indices[0] ].getChild( indices.slice( 1, indices.length ) ) :
2720                                                 null;
2721                         }
2722                 }, true );
2724         CKEDITOR.ui.dialog.vbox.prototype = new CKEDITOR.ui.dialog.hbox();
2728         (function()
2729         {
2730                 var commonBuilder = {
2731                         build : function( dialog, elementDefinition, output )
2732                         {
2733                                 var children = elementDefinition.children,
2734                                         child,
2735                                         childHtmlList = [],
2736                                         childObjList = [];
2737                                 for ( var i = 0 ; ( i < children.length && ( child = children[i] ) ) ; i++ )
2738                                 {
2739                                         var childHtml = [];
2740                                         childHtmlList.push( childHtml );
2741                                         childObjList.push( CKEDITOR.dialog._.uiElementBuilders[ child.type ].build( dialog, child, childHtml ) );
2742                                 }
2743                                 return new CKEDITOR.ui.dialog[elementDefinition.type]( dialog, childObjList, childHtmlList, output, elementDefinition );
2744                         }
2745                 };
2747                 CKEDITOR.dialog.addUIElement( 'hbox', commonBuilder );
2748                 CKEDITOR.dialog.addUIElement( 'vbox', commonBuilder );
2749         })();
2751         /**
2752          * Generic dialog command. It opens a specific dialog when executed.
2753          * @constructor
2754          * @augments CKEDITOR.commandDefinition
2755          * @param {string} dialogName The name of the dialog to open when executing
2756          *              this command.
2757          * @example
2758          * // Register the "link" command, which opens the "link" dialog.
2759          * editor.addCommand( 'link', <b>new CKEDITOR.dialogCommand( 'link' )</b> );
2760          */
2761         CKEDITOR.dialogCommand = function( dialogName )
2762         {
2763                 this.dialogName = dialogName;
2764         };
2766         CKEDITOR.dialogCommand.prototype =
2767         {
2768                 /** @ignore */
2769                 exec : function( editor )
2770                 {
2771                         editor.openDialog( this.dialogName );
2772                 },
2774                 // Dialog commands just open a dialog ui, thus require no undo logic,
2775                 // undo support should dedicate to specific dialog implementation.
2776                 canUndo: false,
2778                 editorFocus : CKEDITOR.env.ie || CKEDITOR.env.webkit
2779         };
2781         (function()
2782         {
2783                 var notEmptyRegex = /^([a]|[^a])+$/,
2784                         integerRegex = /^\d*$/,
2785                         numberRegex = /^\d*(?:\.\d+)?$/;
2787                 CKEDITOR.VALIDATE_OR = 1;
2788                 CKEDITOR.VALIDATE_AND = 2;
2790                 CKEDITOR.dialog.validate =
2791                 {
2792                         functions : function()
2793                         {
2794                                 return function()
2795                                 {
2796                                         /**
2797                                          * It's important for validate functions to be able to accept the value
2798                                          * as argument in addition to this.getValue(), so that it is possible to
2799                                          * combine validate functions together to make more sophisticated
2800                                          * validators.
2801                                          */
2802                                         var value = this && this.getValue ? this.getValue() : arguments[0];
2804                                         var msg = undefined,
2805                                                 relation = CKEDITOR.VALIDATE_AND,
2806                                                 functions = [], i;
2808                                         for ( i = 0 ; i < arguments.length ; i++ )
2809                                         {
2810                                                 if ( typeof( arguments[i] ) == 'function' )
2811                                                         functions.push( arguments[i] );
2812                                                 else
2813                                                         break;
2814                                         }
2816                                         if ( i < arguments.length && typeof( arguments[i] ) == 'string' )
2817                                         {
2818                                                 msg = arguments[i];
2819                                                 i++;
2820                                         }
2822                                         if ( i < arguments.length && typeof( arguments[i]) == 'number' )
2823                                                 relation = arguments[i];
2825                                         var passed = ( relation == CKEDITOR.VALIDATE_AND ? true : false );
2826                                         for ( i = 0 ; i < functions.length ; i++ )
2827                                         {
2828                                                 if ( relation == CKEDITOR.VALIDATE_AND )
2829                                                         passed = passed && functions[i]( value );
2830                                                 else
2831                                                         passed = passed || functions[i]( value );
2832                                         }
2834                                         if ( !passed )
2835                                         {
2836                                                 if ( msg !== undefined )
2837                                                         alert( msg );
2838                                                 if ( this && ( this.select || this.focus ) )
2839                                                         ( this.select || this.focus )();
2840                                                 return false;
2841                                         }
2843                                         return true;
2844                                 };
2845                         },
2847                         regex : function( regex, msg )
2848                         {
2849                                 /*
2850                                  * Can be greatly shortened by deriving from functions validator if code size
2851                                  * turns out to be more important than performance.
2852                                  */
2853                                 return function()
2854                                 {
2855                                         var value = this && this.getValue ? this.getValue() : arguments[0];
2856                                         if ( !regex.test( value ) )
2857                                         {
2858                                                 if ( msg !== undefined )
2859                                                         alert( msg );
2860                                                 if ( this && ( this.select || this.focus ) )
2861                                                 {
2862                                                         if ( this.select )
2863                                                                 this.select();
2864                                                         else
2865                                                                 this.focus();
2866                                                 }
2867                                                 return false;
2868                                         }
2869                                         return true;
2870                                 };
2871                         },
2873                         notEmpty : function( msg )
2874                         {
2875                                 return this.regex( notEmptyRegex, msg );
2876                         },
2878                         integer : function( msg )
2879                         {
2880                                 return this.regex( integerRegex, msg );
2881                         },
2883                         'number' : function( msg )
2884                         {
2885                                 return this.regex( numberRegex, msg );
2886                         },
2888                         equals : function( value, msg )
2889                         {
2890                                 return this.functions( function( val ){ return val == value; }, msg );
2891                         },
2893                         notEqual : function( value, msg )
2894                         {
2895                                 return this.functions( function( val ){ return val != value; }, msg );
2896                         }
2897                 };
2899         CKEDITOR.on( 'instanceDestroyed', function( evt )
2900         {
2901                 // Remove dialog cover on last instance destroy.
2902                 if ( CKEDITOR.tools.isEmpty( CKEDITOR.instances ) )
2903                 {
2904                         var currentTopDialog;
2905                         while ( ( currentTopDialog = CKEDITOR.dialog._.currentTop ) )
2906                                 currentTopDialog.hide();
2907                         removeCovers();
2908                 }
2910                 var dialogs = evt.editor._.storedDialogs;
2911                 for ( var name in dialogs )
2912                         dialogs[ name ].destroy();
2914         });
2916         })();
2918         // Extend the CKEDITOR.editor class with dialog specific functions.
2919         CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
2920                 /** @lends CKEDITOR.editor.prototype */
2921                 {
2922                         /**
2923                          * Loads and opens a registered dialog.
2924                          * @param {String} dialogName The registered name of the dialog.
2925                          * @param {Function} callback The function to be invoked after dialog instance created.
2926                          * @see CKEDITOR.dialog.add
2927                          * @example
2928                          * CKEDITOR.instances.editor1.openDialog( 'smiley' );
2929                          * @returns {CKEDITOR.dialog} The dialog object corresponding to the dialog displayed. null if the dialog name is not registered.
2930                          */
2931                         openDialog : function( dialogName, callback )
2932                         {
2933                                 if ( this.mode == 'wysiwyg' && CKEDITOR.env.ie )
2934                                 {
2935                                         var selection = this.getSelection();
2936                                         selection && selection.lock();
2937                                 }
2939                                 var dialogDefinitions = CKEDITOR.dialog._.dialogDefinitions[ dialogName ],
2940                                                 dialogSkin = this.skin.dialog;
2942                                 if ( CKEDITOR.dialog._.currentTop === null )
2943                                         showCover( this );
2945                                 // If the dialogDefinition is already loaded, open it immediately.
2946                                 if ( typeof dialogDefinitions == 'function' && dialogSkin._isLoaded )
2947                                 {
2948                                         var storedDialogs = this._.storedDialogs ||
2949                                                 ( this._.storedDialogs = {} );
2951                                         var dialog = storedDialogs[ dialogName ] ||
2952                                                 ( storedDialogs[ dialogName ] = new CKEDITOR.dialog( this, dialogName ) );
2954                                         callback && callback.call( dialog, dialog );
2955                                         dialog.show();
2957                                         return dialog;
2958                                 }
2959                                 else if ( dialogDefinitions == 'failed' )
2960                                         throw new Error( '[CKEDITOR.dialog.openDialog] Dialog "' + dialogName + '" failed when loading definition.' );
2962                                 var me = this;
2964                                 function onDialogFileLoaded( success )
2965                                 {
2966                                         var dialogDefinition = CKEDITOR.dialog._.dialogDefinitions[ dialogName ],
2967                                                         skin = me.skin.dialog;
2969                                         // Check if both skin part and definition is loaded.
2970                                         if ( !skin._isLoaded || loadDefinition && typeof success == 'undefined' )
2971                                                 return;
2973                                         // In case of plugin error, mark it as loading failed.
2974                                         if ( typeof dialogDefinition != 'function' )
2975                                                 CKEDITOR.dialog._.dialogDefinitions[ dialogName ] = 'failed';
2977                                         me.openDialog( dialogName, callback );
2978                                 }
2980                                 if ( typeof dialogDefinitions == 'string' )
2981                                 {
2982                                         var loadDefinition = 1;
2983                                         CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( dialogDefinitions ), onDialogFileLoaded, null, 0, 1 );
2984                                 }
2986                                 CKEDITOR.skins.load( this, 'dialog', onDialogFileLoaded );
2988                                 return null;
2989                         }
2990                 });
2991 })();
2993 CKEDITOR.plugins.add( 'dialog',
2994         {
2995                 requires : [ 'dialogui' ]
2996         });
2998 // Dialog related configurations.
3001  * The color of the dialog background cover. It should be a valid CSS color
3002  * string.
3003  * @name CKEDITOR.config.dialog_backgroundCoverColor
3004  * @type String
3005  * @default 'white'
3006  * @example
3007  * config.dialog_backgroundCoverColor = 'rgb(255, 254, 253)';
3008  */
3011  * The opacity of the dialog background cover. It should be a number within the
3012  * range [0.0, 1.0].
3013  * @name CKEDITOR.config.dialog_backgroundCoverOpacity
3014  * @type Number
3015  * @default 0.5
3016  * @example
3017  * config.dialog_backgroundCoverOpacity = 0.7;
3018  */
3021  * If the dialog has more than one tab, put focus into the first tab as soon as dialog is opened.
3022  * @name CKEDITOR.config.dialog_startupFocusTab
3023  * @type Boolean
3024  * @default false
3025  * @example
3026  * config.dialog_startupFocusTab = true;
3027  */
3030  * The distance of magnetic borders used in moving and resizing dialogs,
3031  * measured in pixels.
3032  * @name CKEDITOR.config.dialog_magnetDistance
3033  * @type Number
3034  * @default 20
3035  * @example
3036  * config.dialog_magnetDistance = 30;
3037  */
3040  * The guideline to follow when generating the dialog buttons. There are 3 possible options:
3041  * <ul>
3042  *     <li>'OS' - the buttons will be displayed in the default order of the user's OS;</li>
3043  *     <li>'ltr' - for Left-To-Right order;</li>
3044  *     <li>'rtl' - for Right-To-Left order.</li>
3045  * </ul>
3046  * @name CKEDITOR.config.dialog_buttonsOrder
3047  * @type String
3048  * @default 'OS'
3049  * @since 3.5
3050  * @example
3051  * config.dialog_buttonsOrder = 'rtl';
3052  */
3055  * The dialog contents to removed. It's a string composed by dialog name and tab name with a colon between them.
3056  * Separate each pair with semicolon (see example).
3057  * <b>Note: All names are case-sensitive.</b>
3058  * <b>Note: Be cautious when specifying dialog tabs that are mandatory, like "info", dialog functionality might be broken because of this!</b>
3059  * @name CKEDITOR.config.removeDialogTabs
3060  * @type String
3061  * @since 3.5
3062  * @default ''
3063  * @example
3064  * config.removeDialogTabs = 'flash:advanced;image:Link';
3065  */
3068  * Fired when a dialog definition is about to be used to create a dialog into
3069  * an editor instance. This event makes it possible to customize the definition
3070  * before creating it.
3071  * <p>Note that this event is called only the first time a specific dialog is
3072  * opened. Successive openings will use the cached dialog, and this event will
3073  * not get fired.</p>
3074  * @name CKEDITOR#dialogDefinition
3075  * @event
3076  * @param {CKEDITOR.dialog.dialogDefinition} data The dialog defination that
3077  *              is being loaded.
3078  * @param {CKEDITOR.editor} editor The editor instance that will use the
3079  *              dialog.
3080  */
3083  * Fired when a tab is going to be selected in a dialog
3084  * @name CKEDITOR.dialog#selectPage
3085  * @event
3086  * @param {String} page The id of the page that it's gonna be selected.
3087  * @param {String} currentPage The id of the current page.
3088  */
3091  * Fired when the user tries to dismiss a dialog
3092  * @name CKEDITOR.dialog#cancel
3093  * @event
3094  * @param {Boolean} hide Whether the event should proceed or not.
3095  */
3098  * Fired when the user tries to confirm a dialog
3099  * @name CKEDITOR.dialog#ok
3100  * @event
3101  * @param {Boolean} hide Whether the event should proceed or not.
3102  */
3105  * Fired when a dialog is shown
3106  * @name CKEDITOR.dialog#show
3107  * @event
3108  */
3111  * Fired when a dialog is shown
3112  * @name CKEDITOR.editor#dialogShow
3113  * @event
3114  */
3117  * Fired when a dialog is hidden
3118  * @name CKEDITOR.dialog#hide
3119  * @event
3120  */
3123  * Fired when a dialog is hidden
3124  * @name CKEDITOR.editor#dialogHide
3125  * @event
3126  */
3129  * Fired when a dialog is being resized. The event is fired on
3130  * the 'CKEDITOR.dialog' object, not a dialog instance.
3131  * @name CKEDITOR.dialog#resize
3132  * @since 3.5
3133  * @event
3134  * @param {CKEDITOR.dialog} dialog The dialog being resized.
3135  * @param {String} skin The skin name.
3136  * @param {Number} width The new width.
3137  * @param {Number} height The new height.
3138  */