Roo/bootstrap/RadioSet.js
[roojs1] / Roo / bootstrap / RadioSet.js
1 /*
2  * - LGPL
3  *
4  * RadioSet
5  *
6  *
7  */
8
9 /**
10  * @class Roo.bootstrap.RadioSet
11  * @extends Roo.bootstrap.Input
12  * Bootstrap RadioSet class
13  * @cfg {String} indicatorpos (left|right) default left
14  * @cfg {Boolean} inline (true|false) inline the element (default true)
15  * @cfg {String} weight (primary|warning|info|danger|success) The text that appears beside the radio
16  * @constructor
17  * Create a new RadioSet
18  * @param {Object} config The config object
19  */
20
21 Roo.bootstrap.RadioSet = function(config){
22     
23     Roo.bootstrap.RadioSet.superclass.constructor.call(this, config);
24     
25     this.radioes = [];
26     
27     Roo.bootstrap.RadioSet.register(this);
28     
29     this.addEvents({
30         /**
31         * @event check
32         * Fires when the element is checked or unchecked.
33         * @param {Roo.bootstrap.RadioSet} this This radio
34         * @param {Roo.bootstrap.Radio} item The checked item
35         */
36        check : true
37     });
38     
39 };
40
41 Roo.extend(Roo.bootstrap.RadioSet, Roo.bootstrap.Input,  {
42
43     radioes : false,
44     
45     inline : true,
46     
47     weight : '',
48     
49     fieldLabel : '',
50     
51     indicatorpos : 'left',
52     
53     labellg : 0,
54     labelmd : 0,
55     labelsm : 0,
56     labelxs : 0,
57     
58     getAutoCreate : function()
59     {
60         var label = {
61             tag : 'label',
62             cls : 'roo-radio-set-label',
63             cn : [
64                 {
65                     tag : 'span',
66                     html : this.fieldLabel
67                 }
68             ]
69         };
70         
71         if(this.indicatorpos == 'left'){
72             label.cn.unshift({
73                 tag : 'i',
74                 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
75                 tooltip : 'This field is required'
76             });
77         } else {
78             label.cn.push({
79                 tag : 'i',
80                 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
81                 tooltip : 'This field is required'
82             });
83         }
84         
85         var items = {
86             tag : 'div',
87             cls : 'roo-radio-set-items'
88         };
89         
90         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
91         
92         if (align === 'left' && this.fieldLabel.length) {
93             
94             items = {
95                 cls : "roo-radio-set-right", 
96                 cn: [
97                     items
98                 ]
99             };
100             
101             if(this.labelWidth > 12){
102                 label.style = "width: " + this.labelWidth + 'px';
103             }
104             
105             if(this.labelWidth < 13 && this.labelmd == 0){
106                 this.labelmd = this.labelWidth;
107             }
108             
109             if(this.labellg > 0){
110                 label.cls += ' col-lg-' + this.labellg;
111                 items.cls += ' col-lg-' + (12 - this.labellg);
112             }
113             
114             if(this.labelmd > 0){
115                 label.cls += ' col-md-' + this.labelmd;
116                 items.cls += ' col-md-' + (12 - this.labelmd);
117             }
118             
119             if(this.labelsm > 0){
120                 label.cls += ' col-sm-' + this.labelsm;
121                 items.cls += ' col-sm-' + (12 - this.labelsm);
122             }
123             
124             if(this.labelxs > 0){
125                 label.cls += ' col-xs-' + this.labelxs;
126                 items.cls += ' col-xs-' + (12 - this.labelxs);
127             }
128         }
129         
130         var cfg = {
131             tag : 'div',
132             cls : 'roo-radio-set',
133             cn : [
134                 {
135                     tag : 'input',
136                     cls : 'roo-radio-set-input',
137                     type : 'text',
138                     name : this.name,
139                     value : this.value ? this.value :  ''
140                 },
141                 label,
142                 items
143             ]
144         };
145         
146         if(this.weight.length){
147             cfg.cls += ' roo-radio-' + this.weight;
148         }
149         
150         if(this.inline) {
151             cfg.cls += ' roo-radio-set-inline';
152         }
153         
154         return cfg;
155         
156     },
157
158     initEvents : function()
159     {
160         this.labelEl = this.el.select('.roo-radio-set-label', true).first();
161         this.labelEl.setVisibilityMode(Roo.Element.DISPLAY);
162         
163         if(!this.fieldLabel.length){
164             this.labelEl.hide();
165         }
166         
167         this.itemsEl = this.el.select('.roo-radio-set-items', true).first();
168         this.itemsEl.setVisibilityMode(Roo.Element.DISPLAY);
169         
170         this.indicatorEl().setVisibilityMode(Roo.Element.DISPLAY);
171         this.indicatorEl().hide();
172         
173         this.originalValue = this.getValue();
174         
175     },
176     
177     inputEl: function ()
178     {
179         return this.el.select('.roo-radio-set-input', true).first();
180     },
181     
182     getChildContainer : function()
183     {
184         return this.itemsEl;
185     },
186     
187     register : function(item)
188     {
189         this.radioes.push(item);
190         
191     },
192     
193     validate : function()
194     {   
195         var valid = false;
196         
197         Roo.each(this.radioes, function(i){
198             if(!i.checked){
199                 return;
200             }
201             
202             valid = true;
203             return false;
204         });
205         
206         if(this.disabled || this.allowBlank || valid){
207             this.markValid();
208             return true;
209         }
210         
211         this.markInvalid();
212         return false;
213         
214     },
215     
216     markValid : function()
217     {
218         if(this.labelEl.isVisible(true)){
219             this.indicatorEl().hide();
220         }
221         
222         this.el.removeClass([this.invalidClass, this.validClass]);
223         this.el.addClass(this.validClass);
224         
225         this.fireEvent('valid', this);
226     },
227     
228     markInvalid : function(msg)
229     {
230         if(this.allowBlank || this.disabled){
231             return;
232         }
233         
234         if(this.labelEl.isVisible(true)){
235             this.indicatorEl().show();
236         }
237         
238         this.el.removeClass([this.invalidClass, this.validClass]);
239         this.el.addClass(this.invalidClass);
240         
241         this.fireEvent('invalid', this, msg);
242         
243     },
244     
245     setValue : function(v, suppressEvent)
246     {   
247         Roo.each(this.radioes, function(i){
248             
249             i.checked = false;
250             i.el.removeClass('checked');
251             
252             if(i.value === v || i.value.toString() === v.toString()){
253                 i.checked = true;
254                 i.el.addClass('checked');
255                 
256                 if(suppressEvent !== true){
257                     this.fireEvent('check', this, i);
258                 }
259             }
260             
261         }, this);
262         
263         Roo.bootstrap.RadioSet.superclass.setValue.call(this, v);
264         
265     },
266     
267     clearInvalid : function(){
268         
269         if(!this.el || this.preventMark){
270             return;
271         }
272         
273         if(this.labelEl.isVisible(true)){
274             this.indicatorEl().hide();
275         }
276         
277         this.el.removeClass([this.invalidClass]);
278         
279         this.fireEvent('valid', this);
280     }
281     
282 });
283
284 Roo.apply(Roo.bootstrap.RadioSet, {
285     
286     groups: {},
287     
288     register : function(set)
289     {
290         this.groups[set.name] = set;
291     },
292     
293     get: function(name) 
294     {
295         if (typeof(this.groups[name]) == 'undefined') {
296             return false;
297         }
298         
299         return this.groups[name] ;
300     }
301     
302 });