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