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