2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 CKEDITOR.plugins.add( 'floatpanel',
14 var isShowing = false;
16 function getPanel( editor, doc, parentElement, definition, level )
18 // Generates the panel key: docId-eleId-skinName-langDir[-uiColor][-CSSs][-level]
19 var key = CKEDITOR.tools.genKey( doc.getUniqueId(), parentElement.getUniqueId(), editor.skinName, editor.lang.dir,
20 editor.uiColor || '', definition.css || '', level || '' );
22 var panel = panels[ key ];
26 panel = panels[ key ] = new CKEDITOR.ui.panel( doc, definition );
27 panel.element = parentElement.append( CKEDITOR.dom.element.createFromHtml( panel.renderHtml( editor ), doc ) );
29 panel.element.setStyles(
39 CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(
41 $ : function( editor, parentElement, definition, level )
43 definition.forceIFrame = 1;
45 var doc = parentElement.getDocument(),
46 panel = getPanel( editor, doc, parentElement, definition, level || 0 ),
47 element = panel.element,
48 iframe = element.getFirst().getFirst();
50 this.element = element;
54 // The panel that will be floating.
56 parentElement : parentElement,
57 definition : definition,
64 editor.on( 'mode', function(){ this.hide(); }, this );
69 addBlock : function( name, block )
71 return this._.panel.addBlock( name, block );
74 addListBlock : function( name, multiSelect )
76 return this._.panel.addListBlock( name, multiSelect );
79 getBlock : function( name )
81 return this._.panel.getBlock( name );
97 showBlock : function( name, offsetParent, corner, offsetX, offsetY )
99 var panel = this._.panel,
100 block = panel.showBlock( name );
102 this.allowBlur( false );
105 var element = this.element,
106 iframe = this._.iframe,
107 definition = this._.definition,
108 position = offsetParent.getDocumentPosition( element.getDocument() ),
109 rtl = this._.dir == 'rtl';
111 var left = position.x + ( offsetX || 0 ),
112 top = position.y + ( offsetY || 0 );
114 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)
115 if ( rtl && ( corner == 1 || corner == 4 ) )
116 left += offsetParent.$.offsetWidth;
117 else if ( !rtl && ( corner == 2 || corner == 3 ) )
118 left += offsetParent.$.offsetWidth - 1;
120 if ( corner == 3 || corner == 4 )
121 top += offsetParent.$.offsetHeight - 1;
123 // Memorize offsetParent by it's ID.
124 this._.panel._.offsetParentId = offsetParent.getId();
133 // Don't use display or visibility style because we need to
134 // calculate the rendering layout later and focus the element.
135 element.setOpacity( 0 );
137 // To allow the context menu to decrease back their width
138 element.getFirst().removeStyle( 'width' );
140 // Configure the IFrame blur event. Do that only once.
141 if ( !this._.blurSet )
143 // Non IE prefer the event into a window object.
144 var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );
146 // With addEventListener compatible browsers, we must
147 // useCapture when registering the focus/blur events to
148 // guarantee they will be firing in all situations. (#3068, #3222 )
149 CKEDITOR.event.useCapture = true;
151 focused.on( 'blur', function( ev )
153 if ( !this.allowBlur() )
156 // As we are using capture to register the listener,
157 // the blur event may get fired even when focusing
158 // inside the window itself, so we must ensure the
159 // target is out of it.
161 if ( CKEDITOR.env.ie && !this.allowBlur()
162 || ( target = ev.data.getTarget() )
163 && target.getName && target.getName() != 'iframe' )
166 if ( this.visible && !this._.activeChild && !isShowing )
171 focused.on( 'focus', function()
173 this._.focused = true;
175 this.allowBlur( true );
179 CKEDITOR.event.useCapture = false;
184 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
186 if ( this.onEscape && this.onEscape( keystroke ) === false )
191 CKEDITOR.tools.setTimeout( function()
194 left -= element.$.offsetWidth;
196 var panelLoad = CKEDITOR.tools.bind( function ()
198 var target = element.getFirst();
200 if ( block.autoSize )
202 // We must adjust first the width or IE6 could include extra lines in the height computation
203 var widthNode = block.element.$;
205 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )
206 widthNode = widthNode.parentNode;
208 if ( CKEDITOR.env.ie )
209 widthNode = widthNode.document.body;
211 var width = widthNode.scrollWidth;
212 // Account for extra height needed due to IE quirks box model bug:
213 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug
215 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )
216 width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 );
217 // A little extra at the end.
218 // If not present, IE6 might break into the next line, but also it looks better this way
221 target.setStyle( 'width', width + 'px' );
223 // IE doesn't compute the scrollWidth if a filter is applied previously
224 block.element.addClass( 'cke_frameLoaded' );
226 var height = block.element.$.scrollHeight;
228 // Account for extra height needed due to IE quirks box model bug:
229 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug
231 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )
232 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );
234 target.setStyle( 'height', height + 'px' );
236 // Fix IE < 8 visibility.
237 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );
240 target.removeStyle( 'height' );
242 var panelElement = panel.element,
243 panelWindow = panelElement.getWindow(),
244 windowScroll = panelWindow.getScrollPosition(),
245 viewportSize = panelWindow.getViewPaneSize(),
248 'height' : panelElement.$.offsetHeight,
249 'width' : panelElement.$.offsetWidth
252 // If the menu is horizontal off, shift it toward
253 // the opposite language direction.
254 if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )
255 left += ( panelSize.width * ( rtl ? 1 : -1 ) );
257 // Vertical off screen is simpler.
258 if ( top + panelSize.height > viewportSize.height + windowScroll.y )
259 top -= panelSize.height;
261 // If IE is in RTL, we have troubles with absolute
262 // position and horizontal scrolls. Here we have a
263 // series of hacks to workaround it. (#6146)
264 if ( CKEDITOR.env.ie )
266 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ),
267 scrollParent = offsetParent;
269 // Quirks returns <body>, but standards returns <html>.
270 if ( scrollParent.getName() == 'html' )
271 scrollParent = scrollParent.getDocument().getBody();
273 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' )
275 // For IE8, there is not much logic on this, but it works.
276 if ( CKEDITOR.env.ie8Compat )
277 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2;
279 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth );
283 // Trigger the onHide event of the previously active panel to prevent
284 // incorrect styles from being applied (#6170)
285 var innerElement = element.getFirst(),
287 if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) )
288 activePanel.onHide && activePanel.onHide.call( this, 1 );
289 innerElement.setCustomData( 'activePanel', this );
296 element.setOpacity( 1 );
299 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;
301 // Set the panel frame focus, so the blur event gets fired.
302 CKEDITOR.tools.setTimeout( function()
304 iframe.$.contentWindow.focus();
305 // We need this get fired manually because of unfired focus() function.
306 this.allowBlur( true );
308 }, CKEDITOR.env.air ? 200 : 0, this);
312 this.onShow.call( this );
319 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )
322 this.element.setStyle( 'display', 'none' );
324 this.element.getFirst().removeCustomData( 'activePanel' );
328 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222.
330 var panel = this._.panel;
331 if ( allow != undefined )
332 panel.allowBlur = allow;
334 return panel.allowBlur;
337 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )
339 // Skip reshowing of child which is already visible.
340 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )
345 panel.onHide = CKEDITOR.tools.bind( function()
347 // Use a timeout, so we give time for this menu to get
348 // potentially focused.
349 CKEDITOR.tools.setTimeout( function()
351 if ( !this._.focused )
358 this._.activeChild = panel;
359 this._.focused = false;
361 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );
363 /* #3767 IE: Second level menu may not have borders */
364 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )
366 setTimeout(function()
368 panel.element.getChild( 0 ).$.style.cssText += '';
373 hideChild : function()
375 var activeChild = this._.activeChild;
379 delete activeChild.onHide;
380 delete this._.activeChild;
387 CKEDITOR.on( 'instanceDestroyed', function()
389 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );
391 for ( var i in panels )
393 var panel = panels[ i ];
394 // Safe to destroy it since there're no more instances.(#4241)
395 if ( isLastInstance )
397 // Panel might be used by other instances, just hide them.(#4552)
399 panel.element.hide();
401 // Remove the registration.
402 isLastInstance && ( panels = {} );