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:pointer'
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             Roo.log(this.el.select('label.box-label',true).first());
182             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
183         }
184         
185     },
186     
187     inputEl: function ()
188     {
189         return this.el.select('input.roo-radio',true).first();
190     },
191     onClick : function()
192     {   
193         Roo.log("click");
194         Roo.log("click");
195         this.setChecked(true);
196     },
197     
198     setChecked : function(state,suppressEvent)
199     {
200         if(state){
201             Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
202                 v.dom.checked = false;
203             });
204         }
205         
206         this.checked = state;
207         this.inputEl().dom.checked = state;
208         
209         if(suppressEvent !== true){
210             this.fireEvent('check', this, state);
211         }
212         
213         //this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
214         
215     },
216     
217     getGroupValue : function()
218     {
219         var value = ''
220         Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
221             if(v.dom.checked == true){
222                 value = v.dom.value;
223             }
224         });
225         
226         return value;
227     },
228     
229     /**
230      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
231      * @return {Mixed} value The field value
232      */
233     getValue : function(){
234         return this.getGroupValue();
235     }
236     
237 });
238
239