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
19 var key = CKEDITOR.tools.genKey( doc.getUniqueId(), parentElement.getUniqueId(), editor.skinName, editor.lang.dir,
\r
20 editor.uiColor || '', definition.css || '', level || '' );
\r
22 var panel = panels[ key ];
\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
29 panel.element.setStyles(
\r
32 position : 'absolute'
\r
39 CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(
\r
41 $ : function( editor, parentElement, definition, level )
\r
43 definition.forceIFrame = 1;
\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
50 this.element = element;
\r
54 // The panel that will be floating.
\r
56 parentElement : parentElement,
\r
57 definition : definition,
\r
61 dir : editor.lang.dir
\r
67 addBlock : function( name, block )
\r
69 return this._.panel.addBlock( name, block );
\r
72 addListBlock : function( name, multiSelect )
\r
74 return this._.panel.addListBlock( name, multiSelect );
\r
77 getBlock : function( name )
\r
79 return this._.panel.getBlock( name );
\r
95 showBlock : function( name, offsetParent, corner, offsetX, offsetY )
\r
97 var panel = this._.panel,
\r
98 block = panel.showBlock( name );
\r
100 this.allowBlur( false );
\r
103 var element = this.element,
\r
104 iframe = this._.iframe,
\r
105 definition = this._.definition,
\r
106 position = offsetParent.getDocumentPosition( element.getDocument() ),
\r
107 rtl = this._.dir == 'rtl';
\r
109 var left = position.x + ( offsetX || 0 ),
\r
110 top = position.y + ( offsetY || 0 );
\r
112 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)
\r
113 if ( rtl && ( corner == 1 || corner == 4 ) )
\r
114 left += offsetParent.$.offsetWidth;
\r
115 else if ( !rtl && ( corner == 2 || corner == 3 ) )
\r
116 left += offsetParent.$.offsetWidth - 1;
\r
118 if ( corner == 3 || corner == 4 )
\r
119 top += offsetParent.$.offsetHeight - 1;
\r
121 // Memorize offsetParent by it's ID.
\r
122 this._.panel._.offsetParentId = offsetParent.getId();
\r
130 // Don't use display or visibility style because we need to
\r
131 // calculate the rendering layout later and focus the element.
\r
132 element.setOpacity( 0 );
\r
134 // To allow the context menu to decrease back their width
\r
135 element.getFirst().removeStyle( 'width' );
\r
137 // Configure the IFrame blur event. Do that only once.
\r
138 if ( !this._.blurSet )
\r
140 // Non IE prefer the event into a window object.
\r
141 var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );
\r
143 // With addEventListener compatible browsers, we must
\r
144 // useCapture when registering the focus/blur events to
\r
145 // guarantee they will be firing in all situations. (#3068, #3222 )
\r
146 CKEDITOR.event.useCapture = true;
\r
148 focused.on( 'blur', function( ev )
\r
150 if ( !this.allowBlur() )
\r
153 // As we are using capture to register the listener,
\r
154 // the blur event may get fired even when focusing
\r
155 // inside the window itself, so we must ensure the
\r
156 // target is out of it.
\r
158 if ( CKEDITOR.env.ie && !this.allowBlur()
\r
159 || ( target = ev.data.getTarget() )
\r
160 && target.getName && target.getName() != 'iframe' )
\r
163 if ( this.visible && !this._.activeChild && !isShowing )
\r
168 focused.on( 'focus', function()
\r
170 this._.focused = true;
\r
172 this.allowBlur( true );
\r
176 CKEDITOR.event.useCapture = false;
\r
178 this._.blurSet = 1;
\r
181 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
183 if ( this.onEscape && this.onEscape( keystroke ) === false )
\r
188 CKEDITOR.tools.setTimeout( function()
\r
191 left -= element.$.offsetWidth;
\r
193 var panelLoad = CKEDITOR.tools.bind( function ()
\r
195 var target = element.getFirst();
\r
197 if ( block.autoSize )
\r
199 // We must adjust first the width or IE6 could include extra lines in the height computation
\r
200 var widthNode = block.element.$;
\r
202 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )
\r
203 widthNode = widthNode.parentNode;
\r
205 if ( CKEDITOR.env.ie )
\r
206 widthNode = widthNode.document.body;
\r
208 var width = widthNode.scrollWidth;
\r
209 // Account for extra height needed due to IE quirks box model bug:
\r
210 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug
\r
212 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )
\r
213 width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 );
\r
214 // A little extra at the end.
\r
215 // If not present, IE6 might break into the next line, but also it looks better this way
\r
218 target.setStyle( 'width', width + 'px' );
\r
220 // IE doesn't compute the scrollWidth if a filter is applied previously
\r
221 block.element.addClass( 'cke_frameLoaded' );
\r
223 var height = block.element.$.scrollHeight;
\r
225 // Account for extra height needed due to IE quirks box model bug:
\r
226 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug
\r
228 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )
\r
229 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );
\r
231 target.setStyle( 'height', height + 'px' );
\r
233 // Fix IE < 8 visibility.
\r
234 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );
\r
237 target.removeStyle( 'height' );
\r
239 var panelElement = panel.element,
\r
240 panelWindow = panelElement.getWindow(),
\r
241 windowScroll = panelWindow.getScrollPosition(),
\r
242 viewportSize = panelWindow.getViewPaneSize(),
\r
245 'height' : panelElement.$.offsetHeight,
\r
246 'width' : panelElement.$.offsetWidth
\r
249 // If the menu is horizontal off, shift it toward
\r
250 // the opposite language direction.
\r
251 if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )
\r
252 left += ( panelSize.width * ( rtl ? 1 : -1 ) );
\r
254 // Vertical off screen is simpler.
\r
255 if ( top + panelSize.height > viewportSize.height + windowScroll.y )
\r
256 top -= panelSize.height;
\r
258 // If IE is in RTL, we have troubles with absolute
\r
259 // position and horizontal scrolls. Here we have a
\r
260 // series of hacks to workaround it. (#6146)
\r
261 if ( CKEDITOR.env.ie )
\r
263 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ),
\r
264 scrollParent = offsetParent;
\r
266 // Quirks returns <body>, but standards returns <html>.
\r
267 if ( scrollParent.getName() == 'html' )
\r
268 scrollParent = scrollParent.getDocument().getBody();
\r
270 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' )
\r
272 // For IE8, there is not much logic on this, but it works.
\r
273 if ( CKEDITOR.env.ie8Compat )
\r
274 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2;
\r
276 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth );
\r
280 // Trigger the onHide event of the previously active panel to prevent
\r
281 // incorrect styles from being applied (#6170)
\r
282 var innerElement = element.getFirst(),
\r
284 if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) )
\r
285 activePanel.onHide && activePanel.onHide.call( this, 1 );
\r
286 innerElement.setCustomData( 'activePanel', this );
\r
293 element.setOpacity( 1 );
\r
296 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;
\r
298 // Set the panel frame focus, so the blur event gets fired.
\r
299 CKEDITOR.tools.setTimeout( function()
\r
301 iframe.$.contentWindow.focus();
\r
302 // We need this get fired manually because of unfired focus() function.
\r
303 this.allowBlur( true );
\r
309 this.onShow.call( this );
\r
316 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )
\r
319 this.element.setStyle( 'display', 'none' );
\r
321 this.element.getFirst().removeCustomData( 'activePanel' );
\r
325 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222.
\r
327 var panel = this._.panel;
\r
328 if ( allow != undefined )
\r
329 panel.allowBlur = allow;
\r
331 return panel.allowBlur;
\r
334 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )
\r
336 // Skip reshowing of child which is already visible.
\r
337 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )
\r
342 panel.onHide = CKEDITOR.tools.bind( function()
\r
344 // Use a timeout, so we give time for this menu to get
\r
345 // potentially focused.
\r
346 CKEDITOR.tools.setTimeout( function()
\r
348 if ( !this._.focused )
\r
355 this._.activeChild = panel;
\r
356 this._.focused = false;
\r
358 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );
\r
360 /* #3767 IE: Second level menu may not have borders */
\r
361 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )
\r
363 setTimeout(function()
\r
365 panel.element.getChild( 0 ).$.style.cssText += '';
\r
370 hideChild : function()
\r
372 var activeChild = this._.activeChild;
\r
376 delete activeChild.onHide;
\r
377 delete this._.activeChild;
\r
378 activeChild.hide();
\r
384 CKEDITOR.on( 'instanceDestroyed', function()
\r
386 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );
\r
388 for ( var i in panels )
\r
390 var panel = panels[ i ];
\r
391 // Safe to destroy it since there're no more instances.(#4241)
\r
392 if ( isLastInstance )
\r
394 // Panel might be used by other instances, just hide them.(#4552)
\r
396 panel.element.hide();
\r
398 // Remove the registration.
\r
399 isLastInstance && ( panels = {} );
\r