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) {
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 ] ;
178 //'for': id, // box label is handled by onclick - so no for...
184 boxLabelCfg.tooltip = this.tooltip;
187 cfg.cn.push(boxLabelCfg);
197 * return the real input element.
201 return this.el.select('input.roo-' + this.inputType,true).first();
206 return this.el.select('label.control-label',true).first();
212 return this.labelEl();
215 initEvents : function()
217 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
219 this.inputEl().on('click', this.onClick, this);
222 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
225 this.startValue = this.getValue();
228 Roo.bootstrap.CheckBox.register(this);
234 this.setChecked(!this.checked);
237 setChecked : function(state,suppressEvent)
239 this.startValue = this.getValue();
241 if(this.inputType == 'radio'){
243 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
244 e.dom.checked = false;
247 this.inputEl().dom.checked = true;
249 this.inputEl().dom.value = this.inputValue;
251 if(suppressEvent !== true){
252 this.fireEvent('check', this, true);
260 this.checked = state;
262 this.inputEl().dom.checked = state;
264 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
266 if(suppressEvent !== true){
267 this.fireEvent('check', this, state);
273 getValue : function()
275 if(this.inputType == 'radio'){
276 return this.getGroupValue();
279 return this.inputEl().getValue();
283 getGroupValue : function()
285 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
289 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
292 setValue : function(v,suppressEvent)
294 if(this.inputType == 'radio'){
295 this.setGroupValue(v, suppressEvent);
299 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
304 setGroupValue : function(v, suppressEvent)
306 this.startValue = this.getValue();
308 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
309 e.dom.checked = false;
311 if(e.dom.value == v){
312 e.dom.checked = true;
316 if(suppressEvent !== true){
317 this.fireEvent('check', this, true);
325 validate : function()
329 (this.inputType == 'radio' && this.validateRadio()) ||
330 (this.inputType == 'checkbox' && this.validateCheckbox())
340 validateRadio : function()
344 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
357 validateCheckbox : function()
360 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
363 var group = Roo.bootstrap.CheckBox.get(this.groupId);
376 r = (group[i].getValue() == group[i].inputValue) ? true : false;
383 * Mark this field as valid
385 markValid : function()
393 this.fireEvent('valid', this);
395 if(this.inputType == 'radio'){
396 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
397 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
398 e.findParent('.form-group', false, true).addClass(_this.validClass);
405 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
406 this.el.findParent('.form-group', false, true).addClass(this.validClass);
410 var group = Roo.bootstrap.CheckBox.get(this.groupId);
417 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
418 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
423 * Mark this field as invalid
424 * @param {String} msg The validation message
426 markInvalid : function(msg)
434 this.fireEvent('invalid', this, msg);
436 if(this.inputType == 'radio'){
437 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
438 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
439 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
446 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
447 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
451 var group = Roo.bootstrap.CheckBox.get(this.groupId);
458 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
459 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
466 Roo.apply(Roo.bootstrap.CheckBox, {
471 * register a CheckBox Group
472 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
474 register : function(checkbox)
476 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
477 this.groups[checkbox.groupId] = {};
480 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
484 this.groups[checkbox.groupId][checkbox.name] = checkbox;
488 * fetch a CheckBox Group based on the group ID
489 * @param {string} the group ID
490 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
492 get: function(groupId) {
493 if (typeof(this.groups[groupId]) == 'undefined') {
497 return this.groups[groupId] ;