JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fb5e3a487feb218cd9631516dff3961dd0c8874b
[ckeditor.git] / _source / plugins / floatpanel / 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 CKEDITOR.plugins.add( 'floatpanel',\r
7 {\r
8         requires : [ 'panel' ]\r
9 });\r
10 \r
11 (function()\r
12 {\r
13         var panels = {};\r
14         var isShowing = false;\r
15 \r
16         function getPanel( editor, doc, parentElement, definition, level )\r
17         {\r
18                 // Generates the panel key: docId-eleId-skinName-langDir[-uiColor][-CSSs][-level]\r
19                 var key =\r
20                         doc.getUniqueId() +\r
21                         '-' + parentElement.getUniqueId() +\r
22                         '-' + editor.skinName +\r
23                         '-' + editor.lang.dir +\r
24                         ( ( editor.uiColor && ( '-' + editor.uiColor ) ) || '' ) +\r
25                         ( ( definition.css && ( '-' + definition.css ) ) || '' ) +\r
26                         ( ( level && ( '-' + level ) ) || '' );\r
27 \r
28                 var panel = panels[ key ];\r
29 \r
30                 if ( !panel )\r
31                 {\r
32                         panel = panels[ key ] = new CKEDITOR.ui.panel( doc, definition );\r
33                         panel.element = parentElement.append( CKEDITOR.dom.element.createFromHtml( panel.renderHtml( editor ), doc ) );\r
34 \r
35                         panel.element.setStyles(\r
36                                 {\r
37                                         display : 'none',\r
38                                         position : 'absolute'\r
39                                 });\r
40                 }\r
41 \r
42                 return panel;\r
43         }\r
44 \r
45         CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(\r
46         {\r
47                 $ : function( editor, parentElement, definition, level )\r
48                 {\r
49                         definition.forceIFrame = true;\r
50 \r
51                         var doc = parentElement.getDocument(),\r
52                                 panel = getPanel( editor, doc, parentElement, definition, level || 0 ),\r
53                                 element = panel.element,\r
54                                 iframe = element.getFirst().getFirst();\r
55 \r
56                         this.element = element;\r
57 \r
58                         this._ =\r
59                         {\r
60                                 // The panel that will be floating.\r
61                                 panel : panel,\r
62                                 parentElement : parentElement,\r
63                                 definition : definition,\r
64                                 document : doc,\r
65                                 iframe : iframe,\r
66                                 children : [],\r
67                                 dir : editor.lang.dir\r
68                         };\r
69                 },\r
70 \r
71                 proto :\r
72                 {\r
73                         addBlock : function( name, block )\r
74                         {\r
75                                 return this._.panel.addBlock( name, block );\r
76                         },\r
77 \r
78                         addListBlock : function( name, multiSelect )\r
79                         {\r
80                                 return this._.panel.addListBlock( name, multiSelect );\r
81                         },\r
82 \r
83                         getBlock : function( name )\r
84                         {\r
85                                 return this._.panel.getBlock( name );\r
86                         },\r
87 \r
88                         /*\r
89                                 corner (LTR):\r
90                                         1 = top-left\r
91                                         2 = top-right\r
92                                         3 = bottom-right\r
93                                         4 = bottom-left\r
94 \r
95                                 corner (RTL):\r
96                                         1 = top-right\r
97                                         2 = top-left\r
98                                         3 = bottom-left\r
99                                         4 = bottom-right\r
100                          */\r
101                         showBlock : function( name, offsetParent, corner, offsetX, offsetY )\r
102                         {\r
103                                 var panel = this._.panel,\r
104                                         block = panel.showBlock( name );\r
105 \r
106                                 this.allowBlur( false );\r
107                                 isShowing = true;\r
108 \r
109                                 var element = this.element,\r
110                                         iframe = this._.iframe,\r
111                                         definition = this._.definition,\r
112                                         position = offsetParent.getDocumentPosition( element.getDocument() ),\r
113                                         rtl = this._.dir == 'rtl';\r
114 \r
115                                 var left        = position.x + ( offsetX || 0 ),\r
116                                         top             = position.y + ( offsetY || 0 );\r
117 \r
118                                 if ( ( rtl && ( corner == 1 || corner == 4 ) ) || ( !rtl && ( corner == 2 || corner == 3 ) ) )\r
119                                         left += offsetParent.$.offsetWidth - 1;\r
120 \r
121                                 if ( corner == 3 || corner == 4 )\r
122                                         top += offsetParent.$.offsetHeight - 1;\r
123 \r
124                                 // Memorize offsetParent by it's ID.\r
125                                 this._.panel._.offsetParentId = offsetParent.getId();\r
126 \r
127                                 element.setStyles(\r
128                                         {\r
129                                                 top : top + 'px',\r
130                                                 left : '-3000px',\r
131                                                 visibility : 'hidden',\r
132                                                 opacity : '0',  // FF3 is ignoring "visibility"\r
133                                                 display : ''\r
134                                         });\r
135 \r
136                                 // Configure the IFrame blur event. Do that only once.\r
137                                 if ( !this._.blurSet )\r
138                                 {\r
139                                         // Non IE prefer the event into a window object.\r
140                                         var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );\r
141 \r
142                                         // With addEventListener compatible browsers, we must\r
143                                         // useCapture when registering the focus/blur events to\r
144                                         // guarantee they will be firing in all situations. (#3068, #3222 )\r
145                                         CKEDITOR.event.useCapture = true;\r
146 \r
147                                         focused.on( 'blur', function( ev )\r
148                                                 {\r
149                                                         if ( CKEDITOR.env.ie && !this.allowBlur() )\r
150                                                                 return;\r
151 \r
152                                                         // As we are using capture to register the listener,\r
153                                                         // the blur event may get fired even when focusing\r
154                                                         // inside the window itself, so we must ensure the\r
155                                                         // target is out of it.\r
156                                                         var target = ev.data.getTarget(),\r
157                                                                 targetWindow = target.getWindow && target.getWindow();\r
158 \r
159                                                         if ( targetWindow && targetWindow.equals( focused ) )\r
160                                                                 return;\r
161 \r
162                                                         if ( this.visible && !this._.activeChild && !isShowing )\r
163                                                                 this.hide();\r
164                                                 },\r
165                                                 this );\r
166 \r
167                                         focused.on( 'focus', function()\r
168                                                 {\r
169                                                         this._.focused = true;\r
170                                                         this.hideChild();\r
171                                                         this.allowBlur( true );\r
172                                                 },\r
173                                                 this );\r
174 \r
175                                         CKEDITOR.event.useCapture = false;\r
176 \r
177                                         this._.blurSet = 1;\r
178                                 }\r
179 \r
180                                 panel.onEscape = CKEDITOR.tools.bind( function()\r
181                                         {\r
182                                                 this.onEscape && this.onEscape();\r
183                                         },\r
184                                         this );\r
185 \r
186                                 CKEDITOR.tools.setTimeout( function()\r
187                                         {\r
188                                                 if ( rtl )\r
189                                                         left -= element.$.offsetWidth;\r
190 \r
191                                                 element.setStyles(\r
192                                                         {\r
193                                                                 left : left + 'px',\r
194                                                                 visibility      : '',\r
195                                                                 opacity : '1'   // FF3 is ignoring "visibility"\r
196                                                         });\r
197 \r
198                                                 if ( block.autoSize )\r
199                                                 {\r
200                                                         function setHeight()\r
201                                                         {\r
202                                                                 var target = element.getFirst();\r
203                                                                 var height = block.element.$.scrollHeight;\r
204 \r
205                                                                 // Account for extra height needed due to IE quirks box model bug:\r
206                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
207                                                                 // (#3426)\r
208                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )\r
209                                                                         height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );\r
210 \r
211                                                                 target.setStyle( 'height', height + 'px' );\r
212 \r
213                                                                 // Fix IE < 8 visibility.\r
214                                                                 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );\r
215                                                         }\r
216 \r
217                                                         if ( panel.isLoaded )\r
218                                                                 setHeight();\r
219                                                         else\r
220                                                                 panel.onLoad = setHeight;\r
221                                                 }\r
222                                                 else\r
223                                                         element.getFirst().removeStyle( 'height' );\r
224 \r
225                                                 // Set the IFrame focus, so the blur event gets fired.\r
226                                                 CKEDITOR.tools.setTimeout( function()\r
227                                                         {\r
228                                                                 if ( definition.voiceLabel )\r
229                                                                 {\r
230                                                                         if ( CKEDITOR.env.gecko )\r
231                                                                         {\r
232                                                                                 var container = iframe.getParent();\r
233                                                                                 container.setAttribute( 'role', 'region' );\r
234                                                                                 container.setAttribute( 'title', definition.voiceLabel );\r
235                                                                                 iframe.setAttribute( 'role', 'region' );\r
236                                                                                 iframe.setAttribute( 'title', ' ' );\r
237                                                                         }\r
238                                                                 }\r
239                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )\r
240                                                                         iframe.focus();\r
241                                                                 else\r
242                                                                         iframe.$.contentWindow.focus();\r
243 \r
244                                                                 // We need this get fired manually because of unfired focus() function.\r
245                                                                 if ( CKEDITOR.env.ie && !CKEDITOR.env.quirks )\r
246                                                                         this.allowBlur( true );\r
247                                                         }, 0, this);\r
248                                         }, 0, this);\r
249                                 this.visible = 1;\r
250 \r
251                                 if ( this.onShow )\r
252                                         this.onShow.call( this );\r
253 \r
254                                 isShowing = false;\r
255                         },\r
256 \r
257                         hide : function()\r
258                         {\r
259                                 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
260                                 {\r
261                                         this.hideChild();\r
262                                         this.element.setStyle( 'display', 'none' );\r
263                                         this.visible = 0;\r
264                                 }\r
265                         },\r
266 \r
267                         allowBlur : function( allow )   // Prevent editor from hiding the panel. #3222.\r
268                         {\r
269                                 var panel = this._.panel;\r
270                                 if ( allow != undefined )\r
271                                         panel.allowBlur = allow;\r
272 \r
273                                 return panel.allowBlur;\r
274                         },\r
275 \r
276                         showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )\r
277                         {\r
278                                 // Skip reshowing of child which is already visible.\r
279                                 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )\r
280                                         return;\r
281 \r
282                                 this.hideChild();\r
283 \r
284                                 panel.onHide = CKEDITOR.tools.bind( function()\r
285                                         {\r
286                                                 // Use a timeout, so we give time for this menu to get\r
287                                                 // potentially focused.\r
288                                                 CKEDITOR.tools.setTimeout( function()\r
289                                                         {\r
290                                                                 if ( !this._.focused )\r
291                                                                         this.hide();\r
292                                                         },\r
293                                                         0, this );\r
294                                         },\r
295                                         this );\r
296 \r
297                                 this._.activeChild = panel;\r
298                                 this._.focused = false;\r
299 \r
300                                 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );\r
301 \r
302                                 /* #3767 IE: Second level menu may not have borders */\r
303                                 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )\r
304                                 {\r
305                                         setTimeout(function()\r
306                                                 {\r
307                                                         panel.element.getChild( 0 ).$.style.cssText += '';\r
308                                                 }, 100);\r
309                                 }\r
310                         },\r
311 \r
312                         hideChild : function()\r
313                         {\r
314                                 var activeChild = this._.activeChild;\r
315 \r
316                                 if ( activeChild )\r
317                                 {\r
318                                         delete activeChild.onHide;\r
319                                         delete this._.activeChild;\r
320                                         activeChild.hide();\r
321                                 }\r
322                         }\r
323                 }\r
324         });\r
325 })();\r