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