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     getAutoCreate : function()
54     {
55         var label = {
56             tag : 'label',
57             cls : 'roo-radio-set-label',
58             cn : [
59                 {
60                     tag : 'span',
61                     html : this.fieldLabel
62                 }
63             ]
64         };
65         
66         if(this.indicatorpos == 'left'){
67             label.cn.unshift({
68                 tag : 'i',
69                 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
70                 tooltip : 'This field is required'
71             });
72         } else {
73             label.cn.push({
74                 tag : 'i',
75                 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
76                 tooltip : 'This field is required'
77             });
78         }
79         
80         var items = {
81             tag : 'div',
82             cls : 'roo-radio-set-items'
83         };
84         
85         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
86         
87         if (align === 'left' && this.fieldLabel.length) {
88             
89             items = {
90                 cls : "roo-radio-set-right", 
91                 cn: [
92                     items
93                 ]
94             };
95             
96             Roo.log(this.labelmd);
97             
98             if(this.labelWidth > 12){
99                 label.style = "width: " + this.labelWidth + 'px';
100             }
101             
102             if(this.labelWidth < 13 && this.labelmd == 0){
103                 this.labelmd = this.labelWidth;
104             }
105             
106             if(this.labellg > 0){
107                 label.cls += ' col-lg-' + this.labellg;
108                 items.cls += ' col-lg-' + (12 - this.labellg);
109             }
110             
111             if(this.labelmd > 0){
112                 label.cls += ' col-md-' + this.labelmd;
113                 items.cls += ' col-md-' + (12 - this.labelmd);
114             }
115             
116             if(this.labelsm > 0){
117                 label.cls += ' col-sm-' + this.labelsm;
118                 items.cls += ' col-sm-' + (12 - this.labelsm);
119             }
120             
121             if(this.labelxs > 0){
122                 label.cls += ' col-xs-' + this.labelxs;
123                 items.cls += ' col-xs-' + (12 - this.labelxs);
124             }
125         }
126         
127         var cfg = {
128             tag : 'div',
129             cls : 'roo-radio-set',
130             cn : [
131                 {
132                     tag : 'input',
133                     cls : 'roo-radio-set-input',
134                     type : 'text',
135                     name : this.name,
136                     value : this.value ? this.value :  ''
137                 },
138                 label,
139                 items
140             ]
141         };
142         
143         if(this.weight.length){
144             cfg.cls += ' roo-radio-' + this.weight;
145         }
146         
147         if(this.inline) {
148             cfg.cls += ' roo-radio-set-inline';
149         }
150         
151         return cfg;
152         
153     },
154
155     initEvents : function()
156     {
157         this.labelEl = this.el.select('.roo-radio-set-label', true).first();
158         this.labelEl.setVisibilityMode(Roo.Element.DISPLAY);
159         
160         if(!this.fieldLabel.length){
161             this.labelEl.hide();
162         }
163         
164         this.itemsEl = this.el.select('.roo-radio-set-items', true).first();
165         this.itemsEl.setVisibilityMode(Roo.Element.DISPLAY);
166         
167         this.indicatorEl().setVisibilityMode(Roo.Element.DISPLAY);
168         this.indicatorEl().hide();
169         
170         this.originalValue = this.getValue();
171         
172     },
173     
174     inputEl: function ()
175     {
176         return this.el.select('.roo-radio-set-input', true).first();
177     },
178     
179     getChildContainer : function()
180     {
181         return this.itemsEl;
182     },
183     
184     register : function(item)
185     {
186         this.radioes.push(item);
187         
188     },
189     
190     validate : function()
191     {   
192         var valid = false;
193         
194         Roo.each(this.radioes, function(i){
195             if(!i.checked){
196                 return;
197             }
198             
199             valid = true;
200             return false;
201         });
202         
203         if(this.disabled || this.allowBlank || 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         if(this.labelEl.isVisible(true)){
271             this.indicatorEl().hide();
272         }
273         
274         this.el.removeClass([this.invalidClass]);
275         
276         this.fireEvent('valid', this);
277     }
278     
279 });
280
281 Roo.apply(Roo.bootstrap.RadioSet, {
282     
283     groups: {},
284     
285     register : function(set)
286     {
287         this.groups[set.name] = set;
288     },
289     
290     get: function(name) 
291     {
292         if (typeof(this.groups[name]) == 'undefined') {
293             return false;
294         }
295         
296         return this.groups[name] ;
297     }
298     
299 });