JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / floatpanel / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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                         // Disable native browser menu. (#4825)\r
51                         element.disableContextMenu();\r
52 \r
53                         this.element = element;\r
54 \r
55                         this._ =\r
56                         {\r
57                                 editor : editor,\r
58                                 // The panel that will be floating.\r
59                                 panel : panel,\r
60                                 parentElement : parentElement,\r
61                                 definition : definition,\r
62                                 document : doc,\r
63                                 iframe : iframe,\r
64                                 children : [],\r
65                                 dir : editor.lang.dir\r
66                         };\r
67 \r
68                         editor.on( 'mode', function(){ this.hide(); }, this );\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 = 1;\r
108 \r
109                                 // Record from where the focus is when open panel.\r
110                                 this._.returnFocus = this._.editor.focusManager.hasFocus ? this._.editor : new CKEDITOR.dom.element( CKEDITOR.document.$.activeElement );\r
111 \r
112 \r
113                                 var element = this.element,\r
114                                         iframe = this._.iframe,\r
115                                         definition = this._.definition,\r
116                                         position = offsetParent.getDocumentPosition( element.getDocument() ),\r
117                                         rtl = this._.dir == 'rtl';\r
118 \r
119                                 var left        = position.x + ( offsetX || 0 ),\r
120                                         top             = position.y + ( offsetY || 0 );\r
121 \r
122                                 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)\r
123                                 if ( rtl && ( corner == 1 || corner == 4 ) )\r
124                                         left += offsetParent.$.offsetWidth;\r
125                                 else if ( !rtl && ( corner == 2 || corner == 3 ) )\r
126                                         left += offsetParent.$.offsetWidth - 1;\r
127 \r
128                                 if ( corner == 3 || corner == 4 )\r
129                                         top += offsetParent.$.offsetHeight - 1;\r
130 \r
131                                 // Memorize offsetParent by it's ID.\r
132                                 this._.panel._.offsetParentId = offsetParent.getId();\r
133 \r
134                                 element.setStyles(\r
135                                         {\r
136                                                 top : top + 'px',\r
137                                                 left: 0,\r
138                                                 display : ''\r
139                                         });\r
140 \r
141                                 // Don't use display or visibility style because we need to\r
142                                 // calculate the rendering layout later and focus the element.\r
143                                 element.setOpacity( 0 );\r
144 \r
145                                 // To allow the context menu to decrease back their width\r
146                                 element.getFirst().removeStyle( 'width' );\r
147 \r
148                                 // Configure the IFrame blur event. Do that only once.\r
149                                 if ( !this._.blurSet )\r
150                                 {\r
151                                         // Non IE prefer the event into a window object.\r
152                                         var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );\r
153 \r
154                                         // With addEventListener compatible browsers, we must\r
155                                         // useCapture when registering the focus/blur events to\r
156                                         // guarantee they will be firing in all situations. (#3068, #3222 )\r
157                                         CKEDITOR.event.useCapture = true;\r
158 \r
159                                         focused.on( 'blur', function( ev )\r
160                                                 {\r
161                                                         if ( !this.allowBlur() )\r
162                                                                 return;\r
163 \r
164                                                         // As we are using capture to register the listener,\r
165                                                         // the blur event may get fired even when focusing\r
166                                                         // inside the window itself, so we must ensure the\r
167                                                         // target is out of it.\r
168                                                         var target = ev.data.getTarget() ;\r
169                                                         if ( target.getName && target.getName() != 'iframe' )\r
170                                                                 return;\r
171 \r
172                                                         if ( this.visible && !this._.activeChild && !isShowing )\r
173                                                         {\r
174                                                                 // Panel close is caused by user's navigating away the focus, e.g. click outside the panel.\r
175                                                                 // DO NOT restore focus in this case.\r
176                                                                 delete this._.returnFocus;\r
177                                                                 this.hide();\r
178                                                         }\r
179                                                 },\r
180                                                 this );\r
181 \r
182                                         focused.on( 'focus', function()\r
183                                                 {\r
184                                                         this._.focused = true;\r
185                                                         this.hideChild();\r
186                                                         this.allowBlur( true );\r
187                                                 },\r
188                                                 this );\r
189 \r
190                                         CKEDITOR.event.useCapture = false;\r
191 \r
192                                         this._.blurSet = 1;\r
193                                 }\r
194 \r
195                                 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )\r
196                                         {\r
197                                                 if ( this.onEscape && this.onEscape( keystroke ) === false )\r
198                                                         return false;\r
199                                         },\r
200                                         this );\r
201 \r
202                                 CKEDITOR.tools.setTimeout( function()\r
203                                         {\r
204                                                 var panelLoad = CKEDITOR.tools.bind( function ()\r
205                                                 {\r
206                                                         var target = element.getFirst();\r
207 \r
208                                                         if ( block.autoSize )\r
209                                                         {\r
210                                                                 // We must adjust first the width or IE6 could include extra lines in the height computation\r
211                                                                 var widthNode = block.element.$;\r
212 \r
213                                                                 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )\r
214                                                                         widthNode = widthNode.parentNode;\r
215 \r
216                                                                 if ( CKEDITOR.env.ie )\r
217                                                                         widthNode = widthNode.document.body;\r
218 \r
219                                                                 var width = widthNode.scrollWidth;\r
220                                                                 // Account for extra height needed due to IE quirks box model bug:\r
221                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
222                                                                 // (#3426)\r
223                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )\r
224                                                                         width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 ) + 3;\r
225                                                                 // A little extra at the end.\r
226                                                                 // If not present, IE6 might break into the next line, but also it looks better this way\r
227                                                                 width += 4 ;\r
228 \r
229                                                                 target.setStyle( 'width', width + 'px' );\r
230 \r
231                                                                 // IE doesn't compute the scrollWidth if a filter is applied previously\r
232                                                                 block.element.addClass( 'cke_frameLoaded' );\r
233 \r
234                                                                 var height = block.element.$.scrollHeight;\r
235 \r
236                                                                 // Account for extra height needed due to IE quirks box model bug:\r
237                                                                 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
238                                                                 // (#3426)\r
239                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )\r
240                                                                         height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ) + 3;\r
241 \r
242                                                                 target.setStyle( 'height', height + 'px' );\r
243 \r
244                                                                 // Fix IE < 8 visibility.\r
245                                                                 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );\r
246                                                         }\r
247                                                         else\r
248                                                                 target.removeStyle( 'height' );\r
249 \r
250                                                         // Flip panel layout horizontally in RTL with known width.\r
251                                                         if ( rtl )\r
252                                                                 left -= element.$.offsetWidth;\r
253 \r
254                                                         // Pop the style now for measurement.\r
255                                                         element.setStyle( 'left', left + 'px' );\r
256 \r
257                                                         /* panel layout smartly fit the viewport size. */\r
258                                                         var panelElement = panel.element,\r
259                                                                 panelWindow = panelElement.getWindow(),\r
260                                                                 rect = element.$.getBoundingClientRect(),\r
261                                                                 viewportSize = panelWindow.getViewPaneSize();\r
262 \r
263                                                         // Compensation for browsers that dont support "width" and "height".\r
264                                                         var rectWidth = rect.width || rect.right - rect.left,\r
265                                                         rectHeight = rect.height || rect.bottom - rect.top;\r
266 \r
267                                                         // Check if default horizontal layout is impossible.\r
268                                                         var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left,\r
269                                                                 spaceBefore = rtl ? viewportSize.width - rect.right : rect.left;\r
270 \r
271                                                         if ( rtl )\r
272                                                         {\r
273                                                                 if ( spaceAfter < rectWidth )\r
274                                                                 {\r
275                                                                         // Flip to show on right.\r
276                                                                         if ( spaceBefore > rectWidth )\r
277                                                                                 left += rectWidth;\r
278                                                                         // Align to window left.\r
279                                                                         else if ( viewportSize.width > rectWidth )\r
280                                                                                 left = left - rect.left;\r
281                                                                         // Align to window right, never cutting the panel at right.\r
282                                                                         else\r
283                                                                                 left = left - rect.right + viewportSize.width;\r
284                                                                 }\r
285                                                         }\r
286                                                         else if ( spaceAfter < rectWidth )\r
287                                                         {\r
288                                                                         // Flip to show on left.\r
289                                                                         if ( spaceBefore > rectWidth )\r
290                                                                                 left -= rectWidth;\r
291                                                                         // Align to window right.\r
292                                                                         else if ( viewportSize.width > rectWidth )\r
293                                                                                 left = left - rect.right + viewportSize.width;\r
294                                                                         // Align to window left, never cutting the panel at left.\r
295                                                                         else\r
296                                                                                 left = left - rect.left;\r
297                                                                 }\r
298 \r
299 \r
300                                                         // Check if the default vertical layout is possible.\r
301                                                         var spaceBelow = viewportSize.height - rect.top,\r
302                                                                 spaceAbove = rect.top;\r
303 \r
304                                                         if ( spaceBelow < rectHeight )\r
305                                                         {\r
306                                                                 // Flip to show above.\r
307                                                                 if ( spaceAbove > rectHeight )\r
308                                                                         top -= rectHeight;\r
309                                                                 // Align to window bottom.\r
310                                                                 else if ( viewportSize.height > rectHeight )\r
311                                                                         top = top - rect.bottom + viewportSize.height;\r
312                                                                 // Align to top, never cutting the panel at top.\r
313                                                                 else\r
314                                                                         top = top - rect.top;\r
315                                                         }\r
316 \r
317                                                         // If IE is in RTL, we have troubles with absolute\r
318                                                         // position and horizontal scrolls. Here we have a\r
319                                                         // series of hacks to workaround it. (#6146)\r
320                                                         if ( CKEDITOR.env.ie )\r
321                                                         {\r
322                                                                 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ),\r
323                                                                         scrollParent = offsetParent;\r
324 \r
325                                                                 // Quirks returns <body>, but standards returns <html>.\r
326                                                                 if ( scrollParent.getName() == 'html' )\r
327                                                                         scrollParent = scrollParent.getDocument().getBody();\r
328 \r
329                                                                 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' )\r
330                                                                 {\r
331                                                                         // For IE8, there is not much logic on this, but it works.\r
332                                                                         if ( CKEDITOR.env.ie8Compat )\r
333                                                                                 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2;\r
334                                                                         else\r
335                                                                                 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth );\r
336                                                                 }\r
337                                                         }\r
338 \r
339                                                         // Trigger the onHide event of the previously active panel to prevent\r
340                                                         // incorrect styles from being applied (#6170)\r
341                                                         var innerElement = element.getFirst(),\r
342                                                                 activePanel;\r
343                                                         if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) )\r
344                                                                 activePanel.onHide && activePanel.onHide.call( this, 1 );\r
345                                                         innerElement.setCustomData( 'activePanel', this );\r
346 \r
347                                                         element.setStyles(\r
348                                                                 {\r
349                                                                         top : top + 'px',\r
350                                                                         left : left + 'px'\r
351                                                                 } );\r
352                                                         element.setOpacity( 1 );\r
353                                                 } , this );\r
354 \r
355                                                 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;\r
356 \r
357                                                 // Set the panel frame focus, so the blur event gets fired.\r
358                                                 CKEDITOR.tools.setTimeout( function()\r
359                                                 {\r
360                                                         iframe.$.contentWindow.focus();\r
361                                                         // We need this get fired manually because of unfired focus() function.\r
362                                                         this.allowBlur( true );\r
363                                                 }, 0, this);\r
364                                         },  CKEDITOR.env.air ? 200 : 0, this);\r
365                                 this.visible = 1;\r
366 \r
367                                 if ( this.onShow )\r
368                                         this.onShow.call( this );\r
369 \r
370                                 isShowing = 0;\r
371                         },\r
372 \r
373                         hide : function( returnFocus )\r
374                         {\r
375                                 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
376                                 {\r
377                                         this.hideChild();\r
378                                         // Blur previously focused element. (#6671)\r
379                                         CKEDITOR.env.gecko && this._.iframe.getFrameDocument().$.activeElement.blur();\r
380                                         this.element.setStyle( 'display', 'none' );\r
381                                         this.visible = 0;\r
382                                         this.element.getFirst().removeCustomData( 'activePanel' );\r
383 \r
384                                         // Return focus properly. (#6247)\r
385                                         var focusReturn = returnFocus !== false && this._.returnFocus;\r
386                                         if ( focusReturn )\r
387                                         {\r
388                                                 // Webkit requires focus moved out panel iframe first.\r
389                                                 if ( CKEDITOR.env.webkit && focusReturn.type )\r
390                                                         focusReturn.getWindow().$.focus();\r
391 \r
392                                                 focusReturn.focus();\r
393                                         }\r
394                                 }\r
395                         },\r
396 \r
397                         allowBlur : function( allow )   // Prevent editor from hiding the panel. #3222.\r
398                         {\r
399                                 var panel = this._.panel;\r
400                                 if ( allow != undefined )\r
401                                         panel.allowBlur = allow;\r
402 \r
403                                 return panel.allowBlur;\r
404                         },\r
405 \r
406                         showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )\r
407                         {\r
408                                 // Skip reshowing of child which is already visible.\r
409                                 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )\r
410                                         return;\r
411 \r
412                                 this.hideChild();\r
413 \r
414                                 panel.onHide = CKEDITOR.tools.bind( function()\r
415                                         {\r
416                                                 // Use a timeout, so we give time for this menu to get\r
417                                                 // potentially focused.\r
418                                                 CKEDITOR.tools.setTimeout( function()\r
419                                                         {\r
420                                                                 if ( !this._.focused )\r
421                                                                         this.hide();\r
422                                                         },\r
423                                                         0, this );\r
424                                         },\r
425                                         this );\r
426 \r
427                                 this._.activeChild = panel;\r
428                                 this._.focused = false;\r
429 \r
430                                 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );\r
431 \r
432                                 /* #3767 IE: Second level menu may not have borders */\r
433                                 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )\r
434                                 {\r
435                                         setTimeout(function()\r
436                                                 {\r
437                                                         panel.element.getChild( 0 ).$.style.cssText += '';\r
438                                                 }, 100);\r
439                                 }\r
440                         },\r
441 \r
442                         hideChild : function()\r
443                         {\r
444                                 var activeChild = this._.activeChild;\r
445 \r
446                                 if ( activeChild )\r
447                                 {\r
448                                         delete activeChild.onHide;\r
449                                         // Sub panels don't manage focus. (#7881)\r
450                                         delete activeChild._.returnFocus;\r
451                                         delete this._.activeChild;\r
452                                         activeChild.hide();\r
453                                 }\r
454                         }\r
455                 }\r
456         });\r
457 \r
458         CKEDITOR.on( 'instanceDestroyed', function()\r
459         {\r
460                 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );\r
461 \r
462                 for ( var i in panels )\r
463                 {\r
464                         var panel = panels[ i ];\r
465                         // Safe to destroy it since there're no more instances.(#4241)\r
466                         if ( isLastInstance )\r
467                                 panel.destroy();\r
468                         // Panel might be used by other instances, just hide them.(#4552)\r
469                         else\r
470                                 panel.element.hide();\r
471                 }\r
472                 // Remove the registration.\r
473                 isLastInstance && ( panels = {} );\r
474 \r
475         } );\r
476 })();\r