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         var settings=this;
148         ['xs','sm','md','lg'].map(function(size){
149             if (settings[size]) {
150                 cfg.cls += ' col-' + size + '-' + settings[size];
151             }
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().addClass('invisible');
171         
172         this.originalValue = this.getValue();
173         
174     },
175     
176     inputEl: function ()
177     {
178         return this.el.select('.roo-radio-set-input', true).first();
179     },
180     
181     getChildContainer : function()
182     {
183         return this.itemsEl;
184     },
185     
186     register : function(item)
187     {
188         this.radioes.push(item);
189         
190     },
191     
192     validate : function()
193     {   
194         var valid = false;
195         
196         Roo.each(this.radioes, function(i){
197             if(!i.checked){
198                 return;
199             }
200             
201             valid = true;
202             return false;
203         });
204         
205         if(this.allowBlank) {
206             return true;
207         }
208         
209         if(this.disabled || valid){
210             this.markValid();
211             return true;
212         }
213         
214         this.markInvalid();
215         return false;
216         
217     },
218     
219     markValid : function()
220     {
221         if(this.labelEl.isVisible(true)){
222             this.indicatorEl().hide();
223         }
224         
225         this.el.removeClass([this.invalidClass, this.validClass]);
226         this.el.addClass(this.validClass);
227         
228         this.fireEvent('valid', this);
229     },
230     
231     markInvalid : function(msg)
232     {
233         if(this.allowBlank || this.disabled){
234             return;
235         }
236         
237         if(this.labelEl.isVisible(true)){
238             this.indicatorEl().show();
239         }
240         
241         this.el.removeClass([this.invalidClass, this.validClass]);
242         this.el.addClass(this.invalidClass);
243         
244         this.fireEvent('invalid', this, msg);
245         
246     },
247     
248     setValue : function(v, suppressEvent)
249     {   
250         this.value = v;
251         if(this.rendered){
252             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
253         }
254         
255         Roo.each(this.radioes, function(i){
256             
257             i.checked = false;
258             i.el.removeClass('checked');
259             
260             if(i.value === v || i.value.toString() === v.toString()){
261                 i.checked = true;
262                 i.el.addClass('checked');
263                 
264                 if(suppressEvent !== true){
265                     this.fireEvent('check', this, i);
266                 }
267             }
268             
269         }, this);
270         
271         this.validate();
272     },
273     
274     clearInvalid : function(){
275         
276         if(!this.el || this.preventMark){
277             return;
278         }
279         
280         this.el.removeClass([this.invalidClass]);
281         
282         this.fireEvent('valid', this);
283     }
284     
285 });
286
287 Roo.apply(Roo.bootstrap.RadioSet, {
288     
289     groups: {},
290     
291     register : function(set)
292     {
293         this.groups[set.name] = set;
294     },
295     
296     get: function(name) 
297     {
298         if (typeof(this.groups[name]) == 'undefined') {
299             return false;
300         }
301         
302         return this.groups[name] ;
303     }
304     
305 });