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