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             this.validate();
293             return;
294         }
295         
296         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
297         this.validate();
298     },
299     
300     setGroupValue : function(v, suppressEvent)
301     {
302         this.startValue = this.getValue();
303         
304         Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
305             e.dom.checked = false;
306             
307             if(e.dom.value == v){
308                 e.dom.checked = true;
309             }
310         });
311         
312         if(suppressEvent !== true){
313             this.fireEvent('check', this, true);
314         }
315
316         return;
317     },
318     
319     validate : function()
320     {
321         Roo.log('validate!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
322         if(
323                 this.disabled || 
324                 (this.inputType == 'radio' && this.getValue().length) ||
325                 (this.inputType == 'checkbox' && this.validateGroup())
326         ){
327             this.markValid();
328             return true;
329         }
330         
331         this.markInvalid();
332         return false;
333     },
334     
335     validateGroup : function()
336     {
337         if(!this.groupId){
338             return (this.getValue() == this.inputValue) ? true : false;
339         }
340         
341         var group = Roo.bootstrap.CheckBox.get(this.groupId);
342         
343         if(!group){
344             return false;
345         }
346         
347         var r = false;
348         
349         for(var i in group){
350             if(r){
351                 break;
352             }
353             
354             r = (group[i].getValue() == group[i].inputValue) ? true : false;
355         }
356         
357         return r;
358     },
359     
360     /**
361      * Mark this field as valid
362      */
363     markValid : function()
364     {
365         Roo.log('markValid!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
366         Roo.log(this);
367         this.fireEvent('valid', this);
368         
369         if(this.inputType == 'radio'){
370             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
371                 Roo.log(e);
372                 e.removeClass([this.invalidClass, this.validClass]);
373                 e.addClass(this.validClass);
374             });
375             
376             return;
377         }
378         
379         if(!this.groupId){
380             this.el.removeClass([this.invalidClass, this.validClass]);
381             this.el.addClass(this.validClass);
382             return;
383         }
384         
385         var group = Roo.bootstrap.CheckBox.get(this.groupId);
386             
387         if(!group){
388             return;
389         }
390         
391         for(var i in group){
392             group[i].el.removeClass([this.invalidClass, this.validClass]);
393             group[i].el.addClass(this.validClass);
394         }
395     },
396     
397      /**
398      * Mark this field as invalid
399      * @param {String} msg The validation message
400      */
401     markInvalid : function(msg)
402     {
403         this.fireEvent('invalid', this, msg);
404         
405         if(this.inputType == 'radio'){
406             Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
407                 e.removeClass([this.invalidClass, this.validClass]);
408                 e.addClass(this.invalidClass);
409             });
410             
411             return;
412         }
413         
414         if(!this.groupId){
415             this.el.removeClass([this.invalidClass, this.validClass]);
416             this.el.addClass(this.invalidClass);
417             return;
418         }
419         
420         var group = Roo.bootstrap.CheckBox.get(this.groupId);
421             
422         if(!group){
423             return;
424         }
425         
426         for(var i in group){
427             group[i].el.removeClass([this.invalidClass, this.validClass]);
428             group[i].el.addClass(this.invalidClass);
429         }
430         
431     }
432     
433 });
434
435 Roo.apply(Roo.bootstrap.CheckBox, {
436     
437     groups: {},
438     
439      /**
440     * register a CheckBox Group
441     * @param {Roo.bootstrap.CheckBox} the CheckBox to add
442     */
443     register : function(checkbox)
444     {
445         if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
446             this.groups[checkbox.groupId] = {};
447         }
448         
449         if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
450             return;
451         }
452         
453         this.groups[checkbox.groupId][checkbox.name] = checkbox;
454         
455     },
456     /**
457     * fetch a CheckBox Group based on the group ID
458     * @param {string} the group ID
459     * @returns {Roo.bootstrap.CheckBox} the CheckBox group
460     */
461     get: function(groupId) {
462         if (typeof(this.groups[groupId]) == 'undefined') {
463             return false;
464         }
465         
466         return this.groups[groupId] ;
467     }
468     
469     
470 });