2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'floatpanel',
\r
8 requires : [ 'panel' ]
\r
14 var isShowing = false;
\r
16 function getPanel( editor, doc, parentElement, definition, level )
\r
18 // Generates the panel key: docId-eleId-skinName-langDir[-uiColor][-CSSs][-level]
\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
28 var panel = panels[ key ];
\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
35 panel.element.setStyles(
\r
38 position : 'absolute'
\r
45 CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(
\r
47 $ : function( editor, parentElement, definition, level )
\r
49 definition.forceIFrame = true;
\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
56 this.element = element;
\r
60 // The panel that will be floating.
\r
62 parentElement : parentElement,
\r
63 definition : definition,
\r
67 dir : editor.lang.dir
\r
73 addBlock : function( name, block )
\r
75 return this._.panel.addBlock( name, block );
\r
78 addListBlock : function( name, multiSelect )
\r
80 return this._.panel.addListBlock( name, multiSelect );
\r
83 getBlock : function( name )
\r
85 return this._.panel.getBlock( name );
\r
101 showBlock : function( name, offsetParent, corner, offsetX, offsetY )
\r
103 var panel = this._.panel,
\r
104 block = panel.showBlock( name );
\r
106 this.allowBlur( false );
\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
115 var left = position.x + ( offsetX || 0 ),
\r
116 top = position.y + ( offsetY || 0 );
\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
124 if ( corner == 3 || corner == 4 )
\r
125 top += offsetParent.$.offsetHeight - 1;
\r
127 // Memorize offsetParent by it's ID.
\r
128 this._.panel._.offsetParentId = offsetParent.getId();
\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
139 // To allow the context menu to decrease back their width
\r
140 element.getFirst().removeStyle( 'width' );
\r
142 // Configure the IFrame blur event. Do that only once.
\r
143 if ( !this._.blurSet )
\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
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
153 focused.on( 'blur', function( ev )
\r
155 if ( !this.allowBlur() )
\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
163 if ( CKEDITOR.env.ie && !this.allowBlur()
\r
164 || ( target = ev.data.getTarget() )
\r
165 && target.getName && target.getName() != 'iframe' )
\r
168 if ( this.visible && !this._.activeChild && !isShowing )
\r
173 focused.on( 'focus', function()
\r
175 this._.focused = true;
\r
177 this.allowBlur( true );
\r
181 CKEDITOR.event.useCapture = false;
\r
183 this._.blurSet = 1;
\r
186 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
188 if ( this.onEscape && this.onEscape( keystroke ) === false )
\r
193 CKEDITOR.tools.setTimeout( function()
\r
196 left -= element.$.offsetWidth;
\r
198 var panelLoad = CKEDITOR.tools.bind( function ()
\r
200 var target = element.getFirst();
\r
202 if ( block.autoSize )
\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
207 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )
\r
208 widthNode = widthNode.parentNode;
\r
210 if ( CKEDITOR.env.ie )
\r
211 widthNode = widthNode.document.body;
\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
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
223 target.setStyle( 'width', width + 'px' );
\r
225 // IE doesn't compute the scrollWidth if a filter is applied previously
\r
226 block.element.addClass( 'cke_frameLoaded' );
\r
228 var height = block.element.$.scrollHeight;
\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
233 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )
\r
234 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );
\r
236 target.setStyle( 'height', height + 'px' );
\r
238 // Fix IE < 8 visibility.
\r
239 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );
\r
242 target.removeStyle( 'height' );
\r
244 var panelElement = panel.element,
\r
245 panelWindow = panelElement.getWindow(),
\r
246 windowScroll = panelWindow.getScrollPosition(),
\r
247 viewportSize = panelWindow.getViewPaneSize(),
\r
250 'height' : panelElement.$.offsetHeight,
\r
251 'width' : panelElement.$.offsetWidth
\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
259 // Vertical off screen is simpler.
\r
260 if ( top + panelSize.height > viewportSize.height + windowScroll.y )
\r
261 top -= panelSize.height;
\r
268 element.setOpacity( 1 );
\r
271 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;
\r
273 // Set the panel frame focus, so the blur event gets fired.
\r
274 CKEDITOR.tools.setTimeout( function()
\r
276 iframe.$.contentWindow.focus();
\r
277 // We need this get fired manually because of unfired focus() function.
\r
278 this.allowBlur( true );
\r
284 this.onShow.call( this );
\r
291 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )
\r
294 this.element.setStyle( 'display', 'none' );
\r
299 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222.
\r
301 var panel = this._.panel;
\r
302 if ( allow != undefined )
\r
303 panel.allowBlur = allow;
\r
305 return panel.allowBlur;
\r
308 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )
\r
310 // Skip reshowing of child which is already visible.
\r
311 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )
\r
316 panel.onHide = CKEDITOR.tools.bind( function()
\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
322 if ( !this._.focused )
\r
329 this._.activeChild = panel;
\r
330 this._.focused = false;
\r
332 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );
\r
334 /* #3767 IE: Second level menu may not have borders */
\r
335 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )
\r
337 setTimeout(function()
\r
339 panel.element.getChild( 0 ).$.style.cssText += '';
\r
344 hideChild : function()
\r
346 var activeChild = this._.activeChild;
\r
350 delete activeChild.onHide;
\r
351 delete this._.activeChild;
\r
352 activeChild.hide();
\r
358 CKEDITOR.on( 'instanceDestroyed', function()
\r
360 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );
\r
362 for ( var i in panels )
\r
364 var panel = panels[ i ];
\r
365 // Safe to destroy it since there're no more instances.(#4241)
\r
366 if ( isLastInstance )
\r
368 // Panel might be used by other instances, just hide them.(#4552)
\r
370 panel.element.hide();
\r
372 // Remove the registration.
\r
373 isLastInstance && ( panels = {} );
\r