JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[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 = CKEDITOR.tools.genKey( doc.getUniqueId(), parentElement.getUniqueId(), editor.skinName, editor.lang.dir,\r
20                         editor.uiColor || '', definition.css || '', level || '' );\r
21 \r
22                 var panel = panels[ key ];\r
23 \r
24                 if ( !panel )\r
25                 {\r
26                         panel = panels[ key ] = new CKEDITOR.ui.panel( doc, definition );\r
27                         panel.element = parentElement.append( CKEDITOR.dom.element.createFromHtml( panel.renderHtml( editor ), doc ) );\r
28 \r
29                         panel.element.setStyles(\r
30                                 {\r
31                                         display : 'none',\r
32                                         position : 'absolute'\r
33                                 });\r
34                 }\r
35 \r
36                 return panel;\r
37         }\r
38 \r
39         CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(\r
40         {\r
41                 $ : function( editor, parentElement, definition, level )\r
42                 {\r
43                         definition.forceIFrame = 1;\r
44 \r
45                         var doc = parentElement.getDocument(),\r
46                                 panel = getPanel( editor, doc, parentElement, definition, level || 0 ),\r
47                                 element = panel.element,\r
48                                 iframe = element.getFirst().getFirst();\r
49 \r
50                         this.element = element;\r
51 \r
52                         this._ =\r
53                         {\r
54                                 // The panel that will be floating.\r
55                                 panel : panel,\r
56                                 parentElement : parentElement,\r
57                                 definition : definition,\r
58                                 document : doc,\r
59                                 iframe : iframe,\r
60                                 children : [],\r
61                                 dir : editor.lang.dir\r
62                         };\r
63 \r
64                         editor.on( 'mode', function(){ this.hide(); }, this );\r
65                 },\r
66 \r
67                 proto :\r
68                 {\r
69                         addBlock : function( name, block )\r
70                         {\r
71                                 return this._.panel.addBlock( name, block );\r
72                         },\r
73 \r
74                         addListBlock : function( name, multiSelect )\r
75                         {\r
76                                 return this._.panel.addListBlock( name, multiSelect );\r
77                         },\r
78 \r
79                         getBlock : function( name )\r
80                         {\r
81                                 return this._.panel.getBlock( name );\r
82                         },\r
83 \r
84                         /*\r
85                                 corner (LTR):\r
86                                         1 = top-left\r
87                                         2 = top-right\r
88                                         3 = bottom-right\r
89                                         4 = bottom-left\r
90 \r
91                                 corner (RTL):\r
92                                         1 = top-right\r
93                                         2 = top-left\r
94                                         3 = bottom-left\r
95                                         4 = bottom-right\r
96                          */\r
97                         showBlock : function( name, offsetParent, corner, offsetX, offsetY )\r
98                         {\r
99                                 var panel = this._.panel,\r
100                                         block = panel.showBlock( name );\r
101 \r
102                                 this.allowBlur( false );\r
103                                 isShowing = 1;\r
104 \r
105                                 var element = this.element,\r
106                                         iframe = this._.iframe,\r
107                                         definition = this._.definition,\r
108                                         position = offsetParent.getDocumentPosition( element.getDocument() ),\r
109                                         rtl = this._.dir == 'rtl';\r
110 \r
111                                 var left        = position.x + ( offsetX || 0 ),\r
112                                         top             = position.y + ( offsetY || 0 );\r
113 \r
114                                 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)\r
115                                 if ( rtl && ( corner == 1 || corner == 4 ) )\r
116                                         left += offsetParent.$.offsetWidth;\r
117                                 else if ( !rtl && ( corner == 2 || corner == 3 ) )\r
118                                         left += offsetParent.$.offsetWidth - 1;\r
119 \r
120                                 if ( corner == 3 || corner == 4 )\r
121                                         top += offsetParent.$.offsetHeight - 1;\r
122 \r
123                                 // Memorize offsetParent by it's ID.\r
124                                 this._.panel._.offsetParentId = offsetParent.getId();\r
125 \r
126                                 element.setStyles(\r
127                                         {\r
128                                                 top : top + 'px',\r
129                                                 left: 0,\r
130                                                 display : ''\r
131                                         });\r
132 \r
133                                 // Don't use display or visibility style because we need to\r
134                                 // calculate the rendering layout later and focus the element.\r
135                                 element.setOpacity( 0 );\r
136 \r
137                                 // To allow the context menu to decrease back their width\r
138                                 element.getFirst().removeStyle( 'width' );\r
139 \r
140                                 // Configure the IFrame blur event. Do that only once.\r
141                                 if ( !this._.blurSet )\r
142                                 {\r
143                                         // Non IE prefer the event into a window object.\r
144                                         var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );\r
145 \r
146                                         // With addEventListener compatible browsers, we must\r
147                                         // useCapture when registering the focus/blur events to\r
148                                         // guarantee they will be firing in all situations. (#3068, #3222 )\r
149                                         CKEDITOR.event.useCapture = true;\r
150 \r
151                                         focused.on( 'blur', function( ev )\r
152                                                 {\r
153                                                         if ( !this.allowBlur() )\r
154                                                                 return;\r
155 \r
156                                                         // As we are using capture to register the listener,\r
157                                                         // the blur event may get fired even when focusing\r
158                                                         // inside the window itself, so we must ensure the\r
159                                                         // target is out of it.\r
160                                                         var target;\r
161                                                         if ( CKEDITOR.env.ie && !this.allowBlur()\r
162                                                                  || ( target = ev.data.getTarget() )\r
163                                                                       && target.getName && target.getName() != 'iframe' )\r
164                                                                 return;\r
165 \r
166                                                         if ( this.visible && !this._.activeChild && !isShowing )\r
167                                                                 this.hide();\r
168                                                 },\r
169                                                 this );\r
170 \r
171                                         focused.on( 'focus', function()\r
172                                                 {\r
173                                                         this._.focused = true;\r
174                                                         this.hideChild();\r
175                                                         this.allowBlur( true );\r
176                                                 },\r
177                                                 this );\r
178 \r
179                                         CKEDITOR.event.useCapture = false;\r
180 \r
181                                         this._.blurSet = 1;\r
182                                 }\r
183 \r
184                                 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )\r
185                                         {\r
186                                                 if ( this.onEscape && this.onEscape( keystroke ) === false )\r
187                                                         return false;\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                                                         // If IE is in RTL, we have troubles with absolute\r
262                                                         // position and horizontal scrolls. Here we have a\r
263                                                         // series of hacks to workaround it. (#6146)\r
264                                                         if ( CKEDITOR.env.ie )\r
265                                                         {\r
266                                                                 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ),\r
267                                                                         scrollParent = offsetParent;\r
268 \r
269                                                                 // Quirks returns <body>, but standards returns <html>.\r
270                                                                 if ( scrollParent.getName() == 'html' )\r
271                                                                         scrollParent = scrollParent.getDocument().getBody();\r
272 \r
273                                                                 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' )\r
274                                                                 {\r
275                                                                         // For IE8, there is not much logic on this, but it works.\r
276                                                                         if ( CKEDITOR.env.ie8Compat )\r
277                                                                                 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2;\r
278                                                                         else\r
279                                                                                 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth );\r
280                                                                 }\r
281                                                         }\r
282 \r
283                                                         // Trigger the onHide event of the previously active panel to prevent\r
284                                                         // incorrect styles from being applied (#6170)\r
285                                                         var innerElement = element.getFirst(),\r
286                                                                 activePanel;\r
287                                                         if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) )\r
288                                                                 activePanel.onHide && activePanel.onHide.call( this, 1 );\r
289                                                         innerElement.setCustomData( 'activePanel', this );\r
290 \r
291                                                         element.setStyles(\r
292                                                                 {\r
293                                                                         top : top + 'px',\r
294                                                                         left : left + 'px'\r
295                                                                 } );\r
296                                                         element.setOpacity( 1 );\r
297                                                 } , this );\r
298 \r
299                                                 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;\r
300 \r
301                                                 // Set the panel frame focus, so the blur event gets fired.\r
302                                                 CKEDITOR.tools.setTimeout( function()\r
303                                                 {\r
304                                                         iframe.$.contentWindow.focus();\r
305                                                         // We need this get fired manually because of unfired focus() function.\r
306                                                         this.allowBlur( true );\r
307                                                 }, 0, this);\r
308                                         }, 0, this);\r
309                                 this.visible = 1;\r
310 \r
311                                 if ( this.onShow )\r
312                                         this.onShow.call( this );\r
313 \r
314                                 isShowing = 0;\r
315                         },\r
316 \r
317                         hide : function()\r
318                         {\r
319                                 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
320                                 {\r
321                                         this.hideChild();\r
322                                         this.element.setStyle( 'display', 'none' );\r
323                                         this.visible = 0;\r
324                                         this.element.getFirst().removeCustomData( 'activePanel' );\r
325                                 }\r
326                         },\r
327 \r
328                         allowBlur : function( allow )   // Prevent editor from hiding the panel. #3222.\r
329                         {\r
330                                 var panel = this._.panel;\r
331                                 if ( allow != undefined )\r
332                                         panel.allowBlur = allow;\r
333 \r
334                                 return panel.allowBlur;\r
335                         },\r
336 \r
337                         showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )\r
338                         {\r
339                                 // Skip reshowing of child which is already visible.\r
340                                 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )\r
341                                         return;\r
342 \r
343                                 this.hideChild();\r
344 \r
345                                 panel.onHide = CKEDITOR.tools.bind( function()\r
346                                         {\r
347                                                 // Use a timeout, so we give time for this menu to get\r
348                                                 // potentially focused.\r
349                                                 CKEDITOR.tools.setTimeout( function()\r
350                                                         {\r
351                                                                 if ( !this._.focused )\r
352                                                                         this.hide();\r
353                                                         },\r
354                                                         0, this );\r
355                                         },\r
356                                         this );\r
357 \r
358                                 this._.activeChild = panel;\r
359                                 this._.focused = false;\r
360 \r
361                                 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );\r
362 \r
363                                 /* #3767 IE: Second level menu may not have borders */\r
364                                 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )\r
365                                 {\r
366                                         setTimeout(function()\r
367                                                 {\r
368                                                         panel.element.getChild( 0 ).$.style.cssText += '';\r
369                                                 }, 100);\r
370                                 }\r
371                         },\r
372 \r
373                         hideChild : function()\r
374                         {\r
375                                 var activeChild = this._.activeChild;\r
376 \r
377                                 if ( activeChild )\r
378                                 {\r
379                                         delete activeChild.onHide;\r
380                                         delete this._.activeChild;\r
381                                         activeChild.hide();\r
382                                 }\r
383                         }\r
384                 }\r
385         });\r
386 \r
387         CKEDITOR.on( 'instanceDestroyed', function()\r
388         {\r
389                 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );\r
390 \r
391                 for ( var i in panels )\r
392                 {\r
393                         var panel = panels[ i ];\r
394                         // Safe to destroy it since there're no more instances.(#4241)\r
395                         if ( isLastInstance )\r
396                                 panel.destroy();\r
397                         // Panel might be used by other instances, just hide them.(#4552)\r
398                         else\r
399                                 panel.element.hide();\r
400                 }\r
401                 // Remove the registration.\r
402                 isLastInstance && ( panels = {} );\r
403 \r
404         } );\r
405 })();\r