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