JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / autogrow / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file AutoGrow plugin\r
8  */\r
9 (function(){\r
10         var resizeEditor = function( editor )\r
11         {\r
12                 if ( !editor.window )\r
13                         return;\r
14                 var doc = editor.document,\r
15                         currentHeight = editor.window.getViewPaneSize().height,\r
16                         newHeight;\r
17 \r
18                 // We can not use documentElement to calculate the height for IE (#6061).\r
19                 // It is not good for IE Quirks, yet using offsetHeight would also not work as expected (#6408).\r
20                 // We do the same for FF because of the html height workaround (#6341).\r
21                 if ( CKEDITOR.env.ie || CKEDITOR.env.gecko )\r
22                         newHeight = doc.getBody().$.scrollHeight + ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 0 : 24 );\r
23                 else\r
24                         newHeight = doc.getDocumentElement().$.offsetHeight;\r
25 \r
26                 var min = editor.config.autoGrow_minHeight,\r
27                         max = editor.config.autoGrow_maxHeight;\r
28                 ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );\r
29                 if ( min )\r
30                         newHeight = Math.max( newHeight, min );\r
31                 if ( max )\r
32                         newHeight = Math.min( newHeight, max );\r
33 \r
34                 if ( newHeight != currentHeight )\r
35                 {\r
36                         newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;\r
37                         editor.resize( editor.container.getStyle( 'width' ), newHeight, true );\r
38                 }\r
39         };\r
40         CKEDITOR.plugins.add( 'autogrow',\r
41         {\r
42                 init : function( editor )\r
43                 {\r
44                         for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )\r
45                         {\r
46                                 editor.on( eventName, function( evt )\r
47                                 {\r
48                                         var maximize = editor.getCommand( 'maximize' );\r
49                                         // Some time is required for insertHtml, and it gives other events better performance as well.\r
50                                         if ( evt.editor.mode == 'wysiwyg' &&\r
51                                                 // Disable autogrow when the editor is maximized .(#6339)\r
52                                                 ( !maximize || maximize.state != CKEDITOR.TRISTATE_ON ) )\r
53                                         {\r
54                                                 setTimeout( function(){ resizeEditor( evt.editor ); }, 100 );\r
55                                         }\r
56                                 });\r
57                         }\r
58                 }\r
59         });\r
60 })();\r
61 /**\r
62  * The minimum height to which the editor can reach using AutoGrow.\r
63  * @name CKEDITOR.config.autoGrow_minHeight\r
64  * @type Number\r
65  * @default 200\r
66  * @since 3.4\r
67  * @example\r
68  * config.autoGrow_minHeight = 300;\r
69  */\r
70 \r
71 /**\r
72  * The maximum height to which the editor can reach using AutoGrow. Zero means unlimited.\r
73  * @name CKEDITOR.config.autoGrow_maxHeight\r
74  * @type Number\r
75  * @default 0\r
76  * @since 3.4\r
77  * @example\r
78  * config.autoGrow_maxHeight = 400;\r
79  */\r
80 \r
81 /**\r
82  * Fired when the AutoGrow plugin is about to change the size of the editor.\r
83  * @name CKEDITOR.editor#autogrow\r
84  * @event\r
85  * @param {Number} data.currentHeight The current height of the editor (before the resizing).\r
86  * @param {Number} data.newHeight The new height of the editor (after the resizing). It can be changed\r
87  *                              to determine another height to be used instead.\r
88  */\r