Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / layout / component / field / TextArea.js
1 /**
2  * @private
3  * @class Ext.layout.component.field.TextArea
4  * @extends Ext.layout.component.field.Field
5  * Layout class for {@link Ext.form.field.TextArea} fields. Handles sizing the textarea field.
6  */
7 Ext.define('Ext.layout.component.field.TextArea', {
8     extend: 'Ext.layout.component.field.Text',
9     alias: 'layout.textareafield',
10
11     type: 'textareafield',
12
13
14     /**
15      * Given the target bodyEl dimensions, adjust them if necessary to return the correct final
16      * size based on the text field's {@link Ext.form.field.Text#grow grow config}. Overrides the
17      * textfield layout's implementation to handle height rather than width.
18      * @param {Number} width The bodyEl width
19      * @param {Number} height The bodyEl height
20      * @return {Array} [inputElWidth, inputElHeight]
21      */
22     adjustForGrow: function(width, height) {
23         var me = this,
24             owner = me.owner,
25             inputEl, value, max,
26             curWidth, curHeight, calcHeight,
27             result = [width, height];
28
29         if (owner.grow) {
30             inputEl = owner.inputEl;
31             curWidth = inputEl.getWidth(true); //subtract border/padding to get the available width for the text
32             curHeight = inputEl.getHeight();
33
34             // Get and normalize the field value for measurement
35             value = inputEl.dom.value || ' ';
36             value += owner.growAppend;
37
38             // Translate newlines to <br> tags
39             value = value.replace(/\n/g, '<br>');
40
41             // Find the height that contains the whole text value
42             calcHeight = Ext.util.TextMetrics.measure(inputEl, value, curWidth).height +
43                          inputEl.getBorderWidth("tb") + inputEl.getPadding("tb");
44
45             // Constrain
46             max = owner.growMax;
47             if (Ext.isNumber(height)) {
48                 max = Math.min(max, height);
49             }
50             result[1] = Ext.Number.constrain(calcHeight, owner.growMin, max);
51         }
52
53         return result;
54     }
55
56 });