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} tooltip label tooltip
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',
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.inputValue,
72 cls : 'roo-' + this.inputType, //'form-box',
73 placeholder : this.placeholder || ''
77 if(this.inputType != 'radio'){
81 cls : 'roo-hidden-value',
82 value : this.checked ? this.valueOff : this.inputValue
87 if (this.weight) { // Validity check?
88 cfg.cls += " " + this.inputType + "-" + this.weight;
96 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");
160 cls : 'control-label',
161 html : this.fieldLabel
171 if(this.labelWidth > 12){
172 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
175 if(this.labelWidth < 13 && this.labelmd == 0){
176 this.labelmd = this.labelWidth;
179 if(this.labellg > 0){
180 cfg.cn[0].cls += ' col-lg-' + this.labellg;
181 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
184 if(this.labelmd > 0){
185 cfg.cn[0].cls += ' col-md-' + this.labelmd;
186 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
189 if(this.labelsm > 0){
190 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
191 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
194 if(this.labelxs > 0){
195 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
196 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
199 } else if ( this.fieldLabel.length) {
200 // Roo.log(" label");
204 tag: this.boxLabel ? 'span' : 'label',
206 cls: 'control-label box-input-label',
207 //cls : 'input-group-addon',
208 html : this.fieldLabel
217 // Roo.log(" no label && no align");
218 cfg.cn = [ inputblock ] ;
226 //'for': id, // box label is handled by onclick - so no for...
232 boxLabelCfg.tooltip = this.tooltip;
235 cfg.cn.push(boxLabelCfg);
238 if(this.inputType != 'radio'){
247 * return the real input element.
251 return this.el.select('input.roo-' + this.inputType,true).first();
253 hiddenEl: function ()
255 return this.el.select('input.roo-hidden-value',true).first();
260 return this.el.select('label.control-label',true).first();
266 return this.labelEl();
269 boxLabelEl: function()
271 return this.el.select('label.box-label',true).first();
274 initEvents : function()
276 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
278 this.inputEl().on('click', this.onClick, this);
281 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
284 this.startValue = this.getValue();
287 Roo.bootstrap.CheckBox.register(this);
293 this.setChecked(!this.checked);
296 setChecked : function(state,suppressEvent)
298 this.startValue = this.getValue();
300 if(this.inputType == 'radio'){
302 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
303 e.dom.checked = false;
306 this.inputEl().dom.checked = true;
308 this.inputEl().dom.value = this.inputValue;
310 if(suppressEvent !== true){
311 this.fireEvent('check', this, true);
319 this.checked = state;
321 this.inputEl().dom.checked = state;
324 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
326 if(suppressEvent !== true){
327 this.fireEvent('check', this, state);
333 getValue : function()
335 if(this.inputType == 'radio'){
336 return this.getGroupValue();
339 return this.hiddenEl().dom.value;
343 getGroupValue : function()
345 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
349 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
352 setValue : function(v,suppressEvent)
354 if(this.inputType == 'radio'){
355 this.setGroupValue(v, suppressEvent);
359 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
364 setGroupValue : function(v, suppressEvent)
366 this.startValue = this.getValue();
368 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
369 e.dom.checked = false;
371 if(e.dom.value == v){
372 e.dom.checked = true;
376 if(suppressEvent !== true){
377 this.fireEvent('check', this, true);
385 validate : function()
389 (this.inputType == 'radio' && this.validateRadio()) ||
390 (this.inputType == 'checkbox' && this.validateCheckbox())
400 validateRadio : function()
408 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
421 validateCheckbox : function()
424 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
425 //return (this.getValue() == this.inputValue) ? true : false;
428 var group = Roo.bootstrap.CheckBox.get(this.groupId);
441 r = (group[i].getValue() == group[i].inputValue) ? true : false;
448 * Mark this field as valid
450 markValid : function()
454 this.fireEvent('valid', this);
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.validClass);
476 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
477 this.el.findParent('.form-group', false, true).addClass(this.validClass);
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.validClass);
494 * Mark this field as invalid
495 * @param {String} msg The validation message
497 markInvalid : function(msg)
505 this.fireEvent('invalid', this, msg);
507 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
510 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
517 if(this.inputType == 'radio'){
518 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
519 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
520 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
527 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
528 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
532 var group = Roo.bootstrap.CheckBox.get(this.groupId);
539 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
540 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
545 clearInvalid : function()
547 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
549 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
551 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
554 label.iconEl.removeClass(label.validClass);
555 label.iconEl.removeClass(label.invalidClass);
561 if(this.inputType != 'radio'){
562 Roo.bootstrap.CheckBox.superclass.disable.call(this);
569 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
570 _this.getActionEl().addClass(this.disabledClass);
571 e.dom.disabled = true;
575 this.disabled = true;
576 this.fireEvent("disable", this);
582 if(this.inputType != 'radio'){
583 Roo.bootstrap.CheckBox.superclass.enable.call(this);
590 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
591 _this.getActionEl().removeClass(this.disabledClass);
592 e.dom.disabled = false;
596 this.disabled = false;
597 this.fireEvent("enable", this);
603 Roo.apply(Roo.bootstrap.CheckBox, {
608 * register a CheckBox Group
609 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
611 register : function(checkbox)
613 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
614 this.groups[checkbox.groupId] = {};
617 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
621 this.groups[checkbox.groupId][checkbox.name] = checkbox;
625 * fetch a CheckBox Group based on the group ID
626 * @param {string} the group ID
627 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
629 get: function(groupId) {
630 if (typeof(this.groups[groupId]) == 'undefined') {
634 return this.groups[groupId] ;