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  * @cfg {String} tooltip label tooltip
21  * 
22  * @constructor
23  * Create a new CheckBox
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.CheckBox = function(config){
28     Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
29    
30     this.addEvents({
31         /**
32         * @event check
33         * Fires when the element is checked or unchecked.
34         * @param {Roo.bootstrap.CheckBox} this This input
35         * @param {Boolean} checked The new checked value
36         */
37        check : true
38     });
39     
40 };
41
42 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
43   
44     inputType: 'checkbox',
45     inputValue: 1,
46     valueOff: 0,
47     boxLabel: false,
48     checked: false,
49     weight : false,
50     inline: false,
51     tooltip : '',
52     
53     getAutoCreate : function()
54     {
55         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
56         
57         var id = Roo.id();
58         
59         var cfg = {};
60         
61         cfg.cls = 'form-group ' + this.inputType; //input-group
62         
63         if(this.inline){
64             cfg.cls += ' ' + this.inputType + '-inline';
65         }
66         
67         var input =  {
68             tag: 'input',
69             id : id,
70             type : this.inputType,
71             value : this.inputValue,
72             cls : 'roo-' + this.inputType, //'form-box',
73             placeholder : this.placeholder || ''
74             
75         };
76         
77         if(this.inputType != 'radio'){
78             var hidden =  {
79                 tag: 'input',
80                 type : 'hidden',
81                 cls : 'roo-hidden-value',
82                 value : this.checked ? this.valueOff : this.inputValue
83             };
84         }
85         
86             
87         if (this.weight) { // Validity check?
88             cfg.cls += " " + this.inputType + "-" + this.weight;
89         }
90         
91         if (this.disabled) {
92             input.disabled=true;
93         }
94         
95         if(this.checked){
96             input.checked = this.checked;
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                     cls : "", 
165                     cn: [
166                         inputblock
167                     ]
168                 }
169             ];
170             
171             if(this.labelWidth > 12){
172                 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
173             }
174             
175             if(this.labelWidth < 13 && this.labelmd == 0){
176                 this.labelmd = this.labelWidth;
177             }
178             
179             if(this.labellg > 0){
180                 cfg.cn[0].cls += ' col-lg-' + this.labellg;
181                 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
182             }
183             
184             if(this.labelmd > 0){
185                 cfg.cn[0].cls += ' col-md-' + this.labelmd;
186                 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
187             }
188             
189             if(this.labelsm > 0){
190                 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
191                 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
192             }
193             
194             if(this.labelxs > 0){
195                 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
196                 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
197             }
198             
199         } else if ( this.fieldLabel.length) {
200 //                Roo.log(" label");
201                 cfg.cn = [
202                    
203                     {
204                         tag: this.boxLabel ? 'span' : 'label',
205                         'for': id,
206                         cls: 'control-label box-input-label',
207                         //cls : 'input-group-addon',
208                         html : this.fieldLabel
209                     },
210                     
211                     inputblock
212                     
213                 ];
214
215         } else {
216             
217 //                Roo.log(" no label && no align");
218                 cfg.cn = [  inputblock ] ;
219                 
220                 
221         }
222         
223         if(this.boxLabel){
224              var boxLabelCfg = {
225                 tag: 'label',
226                 //'for': id, // box label is handled by onclick - so no for...
227                 cls: 'box-label',
228                 html: this.boxLabel,
229                 tooltip : this.tooltip ? this.tooltip : ''
230             };
231             
232             if(this.tooltip){
233                 boxLabelCfg.tooltip = this.tooltip;
234             }
235              
236             cfg.cn.push(boxLabelCfg);
237         }
238         
239         if(this.inputType != 'radio'){
240             cfg.cn.push(hidden);
241         }
242         
243         return cfg;
244         
245     },
246     
247     /**
248      * return the real input element.
249      */
250     inputEl: function ()
251     {
252         return this.el.select('input.roo-' + this.inputType,true).first();
253     },
254     hiddenEl: function ()
255     {
256         return this.el.select('input.roo-hidden-value',true).first();
257     },
258     
259     labelEl: function()
260     {
261         return this.el.select('label.control-label',true).first();
262     },
263     /* depricated... */
264     
265     label: function()
266     {
267         return this.labelEl();
268     },
269     
270     boxLabelEl: function()
271     {
272         return this.el.select('label.box-label',true).first();
273     },
274     
275     initEvents : function()
276     {
277 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
278         
279         this.inputEl().on('click', this.onClick,  this);
280         
281         if (this.boxLabel) { 
282             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
283         }
284         
285         this.startValue = this.getValue();
286         
287         if(this.groupId){
288             Roo.bootstrap.CheckBox.register(this);
289         }
290     },
291     
292     onClick : function()
293     {   
294         this.setChecked(!this.checked);
295     },
296     
297     setChecked : function(state,suppressEvent)
298     {
299         this.startValue = this.getValue();
300
301         if(this.inputType == 'radio'){
302             
303             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
304                 e.dom.checked = false;
305             });
306             
307             this.inputEl().dom.checked = true;
308             
309             this.inputEl().dom.value = this.inputValue;
310             
311             if(suppressEvent !== true){
312                 this.fireEvent('check', this, true);
313             }
314             
315             this.validate();
316             
317             return;
318         }
319         
320         this.checked = state;
321         
322         this.inputEl().dom.checked = state;
323         
324         
325         this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
326         
327         if(suppressEvent !== true){
328             this.fireEvent('check', this, state);
329         }
330         
331         this.validate();
332     },
333     
334     getValue : function()
335     {
336         if(this.inputType == 'radio'){
337             return this.getGroupValue();
338         }
339         
340         return this.hiddenEl().dom.value;
341         
342     },
343     
344     getGroupValue : function()
345     {
346         if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
347             return '';
348         }
349         
350         return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
351     },
352     
353     setValue : function(v,suppressEvent)
354     {
355         if(this.inputType == 'radio'){
356             this.setGroupValue(v, suppressEvent);
357             return;
358         }
359         
360         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
361         
362         this.validate();
363     },
364     
365     setGroupValue : function(v, suppressEvent)
366     {
367         this.startValue = this.getValue();
368         
369         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
370             e.dom.checked = false;
371             
372             if(e.dom.value == v){
373                 e.dom.checked = true;
374             }
375         });
376         
377         if(suppressEvent !== true){
378             this.fireEvent('check', this, true);
379         }
380
381         this.validate();
382         
383         return;
384     },
385     
386     validate : function()
387     {
388         if(
389                 this.disabled || 
390                 (this.inputType == 'radio' && this.validateRadio()) ||
391                 (this.inputType == 'checkbox' && this.validateCheckbox())
392         ){
393             this.markValid();
394             return true;
395         }
396         
397         this.markInvalid();
398         return false;
399     },
400     
401     validateRadio : function()
402     {
403         if(this.allowBlank){
404             return true;
405         }
406         
407         var valid = false;
408         
409         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
410             if(!e.dom.checked){
411                 return;
412             }
413             
414             valid = true;
415             
416             return false;
417         });
418         
419         return valid;
420     },
421     
422     validateCheckbox : function()
423     {
424         if(!this.groupId){
425             return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
426             //return (this.getValue() == this.inputValue) ? 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         // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
551         
552         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
553         
554         if (label) {
555             label.iconEl.removeClass(label.validClass);
556             label.iconEl.removeClass(label.invalidClass);
557         }
558     },
559     
560     disable : function()
561     {
562         if(this.inputType != 'radio'){
563             Roo.bootstrap.CheckBox.superclass.disable.call(this);
564             return;
565         }
566         
567         var _this = this;
568         
569         if(this.rendered){
570             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
571                 _this.getActionEl().addClass(this.disabledClass);
572                 e.dom.disabled = true;
573             });
574         }
575         
576         this.disabled = true;
577         this.fireEvent("disable", this);
578         return this;
579     },
580
581     enable : function()
582     {
583         if(this.inputType != 'radio'){
584             Roo.bootstrap.CheckBox.superclass.enable.call(this);
585             return;
586         }
587         
588         var _this = this;
589         
590         if(this.rendered){
591             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
592                 _this.getActionEl().removeClass(this.disabledClass);
593                 e.dom.disabled = false;
594             });
595         }
596         
597         this.disabled = false;
598         this.fireEvent("enable", this);
599         return this;
600     }
601
602 });
603
604 Roo.apply(Roo.bootstrap.CheckBox, {
605     
606     groups: {},
607     
608      /**
609     * register a CheckBox Group
610     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
611     */
612     register : function(checkbox)
613     {
614         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
615             this.groups[checkbox.groupId] = {};
616         }
617         
618         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
619             return;
620         }
621         
622         this.groups[checkbox.groupId][checkbox.name] = checkbox;
623         
624     },
625     /**
626     * fetch a CheckBox Group based on the group ID
627     * @param {string} the group ID
628     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
629     */
630     get: function(groupId) {
631         if (typeof(this.groups[groupId]) == 'undefined') {
632             return false;
633         }
634         
635         return this.groups[groupId] ;
636     }
637     
638     
639 });