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.inputValue,
70 cls : 'roo-' + this.inputType, //'form-box',
71 placeholder : this.placeholder || ''
75 if(this.inputType != 'radio'){
79 cls : 'roo-hidden-value',
80 value : this.checked ? this.valueOff : this.inputValue
85 if (this.weight) { // Validity check?
86 cfg.cls += " " + this.inputType + "-" + this.weight;
94 input.checked = this.checked;
101 input.name = this.name;
103 if(this.inputType != 'radio'){
104 hidden.name = this.name;
105 input.name = '_hidden_' + this.name;
110 input.cls += ' input-' + this.size;
115 ['xs','sm','md','lg'].map(function(size){
116 if (settings[size]) {
117 cfg.cls += ' col-' + size + '-' + settings[size];
121 var inputblock = input;
123 if (this.before || this.after) {
133 cls : 'input-group-addon',
138 inputblock.cn.push(input);
140 if(this.inputType != 'radio'){
141 inputblock.cn.push(hidden);
147 cls : 'input-group-addon',
154 if (align ==='left' && this.fieldLabel.length) {
155 // Roo.log("left and has label");
161 cls : 'control-label col-md-' + this.labelWidth,
162 html : this.fieldLabel
166 cls : "col-md-" + (12 - this.labelWidth),
173 } else if ( this.fieldLabel.length) {
174 // Roo.log(" label");
178 tag: this.boxLabel ? 'span' : 'label',
180 cls: 'control-label box-input-label',
181 //cls : 'input-group-addon',
182 html : this.fieldLabel
192 // Roo.log(" no label && no align");
193 cfg.cn = [ inputblock ] ;
201 //'for': id, // box label is handled by onclick - so no for...
207 boxLabelCfg.tooltip = this.tooltip;
210 cfg.cn.push(boxLabelCfg);
213 if(this.inputType != 'radio'){
222 * return the real input element.
226 return this.el.select('input.roo-' + this.inputType,true).first();
228 hiddenEl: function ()
230 return this.el.select('input.roo-hidden-value',true).first();
235 return this.el.select('label.control-label',true).first();
241 return this.labelEl();
244 boxLabelEl: function()
246 return this.el.select('label.box-label',true).first();
249 initEvents : function()
251 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
253 this.inputEl().on('click', this.onClick, this);
256 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
259 this.startValue = this.getValue();
262 Roo.bootstrap.CheckBox.register(this);
268 this.setChecked(!this.checked);
271 setChecked : function(state,suppressEvent)
273 this.startValue = this.getValue();
275 if(this.inputType == 'radio'){
277 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
278 e.dom.checked = false;
281 this.inputEl().dom.checked = true;
283 this.inputEl().dom.value = this.inputValue;
285 if(suppressEvent !== true){
286 this.fireEvent('check', this, true);
294 this.checked = state;
296 this.inputEl().dom.checked = state;
299 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
301 if(suppressEvent !== true){
302 this.fireEvent('check', this, state);
308 getValue : function()
310 if(this.inputType == 'radio'){
311 return this.getGroupValue();
314 return this.hiddenEl().dom.value;
318 getGroupValue : function()
320 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
324 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
327 setValue : function(v,suppressEvent)
329 if(this.inputType == 'radio'){
330 this.setGroupValue(v, suppressEvent);
334 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
339 setGroupValue : function(v, suppressEvent)
341 this.startValue = this.getValue();
343 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
344 e.dom.checked = false;
346 if(e.dom.value == v){
347 e.dom.checked = true;
351 if(suppressEvent !== true){
352 this.fireEvent('check', this, true);
360 validate : function()
364 (this.inputType == 'radio' && this.validateRadio()) ||
365 (this.inputType == 'checkbox' && this.validateCheckbox())
375 validateRadio : function()
383 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
396 validateCheckbox : function()
399 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
402 var group = Roo.bootstrap.CheckBox.get(this.groupId);
415 r = (group[i].getValue() == group[i].inputValue) ? true : false;
422 * Mark this field as valid
424 markValid : function()
432 this.fireEvent('valid', this);
434 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
437 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
444 if(this.inputType == 'radio'){
445 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
446 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
447 e.findParent('.form-group', false, true).addClass(_this.validClass);
454 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
455 this.el.findParent('.form-group', false, true).addClass(this.validClass);
459 var group = Roo.bootstrap.CheckBox.get(this.groupId);
466 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
467 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
472 * Mark this field as invalid
473 * @param {String} msg The validation message
475 markInvalid : function(msg)
483 this.fireEvent('invalid', this, msg);
485 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
488 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
495 if(this.inputType == 'radio'){
496 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
497 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
498 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
505 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
506 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
510 var group = Roo.bootstrap.CheckBox.get(this.groupId);
517 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
518 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
523 clearInvalid : function()
525 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
527 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
530 label.iconEl.removeClass(label.validClass);
531 label.iconEl.removeClass(label.invalidClass);
537 if(this.inputType != 'radio'){
538 Roo.bootstrap.CheckBox.superclass.disable.call(this);
545 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
546 _this.getActionEl().addClass(this.disabledClass);
547 e.dom.disabled = true;
551 this.disabled = true;
552 this.fireEvent("disable", this);
558 if(this.inputType != 'radio'){
559 Roo.bootstrap.CheckBox.superclass.enable.call(this);
566 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
567 _this.getActionEl().removeClass(this.disabledClass);
568 e.dom.disabled = false;
572 this.disabled = false;
573 this.fireEvent("enable", this);
579 Roo.apply(Roo.bootstrap.CheckBox, {
584 * register a CheckBox Group
585 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
587 register : function(checkbox)
589 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
590 this.groups[checkbox.groupId] = {};
593 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
597 this.groups[checkbox.groupId][checkbox.name] = checkbox;
601 * fetch a CheckBox Group based on the group ID
602 * @param {string} the group ID
603 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
605 get: function(groupId) {
606 if (typeof(this.groups[groupId]) == 'undefined') {
610 return this.groups[groupId] ;