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.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
70 cls : 'roo-' + this.inputType, //'form-box',
71 placeholder : this.placeholder || ''
75 if (this.weight) { // Validity check?
76 cfg.cls += " " + this.inputType + "-" + this.weight;
84 input.checked = this.checked;
88 input.name = this.name;
92 input.cls += ' input-' + this.size;
97 ['xs','sm','md','lg'].map(function(size){
99 cfg.cls += ' col-' + size + '-' + settings[size];
103 var inputblock = input;
105 if (this.before || this.after) {
115 cls : 'input-group-addon',
120 inputblock.cn.push(input);
125 cls : 'input-group-addon',
132 if (align ==='left' && this.fieldLabel.length) {
133 // Roo.log("left and has label");
139 cls : 'control-label col-md-' + this.labelWidth,
140 html : this.fieldLabel
144 cls : "col-md-" + (12 - this.labelWidth),
151 } else if ( this.fieldLabel.length) {
152 // Roo.log(" label");
156 tag: this.boxLabel ? 'span' : 'label',
158 cls: 'control-label box-input-label',
159 //cls : 'input-group-addon',
160 html : this.fieldLabel
170 // Roo.log(" no label && no align");
171 cfg.cn = [ inputblock ] ;
179 //'for': id, // box label is handled by onclick - so no for...
185 boxLabelCfg.tooltip = this.tooltip;
188 cfg.cn.push(boxLabelCfg);
198 * return the real input element.
202 return this.el.select('input.roo-' + this.inputType,true).first();
207 return this.el.select('label.control-label',true).first();
213 return this.labelEl();
216 boxLabelEl: function()
218 return this.el.select('label.box-label',true).first();
221 initEvents : function()
223 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
225 this.inputEl().on('click', this.onClick, this);
228 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
231 this.startValue = this.getValue();
234 Roo.bootstrap.CheckBox.register(this);
240 this.setChecked(!this.checked);
243 setChecked : function(state,suppressEvent)
245 this.startValue = this.getValue();
247 if(this.inputType == 'radio'){
249 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
250 e.dom.checked = false;
253 this.inputEl().dom.checked = true;
255 this.inputEl().dom.value = this.inputValue;
257 if(suppressEvent !== true){
258 this.fireEvent('check', this, true);
266 this.checked = state;
268 this.inputEl().dom.checked = state;
270 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
272 if(suppressEvent !== true){
273 this.fireEvent('check', this, state);
279 getValue : function()
281 if(this.inputType == 'radio'){
282 return this.getGroupValue();
285 return this.inputEl().getValue();
289 getGroupValue : function()
291 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
295 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
298 setValue : function(v,suppressEvent)
300 if(this.inputType == 'radio'){
301 this.setGroupValue(v, suppressEvent);
305 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
310 setGroupValue : function(v, suppressEvent)
312 this.startValue = this.getValue();
314 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
315 e.dom.checked = false;
317 if(e.dom.value == v){
318 e.dom.checked = true;
322 if(suppressEvent !== true){
323 this.fireEvent('check', this, true);
331 validate : function()
335 (this.inputType == 'radio' && this.validateRadio()) ||
336 (this.inputType == 'checkbox' && this.validateCheckbox())
346 validateRadio : function()
354 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
367 validateCheckbox : function()
370 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
373 var group = Roo.bootstrap.CheckBox.get(this.groupId);
386 r = (group[i].getValue() == group[i].inputValue) ? true : false;
393 * Mark this field as valid
395 markValid : function()
403 this.fireEvent('valid', this);
405 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
408 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
415 if(this.inputType == 'radio'){
416 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
417 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
418 e.findParent('.form-group', false, true).addClass(_this.validClass);
425 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
426 this.el.findParent('.form-group', false, true).addClass(this.validClass);
430 var group = Roo.bootstrap.CheckBox.get(this.groupId);
437 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
438 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
443 * Mark this field as invalid
444 * @param {String} msg The validation message
446 markInvalid : function(msg)
454 this.fireEvent('invalid', this, msg);
456 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
459 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
466 if(this.inputType == 'radio'){
467 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
468 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
469 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
476 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
477 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
481 var group = Roo.bootstrap.CheckBox.get(this.groupId);
488 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
489 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
496 if(this.inputType != 'radio'){
497 Roo.bootstrap.CheckBox.superclass.disable.call(this);
504 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
505 _this.getActionEl().addClass(this.disabledClass);
506 e.dom.disabled = true;
510 this.disabled = true;
511 this.fireEvent("disable", this);
517 if(this.inputType != 'radio'){
518 Roo.bootstrap.CheckBox.superclass.enable.call(this);
525 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
526 _this.getActionEl().removeClass(this.disabledClass);
527 e.dom.disabled = false;
531 this.disabled = false;
532 this.fireEvent("enable", this);
539 Roo.apply(Roo.bootstrap.CheckBox, {
544 * register a CheckBox Group
545 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
547 register : function(checkbox)
549 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
550 this.groups[checkbox.groupId] = {};
553 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
557 this.groups[checkbox.groupId][checkbox.name] = checkbox;
561 * fetch a CheckBox Group based on the group ID
562 * @param {string} the group ID
563 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
565 get: function(groupId) {
566 if (typeof(this.groups[groupId]) == 'undefined') {
570 return this.groups[groupId] ;