9 * @class Roo.bootstrap.CheckBox
10 * @extends Roo.bootstrap.Input
11 * Bootstrap CheckBox class
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
23 * Create a new CheckBox
24 * @param {Object} config The config object
27 Roo.bootstrap.CheckBox = function(config){
28 Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
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
40 * Fires when the element is click.
41 * @param {Roo.bootstrap.CheckBox} this This input
48 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
50 inputType: 'checkbox',
59 getAutoCreate : function()
61 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
67 cfg.cls = 'form-group ' + this.inputType; //input-group
70 cfg.cls += ' ' + this.inputType + '-inline';
76 type : this.inputType,
77 value : this.inputValue,
78 cls : 'roo-' + this.inputType, //'form-box',
79 placeholder : this.placeholder || ''
83 if(this.inputType != 'radio'){
87 cls : 'roo-hidden-value',
88 value : this.checked ? this.inputValue : this.valueOff
93 if (this.weight) { // Validity check?
94 cfg.cls += " " + this.inputType + "-" + this.weight;
102 input.checked = this.checked;
107 input.name = this.name;
109 if(this.inputType != 'radio'){
110 hidden.name = this.name;
111 input.name = '_hidden_' + this.name;
116 input.cls += ' input-' + this.size;
121 ['xs','sm','md','lg'].map(function(size){
122 if (settings[size]) {
123 cfg.cls += ' col-' + size + '-' + settings[size];
127 var inputblock = input;
129 if (this.before || this.after) {
139 cls : 'input-group-addon',
144 inputblock.cn.push(input);
146 if(this.inputType != 'radio'){
147 inputblock.cn.push(hidden);
153 cls : 'input-group-addon',
160 if (align ==='left' && this.fieldLabel.length) {
161 // Roo.log("left and has label");
166 cls : 'control-label',
167 html : this.fieldLabel
177 if(this.labelWidth > 12){
178 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
181 if(this.labelWidth < 13 && this.labelmd == 0){
182 this.labelmd = this.labelWidth;
185 if(this.labellg > 0){
186 cfg.cn[0].cls += ' col-lg-' + this.labellg;
187 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
190 if(this.labelmd > 0){
191 cfg.cn[0].cls += ' col-md-' + this.labelmd;
192 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
195 if(this.labelsm > 0){
196 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
197 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
200 if(this.labelxs > 0){
201 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
202 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
205 } else if ( this.fieldLabel.length) {
206 // Roo.log(" label");
210 tag: this.boxLabel ? 'span' : 'label',
212 cls: 'control-label box-input-label',
213 //cls : 'input-group-addon',
214 html : this.fieldLabel
223 // Roo.log(" no label && no align");
224 cfg.cn = [ inputblock ] ;
232 //'for': id, // box label is handled by onclick - so no for...
237 if(this.useFontAwesomeCheckBox) {
238 boxLabelCfg.cls = 'box-label fa-checkbox'
242 boxLabelCfg.tooltip = this.tooltip;
245 cfg.cn.push(boxLabelCfg);
248 if(this.inputType != 'radio'){
257 * return the real input element.
261 return this.el.select('input.roo-' + this.inputType,true).first();
263 hiddenEl: function ()
265 return this.el.select('input.roo-hidden-value',true).first();
270 return this.el.select('label.control-label',true).first();
276 return this.labelEl();
279 boxLabelEl: function()
281 return this.el.select('label.box-label',true).first();
284 initEvents : function()
286 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
288 this.inputEl().on('click', this.onClick, this);
291 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
294 this.startValue = this.getValue();
297 Roo.bootstrap.CheckBox.register(this);
301 onClick : function(e)
303 if(this.fireEvent('click', this, e) !== false){
304 this.setChecked(!this.checked);
309 setChecked : function(state,suppressEvent)
311 this.startValue = this.getValue();
313 if(this.inputType == 'radio'){
315 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
316 e.dom.checked = false;
319 this.inputEl().dom.checked = true;
321 this.inputEl().dom.value = this.inputValue;
323 if(suppressEvent !== true){
324 this.fireEvent('check', this, true);
332 this.checked = state;
334 this.inputEl().dom.checked = state;
337 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
339 if(suppressEvent !== true){
340 this.fireEvent('check', this, state);
346 getValue : function()
348 if(this.inputType == 'radio'){
349 return this.getGroupValue();
352 return this.hiddenEl().dom.value;
356 getGroupValue : function()
358 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
362 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
365 setValue : function(v,suppressEvent)
367 if(this.inputType == 'radio'){
368 this.setGroupValue(v, suppressEvent);
372 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
377 setGroupValue : function(v, suppressEvent)
379 this.startValue = this.getValue();
381 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
382 e.dom.checked = false;
384 if(e.dom.value == v){
385 e.dom.checked = true;
389 if(suppressEvent !== true){
390 this.fireEvent('check', this, true);
398 validate : function()
400 if(this.getVisibilityEl().hasClass('hidden')){
406 (this.inputType == 'radio' && this.validateRadio()) ||
407 (this.inputType == 'checkbox' && this.validateCheckbox())
417 validateRadio : function()
419 if(this.getVisibilityEl().hasClass('hidden')){
429 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
442 validateCheckbox : function()
445 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
446 //return (this.getValue() == this.inputValue) ? true : false;
449 var group = Roo.bootstrap.CheckBox.get(this.groupId);
458 if(group[i].el.isVisible(true)){
471 r = (group[i].getValue() == group[i].inputValue) ? true : false;
478 * Mark this field as valid
480 markValid : function()
484 this.fireEvent('valid', this);
486 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
489 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
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);
506 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
507 this.el.findParent('.form-group', false, true).addClass(this.validClass);
511 var group = Roo.bootstrap.CheckBox.get(this.groupId);
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);
524 * Mark this field as invalid
525 * @param {String} msg The validation message
527 markInvalid : function(msg)
535 this.fireEvent('invalid', this, msg);
537 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
540 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
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);
557 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
558 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
562 var group = Roo.bootstrap.CheckBox.get(this.groupId);
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);
575 clearInvalid : function()
577 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
579 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
581 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
583 if (label && label.iconEl) {
584 label.iconEl.removeClass(label.validClass);
585 label.iconEl.removeClass(label.invalidClass);
591 if(this.inputType != 'radio'){
592 Roo.bootstrap.CheckBox.superclass.disable.call(this);
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;
605 this.disabled = true;
606 this.fireEvent("disable", this);
612 if(this.inputType != 'radio'){
613 Roo.bootstrap.CheckBox.superclass.enable.call(this);
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;
626 this.disabled = false;
627 this.fireEvent("enable", this);
631 setBoxLabel : function(v)
636 this.el.select('label.box-label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
642 Roo.apply(Roo.bootstrap.CheckBox, {
647 * register a CheckBox Group
648 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
650 register : function(checkbox)
652 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
653 this.groups[checkbox.groupId] = {};
656 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
660 this.groups[checkbox.groupId][checkbox.name] = checkbox;
664 * fetch a CheckBox Group based on the group ID
665 * @param {string} the group ID
666 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
668 get: function(groupId) {
669 if (typeof(this.groups[groupId]) == 'undefined') {
673 return this.groups[groupId] ;