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
42 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
44 inputType: 'checkbox',
53 getAutoCreate : function()
55 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
61 cfg.cls = 'form-group ' + this.inputType; //input-group
64 cfg.cls += ' ' + this.inputType + '-inline';
70 type : this.inputType,
71 value : this.inputValue,
72 cls : 'roo-' + this.inputType, //'form-box',
73 placeholder : this.placeholder || ''
77 if(this.inputType != 'radio'){
81 cls : 'roo-hidden-value',
82 value : this.checked ? this.valueOff : this.inputValue
87 if (this.weight) { // Validity check?
88 cfg.cls += " " + this.inputType + "-" + this.weight;
96 input.checked = this.checked;
103 input.name = this.name;
105 if(this.inputType != 'radio'){
106 hidden.name = this.name;
107 input.name = '_hidden_' + this.name;
112 input.cls += ' input-' + this.size;
117 ['xs','sm','md','lg'].map(function(size){
118 if (settings[size]) {
119 cfg.cls += ' col-' + size + '-' + settings[size];
123 var inputblock = input;
125 if (this.before || this.after) {
135 cls : 'input-group-addon',
140 inputblock.cn.push(input);
142 if(this.inputType != 'radio'){
143 inputblock.cn.push(hidden);
149 cls : 'input-group-addon',
156 if (align ==='left' && this.fieldLabel.length) {
157 // Roo.log("left and has label");
162 cls : 'control-label',
163 html : this.fieldLabel
174 if(this.labelWidth > 12){
175 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
178 if(this.labelWidth < 13 && this.labelmd == 0){
179 this.labelmd = this.labelWidth;
182 if(this.labellg > 0){
183 cfg.cn[0].cls += ' col-lg-' + this.labellg;
184 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
187 if(this.labelmd > 0){
188 cfg.cn[0].cls += ' col-md-' + this.labelmd;
189 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
192 if(this.labelsm > 0){
193 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
194 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
197 if(this.labelxs > 0){
198 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
199 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
202 } else if ( this.fieldLabel.length) {
203 // Roo.log(" label");
207 tag: this.boxLabel ? 'span' : 'label',
209 cls: 'control-label box-input-label',
210 //cls : 'input-group-addon',
211 html : this.fieldLabel
221 // Roo.log(" no label && no align");
222 cfg.cn = [ inputblock ] ;
230 //'for': id, // box label is handled by onclick - so no for...
236 boxLabelCfg.tooltip = this.tooltip;
239 cfg.cn.push(boxLabelCfg);
242 if(this.inputType != 'radio'){
251 * return the real input element.
255 return this.el.select('input.roo-' + this.inputType,true).first();
257 hiddenEl: function ()
259 return this.el.select('input.roo-hidden-value',true).first();
264 return this.el.select('label.control-label',true).first();
270 return this.labelEl();
273 boxLabelEl: function()
275 return this.el.select('label.box-label',true).first();
278 initEvents : function()
280 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
282 this.inputEl().on('click', this.onClick, this);
285 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
288 this.startValue = this.getValue();
291 Roo.bootstrap.CheckBox.register(this);
297 this.setChecked(!this.checked);
300 setChecked : function(state,suppressEvent)
302 this.startValue = this.getValue();
304 if(this.inputType == 'radio'){
306 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
307 e.dom.checked = false;
310 this.inputEl().dom.checked = true;
312 this.inputEl().dom.value = this.inputValue;
314 if(suppressEvent !== true){
315 this.fireEvent('check', this, true);
323 this.checked = state;
325 this.inputEl().dom.checked = state;
328 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
330 if(suppressEvent !== true){
331 this.fireEvent('check', this, state);
337 getValue : function()
339 if(this.inputType == 'radio'){
340 return this.getGroupValue();
343 return this.hiddenEl().dom.value;
347 getGroupValue : function()
349 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
353 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
356 setValue : function(v,suppressEvent)
358 if(this.inputType == 'radio'){
359 this.setGroupValue(v, suppressEvent);
363 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
368 setGroupValue : function(v, suppressEvent)
370 this.startValue = this.getValue();
372 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
373 e.dom.checked = false;
375 if(e.dom.value == v){
376 e.dom.checked = true;
380 if(suppressEvent !== true){
381 this.fireEvent('check', this, true);
389 validate : function()
393 (this.inputType == 'radio' && this.validateRadio()) ||
394 (this.inputType == 'checkbox' && this.validateCheckbox())
404 validateRadio : function()
412 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
425 validateCheckbox : function()
428 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
429 //return (this.getValue() == this.inputValue) ? true : false;
432 var group = Roo.bootstrap.CheckBox.get(this.groupId);
445 r = (group[i].getValue() == group[i].inputValue) ? true : false;
452 * Mark this field as valid
454 markValid : function()
458 this.fireEvent('valid', this);
460 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
463 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
470 if(this.inputType == 'radio'){
471 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
472 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
473 e.findParent('.form-group', false, true).addClass(_this.validClass);
480 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
481 this.el.findParent('.form-group', false, true).addClass(this.validClass);
485 var group = Roo.bootstrap.CheckBox.get(this.groupId);
492 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
493 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
498 * Mark this field as invalid
499 * @param {String} msg The validation message
501 markInvalid : function(msg)
509 this.fireEvent('invalid', this, msg);
511 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
514 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
521 if(this.inputType == 'radio'){
522 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
523 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
524 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
531 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
532 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
536 var group = Roo.bootstrap.CheckBox.get(this.groupId);
543 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
544 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
549 clearInvalid : function()
551 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
553 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
555 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
558 label.iconEl.removeClass(label.validClass);
559 label.iconEl.removeClass(label.invalidClass);
565 if(this.inputType != 'radio'){
566 Roo.bootstrap.CheckBox.superclass.disable.call(this);
573 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
574 _this.getActionEl().addClass(this.disabledClass);
575 e.dom.disabled = true;
579 this.disabled = true;
580 this.fireEvent("disable", this);
586 if(this.inputType != 'radio'){
587 Roo.bootstrap.CheckBox.superclass.enable.call(this);
594 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
595 _this.getActionEl().removeClass(this.disabledClass);
596 e.dom.disabled = false;
600 this.disabled = false;
601 this.fireEvent("enable", this);
607 Roo.apply(Roo.bootstrap.CheckBox, {
612 * register a CheckBox Group
613 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
615 register : function(checkbox)
617 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
618 this.groups[checkbox.groupId] = {};
621 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
625 this.groups[checkbox.groupId][checkbox.name] = checkbox;
629 * fetch a CheckBox Group based on the group ID
630 * @param {string} the group ID
631 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
633 get: function(groupId) {
634 if (typeof(this.groups[groupId]) == 'undefined') {
638 return this.groups[groupId] ;