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