sync
[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.useFontAwesomeCheckBox) {
238                 boxLabelCfg.cls = 'box-label fa-checkbox'
239             }
240             
241             if(this.tooltip){
242                 boxLabelCfg.tooltip = this.tooltip;
243             }
244              
245             cfg.cn.push(boxLabelCfg);
246         }
247         
248         if(this.inputType != 'radio'){
249             cfg.cn.push(hidden);
250         }
251         
252         return cfg;
253         
254     },
255     
256     /**
257      * return the real input element.
258      */
259     inputEl: function ()
260     {
261         return this.el.select('input.roo-' + this.inputType,true).first();
262     },
263     hiddenEl: function ()
264     {
265         return this.el.select('input.roo-hidden-value',true).first();
266     },
267     
268     labelEl: function()
269     {
270         return this.el.select('label.control-label',true).first();
271     },
272     /* depricated... */
273     
274     label: function()
275     {
276         return this.labelEl();
277     },
278     
279     boxLabelEl: function()
280     {
281         return this.el.select('label.box-label',true).first();
282     },
283     
284     initEvents : function()
285     {
286 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
287         
288         this.inputEl().on('click', this.onClick,  this);
289         
290         if (this.boxLabel) { 
291             this.el.select('label.box-label',true).first().on('click', this.onClick,  this);
292         }
293         
294         this.startValue = this.getValue();
295         
296         if(this.groupId){
297             Roo.bootstrap.CheckBox.register(this);
298         }
299     },
300     
301     onClick : function(e)
302     {   
303         if(this.fireEvent('click', this, e) !== false){
304             this.setChecked(!this.checked);
305         }
306         
307     },
308     
309     setChecked : function(state,suppressEvent)
310     {
311         this.startValue = this.getValue();
312
313         if(this.inputType == 'radio'){
314             
315             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
316                 e.dom.checked = false;
317             });
318             
319             this.inputEl().dom.checked = true;
320             
321             this.inputEl().dom.value = this.inputValue;
322             
323             if(suppressEvent !== true){
324                 this.fireEvent('check', this, true);
325             }
326             
327             this.validate();
328             
329             return;
330         }
331         
332         this.checked = state;
333         
334         this.inputEl().dom.checked = state;
335         
336         
337         this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
338         
339         if(suppressEvent !== true){
340             this.fireEvent('check', this, state);
341         }
342         
343         this.validate();
344     },
345     
346     getValue : function()
347     {
348         if(this.inputType == 'radio'){
349             return this.getGroupValue();
350         }
351         
352         return this.hiddenEl().dom.value;
353         
354     },
355     
356     getGroupValue : function()
357     {
358         if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
359             return '';
360         }
361         
362         return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
363     },
364     
365     setValue : function(v,suppressEvent)
366     {
367         if(this.inputType == 'radio'){
368             this.setGroupValue(v, suppressEvent);
369             return;
370         }
371         
372         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
373         
374         this.validate();
375     },
376     
377     setGroupValue : function(v, suppressEvent)
378     {
379         this.startValue = this.getValue();
380         
381         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
382             e.dom.checked = false;
383             
384             if(e.dom.value == v){
385                 e.dom.checked = true;
386             }
387         });
388         
389         if(suppressEvent !== true){
390             this.fireEvent('check', this, true);
391         }
392
393         this.validate();
394         
395         return;
396     },
397     
398     validate : function()
399     {
400         if(this.getVisibilityEl().hasClass('hidden')){
401             return true;
402         }
403         
404         if(
405                 this.disabled || 
406                 (this.inputType == 'radio' && this.validateRadio()) ||
407                 (this.inputType == 'checkbox' && this.validateCheckbox())
408         ){
409             this.markValid();
410             return true;
411         }
412         
413         this.markInvalid();
414         return false;
415     },
416     
417     validateRadio : function()
418     {
419         if(this.getVisibilityEl().hasClass('hidden')){
420             return true;
421         }
422         
423         if(this.allowBlank){
424             return true;
425         }
426         
427         var valid = false;
428         
429         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
430             if(!e.dom.checked){
431                 return;
432             }
433             
434             valid = true;
435             
436             return false;
437         });
438         
439         return valid;
440     },
441     
442     validateCheckbox : function()
443     {
444         if(!this.groupId){
445             return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
446             //return (this.getValue() == this.inputValue) ? true : false;
447         }
448         
449         var group = Roo.bootstrap.CheckBox.get(this.groupId);
450         
451         if(!group){
452             return false;
453         }
454         
455         var r = false;
456         
457         for(var i in group){
458             if(group[i].el.isVisible(true)){
459                 r = false;
460                 break;
461             }
462             
463             r = true;
464         }
465         
466         for(var i in group){
467             if(r){
468                 break;
469             }
470             
471             r = (group[i].getValue() == group[i].inputValue) ? true : false;
472         }
473         
474         return r;
475     },
476     
477     /**
478      * Mark this field as valid
479      */
480     markValid : function()
481     {
482         var _this = this;
483         
484         this.fireEvent('valid', this);
485         
486         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
487         
488         if(this.groupId){
489             label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
490         }
491         
492         if(label){
493             label.markValid();
494         }
495
496         if(this.inputType == 'radio'){
497             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
498                 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
499                 e.findParent('.form-group', false, true).addClass(_this.validClass);
500             });
501             
502             return;
503         }
504
505         if(!this.groupId){
506             this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
507             this.el.findParent('.form-group', false, true).addClass(this.validClass);
508             return;
509         }
510         
511         var group = Roo.bootstrap.CheckBox.get(this.groupId);
512         
513         if(!group){
514             return;
515         }
516         
517         for(var i in group){
518             group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
519             group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
520         }
521     },
522     
523      /**
524      * Mark this field as invalid
525      * @param {String} msg The validation message
526      */
527     markInvalid : function(msg)
528     {
529         if(this.allowBlank){
530             return;
531         }
532         
533         var _this = this;
534         
535         this.fireEvent('invalid', this, msg);
536         
537         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
538         
539         if(this.groupId){
540             label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
541         }
542         
543         if(label){
544             label.markInvalid();
545         }
546             
547         if(this.inputType == 'radio'){
548             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
549                 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
550                 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
551             });
552             
553             return;
554         }
555         
556         if(!this.groupId){
557             this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
558             this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
559             return;
560         }
561         
562         var group = Roo.bootstrap.CheckBox.get(this.groupId);
563         
564         if(!group){
565             return;
566         }
567         
568         for(var i in group){
569             group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
570             group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
571         }
572         
573     },
574     
575     clearInvalid : function()
576     {
577         Roo.bootstrap.Input.prototype.clearInvalid.call(this);
578         
579         // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
580         
581         var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
582         
583         if (label && label.iconEl) {
584             label.iconEl.removeClass(label.validClass);
585             label.iconEl.removeClass(label.invalidClass);
586         }
587     },
588     
589     disable : function()
590     {
591         if(this.inputType != 'radio'){
592             Roo.bootstrap.CheckBox.superclass.disable.call(this);
593             return;
594         }
595         
596         var _this = this;
597         
598         if(this.rendered){
599             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
600                 _this.getActionEl().addClass(this.disabledClass);
601                 e.dom.disabled = true;
602             });
603         }
604         
605         this.disabled = true;
606         this.fireEvent("disable", this);
607         return this;
608     },
609
610     enable : function()
611     {
612         if(this.inputType != 'radio'){
613             Roo.bootstrap.CheckBox.superclass.enable.call(this);
614             return;
615         }
616         
617         var _this = this;
618         
619         if(this.rendered){
620             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
621                 _this.getActionEl().removeClass(this.disabledClass);
622                 e.dom.disabled = false;
623             });
624         }
625         
626         this.disabled = false;
627         this.fireEvent("enable", this);
628         return this;
629     },
630     
631     setBoxLabel : function(v)
632     {
633         this.boxLabel = v;
634         
635         if(this.rendered){
636             this.el.select('label.box-label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
637         }
638     }
639
640 });
641
642 Roo.apply(Roo.bootstrap.CheckBox, {
643     
644     groups: {},
645     
646      /**
647     * register a CheckBox Group
648     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
649     */
650     register : function(checkbox)
651     {
652         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
653             this.groups[checkbox.groupId] = {};
654         }
655         
656         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
657             return;
658         }
659         
660         this.groups[checkbox.groupId][checkbox.name] = checkbox;
661         
662     },
663     /**
664     * fetch a CheckBox Group based on the group ID
665     * @param {string} the group ID
666     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
667     */
668     get: function(groupId) {
669         if (typeof(this.groups[groupId]) == 'undefined') {
670             return false;
671         }
672         
673         return this.groups[groupId] ;
674     }
675     
676     
677 });