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