Roo/bootstrap/Radio.js
[roojs1] / Roo / bootstrap / Radio.js
1 /*
2  * - LGPL
3  *
4  * Radio
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Radio
10  * @extends Roo.bootstrap.CheckBox
11  * Bootstrap Radio class
12
13  * @constructor
14  * Create a new Radio
15  * @param {Object} config The config object
16  */
17
18 Roo.bootstrap.Radio = function(config){
19     Roo.bootstrap.Radio.superclass.constructor.call(this, config);
20    
21 };
22
23 Roo.extend(Roo.bootstrap.Radio, Roo.bootstrap.CheckBox,  {
24     
25     inputType: 'radio',
26     inputValue: '',
27     valueOff: '',
28     
29     getAutoCreate : function()
30     {
31         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
32         
33         var id = Roo.id();
34         
35         var cfg = {};
36         
37         cfg.cls = 'form-group' //input-group
38         
39         var input =  {
40             tag: 'input',
41             id : id,
42             type : this.inputType,
43             value : (!this.checked) ? this.valueOff : this.inputValue,
44             cls : 'form-box',
45             placeholder : this.placeholder || ''
46             
47         };
48         
49         if (this.disabled) {
50             input.disabled=true;
51         }
52         
53         if(this.checked){
54             input.checked = this.checked;
55         }
56         
57         if (this.name) {
58             input.name = this.name;
59         }
60         
61         if (this.size) {
62             input.cls += ' input-' + this.size;
63         }
64         
65         var settings=this;
66         ['xs','sm','md','lg'].map(function(size){
67             if (settings[size]) {
68                 cfg.cls += ' col-' + size + '-' + settings[size];
69             }
70         });
71         
72         var inputblock = input;
73         
74         if (this.before || this.after) {
75             
76             inputblock = {
77                 cls : 'input-group',
78                 cn :  [] 
79             };
80             if (this.before) {
81                 inputblock.cn.push({
82                     tag :'span',
83                     cls : 'input-group-addon',
84                     html : this.before
85                 });
86             }
87             inputblock.cn.push(input);
88             if (this.after) {
89                 inputblock.cn.push({
90                     tag :'span',
91                     cls : 'input-group-addon',
92                     html : this.after
93                 });
94             }
95             
96         };
97         
98         if (align ==='left' && this.fieldLabel.length) {
99                 Roo.log("left and has label");
100                 cfg.cn = [
101                     
102                     {
103                         tag: 'label',
104                         'for' :  id,
105                         cls : 'control-label col-md-' + this.labelWidth,
106                         html : this.fieldLabel
107                         
108                     },
109                     {
110                         cls : "col-md-" + (12 - this.labelWidth), 
111                         cn: [
112                             inputblock
113                         ]
114                     }
115                     
116                 ];
117         } else if ( this.fieldLabel.length) {
118                 Roo.log(" label");
119                  cfg.cn = [
120                    
121                     {
122                         tag: 'label',
123                         'for': id,
124                         cls: 'control-label box-input-label',
125                         //cls : 'input-group-addon',
126                         html : this.fieldLabel
127                         
128                     },
129                     
130                     inputblock
131                     
132                 ];
133
134         } else {
135             
136                    Roo.log(" no label && no align");
137                 cfg.cn = [
138                     
139                         inputblock
140                     
141                 ];
142                 
143                 
144         };
145         
146         if(this.boxLabel){
147             cfg.cn.push({
148                 tag: 'label',
149                 'for': id,
150                 cls: 'box-label',
151                 html: this.boxLabel
152             })
153         }
154         
155         return cfg;
156         
157     },
158    
159     onClick : function()
160     {   
161         this.setChecked(true);
162     },
163     
164     setChecked : function(state,suppressEvent)
165     {
166         Roo.log('radio');
167         Roo.log(this.boxLabel);
168         Roo.log(state);
169         if(state){
170             Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
171                 v.dom.checked = false;
172             });
173         }
174         
175         this.checked = state;
176         this.inputEl().dom.checked = state;
177         
178         if(suppressEvent !== true){
179             this.fireEvent('check', this, state);
180         }
181         
182         this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
183         
184     },
185     
186     getGroupValue : function()
187     {
188         var value = ''
189         Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
190             if(v.dom.checked == true){
191                 value = v.dom.value;
192             }
193         });
194         
195         return value;
196     },
197     
198     /**
199      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
200      * @return {Mixed} value The field value
201      */
202     getValue : function(){
203         return this.getGroupValue();
204     },
205     
206     setValue : function(v,suppressEvent)
207     {
208         Roo.log('setValue');
209         Roo.log(v);
210         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
211     }
212     
213 });
214
215