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