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 invalidClass : "has-warning",
44 validClass : "has-success",
45 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.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
72 cls : 'roo-' + this.inputType, //'form-box',
73 placeholder : this.placeholder || ''
77 if (this.weight) { // Validity check?
78 cfg.cls += " " + this.inputType + "-" + this.weight;
86 input.checked = this.checked;
90 input.name = this.name;
94 input.cls += ' input-' + this.size;
99 ['xs','sm','md','lg'].map(function(size){
100 if (settings[size]) {
101 cfg.cls += ' col-' + size + '-' + settings[size];
105 var inputblock = input;
107 if (this.before || this.after) {
117 cls : 'input-group-addon',
122 inputblock.cn.push(input);
127 cls : 'input-group-addon',
134 if (align ==='left' && this.fieldLabel.length) {
135 Roo.log("left and has label");
141 cls : 'control-label col-md-' + this.labelWidth,
142 html : this.fieldLabel
146 cls : "col-md-" + (12 - this.labelWidth),
153 } else if ( this.fieldLabel.length) {
158 tag: this.boxLabel ? 'span' : 'label',
160 cls: 'control-label box-input-label',
161 //cls : 'input-group-addon',
162 html : this.fieldLabel
172 Roo.log(" no label && no align");
173 cfg.cn = [ inputblock ] ;
180 //'for': id, // box label is handled by onclick - so no for...
186 boxLabelCfg.tooltip = this.tooltip;
189 cfg.cn.push(boxLabelCfg);
199 * return the real input element.
203 return this.el.select('input.roo-' + this.inputType,true).first();
208 return this.el.select('label.control-label',true).first();
214 return this.labelEl();
217 initEvents : function()
219 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
221 this.inputEl().on('click', this.onClick, this);
224 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
227 this.startValue = this.getValue();
230 Roo.bootstrap.CheckBox.register(this);
236 this.setChecked(!this.checked);
239 setChecked : function(state,suppressEvent)
241 this.startValue = this.getValue();
243 if(this.inputType == 'radio'){
245 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
246 e.dom.checked = false;
249 this.inputEl().dom.checked = true;
251 this.inputEl().dom.value = this.inputValue;
253 if(suppressEvent !== true){
254 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);
271 getValue : function()
273 if(this.inputType == 'radio'){
274 return this.getGroupValue();
277 return this.inputEl().getValue();
281 getGroupValue : function()
283 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
287 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
290 setValue : function(v,suppressEvent)
292 if(this.inputType == 'radio'){
293 this.setGroupValue(v, suppressEvent);
298 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
302 setGroupValue : function(v, suppressEvent)
304 this.startValue = this.getValue();
306 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
307 e.dom.checked = false;
309 if(e.dom.value == v){
310 e.dom.checked = true;
314 if(suppressEvent !== true){
315 this.fireEvent('check', this, true);
321 validate : function()
325 (this.inputType == 'radio' && this.getValue().length) ||
326 (this.inputType == 'checkbox' && this.validateGroup())
336 validateGroup : function()
339 return (this.getValue() == this.inputValue) ? true : false;
342 var group = Roo.bootstrap.CheckBox.get(this.groupId);
355 r = (group[i].getValue() == group[i].inputValue) ? true : false;
362 * Mark this field as valid
364 markValid : function()
368 this.fireEvent('valid', this);
370 if(this.inputType == 'radio'){
371 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
372 e.removeClass([_this.invalidClass, _this.validClass]);
373 e.addClass(_this.validClass);
380 this.el.removeClass([this.invalidClass, this.validClass]);
381 this.el.addClass(this.validClass);
385 var group = Roo.bootstrap.CheckBox.get(this.groupId);
392 group[i].el.removeClass([this.invalidClass, this.validClass]);
393 group[i].el.addClass(this.validClass);
398 * Mark this field as invalid
399 * @param {String} msg The validation message
401 markInvalid : function(msg)
405 this.fireEvent('invalid', this, msg);
407 if(this.inputType == 'radio'){
408 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
409 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
410 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
417 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
418 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
422 var group = Roo.bootstrap.CheckBox.get(this.groupId);
429 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
430 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
437 Roo.apply(Roo.bootstrap.CheckBox, {
442 * register a CheckBox Group
443 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
445 register : function(checkbox)
447 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
448 this.groups[checkbox.groupId] = {};
451 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
455 this.groups[checkbox.groupId][checkbox.name] = checkbox;
459 * fetch a CheckBox Group based on the group ID
460 * @param {string} the group ID
461 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
463 get: function(groupId) {
464 if (typeof(this.groups[groupId]) == 'undefined') {
468 return this.groups[groupId] ;