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