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...
229 tooltip : this.tooltip ? this.tooltip : ''
233 boxLabelCfg.tooltip = this.tooltip;
236 cfg.cn.push(boxLabelCfg);
239 if(this.inputType != 'radio'){
248 * return the real input element.
252 return this.el.select('input.roo-' + this.inputType,true).first();
254 hiddenEl: function ()
256 return this.el.select('input.roo-hidden-value',true).first();
261 return this.el.select('label.control-label',true).first();
267 return this.labelEl();
270 boxLabelEl: function()
272 return this.el.select('label.box-label',true).first();
275 initEvents : function()
277 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
279 this.inputEl().on('click', this.onClick, this);
282 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
285 this.startValue = this.getValue();
288 Roo.bootstrap.CheckBox.register(this);
294 this.setChecked(!this.checked);
297 setChecked : function(state,suppressEvent)
299 this.startValue = this.getValue();
301 if(this.inputType == 'radio'){
303 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
304 e.dom.checked = false;
307 this.inputEl().dom.checked = true;
309 this.inputEl().dom.value = this.inputValue;
311 if(suppressEvent !== true){
312 this.fireEvent('check', this, true);
320 this.checked = state;
322 this.inputEl().dom.checked = state;
325 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
327 if(suppressEvent !== true){
328 this.fireEvent('check', this, state);
334 getValue : function()
336 if(this.inputType == 'radio'){
337 return this.getGroupValue();
340 return this.hiddenEl().dom.value;
344 getGroupValue : function()
346 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
350 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
353 setValue : function(v,suppressEvent)
355 if(this.inputType == 'radio'){
356 this.setGroupValue(v, suppressEvent);
360 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
365 setGroupValue : function(v, suppressEvent)
367 this.startValue = this.getValue();
369 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
370 e.dom.checked = false;
372 if(e.dom.value == v){
373 e.dom.checked = true;
377 if(suppressEvent !== true){
378 this.fireEvent('check', this, true);
386 validate : function()
390 (this.inputType == 'radio' && this.validateRadio()) ||
391 (this.inputType == 'checkbox' && this.validateCheckbox())
401 validateRadio : function()
409 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
422 validateCheckbox : function()
425 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
426 //return (this.getValue() == this.inputValue) ? true : false;
429 var group = Roo.bootstrap.CheckBox.get(this.groupId);
442 r = (group[i].getValue() == group[i].inputValue) ? true : false;
449 * Mark this field as valid
451 markValid : function()
455 this.fireEvent('valid', this);
457 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
460 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
467 if(this.inputType == 'radio'){
468 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
469 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
470 e.findParent('.form-group', false, true).addClass(_this.validClass);
477 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
478 this.el.findParent('.form-group', false, true).addClass(this.validClass);
482 var group = Roo.bootstrap.CheckBox.get(this.groupId);
489 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
490 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
495 * Mark this field as invalid
496 * @param {String} msg The validation message
498 markInvalid : function(msg)
506 this.fireEvent('invalid', this, msg);
508 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
511 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
518 if(this.inputType == 'radio'){
519 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
520 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
521 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
528 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
529 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
533 var group = Roo.bootstrap.CheckBox.get(this.groupId);
540 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
541 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
546 clearInvalid : function()
548 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
550 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
552 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
555 label.iconEl.removeClass(label.validClass);
556 label.iconEl.removeClass(label.invalidClass);
562 if(this.inputType != 'radio'){
563 Roo.bootstrap.CheckBox.superclass.disable.call(this);
570 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
571 _this.getActionEl().addClass(this.disabledClass);
572 e.dom.disabled = true;
576 this.disabled = true;
577 this.fireEvent("disable", this);
583 if(this.inputType != 'radio'){
584 Roo.bootstrap.CheckBox.superclass.enable.call(this);
591 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
592 _this.getActionEl().removeClass(this.disabledClass);
593 e.dom.disabled = false;
597 this.disabled = false;
598 this.fireEvent("enable", this);
604 Roo.apply(Roo.bootstrap.CheckBox, {
609 * register a CheckBox Group
610 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
612 register : function(checkbox)
614 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
615 this.groups[checkbox.groupId] = {};
618 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
622 this.groups[checkbox.groupId][checkbox.name] = checkbox;
626 * fetch a CheckBox Group based on the group ID
627 * @param {string} the group ID
628 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
630 get: function(groupId) {
631 if (typeof(this.groups[groupId]) == 'undefined') {
635 return this.groups[groupId] ;