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