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
22 * Create a new CheckBox
23 * @param {Object} config The config object
26 Roo.bootstrap.CheckBox = function(config){
27 Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
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
41 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
43 inputType: 'checkbox',
51 getAutoCreate : function()
53 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
59 cfg.cls = 'form-group ' + this.inputType; //input-group
62 cfg.cls += ' ' + this.inputType + '-inline';
68 type : this.inputType,
69 value : this.inputValue,
70 cls : 'roo-' + this.inputType, //'form-box',
71 placeholder : this.placeholder || ''
75 if(this.inputType != 'radio'){
79 cls : 'roo-hidden-value',
80 value : this.checked ? this.valueOff : this.inputValue
85 if (this.weight) { // Validity check?
86 cfg.cls += " " + this.inputType + "-" + this.weight;
94 input.checked = this.checked;
101 input.name = this.name;
103 if(this.inputType != 'radio'){
104 hidden.name = this.name;
105 input.name = '_hidden_' + this.name;
110 input.cls += ' input-' + this.size;
115 ['xs','sm','md','lg'].map(function(size){
116 if (settings[size]) {
117 cfg.cls += ' col-' + size + '-' + settings[size];
121 var inputblock = input;
123 if (this.before || this.after) {
133 cls : 'input-group-addon',
138 inputblock.cn.push(input);
140 if(this.inputType != 'radio'){
141 inputblock.cn.push(hidden);
147 cls : 'input-group-addon',
154 if (align ==='left' && this.fieldLabel.length) {
155 // Roo.log("left and has label");
160 cls : 'control-label',
161 html : this.fieldLabel
172 if(this.labelWidth > 12){
173 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
176 if(this.labelWidth < 13 && this.labelmd == 0){
177 this.labelmd = this.labelWidth;
180 if(this.labellg > 0){
181 cfg.cn[0].cls += ' col-lg-' + this.labellg;
182 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
185 if(this.labelmd > 0){
186 cfg.cn[0].cls += ' col-md-' + this.labelmd;
187 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
190 if(this.labelsm > 0){
191 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
192 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
195 if(this.labelxs > 0){
196 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
197 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
200 } else if ( this.fieldLabel.length) {
201 // Roo.log(" label");
205 tag: this.boxLabel ? 'span' : 'label',
207 cls: 'control-label box-input-label',
208 //cls : 'input-group-addon',
209 html : this.fieldLabel
219 // Roo.log(" no label && no align");
220 cfg.cn = [ inputblock ] ;
228 //'for': id, // box label is handled by onclick - so no for...
234 boxLabelCfg.tooltip = this.tooltip;
237 cfg.cn.push(boxLabelCfg);
240 if(this.inputType != 'radio'){
249 * return the real input element.
253 return this.el.select('input.roo-' + this.inputType,true).first();
255 hiddenEl: function ()
257 return this.el.select('input.roo-hidden-value',true).first();
262 return this.el.select('label.control-label',true).first();
268 return this.labelEl();
271 boxLabelEl: function()
273 return this.el.select('label.box-label',true).first();
276 initEvents : function()
278 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
280 this.inputEl().on('click', this.onClick, this);
283 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
286 this.startValue = this.getValue();
289 Roo.bootstrap.CheckBox.register(this);
295 this.setChecked(!this.checked);
298 setChecked : function(state,suppressEvent)
300 this.startValue = this.getValue();
302 if(this.inputType == 'radio'){
304 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
305 e.dom.checked = false;
308 this.inputEl().dom.checked = true;
310 this.inputEl().dom.value = this.inputValue;
312 if(suppressEvent !== true){
313 this.fireEvent('check', this, true);
321 this.checked = state;
323 this.inputEl().dom.checked = state;
326 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
328 if(suppressEvent !== true){
329 this.fireEvent('check', this, state);
335 getValue : function()
337 if(this.inputType == 'radio'){
338 return this.getGroupValue();
341 return this.hiddenEl().dom.value;
345 getGroupValue : function()
347 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
351 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
354 setValue : function(v,suppressEvent)
356 if(this.inputType == 'radio'){
357 this.setGroupValue(v, suppressEvent);
361 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
366 setGroupValue : function(v, suppressEvent)
368 this.startValue = this.getValue();
370 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
371 e.dom.checked = false;
373 if(e.dom.value == v){
374 e.dom.checked = true;
378 if(suppressEvent !== true){
379 this.fireEvent('check', this, true);
387 validate : function()
391 (this.inputType == 'radio' && this.validateRadio()) ||
392 (this.inputType == 'checkbox' && this.validateCheckbox())
402 validateRadio : function()
410 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
423 validateCheckbox : function()
426 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
429 var group = Roo.bootstrap.CheckBox.get(this.groupId);
442 r = (group[i].getValue() == group[i].inputValue) ? true : false;
449 * Mark this field as valid
451 markValid : function()
455 this.fireEvent('valid', this);
457 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
460 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
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);
477 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
478 this.el.findParent('.form-group', false, true).addClass(this.validClass);
482 var group = Roo.bootstrap.CheckBox.get(this.groupId);
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);
495 * Mark this field as invalid
496 * @param {String} msg The validation message
498 markInvalid : function(msg)
506 this.fireEvent('invalid', this, msg);
508 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
511 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
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);
528 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
529 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
533 var group = Roo.bootstrap.CheckBox.get(this.groupId);
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);
546 clearInvalid : function()
548 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
550 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
553 label.iconEl.removeClass(label.validClass);
554 label.iconEl.removeClass(label.invalidClass);
560 if(this.inputType != 'radio'){
561 Roo.bootstrap.CheckBox.superclass.disable.call(this);
568 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
569 _this.getActionEl().addClass(this.disabledClass);
570 e.dom.disabled = true;
574 this.disabled = true;
575 this.fireEvent("disable", this);
581 if(this.inputType != 'radio'){
582 Roo.bootstrap.CheckBox.superclass.enable.call(this);
589 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
590 _this.getActionEl().removeClass(this.disabledClass);
591 e.dom.disabled = false;
595 this.disabled = false;
596 this.fireEvent("enable", this);
602 Roo.apply(Roo.bootstrap.CheckBox, {
607 * register a CheckBox Group
608 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
610 register : function(checkbox)
612 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
613 this.groups[checkbox.groupId] = {};
616 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
620 this.groups[checkbox.groupId][checkbox.name] = checkbox;
624 * fetch a CheckBox Group based on the group ID
625 * @param {string} the group ID
626 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
628 get: function(groupId) {
629 if (typeof(this.groups[groupId]) == 'undefined') {
633 return this.groups[groupId] ;