JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
cd3fc3da65e484e6682a94d3cf35bfd9ff851795
[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                 if ( CKEDITOR.env.ie )\r
18                         newHeight = doc.getBody().$.scrollHeight + 24;\r
19                 else\r
20                         newHeight = doc.getDocumentElement().$.offsetHeight;\r
21 \r
22                 var min = editor.config.autoGrow_minHeight,\r
23                         max = editor.config.autoGrow_maxHeight;\r
24                 ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );\r
25                 if ( min )\r
26                         newHeight = Math.max( newHeight, min );\r
27                 if ( max )\r
28                         newHeight = Math.min( newHeight, max );\r
29 \r
30                 if ( newHeight != currentHeight )\r
31                 {\r
32                         newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;\r
33                         editor.resize( editor.container.getStyle( 'width' ), newHeight, true );\r
34                 }\r
35         };\r
36         CKEDITOR.plugins.add( 'autogrow',\r
37         {\r
38                 init : function( editor )\r
39                 {\r
40                         for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )\r
41                         {\r
42                                 editor.on( eventName, function( evt )\r
43                                 {\r
44                                         // Some time is required for insertHtml, and it gives other events better performance as well.\r
45                                         if ( evt.editor.mode == 'wysiwyg' )\r
46                                                 setTimeout( function(){ resizeEditor( evt.editor ); }, 100 );\r
47                                 });\r
48                         }\r
49                 }\r
50         });\r
51 })();\r
52 /**\r
53  * The minimum height to which the editor can reach using AutoGrow.\r
54  * @name CKEDITOR.config.autoGrow_minHeight\r
55  * @type Number\r
56  * @default 200\r
57  * @since 3.4\r
58  * @example\r
59  * config.autoGrow_minHeight = 300;\r
60  */\r
61 \r
62 /**\r
63  * The maximum height to which the editor can reach using AutoGrow. Zero means unlimited.\r
64  * @name CKEDITOR.config.autoGrow_maxHeight\r
65  * @type Number\r
66  * @default 0\r
67  * @since 3.4\r
68  * @example\r
69  * config.autoGrow_maxHeight = 400;\r
70  */\r
71 \r
72 /**\r
73  * Fired when the AutoGrow plugin is about to change the size of the editor.\r
74  * @name CKEDITOR#autogrow\r
75  * @event\r
76  * @param {Number} data.currentHeight The current height of the editor (before the resizing).\r
77  * @param {Number} data.newHeight The new height of the editor (after the resizing). It can be changed\r
78  *                              to determine another height to be used instead.\r
79  */\r