X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ffloatpanel%2Fplugin.js;h=4a24dfa4d31a5781af7cb6a8ed9646cc4b18885f;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=fb5e3a487feb218cd9631516dff3961dd0c8874b;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/plugins/floatpanel/plugin.js b/_source/plugins/floatpanel/plugin.js index fb5e3a4..4a24dfa 100644 --- a/_source/plugins/floatpanel/plugin.js +++ b/_source/plugins/floatpanel/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -16,14 +16,8 @@ CKEDITOR.plugins.add( 'floatpanel', function getPanel( editor, doc, parentElement, definition, level ) { // Generates the panel key: docId-eleId-skinName-langDir[-uiColor][-CSSs][-level] - var key = - doc.getUniqueId() + - '-' + parentElement.getUniqueId() + - '-' + editor.skinName + - '-' + editor.lang.dir + - ( ( editor.uiColor && ( '-' + editor.uiColor ) ) || '' ) + - ( ( definition.css && ( '-' + definition.css ) ) || '' ) + - ( ( level && ( '-' + level ) ) || '' ); + var key = CKEDITOR.tools.genKey( doc.getUniqueId(), parentElement.getUniqueId(), editor.skinName, editor.lang.dir, + editor.uiColor || '', definition.css || '', level || '' ); var panel = panels[ key ]; @@ -46,17 +40,21 @@ CKEDITOR.plugins.add( 'floatpanel', { $ : function( editor, parentElement, definition, level ) { - definition.forceIFrame = true; + definition.forceIFrame = 1; var doc = parentElement.getDocument(), panel = getPanel( editor, doc, parentElement, definition, level || 0 ), element = panel.element, iframe = element.getFirst().getFirst(); + // Disable native browser menu. (#4825) + element.disableContextMenu(); + this.element = element; this._ = { + editor : editor, // The panel that will be floating. panel : panel, parentElement : parentElement, @@ -66,6 +64,8 @@ CKEDITOR.plugins.add( 'floatpanel', children : [], dir : editor.lang.dir }; + + editor.on( 'mode', function(){ this.hide(); }, this ); }, proto : @@ -104,7 +104,11 @@ CKEDITOR.plugins.add( 'floatpanel', block = panel.showBlock( name ); this.allowBlur( false ); - isShowing = true; + isShowing = 1; + + // Record from where the focus is when open panel. + this._.returnFocus = this._.editor.focusManager.hasFocus ? this._.editor : new CKEDITOR.dom.element( CKEDITOR.document.$.activeElement ); + var element = this.element, iframe = this._.iframe, @@ -115,7 +119,10 @@ CKEDITOR.plugins.add( 'floatpanel', var left = position.x + ( offsetX || 0 ), top = position.y + ( offsetY || 0 ); - if ( ( rtl && ( corner == 1 || corner == 4 ) ) || ( !rtl && ( corner == 2 || corner == 3 ) ) ) + // Floating panels are off by (-1px, 0px) in RTL mode. (#3438) + if ( rtl && ( corner == 1 || corner == 4 ) ) + left += offsetParent.$.offsetWidth; + else if ( !rtl && ( corner == 2 || corner == 3 ) ) left += offsetParent.$.offsetWidth - 1; if ( corner == 3 || corner == 4 ) @@ -127,12 +134,17 @@ CKEDITOR.plugins.add( 'floatpanel', element.setStyles( { top : top + 'px', - left : '-3000px', - visibility : 'hidden', - opacity : '0', // FF3 is ignoring "visibility" + left: 0, display : '' }); + // Don't use display or visibility style because we need to + // calculate the rendering layout later and focus the element. + element.setOpacity( 0 ); + + // To allow the context menu to decrease back their width + element.getFirst().removeStyle( 'width' ); + // Configure the IFrame blur event. Do that only once. if ( !this._.blurSet ) { @@ -146,21 +158,24 @@ CKEDITOR.plugins.add( 'floatpanel', focused.on( 'blur', function( ev ) { - if ( CKEDITOR.env.ie && !this.allowBlur() ) + if ( !this.allowBlur() ) return; // As we are using capture to register the listener, // the blur event may get fired even when focusing // inside the window itself, so we must ensure the // target is out of it. - var target = ev.data.getTarget(), - targetWindow = target.getWindow && target.getWindow(); - - if ( targetWindow && targetWindow.equals( focused ) ) + var target = ev.data.getTarget() ; + if ( target.getName && target.getName() != 'iframe' ) return; if ( this.visible && !this._.activeChild && !isShowing ) + { + // Panel close is caused by user's navigating away the focus, e.g. click outside the panel. + // DO NOT restore focus in this case. + delete this._.returnFocus; this.hide(); + } }, this ); @@ -177,90 +192,198 @@ CKEDITOR.plugins.add( 'floatpanel', this._.blurSet = 1; } - panel.onEscape = CKEDITOR.tools.bind( function() + panel.onEscape = CKEDITOR.tools.bind( function( keystroke ) { - this.onEscape && this.onEscape(); + if ( this.onEscape && this.onEscape( keystroke ) === false ) + return false; }, this ); CKEDITOR.tools.setTimeout( function() { - if ( rtl ) - left -= element.$.offsetWidth; + var panelLoad = CKEDITOR.tools.bind( function () + { + var target = element.getFirst(); - element.setStyles( + if ( block.autoSize ) { - left : left + 'px', - visibility : '', - opacity : '1' // FF3 is ignoring "visibility" - }); + var panelDoc = block.element.getDocument(); + var width = ( CKEDITOR.env.webkit? block.element : panelDoc.getBody() )[ '$' ].scrollWidth; + + // Account for extra height needed due to IE quirks box model bug: + // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug + // (#3426) + if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 ) + width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 ) + 3; + // A little extra at the end. + // If not present, IE6 might break into the next line, but also it looks better this way + width += 4 ; + + target.setStyle( 'width', width + 'px' ); + + // IE doesn't compute the scrollWidth if a filter is applied previously + block.element.addClass( 'cke_frameLoaded' ); - if ( block.autoSize ) - { - function setHeight() - { - var target = element.getFirst(); var height = block.element.$.scrollHeight; // Account for extra height needed due to IE quirks box model bug: // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug // (#3426) if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 ) - height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ); + height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ) + 3; target.setStyle( 'height', height + 'px' ); // Fix IE < 8 visibility. panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' ); } - - if ( panel.isLoaded ) - setHeight(); else - panel.onLoad = setHeight; - } - else - element.getFirst().removeStyle( 'height' ); + target.removeStyle( 'height' ); - // Set the IFrame focus, so the blur event gets fired. - CKEDITOR.tools.setTimeout( function() + // Flip panel layout horizontally in RTL with known width. + if ( rtl ) + left -= element.$.offsetWidth; + + // Pop the style now for measurement. + element.setStyle( 'left', left + 'px' ); + + /* panel layout smartly fit the viewport size. */ + var panelElement = panel.element, + panelWindow = panelElement.getWindow(), + rect = element.$.getBoundingClientRect(), + viewportSize = panelWindow.getViewPaneSize(); + + // Compensation for browsers that dont support "width" and "height". + var rectWidth = rect.width || rect.right - rect.left, + rectHeight = rect.height || rect.bottom - rect.top; + + // Check if default horizontal layout is impossible. + var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left, + spaceBefore = rtl ? viewportSize.width - rect.right : rect.left; + + if ( rtl ) { - if ( definition.voiceLabel ) + if ( spaceAfter < rectWidth ) { - if ( CKEDITOR.env.gecko ) - { - var container = iframe.getParent(); - container.setAttribute( 'role', 'region' ); - container.setAttribute( 'title', definition.voiceLabel ); - iframe.setAttribute( 'role', 'region' ); - iframe.setAttribute( 'title', ' ' ); - } + // Flip to show on right. + if ( spaceBefore > rectWidth ) + left += rectWidth; + // Align to window left. + else if ( viewportSize.width > rectWidth ) + left = left - rect.left; + // Align to window right, never cutting the panel at right. + else + left = left - rect.right + viewportSize.width; + } + } + else if ( spaceAfter < rectWidth ) + { + // Flip to show on left. + if ( spaceBefore > rectWidth ) + left -= rectWidth; + // Align to window right. + else if ( viewportSize.width > rectWidth ) + left = left - rect.right + viewportSize.width; + // Align to window left, never cutting the panel at left. + else + left = left - rect.left; } - if ( CKEDITOR.env.ie && CKEDITOR.env.quirks ) - iframe.focus(); + + + // Check if the default vertical layout is possible. + var spaceBelow = viewportSize.height - rect.top, + spaceAbove = rect.top; + + if ( spaceBelow < rectHeight ) + { + // Flip to show above. + if ( spaceAbove > rectHeight ) + top -= rectHeight; + // Align to window bottom. + else if ( viewportSize.height > rectHeight ) + top = top - rect.bottom + viewportSize.height; + // Align to top, never cutting the panel at top. else - iframe.$.contentWindow.focus(); + top = top - rect.top; + } + + // If IE is in RTL, we have troubles with absolute + // position and horizontal scrolls. Here we have a + // series of hacks to workaround it. (#6146) + if ( CKEDITOR.env.ie ) + { + var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ), + scrollParent = offsetParent; + + // Quirks returns , but standards returns . + if ( scrollParent.getName() == 'html' ) + scrollParent = scrollParent.getDocument().getBody(); + + if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' ) + { + // For IE8, there is not much logic on this, but it works. + if ( CKEDITOR.env.ie8Compat ) + left -= element.getDocument().getDocumentElement().$.scrollLeft * 2; + else + left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth ); + } + } + + // Trigger the onHide event of the previously active panel to prevent + // incorrect styles from being applied (#6170) + var innerElement = element.getFirst(), + activePanel; + if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) ) + activePanel.onHide && activePanel.onHide.call( this, 1 ); + innerElement.setCustomData( 'activePanel', this ); + + element.setStyles( + { + top : top + 'px', + left : left + 'px' + } ); + element.setOpacity( 1 ); + } , this ); + + panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad; - // We need this get fired manually because of unfired focus() function. - if ( CKEDITOR.env.ie && !CKEDITOR.env.quirks ) - this.allowBlur( true ); - }, 0, this); - }, 0, this); + // Set the panel frame focus, so the blur event gets fired. + CKEDITOR.tools.setTimeout( function() + { + iframe.$.contentWindow.focus(); + // We need this get fired manually because of unfired focus() function. + this.allowBlur( true ); + }, 0, this); + }, CKEDITOR.env.air ? 200 : 0, this); this.visible = 1; if ( this.onShow ) this.onShow.call( this ); - isShowing = false; + isShowing = 0; }, - hide : function() + hide : function( returnFocus ) { if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) ) { this.hideChild(); + // Blur previously focused element. (#6671) + CKEDITOR.env.gecko && this._.iframe.getFrameDocument().$.activeElement.blur(); this.element.setStyle( 'display', 'none' ); this.visible = 0; + this.element.getFirst().removeCustomData( 'activePanel' ); + + // Return focus properly. (#6247) + var focusReturn = returnFocus !== false && this._.returnFocus; + if ( focusReturn ) + { + // Webkit requires focus moved out panel iframe first. + if ( CKEDITOR.env.webkit && focusReturn.type ) + focusReturn.getWindow().$.focus(); + + focusReturn.focus(); + } } }, @@ -316,10 +439,31 @@ CKEDITOR.plugins.add( 'floatpanel', if ( activeChild ) { delete activeChild.onHide; + // Sub panels don't manage focus. (#7881) + delete activeChild._.returnFocus; delete this._.activeChild; activeChild.hide(); } } } }); + + CKEDITOR.on( 'instanceDestroyed', function() + { + var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances ); + + for ( var i in panels ) + { + var panel = panels[ i ]; + // Safe to destroy it since there're no more instances.(#4241) + if ( isLastInstance ) + panel.destroy(); + // Panel might be used by other instances, just hide them.(#4552) + else + panel.element.hide(); + } + // Remove the registration. + isLastInstance && ( panels = {} ); + + } ); })();