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         var inputblock = input;
104          
105         if (this.before || this.after) {
106             
107             inputblock = {
108                 cls : 'input-group',
109                 cn :  [] 
110             };
111             
112             if (this.before) {
113                 inputblock.cn.push({
114                     tag :'span',
115                     cls : 'input-group-addon',
116                     html : this.before
117                 });
118             }
119             
120             inputblock.cn.push(input);
121             
122             if (this.after) {
123                 inputblock.cn.push({
124                     tag :'span',
125                     cls : 'input-group-addon',
126                     html : this.after
127                 });
128             }
129             
130         }
131         
132         if (align ==='left' && this.fieldLabel.length) {
133                 Roo.log("left and has label");
134                 cfg.cn = [
135                     
136                     {
137                         tag: 'label',
138                         'for' :  id,
139                         cls : 'control-label col-md-' + this.labelWidth,
140                         html : this.fieldLabel
141                         
142                     },
143                     {
144                         cls : "col-md-" + (12 - this.labelWidth), 
145                         cn: [
146                             inputblock
147                         ]
148                     }
149                     
150                 ];
151         } else if ( this.fieldLabel.length) {
152                 Roo.log(" label");
153                 cfg.cn = [
154                    
155                     {
156                         tag: this.boxLabel ? 'span' : 'label',
157                         'for': id,
158                         cls: 'control-label box-input-label',
159                         //cls : 'input-group-addon',
160                         html : this.fieldLabel
161                         
162                     },
163                     
164                     inputblock
165                     
166                 ];
167
168         } else {
169             
170                 Roo.log(" no label && no align");
171                 cfg.cn = [  inputblock ] ;
172                 
173                 
174         }
175         if(this.boxLabel){
176              var boxLabelCfg = {
177                 tag: 'label',
178                 //'for': id, // box label is handled by onclick - so no for...
179                 cls: 'box-label',
180                 html: this.boxLabel
181             }
182             
183             if(this.tooltip){
184                 boxLabelCfg.tooltip = this.tooltip;
185             }
186              
187             cfg.cn.push(boxLabelCfg);
188         }
189         
190         
191        
192         return cfg;
193         
194     },
195     
196     /**
197      * return the real input element.
198      */
199     inputEl: function ()
200     {
201         return this.el.select('input.roo-' + this.inputType,true).first();
202     },
203     
204     labelEl: function()
205     {
206         return this.el.select('label.control-label',true).first();
207     },
208     /* depricated... */
209     
210     label: function()
211     {
212         return this.labelEl();
213     },
214     
215     initEvents : function()
216     {
217 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
218         
219         this.inputEl().on('click', this.onClick,  this);
220         
221         if (this.boxLabel) { 
222             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
223         }
224         
225         this.startValue = this.getValue();
226         
227         if(this.groupId){
228             Roo.bootstrap.CheckBox.register(this);
229         }
230     },
231     
232     onClick : function()
233     {   
234         this.setChecked(!this.checked);
235     },
236     
237     setChecked : function(state,suppressEvent)
238     {
239         this.startValue = this.getValue();
240         
241         if(this.inputType == 'radio'){
242             
243             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
244                 e.dom.checked = false;
245             });
246             
247             this.inputEl().dom.checked = true;
248             
249             this.inputEl().dom.value = this.inputValue;
250             
251             if(suppressEvent !== true){
252                 this.fireEvent('check', this, true);
253             }
254             
255             return;
256         }
257         
258         this.checked = state;
259         
260         this.inputEl().dom.checked = state;
261         
262         this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
263         
264         if(suppressEvent !== true){
265             this.fireEvent('check', this, state);
266         }
267     },
268     
269     getValue : function()
270     {
271         if(this.inputType == 'radio'){
272             return this.getGroupValue();
273         }
274         
275         return this.inputEl().getValue();
276         
277     },
278     
279     getGroupValue : function()
280     {
281         if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
282             return '';
283         }
284         
285         return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
286     },
287     
288     setValue : function(v,suppressEvent)
289     {
290         if(this.inputType == 'radio'){
291             this.setGroupValue(v, suppressEvent);
292             return;
293         }
294         
295         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
296     },
297     
298     setGroupValue : function(v, suppressEvent)
299     {
300         this.startValue = this.getValue();
301         
302         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
303             e.dom.checked = false;
304             
305             if(e.dom.value == v){
306                 e.dom.checked = true;
307             }
308         });
309         
310         if(suppressEvent !== true){
311             this.fireEvent('check', this, true);
312         }
313
314         return;
315     },
316     
317     validate : function()
318     {
319         if(
320                 this.disabled || 
321                 (this.inputType == 'radio' && this.getValue().length) ||
322                 (this.inputType == 'checkbox' && this.validateGroup())
323         ){
324             this.markValid();
325             return true;
326         }
327         
328         this.markInvalid();
329         return false;
330     },
331     
332     validateGroup : function()
333     {
334         if(!this.groupId){
335             return (this.getValue() == this.inputValue) ? true : false;
336         }
337         
338         var group = Roo.bootstrap.CheckBox.get(this.groupId);
339         
340         if(!group){
341             return false;
342         }
343         
344         var r = false;
345         
346         for(var i in group){
347             if(r){
348                 break;
349             }
350             
351             r = (group[i].getValue() == group[i].inputValue) ? true : false;
352         }
353         
354         return r;
355     },
356     
357     /**
358      * Mark this field as valid
359      */
360     markValid : function(){
361         Roo.log('run marValid on checkbox');
362         if(!this.el  || this.preventMark){ // not rendered
363             return;
364         }
365         
366         this.el.removeClass([this.invalidClass, this.validClass]);
367         
368         this.el.addClass(this.validClass);
369         
370         this.fireEvent('valid', this);
371     },
372     
373      /**
374      * Mark this field as invalid
375      * @param {String} msg The validation message
376      */
377     markInvalid : function(){
378         if(!this.el  || this.preventMark){ // not rendered
379             return;
380         }
381         
382         this.el.removeClass([this.invalidClass, this.validClass]);
383         
384         this.el.addClass(this.invalidClass);
385         
386         this.fireEvent('invalid', this, msg);
387     }
388     
389 });
390
391 Roo.apply(Roo.bootstrap.CheckBox, {
392     
393     groups: {},
394     
395      /**
396     * register a CheckBox Group
397     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
398     */
399     register : function(checkbox)
400     {
401         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
402             this.groups[checkbox.groupId] = {};
403         }
404         
405         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
406             return;
407         }
408         
409         this.groups[checkbox.groupId][checkbox.name] = checkbox;
410         
411     },
412     /**
413     * fetch a CheckBox Group based on the group ID
414     * @param {string} the group ID
415     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
416     */
417     get: function(groupId) {
418         if (typeof(this.groups[groupId]) == 'undefined') {
419             return false;
420         }
421         
422         return this.groups[groupId] ;
423     }
424     
425     
426 });