2 Copyright (c) 2003-2011, 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
64 editor.on( 'mode', function(){ this.hide(); }, this );
\r
69 addBlock : function( name, block )
\r
71 return this._.panel.addBlock( name, block );
\r
74 addListBlock : function( name, multiSelect )
\r
76 return this._.panel.addListBlock( name, multiSelect );
\r
79 getBlock : function( name )
\r
81 return this._.panel.getBlock( name );
\r
97 showBlock : function( name, offsetParent, corner, offsetX, offsetY )
\r
99 var panel = this._.panel,
\r
100 block = panel.showBlock( name );
\r
102 this.allowBlur( false );
\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
111 var left = position.x + ( offsetX || 0 ),
\r
112 top = position.y + ( offsetY || 0 );
\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
120 if ( corner == 3 || corner == 4 )
\r
121 top += offsetParent.$.offsetHeight - 1;
\r
123 // Memorize offsetParent by it's ID.
\r
124 this._.panel._.offsetParentId = offsetParent.getId();
\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
137 // To allow the context menu to decrease back their width
\r
138 element.getFirst().removeStyle( 'width' );
\r
140 // Configure the IFrame blur event. Do that only once.
\r
141 if ( !this._.blurSet )
\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
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
151 focused.on( 'blur', function( ev )
\r
153 if ( !this.allowBlur() )
\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
161 if ( CKEDITOR.env.ie && !this.allowBlur()
\r
162 || ( target = ev.data.getTarget() )
\r
163 && target.getName && target.getName() != 'iframe' )
\r
166 if ( this.visible && !this._.activeChild && !isShowing )
\r
171 focused.on( 'focus', function()
\r
173 this._.focused = true;
\r
175 this.allowBlur( true );
\r
179 CKEDITOR.event.useCapture = false;
\r
181 this._.blurSet = 1;
\r
184 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
186 if ( this.onEscape && this.onEscape( keystroke ) === false )
\r
191 CKEDITOR.tools.setTimeout( function()
\r
194 left -= element.$.offsetWidth;
\r
196 var panelLoad = CKEDITOR.tools.bind( function ()
\r
198 var target = element.getFirst();
\r
200 if ( block.autoSize )
\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
205 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )
\r
206 widthNode = widthNode.parentNode;
\r
208 if ( CKEDITOR.env.ie )
\r
209 widthNode = widthNode.document.body;
\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
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
221 target.setStyle( 'width', width + 'px' );
\r
223 // IE doesn't compute the scrollWidth if a filter is applied previously
\r
224 block.element.addClass( 'cke_frameLoaded' );
\r
226 var height = block.element.$.scrollHeight;
\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
231 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )
\r
232 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );
\r
234 target.setStyle( 'height', height + 'px' );
\r
236 // Fix IE < 8 visibility.
\r
237 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' );
\r
240 target.removeStyle( 'height' );
\r
242 var panelElement = panel.element,
\r
243 panelWindow = panelElement.getWindow(),
\r
244 windowScroll = panelWindow.getScrollPosition(),
\r
245 viewportSize = panelWindow.getViewPaneSize(),
\r
248 'height' : panelElement.$.offsetHeight,
\r
249 'width' : panelElement.$.offsetWidth
\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
257 // Vertical off screen is simpler.
\r
258 if ( top + panelSize.height > viewportSize.height + windowScroll.y )
\r
259 top -= panelSize.height;
\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
266 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ),
\r
267 scrollParent = offsetParent;
\r
269 // Quirks returns <body>, but standards returns <html>.
\r
270 if ( scrollParent.getName() == 'html' )
\r
271 scrollParent = scrollParent.getDocument().getBody();
\r
273 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' )
\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
279 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth );
\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
287 if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) )
\r
288 activePanel.onHide && activePanel.onHide.call( this, 1 );
\r
289 innerElement.setCustomData( 'activePanel', this );
\r
296 element.setOpacity( 1 );
\r
299 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;
\r
301 // Set the panel frame focus, so the blur event gets fired.
\r
302 CKEDITOR.tools.setTimeout( function()
\r
304 iframe.$.contentWindow.focus();
\r
305 // We need this get fired manually because of unfired focus() function.
\r
306 this.allowBlur( true );
\r
308 }, CKEDITOR.env.air ? 200 : 0, this);
\r
312 this.onShow.call( this );
\r
319 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )
\r
322 this.element.setStyle( 'display', 'none' );
\r
324 this.element.getFirst().removeCustomData( 'activePanel' );
\r
328 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222.
\r
330 var panel = this._.panel;
\r
331 if ( allow != undefined )
\r
332 panel.allowBlur = allow;
\r
334 return panel.allowBlur;
\r
337 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )
\r
339 // Skip reshowing of child which is already visible.
\r
340 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )
\r
345 panel.onHide = CKEDITOR.tools.bind( function()
\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
351 if ( !this._.focused )
\r
358 this._.activeChild = panel;
\r
359 this._.focused = false;
\r
361 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );
\r
363 /* #3767 IE: Second level menu may not have borders */
\r
364 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )
\r
366 setTimeout(function()
\r
368 panel.element.getChild( 0 ).$.style.cssText += '';
\r
373 hideChild : function()
\r
375 var activeChild = this._.activeChild;
\r
379 delete activeChild.onHide;
\r
380 delete this._.activeChild;
\r
381 activeChild.hide();
\r
387 CKEDITOR.on( 'instanceDestroyed', function()
\r
389 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );
\r
391 for ( var i in panels )
\r
393 var panel = panels[ i ];
\r
394 // Safe to destroy it since there're no more instances.(#4241)
\r
395 if ( isLastInstance )
\r
397 // Panel might be used by other instances, just hide them.(#4552)
\r
399 panel.element.hide();
\r
401 // Remove the registration.
\r
402 isLastInstance && ( panels = {} );
\r