JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1.1
[ckeditor.git] / _source / plugins / floatpanel / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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                                 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)\r
119                                 if ( rtl && ( corner == 1 || corner == 4 ) )\r
120                                         left += offsetParent.$.offsetWidth;\r
121                                 else if ( !rtl && ( corner == 2 || corner == 3 ) )\r
122                                         left += offsetParent.$.offsetWidth - 1;\r
123 \r
124                                 if ( corner == 3 || corner == 4 )\r
125                                         top += offsetParent.$.offsetHeight - 1;\r
126 \r
127                                 // Memorize offsetParent by it's ID.\r
128                                 this._.panel._.offsetParentId = offsetParent.getId();\r
129 \r
130                                 element.setStyles(\r
131                                         {\r
132                                                 top : top + 'px',\r
133                                                 left : '-3000px',\r
134                                                 opacity : '0',  // FF3 is ignoring "visibility"\r
135                                                 display : ''\r
136                                         });\r
137 \r
138                                 // To allow the context menu to decrease back their width\r
139                                 element.getFirst().removeStyle('width');\r
140 \r
141                                 // Configure the IFrame blur event. Do that only once.\r
142                                 if ( !this._.blurSet )\r
143                                 {\r
144                                         // Non IE prefer the event into a window object.\r
145                                         var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );\r
146 \r
147                                         // With addEventListener compatible browsers, we must\r
148                                         // useCapture when registering the focus/blur events to\r
149                                         // guarantee they will be firing in all situations. (#3068, #3222 )\r
150                                         CKEDITOR.event.useCapture = true;\r
151 \r
152                                         focused.on( 'blur', function( ev )\r
153                                                 {\r
154                                                         if ( !this.allowBlur() )\r
155                                                                 return;\r
156 \r
157                                                         // As we are using capture to register the listener,\r
158                                                         // the blur event may get fired even when focusing\r
159                                                         // inside the window itself, so we must ensure the\r
160                                                         // target is out of it.\r
161                                                         var target = ev.data.getTarget(),\r
162                                                                 targetWindow = target.getWindow && target.getWindow();\r
163 \r
164                                                         if ( targetWindow && targetWindow.equals( focused ) )\r
165                                                                 return;\r
166 \r
167                                                         if ( this.visible && !this._.activeChild && !isShowing )\r
168                                                                 this.hide();\r
169                                                 },\r
170                                                 this );\r
171 \r
172                                         focused.on( 'focus', function()\r
173                                                 {\r
174                                                         this._.focused = true;\r
175                                                         this.hideChild();\r
176                                                         this.allowBlur( true );\r
177                                                 },\r
178                                                 this );\r
179 \r
180                                         CKEDITOR.event.useCapture = false;\r
181 \r
182                                         this._.blurSet = 1;\r
183                                 }\r
184 \r
185                                 panel.onEscape = CKEDITOR.tools.bind( function()\r
186                                         {\r
187                                                 this.onEscape && this.onEscape();\r
188                                         },\r
189                                         this );\r
190 \r
191                                 CKEDITOR.tools.setTimeout( function()\r
192                                         {\r
193                                                 if ( rtl )\r
194                                                         left -= element.$.offsetWidth;\r
195 \r
196                                                 var panelLoad = CKEDITOR.tools.bind( function ()\r
197                                                 {\r
198                                                         var target = element.getFirst();\r
199 \r
200                                                         if ( block.autoSize )\r
201                                                         {\r
202                                                                 // We must adjust first the width or IE6 could include extra lines in the height computation\r
203                                                                 var widthNode = block.element.$;\r
204 \r
205                                                                 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera)\r
206                                                                         widthNode = widthNode.parentNode;\r
207 \r
208                                                                 if ( CKEDITOR.env.ie)\r
209                                                                         widthNode = widthNode.document.body;\r
210 \r
211                                                                 var width = widthNode.scrollWidth;\r
212                                                                 // Account for extra height needed due to IE quirks box model bug:\r
213                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
214                                                                 // (#3426)\r
215                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )\r
216                                                                         width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 );\r
217                                                                 // A little extra at the end.\r
218                                                                 // If not present, IE6 might break into the next line, but also it looks better this way\r
219                                                                 width += 4 ;\r
220 \r
221                                                                 target.setStyle( 'width', width + 'px' );\r
222 \r
223                                                                 // IE doesn't compute the scrollWidth if a filter is applied previously\r
224                                                                 block.element.addClass( 'cke_frameLoaded' );\r
225 \r
226                                                                 var height = block.element.$.scrollHeight;\r
227 \r
228                                                                 // Account for extra height needed due to IE quirks box model bug:\r
229                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
230                                                                 // (#3426)\r
231                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )\r
232                                                                         height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );\r
233 \r
234                                                                 target.setStyle( 'height', height + 'px' );\r
235 \r
236                                                                 // Fix IE < 8 visibility.\r
237                                                                 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );\r
238                                                         }\r
239                                                         else\r
240                                                                 target.removeStyle( 'height' );\r
241 \r
242                                                         var panelElement = panel.element,\r
243                                                                 panelWindow = panelElement.getWindow(),\r
244                                                                 windowScroll = panelWindow.getScrollPosition(),\r
245                                                                 viewportSize = panelWindow.getViewPaneSize(),\r
246                                                                 panelSize =\r
247                                                                 {\r
248                                                                         'height' : panelElement.$.offsetHeight,\r
249                                                                         'width' : panelElement.$.offsetWidth\r
250                                                                 };\r
251 \r
252                                                         // If the menu is horizontal off, shift it toward\r
253                                                         // the opposite language direction.\r
254                                                         if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )\r
255                                                                 left += ( panelSize.width * ( rtl ? 1 : -1 ) );\r
256 \r
257                                                         // Vertical off screen is simpler.\r
258                                                         if ( top + panelSize.height > viewportSize.height + windowScroll.y )\r
259                                                                 top -= panelSize.height;\r
260 \r
261                                                         element.setStyles(\r
262                                                                 {\r
263                                                                         top : top + 'px',\r
264                                                                         left : left + 'px',\r
265                                                                         opacity : '1'\r
266                                                                 } );\r
267 \r
268                                                 } , this );\r
269 \r
270                                                 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;\r
271 \r
272                                                 // Set the panel frame focus, so the blur event gets fired.\r
273                                                 CKEDITOR.tools.setTimeout( function()\r
274                                                         {\r
275                                                                 if ( definition.voiceLabel )\r
276                                                                 {\r
277                                                                         if ( CKEDITOR.env.gecko )\r
278                                                                         {\r
279                                                                                 var container = iframe.getParent();\r
280                                                                                 container.setAttribute( 'role', 'region' );\r
281                                                                                 container.setAttribute( 'title', definition.voiceLabel );\r
282                                                                                 iframe.setAttribute( 'role', 'region' );\r
283                                                                                 iframe.setAttribute( 'title', ' ' );\r
284                                                                         }\r
285                                                                 }\r
286 \r
287                                                                 iframe.$.contentWindow.focus();\r
288                                                                 // We need this get fired manually because of unfired focus() function.\r
289                                                                 this.allowBlur( true );\r
290 \r
291                                                         }, 0, this);\r
292                                         }, 0, this);\r
293                                 this.visible = 1;\r
294 \r
295                                 if ( this.onShow )\r
296                                         this.onShow.call( this );\r
297 \r
298                                 isShowing = false;\r
299                         },\r
300 \r
301                         hide : function()\r
302                         {\r
303                                 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
304                                 {\r
305                                         this.hideChild();\r
306                                         this.element.setStyle( 'display', 'none' );\r
307                                         this.visible = 0;\r
308                                 }\r
309                         },\r
310 \r
311                         allowBlur : function( allow )   // Prevent editor from hiding the panel. #3222.\r
312                         {\r
313                                 var panel = this._.panel;\r
314                                 if ( allow != undefined )\r
315                                         panel.allowBlur = allow;\r
316 \r
317                                 return panel.allowBlur;\r
318                         },\r
319 \r
320                         showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )\r
321                         {\r
322                                 // Skip reshowing of child which is already visible.\r
323                                 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )\r
324                                         return;\r
325 \r
326                                 this.hideChild();\r
327 \r
328                                 panel.onHide = CKEDITOR.tools.bind( function()\r
329                                         {\r
330                                                 // Use a timeout, so we give time for this menu to get\r
331                                                 // potentially focused.\r
332                                                 CKEDITOR.tools.setTimeout( function()\r
333                                                         {\r
334                                                                 if ( !this._.focused )\r
335                                                                         this.hide();\r
336                                                         },\r
337                                                         0, this );\r
338                                         },\r
339                                         this );\r
340 \r
341                                 this._.activeChild = panel;\r
342                                 this._.focused = false;\r
343 \r
344                                 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );\r
345 \r
346                                 /* #3767 IE: Second level menu may not have borders */\r
347                                 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )\r
348                                 {\r
349                                         setTimeout(function()\r
350                                                 {\r
351                                                         panel.element.getChild( 0 ).$.style.cssText += '';\r
352                                                 }, 100);\r
353                                 }\r
354                         },\r
355 \r
356                         hideChild : function()\r
357                         {\r
358                                 var activeChild = this._.activeChild;\r
359 \r
360                                 if ( activeChild )\r
361                                 {\r
362                                         delete activeChild.onHide;\r
363                                         delete this._.activeChild;\r
364                                         activeChild.hide();\r
365                                 }\r
366                         }\r
367                 }\r
368         });\r
369 \r
370         CKEDITOR.on( 'instanceDestroyed', function()\r
371         {\r
372                 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );\r
373 \r
374                 for ( var i in panels )\r
375                 {\r
376                         var panel = panels[ i ];\r
377                         // Safe to destroy it since there're no more instances.(#4241)\r
378                         if ( isLastInstance )\r
379                                 panel.destroy();\r
380                         // Panel might be used by other instances, just hide them.(#4552)\r
381                         else\r
382                                 panel.element.hide();\r
383                 }\r
384                 // Remove the registration.\r
385                 isLastInstance && ( panels = {} );\r
386 \r
387         } );\r
388 })();\r