JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[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;\r
162                                                         if ( CKEDITOR.env.ie && !this.allowBlur()\r
163                                                                  || ( target = ev.data.getTarget() )\r
164                                                                       && target.getName && target.getName() != 'iframe' )\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( keystroke )\r
186                                         {\r
187                                                 if ( this.onEscape && this.onEscape( keystroke ) === false )\r
188                                                         return false;\r
189                                         },\r
190                                         this );\r
191 \r
192                                 CKEDITOR.tools.setTimeout( function()\r
193                                         {\r
194                                                 if ( rtl )\r
195                                                         left -= element.$.offsetWidth;\r
196 \r
197                                                 var panelLoad = CKEDITOR.tools.bind( function ()\r
198                                                 {\r
199                                                         var target = element.getFirst();\r
200 \r
201                                                         if ( block.autoSize )\r
202                                                         {\r
203                                                                 // We must adjust first the width or IE6 could include extra lines in the height computation\r
204                                                                 var widthNode = block.element.$;\r
205 \r
206                                                                 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera)\r
207                                                                         widthNode = widthNode.parentNode;\r
208 \r
209                                                                 if ( CKEDITOR.env.ie)\r
210                                                                         widthNode = widthNode.document.body;\r
211 \r
212                                                                 var width = widthNode.scrollWidth;\r
213                                                                 // Account for extra height needed due to IE quirks box model bug:\r
214                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
215                                                                 // (#3426)\r
216                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )\r
217                                                                         width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 );\r
218                                                                 // A little extra at the end.\r
219                                                                 // If not present, IE6 might break into the next line, but also it looks better this way\r
220                                                                 width += 4 ;\r
221 \r
222                                                                 target.setStyle( 'width', width + 'px' );\r
223 \r
224                                                                 // IE doesn't compute the scrollWidth if a filter is applied previously\r
225                                                                 block.element.addClass( 'cke_frameLoaded' );\r
226 \r
227                                                                 var height = block.element.$.scrollHeight;\r
228 \r
229                                                                 // Account for extra height needed due to IE quirks box model bug:\r
230                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
231                                                                 // (#3426)\r
232                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )\r
233                                                                         height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );\r
234 \r
235                                                                 target.setStyle( 'height', height + 'px' );\r
236 \r
237                                                                 // Fix IE < 8 visibility.\r
238                                                                 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );\r
239                                                         }\r
240                                                         else\r
241                                                                 target.removeStyle( 'height' );\r
242 \r
243                                                         var panelElement = panel.element,\r
244                                                                 panelWindow = panelElement.getWindow(),\r
245                                                                 windowScroll = panelWindow.getScrollPosition(),\r
246                                                                 viewportSize = panelWindow.getViewPaneSize(),\r
247                                                                 panelSize =\r
248                                                                 {\r
249                                                                         'height' : panelElement.$.offsetHeight,\r
250                                                                         'width' : panelElement.$.offsetWidth\r
251                                                                 };\r
252 \r
253                                                         // If the menu is horizontal off, shift it toward\r
254                                                         // the opposite language direction.\r
255                                                         if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )\r
256                                                                 left += ( panelSize.width * ( rtl ? 1 : -1 ) );\r
257 \r
258                                                         // Vertical off screen is simpler.\r
259                                                         if ( top + panelSize.height > viewportSize.height + windowScroll.y )\r
260                                                                 top -= panelSize.height;\r
261 \r
262                                                         element.setStyles(\r
263                                                                 {\r
264                                                                         top : top + 'px',\r
265                                                                         left : left + 'px',\r
266                                                                         opacity : '1'\r
267                                                                 } );\r
268 \r
269                                                 } , this );\r
270 \r
271                                                 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;\r
272 \r
273                                                 // Set the panel frame focus, so the blur event gets fired.\r
274                                                 CKEDITOR.tools.setTimeout( function()\r
275                                                 {\r
276                                                         iframe.$.contentWindow.focus();\r
277                                                         // We need this get fired manually because of unfired focus() function.\r
278                                                         this.allowBlur( true );\r
279                                                 }, 0, this);\r
280                                         }, 0, this);\r
281                                 this.visible = 1;\r
282 \r
283                                 if ( this.onShow )\r
284                                         this.onShow.call( this );\r
285 \r
286                                 isShowing = false;\r
287                         },\r
288 \r
289                         hide : function()\r
290                         {\r
291                                 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
292                                 {\r
293                                         this.hideChild();\r
294                                         this.element.setStyle( 'display', 'none' );\r
295                                         this.visible = 0;\r
296                                 }\r
297                         },\r
298 \r
299                         allowBlur : function( allow )   // Prevent editor from hiding the panel. #3222.\r
300                         {\r
301                                 var panel = this._.panel;\r
302                                 if ( allow != undefined )\r
303                                         panel.allowBlur = allow;\r
304 \r
305                                 return panel.allowBlur;\r
306                         },\r
307 \r
308                         showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )\r
309                         {\r
310                                 // Skip reshowing of child which is already visible.\r
311                                 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )\r
312                                         return;\r
313 \r
314                                 this.hideChild();\r
315 \r
316                                 panel.onHide = CKEDITOR.tools.bind( function()\r
317                                         {\r
318                                                 // Use a timeout, so we give time for this menu to get\r
319                                                 // potentially focused.\r
320                                                 CKEDITOR.tools.setTimeout( function()\r
321                                                         {\r
322                                                                 if ( !this._.focused )\r
323                                                                         this.hide();\r
324                                                         },\r
325                                                         0, this );\r
326                                         },\r
327                                         this );\r
328 \r
329                                 this._.activeChild = panel;\r
330                                 this._.focused = false;\r
331 \r
332                                 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );\r
333 \r
334                                 /* #3767 IE: Second level menu may not have borders */\r
335                                 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )\r
336                                 {\r
337                                         setTimeout(function()\r
338                                                 {\r
339                                                         panel.element.getChild( 0 ).$.style.cssText += '';\r
340                                                 }, 100);\r
341                                 }\r
342                         },\r
343 \r
344                         hideChild : function()\r
345                         {\r
346                                 var activeChild = this._.activeChild;\r
347 \r
348                                 if ( activeChild )\r
349                                 {\r
350                                         delete activeChild.onHide;\r
351                                         delete this._.activeChild;\r
352                                         activeChild.hide();\r
353                                 }\r
354                         }\r
355                 }\r
356         });\r
357 \r
358         CKEDITOR.on( 'instanceDestroyed', function()\r
359         {\r
360                 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );\r
361 \r
362                 for ( var i in panels )\r
363                 {\r
364                         var panel = panels[ i ];\r
365                         // Safe to destroy it since there're no more instances.(#4241)\r
366                         if ( isLastInstance )\r
367                                 panel.destroy();\r
368                         // Panel might be used by other instances, just hide them.(#4552)\r
369                         else\r
370                                 panel.element.hide();\r
371                 }\r
372                 // Remove the registration.\r
373                 isLastInstance && ( panels = {} );\r
374 \r
375         } );\r
376 })();\r