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