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