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;
427 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
430 var group = Roo.bootstrap.CheckBox.get(this.groupId);
443 r = (group[i].getValue() == group[i].inputValue) ? true : false;
450 * Mark this field as valid
452 markValid : function()
456 this.fireEvent('valid', this);
458 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
461 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
468 if(this.inputType == 'radio'){
469 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
470 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
471 e.findParent('.form-group', false, true).addClass(_this.validClass);
478 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
479 this.el.findParent('.form-group', false, true).addClass(this.validClass);
483 var group = Roo.bootstrap.CheckBox.get(this.groupId);
490 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
491 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
496 * Mark this field as invalid
497 * @param {String} msg The validation message
499 markInvalid : function(msg)
507 this.fireEvent('invalid', this, msg);
509 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
512 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
519 if(this.inputType == 'radio'){
520 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
521 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
522 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
529 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
530 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
534 var group = Roo.bootstrap.CheckBox.get(this.groupId);
541 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
542 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
547 clearInvalid : function()
549 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
551 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
553 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
556 label.iconEl.removeClass(label.validClass);
557 label.iconEl.removeClass(label.invalidClass);
563 if(this.inputType != 'radio'){
564 Roo.bootstrap.CheckBox.superclass.disable.call(this);
571 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
572 _this.getActionEl().addClass(this.disabledClass);
573 e.dom.disabled = true;
577 this.disabled = true;
578 this.fireEvent("disable", this);
584 if(this.inputType != 'radio'){
585 Roo.bootstrap.CheckBox.superclass.enable.call(this);
592 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
593 _this.getActionEl().removeClass(this.disabledClass);
594 e.dom.disabled = false;
598 this.disabled = false;
599 this.fireEvent("enable", this);
605 Roo.apply(Roo.bootstrap.CheckBox, {
610 * register a CheckBox Group
611 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
613 register : function(checkbox)
615 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
616 this.groups[checkbox.groupId] = {};
619 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
623 this.groups[checkbox.groupId][checkbox.name] = checkbox;
627 * fetch a CheckBox Group based on the group ID
628 * @param {string} the group ID
629 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
631 get: function(groupId) {
632 if (typeof(this.groups[groupId]) == 'undefined') {
636 return this.groups[groupId] ;