JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.2
[ckeditor.git] / _source / plugins / autogrow / plugin.js
index d6c4697..c8c668e 100644 (file)
@@ -7,41 +7,69 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
  * @file AutoGrow plugin\r
  */\r
 (function(){\r
+\r
+       // Actual content height, figured out by appending check the last element's document position.\r
+       function contentHeight( scrollable )\r
+       {\r
+               var overflowY = scrollable.getStyle( 'overflow-y' );\r
+\r
+               var doc = scrollable.getDocument();\r
+               // Create a temporary marker element.\r
+               var marker = CKEDITOR.dom.element.createFromHtml( '<span style="margin:0;padding:0;border:0;clear:both;width:1px;height:1px;display:block;">' + ( CKEDITOR.env.webkit ? '&nbsp;' : '' ) + '</span>', doc );\r
+               doc[ CKEDITOR.env.ie? 'getBody' : 'getDocumentElement']().append( marker );\r
+\r
+               var height = marker.getDocumentPosition( doc ).y + marker.$.offsetHeight;\r
+               marker.remove();\r
+               scrollable.setStyle( 'overflow-y', overflowY );\r
+               return height;\r
+       }\r
+\r
        var resizeEditor = function( editor )\r
        {\r
                if ( !editor.window )\r
                        return;\r
+\r
                var doc = editor.document,\r
+                       iframe = new CKEDITOR.dom.element( doc.getWindow().$.frameElement ),\r
+                       body = doc.getBody(),\r
+                       htmlElement = doc.getDocumentElement(),\r
                        currentHeight = editor.window.getViewPaneSize().height,\r
-                       newHeight;\r
+                       // Quirks mode overflows body, standards overflows document element\r
+                       scrollable = doc.$.compatMode == 'BackCompat' ? body : htmlElement,\r
+                       newHeight = contentHeight( scrollable );\r
 \r
-               // We can not use documentElement to calculate the height for IE (#6061).\r
-               // It is not good for IE Quirks, yet using offsetHeight would also not work as expected (#6408).\r
-               // We do the same for FF because of the html height workaround (#6341).\r
-               if ( CKEDITOR.env.ie || CKEDITOR.env.gecko )\r
-                       newHeight = doc.getBody().$.scrollHeight + ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 0 : 24 );\r
-               else\r
-                       newHeight = doc.getDocumentElement().$.offsetHeight;\r
+               // Additional space specified by user.\r
+               newHeight += ( editor.config.autoGrow_bottomSpace || 0 );\r
+\r
+               var min = editor.config.autoGrow_minHeight != undefined ? editor.config.autoGrow_minHeight : 200,\r
+                       max = editor.config.autoGrow_maxHeight || Infinity;\r
 \r
-               var min = editor.config.autoGrow_minHeight,\r
-                       max = editor.config.autoGrow_maxHeight;\r
-               ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );\r
-               if ( min )\r
-                       newHeight = Math.max( newHeight, min );\r
-               if ( max )\r
-                       newHeight = Math.min( newHeight, max );\r
+               newHeight = Math.max( newHeight, min );\r
+               newHeight = Math.min( newHeight, max );\r
 \r
                if ( newHeight != currentHeight )\r
                {\r
                        newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;\r
                        editor.resize( editor.container.getStyle( 'width' ), newHeight, true );\r
                }\r
+\r
+               if ( scrollable.$.scrollHeight > scrollable.$.clientHeight && newHeight < max )\r
+                       scrollable.setStyle( 'overflow-y', 'hidden' );\r
+               else\r
+                       scrollable.removeStyle( 'overflow-y' );\r
+\r
+\r
        };\r
+\r
        CKEDITOR.plugins.add( 'autogrow',\r
        {\r
                init : function( editor )\r
                {\r
-                       for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )\r
+                       editor.addCommand( 'autogrow', { exec : resizeEditor, modes : { wysiwyg:1 }, readOnly: 1, canUndo: false, editorFocus: false } );\r
+\r
+                       var eventsList = { contentDom:1, key:1, selectionChange:1, insertElement:1 };\r
+                       editor.config.autoGrow_onStartup && ( eventsList[ 'instanceReady' ] = 1 );\r
+                       for ( var eventName in eventsList )\r
                        {\r
                                editor.on( eventName, function( evt )\r
                                {\r
@@ -51,7 +79,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                // Disable autogrow when the editor is maximized .(#6339)\r
                                                ( !maximize || maximize.state != CKEDITOR.TRISTATE_ON ) )\r
                                        {\r
-                                               setTimeout( function(){ resizeEditor( evt.editor ); }, 100 );\r
+                                               setTimeout( function()\r
+                                               {\r
+                                                       resizeEditor( evt.editor );\r
+                                                       // Second pass to make correction upon\r
+                                                       // the first resize, e.g. scrollbar.\r
+                                                       resizeEditor( evt.editor );\r
+                                               }, 100 );\r
                                        }\r
                                });\r
                        }\r
@@ -59,30 +93,49 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        });\r
 })();\r
 /**\r
- * The minimum height to which the editor can reach using AutoGrow.\r
+ * The minimum height that the editor can reach using the AutoGrow feature.\r
  * @name CKEDITOR.config.autoGrow_minHeight\r
  * @type Number\r
- * @default 200\r
+ * @default <code>200</code>\r
  * @since 3.4\r
  * @example\r
  * config.autoGrow_minHeight = 300;\r
  */\r
 \r
 /**\r
- * The maximum height to which the editor can reach using AutoGrow. Zero means unlimited.\r
+ * The maximum height that the editor can reach using the AutoGrow feature. Zero means unlimited.\r
  * @name CKEDITOR.config.autoGrow_maxHeight\r
  * @type Number\r
- * @default 0\r
+ * @default <code>0</code>\r
  * @since 3.4\r
  * @example\r
  * config.autoGrow_maxHeight = 400;\r
  */\r
 \r
+ /**\r
+ * Whether to have the auto grow happen on editor creation.\r
+ * @name CKEDITOR.config.autoGrow_onStartup\r
+ * @type Boolean\r
+ * @default false\r
+ * @since 3.6.2\r
+ * @example\r
+ * config.autoGrow_onStartup = true;\r
+ */\r
+\r
 /**\r
  * Fired when the AutoGrow plugin is about to change the size of the editor.\r
  * @name CKEDITOR.editor#autogrow\r
  * @event\r
- * @param {Number} data.currentHeight The current height of the editor (before the resizing).\r
- * @param {Number} data.newHeight The new height of the editor (after the resizing). It can be changed\r
- *                             to determine another height to be used instead.\r
+ * @param {Number} data.currentHeight The current height of the editor (before resizing).\r
+ * @param {Number} data.newHeight The new height of the editor (after resizing). It can be changed\r
+ *                             to determine a different height value to be used instead.\r
+ */\r
+\r
+\r
+/**\r
+ *  Extra height in pixel to leave between the bottom boundary of content with document size when auto resizing.\r
+ * @name CKEDITOR.config.autoGrow_bottomSpace\r
+ * @type Number\r
+ * @default 0\r
+ * @since 3.6.2\r
  */\r