JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.6.1
[ckeditor.git] / _source / plugins / floatpanel / plugin.js
index 8940192..4a24dfa 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -47,10 +47,14 @@ CKEDITOR.plugins.add( 'floatpanel',
                                element = panel.element,\r
                                iframe = element.getFirst().getFirst();\r
 \r
+                       // Disable native browser menu. (#4825)\r
+                       element.disableContextMenu();\r
+\r
                        this.element = element;\r
 \r
                        this._ =\r
                        {\r
+                               editor : editor,\r
                                // The panel that will be floating.\r
                                panel : panel,\r
                                parentElement : parentElement,\r
@@ -60,6 +64,8 @@ CKEDITOR.plugins.add( 'floatpanel',
                                children : [],\r
                                dir : editor.lang.dir\r
                        };\r
+\r
+                       editor.on( 'mode', function(){ this.hide(); }, this );\r
                },\r
 \r
                proto :\r
@@ -100,6 +106,10 @@ CKEDITOR.plugins.add( 'floatpanel',
                                this.allowBlur( false );\r
                                isShowing = 1;\r
 \r
+                               // Record from where the focus is when open panel.\r
+                               this._.returnFocus = this._.editor.focusManager.hasFocus ? this._.editor : new CKEDITOR.dom.element( CKEDITOR.document.$.activeElement );\r
+\r
+\r
                                var element = this.element,\r
                                        iframe = this._.iframe,\r
                                        definition = this._.definition,\r
@@ -123,10 +133,11 @@ CKEDITOR.plugins.add( 'floatpanel',
 \r
                                element.setStyles(\r
                                        {\r
-                                               top : 0,\r
+                                               top : top + 'px',\r
                                                left: 0,\r
                                                display : ''\r
                                        });\r
+\r
                                // Don't use display or visibility style because we need to\r
                                // calculate the rendering layout later and focus the element.\r
                                element.setOpacity( 0 );\r
@@ -154,14 +165,17 @@ CKEDITOR.plugins.add( 'floatpanel',
                                                        // the blur event may get fired even when focusing\r
                                                        // inside the window itself, so we must ensure the\r
                                                        // target is out of it.\r
-                                                       var target;\r
-                                                       if ( CKEDITOR.env.ie && !this.allowBlur()\r
-                                                                || ( target = ev.data.getTarget() )\r
-                                                                     && target.getName && target.getName() != 'iframe' )\r
+                                                       var target = ev.data.getTarget() ;\r
+                                                       if ( target.getName && target.getName() != 'iframe' )\r
                                                                return;\r
 \r
                                                        if ( this.visible && !this._.activeChild && !isShowing )\r
+                                                       {\r
+                                                               // Panel close is caused by user's navigating away the focus, e.g. click outside the panel.\r
+                                                               // DO NOT restore focus in this case.\r
+                                                               delete this._.returnFocus;\r
                                                                this.hide();\r
+                                                       }\r
                                                },\r
                                                this );\r
 \r
@@ -187,30 +201,20 @@ CKEDITOR.plugins.add( 'floatpanel',
 \r
                                CKEDITOR.tools.setTimeout( function()\r
                                        {\r
-                                               if ( rtl )\r
-                                                       left -= element.$.offsetWidth;\r
-\r
                                                var panelLoad = CKEDITOR.tools.bind( function ()\r
                                                {\r
                                                        var target = element.getFirst();\r
 \r
                                                        if ( block.autoSize )\r
                                                        {\r
-                                                               // We must adjust first the width or IE6 could include extra lines in the height computation\r
-                                                               var widthNode = block.element.$;\r
-\r
-                                                               if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )\r
-                                                                       widthNode = widthNode.parentNode;\r
+                                                               var panelDoc = block.element.getDocument();\r
+                                                               var width = ( CKEDITOR.env.webkit? block.element : panelDoc.getBody() )[ '$' ].scrollWidth;\r
 \r
-                                                               if ( CKEDITOR.env.ie )\r
-                                                                       widthNode = widthNode.document.body;\r
-\r
-                                                               var width = widthNode.scrollWidth;\r
                                                                // Account for extra height needed due to IE quirks box model bug:\r
                                                                // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
                                                                // (#3426)\r
                                                                if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 )\r
-                                                                       width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 );\r
+                                                                       width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 ) + 3;\r
                                                                // A little extra at the end.\r
                                                                // If not present, IE6 might break into the next line, but also it looks better this way\r
                                                                width += 4 ;\r
@@ -226,7 +230,7 @@ CKEDITOR.plugins.add( 'floatpanel',
                                                                // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug\r
                                                                // (#3426)\r
                                                                if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 )\r
-                                                                       height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 );\r
+                                                                       height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ) + 3;\r
 \r
                                                                target.setStyle( 'height', height + 'px' );\r
 \r
@@ -236,24 +240,72 @@ CKEDITOR.plugins.add( 'floatpanel',
                                                        else\r
                                                                target.removeStyle( 'height' );\r
 \r
+                                                       // Flip panel layout horizontally in RTL with known width.\r
+                                                       if ( rtl )\r
+                                                               left -= element.$.offsetWidth;\r
+\r
+                                                       // Pop the style now for measurement.\r
+                                                       element.setStyle( 'left', left + 'px' );\r
+\r
+                                                       /* panel layout smartly fit the viewport size. */\r
                                                        var panelElement = panel.element,\r
                                                                panelWindow = panelElement.getWindow(),\r
-                                                               windowScroll = panelWindow.getScrollPosition(),\r
-                                                               viewportSize = panelWindow.getViewPaneSize(),\r
-                                                               panelSize =\r
+                                                               rect = element.$.getBoundingClientRect(),\r
+                                                               viewportSize = panelWindow.getViewPaneSize();\r
+\r
+                                                       // Compensation for browsers that dont support "width" and "height".\r
+                                                       var rectWidth = rect.width || rect.right - rect.left,\r
+                                                       rectHeight = rect.height || rect.bottom - rect.top;\r
+\r
+                                                       // Check if default horizontal layout is impossible.\r
+                                                       var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left,\r
+                                                               spaceBefore = rtl ? viewportSize.width - rect.right : rect.left;\r
+\r
+                                                       if ( rtl )\r
+                                                       {\r
+                                                               if ( spaceAfter < rectWidth )\r
                                                                {\r
-                                                                       'height' : panelElement.$.offsetHeight,\r
-                                                                       'width' : panelElement.$.offsetWidth\r
-                                                               };\r
+                                                                       // Flip to show on right.\r
+                                                                       if ( spaceBefore > rectWidth )\r
+                                                                               left += rectWidth;\r
+                                                                       // Align to window left.\r
+                                                                       else if ( viewportSize.width > rectWidth )\r
+                                                                               left = left - rect.left;\r
+                                                                       // Align to window right, never cutting the panel at right.\r
+                                                                       else\r
+                                                                               left = left - rect.right + viewportSize.width;\r
+                                                               }\r
+                                                       }\r
+                                                       else if ( spaceAfter < rectWidth )\r
+                                                       {\r
+                                                                       // Flip to show on left.\r
+                                                                       if ( spaceBefore > rectWidth )\r
+                                                                               left -= rectWidth;\r
+                                                                       // Align to window right.\r
+                                                                       else if ( viewportSize.width > rectWidth )\r
+                                                                               left = left - rect.right + viewportSize.width;\r
+                                                                       // Align to window left, never cutting the panel at left.\r
+                                                                       else\r
+                                                                               left = left - rect.left;\r
+                                                               }\r
+\r
 \r
-                                                       // If the menu is horizontal off, shift it toward\r
-                                                       // the opposite language direction.\r
-                                                       if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )\r
-                                                               left += ( panelSize.width * ( rtl ? 1 : -1 ) );\r
+                                                       // Check if the default vertical layout is possible.\r
+                                                       var spaceBelow = viewportSize.height - rect.top,\r
+                                                               spaceAbove = rect.top;\r
 \r
-                                                       // Vertical off screen is simpler.\r
-                                                       if ( top + panelSize.height > viewportSize.height + windowScroll.y )\r
-                                                               top -= panelSize.height;\r
+                                                       if ( spaceBelow < rectHeight )\r
+                                                       {\r
+                                                               // Flip to show above.\r
+                                                               if ( spaceAbove > rectHeight )\r
+                                                                       top -= rectHeight;\r
+                                                               // Align to window bottom.\r
+                                                               else if ( viewportSize.height > rectHeight )\r
+                                                                       top = top - rect.bottom + viewportSize.height;\r
+                                                               // Align to top, never cutting the panel at top.\r
+                                                               else\r
+                                                                       top = top - rect.top;\r
+                                                       }\r
 \r
                                                        // If IE is in RTL, we have troubles with absolute\r
                                                        // position and horizontal scrolls. Here we have a\r
@@ -302,7 +354,7 @@ CKEDITOR.plugins.add( 'floatpanel',
                                                        // We need this get fired manually because of unfired focus() function.\r
                                                        this.allowBlur( true );\r
                                                }, 0, this);\r
-                                       }, 0, this);\r
+                                       },  CKEDITOR.env.air ? 200 : 0, this);\r
                                this.visible = 1;\r
 \r
                                if ( this.onShow )\r
@@ -311,14 +363,27 @@ CKEDITOR.plugins.add( 'floatpanel',
                                isShowing = 0;\r
                        },\r
 \r
-                       hide : function()\r
+                       hide : function( returnFocus )\r
                        {\r
                                if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )\r
                                {\r
                                        this.hideChild();\r
+                                       // Blur previously focused element. (#6671)\r
+                                       CKEDITOR.env.gecko && this._.iframe.getFrameDocument().$.activeElement.blur();\r
                                        this.element.setStyle( 'display', 'none' );\r
                                        this.visible = 0;\r
                                        this.element.getFirst().removeCustomData( 'activePanel' );\r
+\r
+                                       // Return focus properly. (#6247)\r
+                                       var focusReturn = returnFocus !== false && this._.returnFocus;\r
+                                       if ( focusReturn )\r
+                                       {\r
+                                               // Webkit requires focus moved out panel iframe first.\r
+                                               if ( CKEDITOR.env.webkit && focusReturn.type )\r
+                                                       focusReturn.getWindow().$.focus();\r
+\r
+                                               focusReturn.focus();\r
+                                       }\r
                                }\r
                        },\r
 \r
@@ -374,6 +439,8 @@ CKEDITOR.plugins.add( 'floatpanel',
                                if ( activeChild )\r
                                {\r
                                        delete activeChild.onHide;\r
+                                       // Sub panels don't manage focus. (#7881)\r
+                                       delete activeChild._.returnFocus;\r
                                        delete this._.activeChild;\r
                                        activeChild.hide();\r
                                }\r