Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / form / TextArea.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.form.TextArea
9  * @extends Ext.form.TextField
10  * Multiline text field.  Can be used as a direct replacement for traditional textarea fields, plus adds
11  * support for auto-sizing.
12  * @constructor
13  * Creates a new TextArea
14  * @param {Object} config Configuration options
15  * @xtype textarea
16  */
17 Ext.form.TextArea = Ext.extend(Ext.form.TextField,  {
18     /**
19      * @cfg {Number} growMin The minimum height to allow when <tt>{@link Ext.form.TextField#grow grow}=true</tt>
20      * (defaults to <tt>60</tt>)
21      */
22     growMin : 60,
23     /**
24      * @cfg {Number} growMax The maximum height to allow when <tt>{@link Ext.form.TextField#grow grow}=true</tt>
25      * (defaults to <tt>1000</tt>)
26      */
27     growMax: 1000,
28     growAppend : '&#160;\n&#160;',
29     growPad : Ext.isWebKit ? -6 : 0,
30
31     enterIsSpecial : false,
32
33     /**
34      * @cfg {Boolean} preventScrollbars <tt>true</tt> to prevent scrollbars from appearing regardless of how much text is
35      * in the field. This option is only relevant when {@link #grow} is <tt>true</tt>. Equivalent to setting overflow: hidden, defaults to 
36      * <tt>false</tt>.
37      */
38     preventScrollbars: false,
39     /**
40      * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
41      * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
42      * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details.  Defaults to:</p>
43      * <pre><code>{tag: "textarea", style: "width:100px;height:60px;", autocomplete: "off"}</code></pre>
44      */
45
46     // private
47     onRender : function(ct, position){
48         if(!this.el){
49             this.defaultAutoCreate = {
50                 tag: "textarea",
51                 style:"width:100px;height:60px;",
52                 autocomplete: "off"
53             };
54         }
55         Ext.form.TextArea.superclass.onRender.call(this, ct, position);
56         if(this.grow){
57             this.textSizeEl = Ext.DomHelper.append(document.body, {
58                 tag: "pre", cls: "x-form-grow-sizer"
59             });
60             if(this.preventScrollbars){
61                 this.el.setStyle("overflow", "hidden");
62             }
63             this.el.setHeight(this.growMin);
64         }
65     },
66
67     onDestroy : function(){
68         Ext.destroy(this.textSizeEl);
69         Ext.form.TextArea.superclass.onDestroy.call(this);
70     },
71
72     fireKey : function(e){
73         if(e.isSpecialKey() && (this.enterIsSpecial || (e.getKey() != e.ENTER || e.hasModifier()))){
74             this.fireEvent("specialkey", this, e);
75         }
76     },
77
78     // private
79     onKeyUp : function(e){
80         if(!e.isNavKeyPress() || e.getKey() == e.ENTER){
81             this.autoSize();
82         }
83         Ext.form.TextArea.superclass.onKeyUp.call(this, e);
84     },
85
86     /**
87      * Automatically grows the field to accomodate the height of the text up to the maximum field height allowed.
88      * This only takes effect if grow = true, and fires the {@link #autosize} event if the height changes.
89      */
90     autoSize: function(){
91         if(!this.grow || !this.textSizeEl){
92             return;
93         }
94         var el = this.el;
95         var v = el.dom.value;
96         var ts = this.textSizeEl;
97         ts.innerHTML = '';
98         ts.appendChild(document.createTextNode(v));
99         v = ts.innerHTML;
100         Ext.fly(ts).setWidth(this.el.getWidth());
101         if(v.length < 1){
102             v = "&#160;&#160;";
103         }else{
104             v += this.growAppend;
105             if(Ext.isIE){
106                 v = v.replace(/\n/g, '<br />');
107             }
108         }
109         ts.innerHTML = v;
110         var h = Math.min(this.growMax, Math.max(ts.offsetHeight, this.growMin) + this.growPad);
111         if(h != this.lastHeight){
112             this.lastHeight = h;
113             this.el.setHeight(h);
114             this.fireEvent("autosize", this, h);
115         }
116     }
117 });
118 Ext.reg('textarea', Ext.form.TextArea);