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