Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / layout / component / field / TextArea.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @private
17  * @class Ext.layout.component.field.TextArea
18  * @extends Ext.layout.component.field.Field
19  * Layout class for {@link Ext.form.field.TextArea} fields. Handles sizing the textarea field.
20  */
21 Ext.define('Ext.layout.component.field.TextArea', {
22     extend: 'Ext.layout.component.field.Text',
23     alias: 'layout.textareafield',
24
25     type: 'textareafield',
26
27
28     /**
29      * Given the target bodyEl dimensions, adjust them if necessary to return the correct final
30      * size based on the text field's {@link Ext.form.field.Text#grow grow config}. Overrides the
31      * textfield layout's implementation to handle height rather than width.
32      * @param {Number} width The bodyEl width
33      * @param {Number} height The bodyEl height
34      * @return {Number[]} [inputElWidth, inputElHeight]
35      */
36     adjustForGrow: function(width, height) {
37         var me = this,
38             owner = me.owner,
39             inputEl, value, max,
40             curWidth, curHeight, calcHeight,
41             result = [width, height];
42
43         if (owner.grow) {
44             inputEl = owner.inputEl;
45             curWidth = inputEl.getWidth(true); //subtract border/padding to get the available width for the text
46             curHeight = inputEl.getHeight();
47
48             // Get and normalize the field value for measurement
49             value = inputEl.dom.value || ' ';
50             value += owner.growAppend;
51
52             // Translate newlines to <br> tags
53             value = value.replace(/\n/g, '<br>');
54
55             // Find the height that contains the whole text value
56             calcHeight = Ext.util.TextMetrics.measure(inputEl, value, curWidth).height +
57                          inputEl.getBorderWidth("tb") + inputEl.getPadding("tb");
58
59             // Constrain
60             max = owner.growMax;
61             if (Ext.isNumber(height)) {
62                 max = Math.min(max, height);
63             }
64             result[1] = Ext.Number.constrain(calcHeight, owner.growMin, max);
65         }
66
67         return result;
68     }
69
70 });