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} targetLabel
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',
52 getAutoCreate : function()
54 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
60 cfg.cls = 'form-group ' + this.inputType; //input-group
63 cfg.cls += ' ' + this.inputType + '-inline';
69 type : this.inputType,
70 value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
71 cls : 'roo-' + this.inputType, //'form-box',
72 placeholder : this.placeholder || ''
76 if (this.weight) { // Validity check?
77 cfg.cls += " " + this.inputType + "-" + this.weight;
85 input.checked = this.checked;
89 input.name = this.name;
93 input.cls += ' input-' + this.size;
98 ['xs','sm','md','lg'].map(function(size){
100 cfg.cls += ' col-' + size + '-' + settings[size];
104 var inputblock = input;
106 if (this.before || this.after) {
116 cls : 'input-group-addon',
121 inputblock.cn.push(input);
126 cls : 'input-group-addon',
133 if (align ==='left' && this.fieldLabel.length) {
134 Roo.log("left and has label");
140 cls : 'control-label col-md-' + this.labelWidth,
141 html : this.fieldLabel
145 cls : "col-md-" + (12 - this.labelWidth),
152 } else if ( this.fieldLabel.length) {
157 tag: this.boxLabel ? 'span' : 'label',
159 cls: 'control-label box-input-label',
160 //cls : 'input-group-addon',
161 html : this.fieldLabel
171 Roo.log(" no label && no align");
172 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 initEvents : function()
218 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
220 this.inputEl().on('click', this.onClick, this);
223 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
226 this.startValue = this.getValue();
229 Roo.bootstrap.CheckBox.register(this);
235 this.setChecked(!this.checked);
238 setChecked : function(state,suppressEvent)
240 this.startValue = this.getValue();
242 if(this.inputType == 'radio'){
244 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
245 e.dom.checked = false;
248 this.inputEl().dom.checked = true;
250 this.inputEl().dom.value = this.inputValue;
252 if(suppressEvent !== true){
253 this.fireEvent('check', this, true);
261 this.checked = state;
263 this.inputEl().dom.checked = state;
265 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
267 if(suppressEvent !== true){
268 this.fireEvent('check', this, state);
274 getValue : function()
276 if(this.inputType == 'radio'){
277 return this.getGroupValue();
280 return this.inputEl().getValue();
284 getGroupValue : function()
286 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
290 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
293 setValue : function(v,suppressEvent)
295 if(this.inputType == 'radio'){
296 this.setGroupValue(v, suppressEvent);
300 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
305 setGroupValue : function(v, suppressEvent)
307 this.startValue = this.getValue();
309 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
310 e.dom.checked = false;
312 if(e.dom.value == v){
313 e.dom.checked = true;
317 if(suppressEvent !== true){
318 this.fireEvent('check', this, true);
326 validate : function()
330 (this.inputType == 'radio' && this.validateRadio()) ||
331 (this.inputType == 'checkbox' && this.validateCheckbox())
341 validateRadio : function()
345 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
358 validateCheckbox : function()
361 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
364 var group = Roo.bootstrap.CheckBox.get(this.groupId);
377 r = (group[i].getValue() == group[i].inputValue) ? true : false;
384 * Mark this field as valid
386 markValid : function()
394 this.fireEvent('valid', this);
396 if(this.inputType == 'radio'){
397 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
398 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
399 e.findParent('.form-group', false, true).addClass(_this.validClass);
406 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
407 this.el.findParent('.form-group', false, true).addClass(this.validClass);
411 var group = Roo.bootstrap.CheckBox.get(this.groupId);
418 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
419 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
424 * Mark this field as invalid
425 * @param {String} msg The validation message
427 markInvalid : function(msg)
435 this.fireEvent('invalid', this, msg);
439 if(this.inputType == 'radio'){
440 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
441 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
442 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
449 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
450 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
454 var group = Roo.bootstrap.CheckBox.get(this.groupId);
461 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
462 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
469 Roo.apply(Roo.bootstrap.CheckBox, {
474 * register a CheckBox Group
475 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
477 register : function(checkbox)
479 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
480 this.groups[checkbox.groupId] = {};
483 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
487 this.groups[checkbox.groupId][checkbox.name] = checkbox;
491 * fetch a CheckBox Group based on the group ID
492 * @param {string} the group ID
493 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
495 get: function(groupId) {
496 if (typeof(this.groups[groupId]) == 'undefined') {
500 return this.groups[groupId] ;