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