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                 cls : '',
60                 cn : []
61         };
62         
63         var inline = this.inline ? ' radio-inline' : '';
64         
65         var lbl = {
66                 tag: 'label' ,
67                 // does not need for, as we wrap the input with it..
68                 'for' : id,
69                 cls : 'control-label box-label' + inline,
70                 cn : []
71         };
72         
73         var fieldLabel = {
74             tag: 'label' ,
75             cls : 'control-label' + inline,
76             html : this.fieldLabel,
77             style : 'cursor:default'
78         };
79         
80  
81         
82         
83         var input =  {
84             tag: 'input',
85             id : id,
86             type : this.inputType,
87             //value : (!this.checked) ? this.valueOff : this.inputValue,
88             value : this.inputValue,
89             cls : 'roo-radio',
90             placeholder : this.placeholder || '' // ?? needed????
91             
92         };
93         if (this.weight) { // Validity check?
94             input.cls += " radio-" + this.weight;
95         }
96         if (this.disabled) {
97             input.disabled=true;
98         }
99         
100         if(this.checked){
101             input.checked = this.checked;
102         }
103         
104         if (this.name) {
105             input.name = this.name;
106         }
107         
108         if (this.size) {
109             input.cls += ' input-' + this.size;
110         }
111         
112         //?? can span's inline have a width??
113         
114         var settings=this;
115         ['xs','sm','md','lg'].map(function(size){
116             if (settings[size]) {
117                 cfg.cls += ' col-' + size + '-' + settings[size];
118             }
119         });
120         
121         var inputblock = input;
122         
123         if (this.before || this.after) {
124             
125             inputblock = {
126                 cls : 'input-group',
127                 tag : 'span',
128                 cn :  [] 
129             };
130             if (this.before) {
131                 inputblock.cn.push({
132                     tag :'span',
133                     cls : 'input-group-addon',
134                     html : this.before
135                 });
136             }
137             inputblock.cn.push(input);
138             if (this.after) {
139                 inputblock.cn.push({
140                     tag :'span',
141                     cls : 'input-group-addon',
142                     html : this.after
143                 });
144             }
145             
146         };
147         
148         
149         if (this.fieldLabel && this.fieldLabel.length) {
150             cfg.cn.push(fieldLabel);
151         }
152        
153         lbl.cn.push(inputblock);
154         cfg.cn.push( {
155             tag: 'span',
156             cls: 'radio' + inline,
157             cn: [
158                 lbl
159             ]
160         });
161         
162         if(this.boxLabel){
163             lbl.cn.push({
164                 tag: 'span',
165                 html: this.boxLabel
166             })
167         }
168          
169         
170         return cfg;
171         
172     },
173     
174     initEvents : function()
175     {
176 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
177         
178         this.inputEl().on('click', this.onClick,  this);
179         if (this.boxLabel) {
180             Roo.log('find label')
181             this.el.select('span.radio label span',true).first().on('click', this.onClick,  this);
182         }
183         
184     },
185     
186     inputEl: function ()
187     {
188         return this.el.select('input.roo-radio',true).first();
189     },
190     onClick : function()
191     {   
192         Roo.log("click");
193         this.setChecked(true);
194     },
195     
196     setChecked : function(state,suppressEvent)
197     {
198         if(state){
199             Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
200                 v.dom.checked = false;
201             });
202         }
203         Roo.log(this.inputEl().dom);
204         this.checked = state;
205         this.inputEl().dom.checked = state;
206         
207         if(suppressEvent !== true){
208             this.fireEvent('check', this, state);
209         }
210         
211         //this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
212         
213     },
214     
215     getGroupValue : function()
216     {
217         var value = ''
218         Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
219             if(v.dom.checked == true){
220                 value = v.dom.value;
221             }
222         });
223         
224         return value;
225     },
226     
227     /**
228      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
229      * @return {Mixed} value The field value
230      */
231     getValue : function(){
232         return this.getGroupValue();
233     }
234     
235 });
236
237