alecpl
2011-11-15 2eeb128d06ad37dea9bd1765784fb53e47ef527c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * editor_plugin_src.js
 *
 * Copyright 2009, Moxiecode Systems AB
 * Released under LGPL License.
 *
 * License: http://tinymce.moxiecode.com/license
 * Contributing: http://tinymce.moxiecode.com/contributing
 */
 
(function() {
    /**
     * Auto Resize
     * 
     * This plugin automatically resizes the content area to fit its content height.
     * It will retain a minimum height, which is the height of the content area when
     * it's initialized.
     */
    tinymce.create('tinymce.plugins.AutoResizePlugin', {
        /**
         * Initializes the plugin, this will be executed after the plugin has been created.
         * This call is done before the editor instance has finished it's initialization so use the onInit event
         * of the editor instance to intercept that event.
         *
         * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
         * @param {string} url Absolute URL to where the plugin is located.
         */
        init : function(ed, url) {
            var t = this, oldSize = 0;
 
            if (ed.getParam('fullscreen_is_enabled'))
                return;
 
            /**
             * This method gets executed each time the editor needs to resize.
             */
            function resize() {
                var d = ed.getDoc(), b = d.body, de = d.documentElement, DOM = tinymce.DOM, resizeHeight = t.autoresize_min_height, myHeight;
 
                // Get height differently depending on the browser used
                myHeight = tinymce.isIE ? b.scrollHeight : d.body.offsetHeight;
 
                // Don't make it smaller than the minimum height
                if (myHeight > t.autoresize_min_height)
                    resizeHeight = myHeight;
 
                // If a maximum height has been defined don't exceed this height
                if (t.autoresize_max_height && myHeight > t.autoresize_max_height) {
                    resizeHeight = t.autoresize_max_height;
                    ed.getBody().style.overflowY = "auto";
                } else
                    ed.getBody().style.overflowY = "hidden";
 
                // Resize content element
                if (resizeHeight !== oldSize) {
                    DOM.setStyle(DOM.get(ed.id + '_ifr'), 'height', resizeHeight + 'px');
                    oldSize = resizeHeight;
                }
 
                // if we're throbbing, we'll re-throb to match the new size
                if (t.throbbing) {
                    ed.setProgressState(false);
                    ed.setProgressState(true);
                }
            };
 
            t.editor = ed;
 
            // Define minimum height
            t.autoresize_min_height = parseInt( ed.getParam('autoresize_min_height', ed.getElement().offsetHeight) );
 
            // Define maximum height    
            t.autoresize_max_height = parseInt( ed.getParam('autoresize_max_height', 0) );
 
            // Add padding at the bottom for better UX
            ed.onInit.add(function(ed){
                ed.dom.setStyle(ed.getBody(), 'paddingBottom', ed.getParam('autoresize_bottom_margin', 50) + 'px');
            });
 
            // Add appropriate listeners for resizing content area
            ed.onChange.add(resize);
            ed.onSetContent.add(resize);
            ed.onPaste.add(resize);
            ed.onKeyUp.add(resize);
            ed.onPostRender.add(resize);
 
            if (ed.getParam('autoresize_on_init', true)) {
                // Things to do when the editor is ready
                ed.onInit.add(function(ed, l) {
                    // Show throbber until content area is resized properly
                    ed.setProgressState(true);
                    t.throbbing = true;
 
                    // Hide scrollbars
                    ed.getBody().style.overflowY = "hidden";
                });
 
                ed.onLoadContent.add(function(ed, l) {
                    resize();
 
                    // Because the content area resizes when its content CSS loads,
                    // and we can't easily add a listener to its onload event,
                    // we'll just trigger a resize after a short loading period
                    setTimeout(function() {
                        resize();
 
                        // Disable throbber
                        ed.setProgressState(false);
                        t.throbbing = false;
                    }, 1250);
                });
            }
 
            // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceExample');
            ed.addCommand('mceAutoResize', resize);
        },
 
        /**
         * Returns information about the plugin as a name/value array.
         * The current keys are longname, author, authorurl, infourl and version.
         *
         * @return {Object} Name/value array containing information about the plugin.
         */
        getInfo : function() {
            return {
                longname : 'Auto Resize',
                author : 'Moxiecode Systems AB',
                authorurl : 'http://tinymce.moxiecode.com',
                infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autoresize',
                version : tinymce.majorVersion + "." + tinymce.minorVersion
            };
        }
    });
 
    // Register plugin
    tinymce.PluginManager.add('autoresize', tinymce.plugins.AutoResizePlugin);
})();