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.inputValue,
70             cls : 'roo-' + this.inputType, //'form-box',
71             placeholder : this.placeholder || ''
72             
73         };
74         
75         if(this.inputType != 'radio'){
76             var hidden =  {
77                 tag: 'input',
78                 type : 'hidden',
79                 cls : 'roo-hidden-value',
80                 value : this.checked ? this.valueOff : this.inputValue
81             };
82         }
83         
84             
85         if (this.weight) { // Validity check?
86             cfg.cls += " " + this.inputType + "-" + this.weight;
87         }
88         
89         if (this.disabled) {
90             input.disabled=true;
91         }
92         
93         if(this.checked){
94             input.checked = this.checked;
95             
96         }
97         
98         
99         if (this.name) {
100             
101             input.name = this.name;
102             
103             if(this.inputType != 'radio'){
104                 hidden.name = this.name;
105                 input.name = '_hidden_' + this.name;
106             }
107         }
108         
109         if (this.size) {
110             input.cls += ' input-' + this.size;
111         }
112         
113         var settings=this;
114         
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                 cn :  [] 
128             };
129             
130             if (this.before) {
131                 inputblock.cn.push({
132                     tag :'span',
133                     cls : 'input-group-addon',
134                     html : this.before
135                 });
136             }
137             
138             inputblock.cn.push(input);
139             
140             if(this.inputType != 'radio'){
141                 inputblock.cn.push(hidden);
142             }
143             
144             if (this.after) {
145                 inputblock.cn.push({
146                     tag :'span',
147                     cls : 'input-group-addon',
148                     html : this.after
149                 });
150             }
151             
152         }
153         
154         if (align ==='left' && this.fieldLabel.length) {
155 //                Roo.log("left and has label");
156             cfg.cn = [
157                 {
158                     tag: 'label',
159                     'for' :  id,
160                     cls : 'control-label',
161                     html : this.fieldLabel
162
163                 },
164                 {
165                     cls : "", 
166                     cn: [
167                         inputblock
168                     ]
169                 }
170             ];
171             
172             if(this.labelWidth > 12){
173                 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
174             }
175             
176             if(this.labelWidth < 13 && this.labelmd == 0){
177                 this.labelmd = this.labelWidth;
178             }
179             
180             if(this.labellg > 0){
181                 cfg.cn[0].cls += ' col-lg-' + this.labellg;
182                 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
183             }
184             
185             if(this.labelmd > 0){
186                 cfg.cn[0].cls += ' col-md-' + this.labelmd;
187                 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
188             }
189             
190             if(this.labelsm > 0){
191                 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
192                 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
193             }
194             
195             if(this.labelxs > 0){
196                 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
197                 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
198             }
199             
200         } else if ( this.fieldLabel.length) {
201 //                Roo.log(" label");
202                 cfg.cn = [
203                    
204                     {
205                         tag: this.boxLabel ? 'span' : 'label',
206                         'for': id,
207                         cls: 'control-label box-input-label',
208                         //cls : 'input-group-addon',
209                         html : this.fieldLabel
210                         
211                     },
212                     
213                     inputblock
214                     
215                 ];
216
217         } else {
218             
219 //                Roo.log(" no label && no align");
220                 cfg.cn = [  inputblock ] ;
221                 
222                 
223         }
224         
225         if(this.boxLabel){
226              var boxLabelCfg = {
227                 tag: 'label',
228                 //'for': id, // box label is handled by onclick - so no for...
229                 cls: 'box-label',
230                 html: this.boxLabel
231             };
232             
233             if(this.tooltip){
234                 boxLabelCfg.tooltip = this.tooltip;
235             }
236              
237             cfg.cn.push(boxLabelCfg);
238         }
239         
240         if(this.inputType != 'radio'){
241             cfg.cn.push(hidden);
242         }
243         
244         return cfg;
245         
246     },
247     
248     /**
249      * return the real input element.
250      */
251     inputEl: function ()
252     {
253         return this.el.select('input.roo-' + this.inputType,true).first();
254     },
255     hiddenEl: function ()
256     {
257         return this.el.select('input.roo-hidden-value',true).first();
258     },
259     
260     labelEl: function()
261     {
262         return this.el.select('label.control-label',true).first();
263     },
264     /* depricated... */
265     
266     label: function()
267     {
268         return this.labelEl();
269     },
270     
271     boxLabelEl: function()
272     {
273         return this.el.select('label.box-label',true).first();
274     },
275     
276     initEvents : function()
277     {
278 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
279         
280         this.inputEl().on('click', this.onClick,  this);
281         
282         if (this.boxLabel) { 
283             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
284         }
285         
286         this.startValue = this.getValue();
287         
288         if(this.groupId){
289             Roo.bootstrap.CheckBox.register(this);
290         }
291     },
292     
293     onClick : function()
294     {   
295         this.setChecked(!this.checked);
296     },
297     
298     setChecked : function(state,suppressEvent)
299     {
300         this.startValue = this.getValue();
301
302         if(this.inputType == 'radio'){
303             
304             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
305                 e.dom.checked = false;
306             });
307             
308             this.inputEl().dom.checked = true;
309             
310             this.inputEl().dom.value = this.inputValue;
311             
312             if(suppressEvent !== true){
313                 this.fireEvent('check', this, true);
314             }
315             
316             this.validate();
317             
318             return;
319         }
320         
321         this.checked = state;
322         
323         this.inputEl().dom.checked = state;
324         
325         
326         this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
327         
328         if(suppressEvent !== true){
329             this.fireEvent('check', this, state);
330         }
331         
332         this.validate();
333     },
334     
335     getValue : function()
336     {
337         if(this.inputType == 'radio'){
338             return this.getGroupValue();
339         }
340         
341         return this.hiddenEl().dom.value;
342         
343     },
344     
345     getGroupValue : function()
346     {
347         if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
348             return '';
349         }
350         
351         return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
352     },
353     
354     setValue : function(v,suppressEvent)
355     {
356         if(this.inputType == 'radio'){
357             this.setGroupValue(v, suppressEvent);
358             return;
359         }
360         
361         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
362         
363         this.validate();
364     },
365     
366     setGroupValue : function(v, suppressEvent)
367     {
368         this.startValue = this.getValue();
369         
370         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
371             e.dom.checked = false;
372             
373             if(e.dom.value == v){
374                 e.dom.checked = true;
375             }
376         });
377         
378         if(suppressEvent !== true){
379             this.fireEvent('check', this, true);
380         }
381
382         this.validate();
383         
384         return;
385     },
386     
387     validate : function()
388     {
389         if(
390                 this.disabled || 
391                 (this.inputType == 'radio' && this.validateRadio()) ||
392                 (this.inputType == 'checkbox' && this.validateCheckbox())
393         ){
394             this.markValid();
395             return true;
396         }
397         
398         this.markInvalid();
399         return false;
400     },
401     
402     validateRadio : function()
403     {
404         if(this.allowBlank){
405             return true;
406         }
407         
408         var valid = false;
409         
410         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
411             if(!e.dom.checked){
412                 return;
413             }
414             
415             valid = true;
416             
417             return false;
418         });
419         
420         return valid;
421     },
422     
423     validateCheckbox : function()
424     {
425         if(!this.groupId){
426             return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
427         }
428         
429         var group = Roo.bootstrap.CheckBox.get(this.groupId);
430         
431         if(!group){
432             return false;
433         }
434         
435         var r = false;
436         
437         for(var i in group){
438             if(r){
439                 break;
440             }
441             
442             r = (group[i].getValue() == group[i].inputValue) ? true : false;
443         }
444         
445         return r;
446     },
447     
448     /**
449      * Mark this field as valid
450      */
451     markValid : function()
452     {
453         var _this = this;
454         
455         this.fireEvent('valid', this);
456         
457         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
458         
459         if(this.groupId){
460             label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
461         }
462         
463         if(label){
464             label.markValid();
465         }
466
467         if(this.inputType == 'radio'){
468             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
469                 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
470                 e.findParent('.form-group', false, true).addClass(_this.validClass);
471             });
472             
473             return;
474         }
475
476         if(!this.groupId){
477             this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
478             this.el.findParent('.form-group', false, true).addClass(this.validClass);
479             return;
480         }
481         
482         var group = Roo.bootstrap.CheckBox.get(this.groupId);
483         
484         if(!group){
485             return;
486         }
487         
488         for(var i in group){
489             group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
490             group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
491         }
492     },
493     
494      /**
495      * Mark this field as invalid
496      * @param {String} msg The validation message
497      */
498     markInvalid : function(msg)
499     {
500         if(this.allowBlank){
501             return;
502         }
503         
504         var _this = this;
505         
506         this.fireEvent('invalid', this, msg);
507         
508         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
509         
510         if(this.groupId){
511             label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
512         }
513         
514         if(label){
515             label.markInvalid();
516         }
517             
518         if(this.inputType == 'radio'){
519             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
520                 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
521                 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
522             });
523             
524             return;
525         }
526         
527         if(!this.groupId){
528             this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
529             this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
530             return;
531         }
532         
533         var group = Roo.bootstrap.CheckBox.get(this.groupId);
534         
535         if(!group){
536             return;
537         }
538         
539         for(var i in group){
540             group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
541             group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
542         }
543         
544     },
545     
546     clearInvalid : function()
547     {
548         Roo.bootstrap.Input.prototype.clearInvalid.call(this);
549         
550         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
551         
552         if (label) {
553             label.iconEl.removeClass(label.validClass);
554             label.iconEl.removeClass(label.invalidClass);
555         }
556     },
557     
558     disable : function()
559     {
560         if(this.inputType != 'radio'){
561             Roo.bootstrap.CheckBox.superclass.disable.call(this);
562             return;
563         }
564         
565         var _this = this;
566         
567         if(this.rendered){
568             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
569                 _this.getActionEl().addClass(this.disabledClass);
570                 e.dom.disabled = true;
571             });
572         }
573         
574         this.disabled = true;
575         this.fireEvent("disable", this);
576         return this;
577     },
578
579     enable : function()
580     {
581         if(this.inputType != 'radio'){
582             Roo.bootstrap.CheckBox.superclass.enable.call(this);
583             return;
584         }
585         
586         var _this = this;
587         
588         if(this.rendered){
589             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
590                 _this.getActionEl().removeClass(this.disabledClass);
591                 e.dom.disabled = false;
592             });
593         }
594         
595         this.disabled = false;
596         this.fireEvent("enable", this);
597         return this;
598     }
599
600 });
601
602 Roo.apply(Roo.bootstrap.CheckBox, {
603     
604     groups: {},
605     
606      /**
607     * register a CheckBox Group
608     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
609     */
610     register : function(checkbox)
611     {
612         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
613             this.groups[checkbox.groupId] = {};
614         }
615         
616         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
617             return;
618         }
619         
620         this.groups[checkbox.groupId][checkbox.name] = checkbox;
621         
622     },
623     /**
624     * fetch a CheckBox Group based on the group ID
625     * @param {string} the group ID
626     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
627     */
628     get: function(groupId) {
629         if (typeof(this.groups[groupId]) == 'undefined') {
630             return false;
631         }
632         
633         return this.groups[groupId] ;
634     }
635     
636     
637 });