e692a94302c798190d9e2ef24d57e4b20a1f5c85
[roojs1] / Roo / bootstrap / FieldLabel.js
1 /*
2  * - LGPL
3  *
4  * FieldLabel
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.FieldLabel
10  * @extends Roo.bootstrap.Component
11  * Bootstrap FieldLabel class
12  * @cfg {String} html contents of the element
13  * @cfg {String} tag tag of the element default label
14  * @cfg {String} cls class of the element
15  * @cfg {String} target label target 
16  * @cfg {Boolean} allowBlank (true|false) target allowBlank default true
17  * @cfg {String} invalidClass default "text-warning"
18  * @cfg {String} validClass default "text-success"
19  * @cfg {String} iconTooltip default "This field is required"
20  * @cfg {String} indicatorpos (left|right) default left
21  * 
22  * @constructor
23  * Create a new FieldLabel
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.FieldLabel = function(config){
28     Roo.bootstrap.Element.superclass.constructor.call(this, config);
29     
30     this.addEvents({
31             /**
32              * @event invalid
33              * Fires after the field has been marked as invalid.
34              * @param {Roo.form.FieldLabel} this
35              * @param {String} msg The validation message
36              */
37             invalid : true,
38             /**
39              * @event valid
40              * Fires after the field has been validated with no errors.
41              * @param {Roo.form.FieldLabel} this
42              */
43             valid : true
44         });
45 };
46
47 Roo.extend(Roo.bootstrap.FieldLabel, Roo.bootstrap.Component,  {
48     
49     tag: 'label',
50     cls: '',
51     html: '',
52     target: '',
53     allowBlank : true,
54     invalidClass : 'has-warning',
55     validClass : 'has-success',
56     iconTooltip : 'This field is required',
57     indicatorpos : 'left',
58     
59     getAutoCreate : function(){
60         
61         var cls = "";
62         
63         if (!this.allowBlank) {
64             cls  = "visible";
65         }
66         
67         var cfg = {
68             tag : this.tag,
69             cls : 'roo-bootstrap-field-label ' + this.cls,
70             for : this.target,
71             cn : [
72                 {
73                     tag : 'span',
74                     html : this.html
75                 }
76             ] 
77         };
78         
79         if(!this.allowBlank){
80             
81             var cfg = {
82                 tag : this.tag,
83                 cls : 'roo-bootstrap-field-label ' + this.cls,
84                 for : this.target,
85                 cn : [
86                     {
87                         tag : 'i',
88                         cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star ' + cls,
89                         tooltip : this.iconTooltip
90                     },
91                     {
92                         tag : 'span',
93                         html : this.html
94                     }
95                 ] 
96             };
97
98             if(this.indicatorpos == 'right'){
99                 var cfg = {
100                     tag : this.tag,
101                     cls : 'roo-bootstrap-field-label ' + this.cls,
102                     for : this.target,
103                     cn : [
104                         {
105                             tag : 'span',
106                             html : this.html
107                         },
108                         {
109                             tag : 'i',
110                             cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star '+ cls,
111                             tooltip : this.iconTooltip
112                         }
113                     ] 
114                 };
115             }
116         }
117         
118         return cfg;
119     },
120     
121     initEvents: function() 
122     {
123         Roo.bootstrap.Element.superclass.initEvents.call(this);
124         
125         this.indicator = this.indicatorEl();
126         
127         if(this.indicator){
128             this.indicator.removeClass('visible');
129             this.indicator.addClass('invisible');
130         }
131         
132         Roo.bootstrap.FieldLabel.register(this);
133     },
134     
135     indicatorEl : function()
136     {
137         var indicator = this.el.select('i.roo-required-indicator',true).first();
138         
139         if(!indicator){
140             return false;
141         }
142         
143         return indicator;
144         
145     },
146     
147     /**
148      * Mark this field as valid
149      */
150     markValid : function()
151     {
152         if(this.indicator){
153             this.indicator.removeClass('visible');
154             this.indicator.addClass('invisible');
155         }
156         
157         this.el.removeClass(this.invalidClass);
158         
159         this.el.addClass(this.validClass);
160         
161         this.fireEvent('valid', this);
162     },
163     
164     /**
165      * Mark this field as invalid
166      * @param {String} msg The validation message
167      */
168     markInvalid : function(msg)
169     {
170         if(this.indicator){
171             this.indicator.removeClass('invisible');
172             this.indicator.addClass('visible');
173         }
174         
175         this.el.removeClass(this.validClass);
176         
177         this.el.addClass(this.invalidClass);
178         
179         this.fireEvent('invalid', this, msg);
180     }
181     
182    
183 });
184
185 Roo.apply(Roo.bootstrap.FieldLabel, {
186     
187     groups: {},
188     
189      /**
190     * register a FieldLabel Group
191     * @param {Roo.bootstrap.FieldLabel} the FieldLabel to add
192     */
193     register : function(label)
194     {
195         if(this.groups.hasOwnProperty(label.target)){
196             return;
197         }
198      
199         this.groups[label.target] = label;
200         
201     },
202     /**
203     * fetch a FieldLabel Group based on the target
204     * @param {string} target
205     * @returns {Roo.bootstrap.FieldLabel} the CheckBox group
206     */
207     get: function(target) {
208         if (typeof(this.groups[target]) == 'undefined') {
209             return false;
210         }
211         
212         return this.groups[target] ;
213     }
214 });
215
216  
217
218