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