Ticket #8050: 8050_5.js

File 8050_5.js, 4.4 KB (added by Dinu, 13 years ago)
Line 
1/*
2Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6/**
7 * @file AutoGrow plugin
8 */
9
10(function(){
11
12        // Actual content height, figured out by simply check the last element's document position.
13        function docContentHeight( doc, scrollable )
14        {
15                // Always create marker, to account for margin
16                // ??? Any reason to use <SPAN> here? it doesn't account for floated elements (old problem with autogrow...)
17                // !!! Please check IE6 and quirks
18                // TODO: div style can be affected by document stylesheets. Needs complete reset (display:block, reset width, left, top, bottom, what else???) and !important
19                last = CKEDITOR.dom.element.createFromHtml( '<div style="clear:both; height:1px; line-height:1px"></div>', doc );
20                doc.getBody().append( last );
21                var height = last.getDocumentPosition( doc ).y;
22                last.remove();
23                // Depending on browser, and whether body has margin, largest of these is valid in
24                if(CKEDITOR.env.webkit)
25                        return Math.max( scrollable.$.scrollHeight, height );
26                return height;
27        }
28
29        CKEDITOR.plugins.add( 'autogrow',
30        {
31                init : function( editor )
32                {
33                        // The offset size between content height and editor size.
34                        var contentMargin = 0;
35                        var resizeEditor = function( editor )
36                        {
37                                if ( !editor.window )
38                                        return;
39
40                                var doc = editor.document,
41                                        resizeable = editor.getResizable( 1 );
42                                var     body = doc.getBody(),
43                                        htmlElement = doc.getDocumentElement(),
44                                        currentHeight = resizeable.$.offsetHeight,
45                                        newHeight;
46
47                                // Quirks mode overflows body except for IE9, standards overflows document element.
48                                var scrollable = !CKEDITOR.env.ie9Compat && doc.$.compatMode == 'BackCompat' ? body : htmlElement;
49
50                                // Get maximum possible height
51                                newHeight = docContentHeight( doc, scrollable );
52                                if ( newHeight != currentHeight ) {
53                                        // And resize to it
54                                        resizeable.setStyle( 'height', newHeight + 'px' );
55                                        // If vertical scrollbar disappeared
56                                        if ( scrollable.$.clientWidth >= scrollable.$.scrollWidth )
57                                                if ( newHeight > currentHeight )
58                                                        // Resize down to size gained by removal of vertical and horizontal scrollbar
59                                                        newHeight = docContentHeight( doc, scrollable );
60                                                else;
61                                        else
62                                                // Otherwise add height of scrollbar
63                                                newHeight += scrollable.$.offsetHeight - scrollable.$.clientHeight;
64
65                                        var min = editor.config.autoGrow_minHeight,
66                                                max = editor.config.autoGrow_maxHeight;
67
68                                        ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );
69                                        if ( min )
70                                                newHeight = Math.max( newHeight, min );
71                                        if ( max )
72                                                newHeight = Math.min( newHeight, max );
73
74                                        if ( newHeight != currentHeight )
75                                        {
76                                                newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;
77                                                editor.fire( 'resize' );
78                                        }
79                                        resizeable.setStyle( 'height', newHeight + 'px' );
80                                }
81                        };
82
83                        for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )
84                        {
85                                editor.on( eventName, function( evt )
86                                {
87                                        var maximize = editor.getCommand( 'maximize' );
88                                        // Some time is required for insertHtml, and it gives other events better performance as well.
89                                        if ( evt.editor.mode == 'wysiwyg' &&
90                                                // Disable autogrow when the editor is maximized .(#6339)
91                                                ( !maximize || maximize.state != CKEDITOR.TRISTATE_ON ) )
92                                        {
93                                                setTimeout( function(){ resizeEditor( evt.editor ); }, 100 );
94                                        }
95                        });
96                }
97                }
98        });
99})();
100/**
101 * The minimum height to which the editor can reach using AutoGrow.
102 * @name CKEDITOR.config.autoGrow_minHeight
103 * @type Number
104 * @default 200
105 * @since 3.4
106 * @example
107 * config.autoGrow_minHeight = 300;
108 */
109
110/**
111 * The maximum height to which the editor can reach using AutoGrow. Zero means unlimited.
112 * @name CKEDITOR.config.autoGrow_maxHeight
113 * @type Number
114 * @default 0
115 * @since 3.4
116 * @example
117 * config.autoGrow_maxHeight = 400;
118 */
119
120/**
121 * Fired when the AutoGrow plugin is about to change the size of the editor.
122 * @name CKEDITOR.editor#autogrow
123 * @event
124 * @param {Number} data.currentHeight The current height of the editor (before the resizing).
125 * @param {Number} data.newHeight The new height of the editor (after the resizing). It can be changed
126 *                              to determine another height to be used instead.
127 */
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy