JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / autogrow / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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 \r
11         // Actual content height, figured out by appending check the last element's document position.\r
12         function contentHeight( scrollable )\r
13         {\r
14                 var overflowY = scrollable.getStyle( 'overflow-y' );\r
15 \r
16                 var doc = scrollable.getDocument();\r
17                 // Create a temporary marker element.\r
18                 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
19                 doc[ CKEDITOR.env.ie? 'getBody' : 'getDocumentElement']().append( marker );\r
20 \r
21                 var height = marker.getDocumentPosition( doc ).y + marker.$.offsetHeight;\r
22                 marker.remove();\r
23                 scrollable.setStyle( 'overflow-y', overflowY );\r
24                 return height;\r
25         }\r
26 \r
27         var resizeEditor = function( editor )\r
28         {\r
29                 if ( !editor.window )\r
30                         return;\r
31 \r
32                 var doc = editor.document,\r
33                         iframe = new CKEDITOR.dom.element( doc.getWindow().$.frameElement ),\r
34                         body = doc.getBody(),\r
35                         htmlElement = doc.getDocumentElement(),\r
36                         currentHeight = editor.window.getViewPaneSize().height,\r
37                         // Quirks mode overflows body, standards overflows document element\r
38                         scrollable = doc.$.compatMode == 'BackCompat' ? body : htmlElement,\r
39                         newHeight = contentHeight( scrollable );\r
40 \r
41                 // Additional space specified by user.\r
42                 newHeight += ( editor.config.autoGrow_bottomSpace || 0 );\r
43 \r
44                 var min = editor.config.autoGrow_minHeight != undefined ? editor.config.autoGrow_minHeight : 200,\r
45                         max = editor.config.autoGrow_maxHeight || Infinity;\r
46 \r
47                 newHeight = Math.max( newHeight, min );\r
48                 newHeight = Math.min( newHeight, max );\r
49 \r
50                 if ( newHeight != currentHeight )\r
51                 {\r
52                         newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;\r
53                         editor.resize( editor.container.getStyle( 'width' ), newHeight, true );\r
54                 }\r
55 \r
56                 if ( scrollable.$.scrollHeight > scrollable.$.clientHeight && newHeight < max )\r
57                         scrollable.setStyle( 'overflow-y', 'hidden' );\r
58                 else\r
59                         scrollable.removeStyle( 'overflow-y' );\r
60 \r
61 \r
62         };\r
63 \r
64         CKEDITOR.plugins.add( 'autogrow',\r
65         {\r
66                 init : function( editor )\r
67                 {\r
68                         editor.addCommand( 'autogrow', { exec : resizeEditor, modes : { wysiwyg:1 }, readOnly: 1, canUndo: false, editorFocus: false } );\r
69 \r
70                         var eventsList = { contentDom:1, key:1, selectionChange:1, insertElement:1, mode:1 };\r
71                         editor.config.autoGrow_onStartup && ( eventsList[ 'instanceReady' ] = 1 );\r
72                         for ( var eventName in eventsList )\r
73                         {\r
74                                 editor.on( eventName, function( evt )\r
75                                 {\r
76                                         var maximize = editor.getCommand( 'maximize' );\r
77                                         // Some time is required for insertHtml, and it gives other events better performance as well.\r
78                                         if ( evt.editor.mode == 'wysiwyg' &&\r
79                                                 // Disable autogrow when the editor is maximized .(#6339)\r
80                                                 ( !maximize || maximize.state != CKEDITOR.TRISTATE_ON ) )\r
81                                         {\r
82                                                 setTimeout( function()\r
83                                                 {\r
84                                                         resizeEditor( evt.editor );\r
85                                                         // Second pass to make correction upon\r
86                                                         // the first resize, e.g. scrollbar.\r
87                                                         resizeEditor( evt.editor );\r
88                                                 }, 100 );\r
89                                         }\r
90                                 });\r
91                         }\r
92                 }\r
93         });\r
94 })();\r
95 /**\r
96  * The minimum height that the editor can reach using the AutoGrow feature.\r
97  * @name CKEDITOR.config.autoGrow_minHeight\r
98  * @type Number\r
99  * @default <code>200</code>\r
100  * @since 3.4\r
101  * @example\r
102  * config.autoGrow_minHeight = 300;\r
103  */\r
104 \r
105 /**\r
106  * The maximum height that the editor can reach using the AutoGrow feature. Zero means unlimited.\r
107  * @name CKEDITOR.config.autoGrow_maxHeight\r
108  * @type Number\r
109  * @default <code>0</code>\r
110  * @since 3.4\r
111  * @example\r
112  * config.autoGrow_maxHeight = 400;\r
113  */\r
114 \r
115  /**\r
116  * Whether to have the auto grow happen on editor creation.\r
117  * @name CKEDITOR.config.autoGrow_onStartup\r
118  * @type Boolean\r
119  * @default false\r
120  * @since 3.6.2\r
121  * @example\r
122  * config.autoGrow_onStartup = true;\r
123  */\r
124 \r
125 /**\r
126  * Fired when the AutoGrow plugin is about to change the size of the editor.\r
127  * @name CKEDITOR.editor#autogrow\r
128  * @event\r
129  * @param {Number} data.currentHeight The current height of the editor (before resizing).\r
130  * @param {Number} data.newHeight The new height of the editor (after resizing). It can be changed\r
131  *                              to determine a different height value to be used instead.\r
132  */\r
133 \r
134 \r
135 /**\r
136  *  Extra height in pixel to leave between the bottom boundary of content with document size when auto resizing.\r
137  * @name CKEDITOR.config.autoGrow_bottomSpace\r
138  * @type Number\r
139  * @default 0\r
140  * @since 3.6.2\r
141  */\r