Roo/bootstrap/CheckBox.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 : 'form-group',
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         var labelWidth = this.labelWidth ? this.labelWidth *1 : 100;
73
74         var fieldLabel = {
75             tag: 'label' ,
76             //cls : 'control-label' + inline,
77             html : this.fieldLabel,
78             style : 'width:' +  labelWidth  + 'px;line-height:1;vertical-align:bottom;cursor:default;' // should be css really.
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         // normal bootstrap puts the input inside the label.
152         // however with our styled version - it has to go after the input.
153
154         //lbl.cn.push(inputblock);
155
156         var lblwrap =  {
157             tag: 'span',
158             cls: 'radio' + inline,
159             cn: [
160                 inputblock,
161                 lbl
162             ]
163         };
164
165         cfg.cn.push( lblwrap);
166
167         if(this.boxLabel){
168             lbl.cn.push({
169                 tag: 'span',
170                 html: this.boxLabel
171             })
172         }
173
174
175         return cfg;
176
177     },
178
179     initEvents : function()
180     {
181 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
182
183         this.inputEl().on('click', this.onClick,  this);
184         if (this.boxLabel) {
185             //Roo.log('find label');
186             this.el.select('span.radio label span',true).first().on('click', this.onClick,  this);
187         }
188
189     },
190
191     inputEl: function ()
192     {
193         return this.el.select('input.roo-radio',true).first();
194     },
195     onClick : function()
196     {
197         Roo.log("click");
198         this.setChecked(true);
199     },
200
201     setChecked : function(state,suppressEvent)
202     {
203         if(state){
204             Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
205                 v.dom.checked = false;
206             });
207         }
208         this.checked = state;
209         this.inputEl().dom.checked = state;
210
211         if(suppressEvent !== true){
212             this.fireEvent('check', this, state);
213         }
214         //this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
215         Roo.log('validating: '+this.name);
216         this.validate();
217     },
218
219     getGroupValue : function()
220     {
221         var value = '';
222         Roo.each(this.inputEl().up('form').select('input[name='+this.inputEl().dom.name+']', true).elements, function(v){
223             if(v.dom.checked == true){
224                 value = v.dom.value;
225             }
226         });
227
228         return value;
229     },
230
231     /**
232      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
233      * @return {Mixed} value The field value
234      */
235     getValue : function(){
236         return this.getGroupValue();
237     }
238
239 });