Roo/bootstrap/CheckBox.js
[roojs1] / Roo / bootstrap / CheckBox.js
1 /*
2  * - LGPL
3  *
4  * CheckBox
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.CheckBox
10  * @extends Roo.bootstrap.Input
11  * Bootstrap CheckBox class
12  * 
13  * @cfg {String} valueOff The value that should go into the generated input element's value when unchecked.
14  * @cfg {String} inputValue The value that should go into the generated input element's value when checked.
15  * @cfg {String} boxLabel The text that appears beside the checkbox
16  * @cfg {String} weight (primary|warning|info|danger|success) The text that appears beside the checkbox
17  * @cfg {Boolean} checked initnal the element
18  * @cfg {Boolean} inline inline the element (default false)
19  * @cfg {String} groupId the checkbox group id // normal just use for checkbox
20  * 
21  * @constructor
22  * Create a new CheckBox
23  * @param {Object} config The config object
24  */
25
26 Roo.bootstrap.CheckBox = function(config){
27     Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
28    
29     this.addEvents({
30         /**
31         * @event check
32         * Fires when the element is checked or unchecked.
33         * @param {Roo.bootstrap.CheckBox} this This input
34         * @param {Boolean} checked The new checked value
35         */
36        check : true
37     });
38     
39 };
40
41 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
42     
43     inputType: 'checkbox',
44     inputValue: 1,
45     valueOff: 0,
46     boxLabel: false,
47     checked: false,
48     weight : false,
49     inline: false,
50     
51     getAutoCreate : function()
52     {
53         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
54         
55         var id = Roo.id();
56         
57         var cfg = {};
58         
59         cfg.cls = 'form-group ' + this.inputType; //input-group
60         
61         if(this.inline){
62             cfg.cls += ' ' + this.inputType + '-inline';
63         }
64         
65         var input =  {
66             tag: 'input',
67             id : id,
68             type : this.inputType,
69             value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
70             cls : 'roo-' + this.inputType, //'form-box',
71             placeholder : this.placeholder || ''
72             
73         };
74         
75         if (this.weight) { // Validity check?
76             cfg.cls += " " + this.inputType + "-" + this.weight;
77         }
78         
79         if (this.disabled) {
80             input.disabled=true;
81         }
82         
83         if(this.checked){
84             input.checked = this.checked;
85         }
86         
87         if (this.name) {
88             input.name = this.name;
89         }
90         
91         if (this.size) {
92             input.cls += ' input-' + this.size;
93         }
94         
95         var settings=this;
96         
97         ['xs','sm','md','lg'].map(function(size){
98             if (settings[size]) {
99                 cfg.cls += ' col-' + size + '-' + settings[size];
100             }
101         });
102         
103        
104         
105         var inputblock = input;
106         
107         
108         
109         
110         if (this.before || this.after) {
111             
112             inputblock = {
113                 cls : 'input-group',
114                 cn :  [] 
115             };
116             
117             if (this.before) {
118                 inputblock.cn.push({
119                     tag :'span',
120                     cls : 'input-group-addon',
121                     html : this.before
122                 });
123             }
124             
125             inputblock.cn.push(input);
126             
127             if (this.after) {
128                 inputblock.cn.push({
129                     tag :'span',
130                     cls : 'input-group-addon',
131                     html : this.after
132                 });
133             }
134             
135         }
136         
137         if (align ==='left' && this.fieldLabel.length) {
138                 Roo.log("left and has label");
139                 cfg.cn = [
140                     
141                     {
142                         tag: 'label',
143                         'for' :  id,
144                         cls : 'control-label col-md-' + this.labelWidth,
145                         html : this.fieldLabel
146                         
147                     },
148                     {
149                         cls : "col-md-" + (12 - this.labelWidth), 
150                         cn: [
151                             inputblock
152                         ]
153                     }
154                     
155                 ];
156         } else if ( this.fieldLabel.length) {
157                 Roo.log(" label");
158                 cfg.cn = [
159                    
160                     {
161                         tag: this.boxLabel ? 'span' : 'label',
162                         'for': id,
163                         cls: 'control-label box-input-label',
164                         //cls : 'input-group-addon',
165                         html : this.fieldLabel
166                         
167                     },
168                     
169                     inputblock
170                     
171                 ];
172
173         } else {
174             
175                 Roo.log(" no label && no align");
176                 cfg.cn = [  inputblock ] ;
177                 
178                 
179         }
180         if(this.boxLabel){
181              var boxLabelCfg = {
182                 tag: 'label',
183                 //'for': id, // box label is handled by onclick - so no for...
184                 cls: 'box-label',
185                 html: this.boxLabel
186             }
187             
188             if(this.tooltip){
189                 boxLabelCfg.tooltip = this.tooltip;
190             }
191              
192             cfg.cn.push(boxLabelCfg);
193         }
194         
195         
196        
197         return cfg;
198         
199     },
200     
201     /**
202      * return the real input element.
203      */
204     inputEl: function ()
205     {
206         return this.el.select('input.roo-' + this.inputType,true).first();
207     },
208     
209     labelEl: function()
210     {
211         return this.el.select('label.control-label',true).first();
212     },
213     /* depricated... */
214     
215     label: function()
216     {
217         return this.labelEl();
218     },
219     
220     initEvents : function()
221     {
222 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
223         
224         if(this.groupId){
225             Roo.bootstrap.CheckBox.register(this);
226         }
227     
228         this.inputEl().on('click', this.onClick,  this);
229         
230         if (this.boxLabel) { 
231             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
232         }
233         
234         this.startValue = this.getValue();
235         
236     },
237     
238     onClick : function()
239     {   
240         this.setChecked(!this.checked);
241     },
242     
243     setChecked : function(state,suppressEvent)
244     {
245         this.startValue = this.getValue();
246         
247         if(this.inputType == 'radio'){
248             
249             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
250                 e.dom.checked = false;
251             });
252             
253             this.inputEl().dom.checked = true;
254             
255             this.inputEl().dom.value = this.inputValue;
256             
257             if(suppressEvent !== true){
258                 this.fireEvent('check', this, true);
259             }
260             
261             return;
262         }
263         
264         this.checked = state;
265         
266         this.inputEl().dom.checked = state;
267         
268         this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
269         
270         if(suppressEvent !== true){
271             this.fireEvent('check', this, state);
272         }
273     },
274     
275     getValue : function()
276     {
277         if(this.inputType == 'radio'){
278             return this.getGroupValue();
279         }
280         
281         return this.inputEl().getValue();
282         
283     },
284     
285     getGroupValue : function()
286     {
287         if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
288             return '';
289         }
290         
291         return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
292     },
293     
294     setValue : function(v,suppressEvent)
295     {
296         if(this.inputType == 'radio'){
297             this.setGroupValue(v, suppressEvent);
298             return;
299         }
300         
301         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
302     },
303     
304     setGroupValue : function(v, suppressEvent)
305     {
306         this.startValue = this.getValue();
307         
308         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
309             e.dom.checked = false;
310             
311             if(e.dom.value == v){
312                 e.dom.checked = true;
313             }
314         });
315         
316         if(suppressEvent !== true){
317             this.fireEvent('check', this, true);
318         }
319
320         return;
321     },
322     
323     validate : function()
324     {
325         if(
326                 this.disabled || 
327                 (this.inputType == 'radio' && this.getValue().length) ||
328                 (this.inputType == 'checkbox' && this.getValue() == this.inputValue)
329         ){
330             return true;
331         }
332         
333         return false;
334     }
335     
336 });
337
338 Roo.apply(Roo.bootstrap.CheckBox, {
339     
340     groups: {},
341     
342      /**
343     * register a CheckBox Group
344     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
345     */
346     register : function(checkbox)
347     {
348         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
349             this.groups[checkbox.groupId] = {};
350         }
351         
352         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
353             return;
354         }
355         
356         this.groups[checkbox.groupId][checkbox.name] = checkbox;
357         
358     },
359     /**
360     * fetch a CheckBox Group based on the group ID
361     * @param {string} the group ID
362     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
363     */
364     get: function(groupId) {
365         if (typeof(this.groups[groupId]) == 'undefined') {
366             return false;
367         }
368         
369         return this.groups[groupId] ;
370     }
371     
372     
373 });