Roo/bootstrap/TextArea.js
[roojs1] / Roo / bootstrap / TextArea.js
1 /*
2  * - LGPL
3  *
4  * Input
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.TextArea
10  * @extends Roo.bootstrap.Input
11  * Bootstrap TextArea class
12  * @cfg {Number} cols Specifies the visible width of a text area
13  * @cfg {Number} rows Specifies the visible number of lines in a text area
14  * @cfg {string} wrap (soft|hard)Specifies how the text in a text area is to be wrapped when submitted in a form
15  * @cfg {string} resize (none|both|horizontal|vertical|inherit|initial)
16  * @cfg {string} html text
17  * 
18  * @constructor
19  * Create a new TextArea
20  * @param {Object} config The config object
21  */
22
23 Roo.bootstrap.TextArea = function(config){
24     Roo.bootstrap.TextArea.superclass.constructor.call(this, config);
25    
26 };
27
28 Roo.extend(Roo.bootstrap.TextArea, Roo.bootstrap.Input,  {
29      
30     cols : false,
31     rows : 5,
32     readOnly : false,
33     warp : 'soft',
34     resize : false,
35     value: false,
36     html: false,
37     
38     getAutoCreate : function(){
39         
40         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
41         
42         var id = Roo.id();
43         
44         var cfg = {};
45         
46         var input =  {
47             tag: 'textarea',
48             id : id,
49             warp : this.warp,
50             rows : this.rows,
51             value : this.value || '',
52             html: this.html || '',
53             cls : 'form-control',
54             placeholder : this.placeholder || '' 
55             
56         };
57         
58         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
59             input.maxLength = this.maxLength;
60         }
61         
62         if(this.resize){
63             input.style = (typeof(input.style) == 'undefined') ? 'resize:' + this.resize : input.style + 'resize:' + this.resize;
64         }
65         
66         if(this.cols){
67             input.cols = this.cols;
68         }
69         
70         if (this.readOnly) {
71             input.readonly = true;
72         }
73         
74         if (this.name) {
75             input.name = this.name;
76         }
77         
78         if (this.size) {
79             input.cls = (typeof(input.cls) == 'undefined') ? 'input-' + this.size : input.cls + ' input-' + this.size;
80         }
81         
82         var settings=this;
83         ['xs','sm','md','lg'].map(function(size){
84             if (settings[size]) {
85                 cfg.cls += ' col-' + size + '-' + settings[size];
86             }
87         });
88         
89         var inputblock = input;
90         
91         if(this.hasFeedback && !this.allowBlank){
92             
93             var feedback = {
94                 tag: 'span',
95                 cls: 'glyphicon form-control-feedback'
96             };
97
98             inputblock = {
99                 cls : 'has-feedback',
100                 cn :  [
101                     input,
102                     feedback
103                 ] 
104             };  
105         }
106         
107         
108         if (this.before || this.after) {
109             
110             inputblock = {
111                 cls : 'input-group',
112                 cn :  [] 
113             };
114             if (this.before) {
115                 inputblock.cn.push({
116                     tag :'span',
117                     cls : 'input-group-addon',
118                     html : this.before
119                 });
120             }
121             
122             inputblock.cn.push(input);
123             
124             if(this.hasFeedback && !this.allowBlank){
125                 inputblock.cls += ' has-feedback';
126                 inputblock.cn.push(feedback);
127             }
128             
129             if (this.after) {
130                 inputblock.cn.push({
131                     tag :'span',
132                     cls : 'input-group-addon',
133                     html : this.after
134                 });
135             }
136             
137         }
138         
139         if (align ==='left' && this.fieldLabel.length) {
140             cfg.cn = [
141
142                 {
143                     tag: 'label',
144                     'for' :  id,
145                     cls : 'control-label col-sm-' + this.labelWidth,
146                     html : this.fieldLabel
147
148                 },
149                 {
150                     cls : "col-sm-" + (12 - this.labelWidth), 
151                     cn: [
152                         inputblock
153                     ]
154                 }
155
156             ];
157         } else if ( this.fieldLabel.length) {
158             cfg.cn = [
159
160                {
161                    tag: 'label',
162                    //cls : 'input-group-addon',
163                    html : this.fieldLabel
164
165                },
166
167                inputblock
168
169            ];
170
171         } else {
172
173             cfg.cn = [
174
175                 inputblock
176
177             ];
178                 
179         }
180         
181         if (this.disabled) {
182             input.disabled=true;
183         }
184         
185         return cfg;
186         
187     },
188     /**
189      * return the real textarea element.
190      */
191     inputEl: function ()
192     {
193         return this.el.select('textarea.form-control',true).first();
194     },
195     
196     /**
197      * Clear any invalid styles/messages for this field
198      */
199     clearInvalid : function()
200     {
201         
202         if(!this.el || this.preventMark){ // not rendered
203             return;
204         }
205         
206         var label = this.el.select('label', true).first();
207         var icon = this.el.select('i.fa-star', true).first();
208         
209         if(label && icon){
210             icon.remove();
211         }
212         
213         this.el.removeClass(this.invalidClass);
214         
215         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
216             
217             var feedback = this.el.select('.form-control-feedback', true).first();
218             
219             if(feedback){
220                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
221             }
222             
223         }
224         
225         this.fireEvent('valid', this);
226     },
227     
228      /**
229      * Mark this field as valid
230      */
231     markValid : function()
232     {
233         if(!this.el  || this.preventMark){ // not rendered
234             return;
235         }
236         
237         this.el.removeClass([this.invalidClass, this.validClass]);
238         
239         var feedback = this.el.select('.form-control-feedback', true).first();
240             
241         if(feedback){
242             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
243         }
244
245         if(this.disabled || this.allowBlank){
246             return;
247         }
248         
249         var label = this.el.select('label', true).first();
250         var icon = this.el.select('i.fa-star', true).first();
251         
252         if(label && icon){
253             icon.remove();
254         }
255         
256         this.el.addClass(this.validClass);
257         
258         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
259             
260             var feedback = this.el.select('.form-control-feedback', true).first();
261             
262             if(feedback){
263                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
264                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
265             }
266             
267         }
268         
269         this.fireEvent('valid', this);
270     },
271     
272      /**
273      * Mark this field as invalid
274      * @param {String} msg The validation message
275      */
276     markInvalid : function(msg)
277     {
278         if(!this.el  || this.preventMark){ // not rendered
279             return;
280         }
281         
282         this.el.removeClass([this.invalidClass, this.validClass]);
283         
284         var feedback = this.el.select('.form-control-feedback', true).first();
285             
286         if(feedback){
287             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
288         }
289
290         if(this.disabled || this.allowBlank){
291             return;
292         }
293         
294         var label = this.el.select('label', true).first();
295         var icon = this.el.select('i.fa-star', true).first();
296         
297         if(!this.getValue().length && label && !icon){
298             this.el.createChild({
299                 tag : 'i',
300                 cls : 'text-danger fa fa-lg fa-star',
301                 tooltip : 'This field is required',
302                 style : 'margin-right:5px;'
303             }, label, true);
304         }
305
306         this.el.addClass(this.invalidClass);
307         
308         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
309             
310             var feedback = this.el.select('.form-control-feedback', true).first();
311             
312             if(feedback){
313                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
314                 
315                 if(this.getValue().length || this.forceFeedback){
316                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
317                 }
318                 
319             }
320             
321         }
322         
323         this.fireEvent('invalid', this, msg);
324     }
325 });
326
327