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 || ''
78 Roo.log(this.checked);
80 if(this.inputType != 'radio'){
84 cls : 'roo-hidden-value',
85 value : this.checked ? this.valueOff : this.inputValue
90 if (this.weight) { // Validity check?
91 cfg.cls += " " + this.inputType + "-" + this.weight;
99 input.checked = this.checked;
104 input.name = this.name;
106 if(this.inputType != 'radio'){
107 hidden.name = this.name;
108 input.name = '_hidden_' + this.name;
113 input.cls += ' input-' + this.size;
118 ['xs','sm','md','lg'].map(function(size){
119 if (settings[size]) {
120 cfg.cls += ' col-' + size + '-' + settings[size];
124 var inputblock = input;
126 if (this.before || this.after) {
136 cls : 'input-group-addon',
141 inputblock.cn.push(input);
143 if(this.inputType != 'radio'){
144 inputblock.cn.push(hidden);
150 cls : 'input-group-addon',
157 if (align ==='left' && this.fieldLabel.length) {
158 // Roo.log("left and has label");
163 cls : 'control-label',
164 html : this.fieldLabel
174 if(this.labelWidth > 12){
175 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
178 if(this.labelWidth < 13 && this.labelmd == 0){
179 this.labelmd = this.labelWidth;
182 if(this.labellg > 0){
183 cfg.cn[0].cls += ' col-lg-' + this.labellg;
184 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
187 if(this.labelmd > 0){
188 cfg.cn[0].cls += ' col-md-' + this.labelmd;
189 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
192 if(this.labelsm > 0){
193 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
194 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
197 if(this.labelxs > 0){
198 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
199 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
202 } else if ( this.fieldLabel.length) {
203 // Roo.log(" label");
207 tag: this.boxLabel ? 'span' : 'label',
209 cls: 'control-label box-input-label',
210 //cls : 'input-group-addon',
211 html : this.fieldLabel
220 // Roo.log(" no label && no align");
221 cfg.cn = [ inputblock ] ;
229 //'for': id, // box label is handled by onclick - so no for...
235 boxLabelCfg.tooltip = this.tooltip;
238 cfg.cn.push(boxLabelCfg);
241 if(this.inputType != 'radio'){
250 * return the real input element.
254 return this.el.select('input.roo-' + this.inputType,true).first();
256 hiddenEl: function ()
258 return this.el.select('input.roo-hidden-value',true).first();
263 return this.el.select('label.control-label',true).first();
269 return this.labelEl();
272 boxLabelEl: function()
274 return this.el.select('label.box-label',true).first();
277 initEvents : function()
279 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
281 this.inputEl().on('click', this.onClick, this);
284 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
287 this.startValue = this.getValue();
290 Roo.bootstrap.CheckBox.register(this);
296 this.setChecked(!this.checked);
299 setChecked : function(state,suppressEvent)
301 this.startValue = this.getValue();
303 if(this.inputType == 'radio'){
305 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
306 e.dom.checked = false;
309 this.inputEl().dom.checked = true;
311 this.inputEl().dom.value = this.inputValue;
313 if(suppressEvent !== true){
314 this.fireEvent('check', this, true);
322 this.checked = state;
324 this.inputEl().dom.checked = state;
327 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
329 if(suppressEvent !== true){
330 this.fireEvent('check', this, state);
336 getValue : function()
338 if(this.inputType == 'radio'){
339 return this.getGroupValue();
342 return this.hiddenEl().dom.value;
346 getGroupValue : function()
348 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
352 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
355 setValue : function(v,suppressEvent)
357 if(this.inputType == 'radio'){
358 this.setGroupValue(v, suppressEvent);
362 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
367 setGroupValue : function(v, suppressEvent)
369 this.startValue = this.getValue();
371 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
372 e.dom.checked = false;
374 if(e.dom.value == v){
375 e.dom.checked = true;
379 if(suppressEvent !== true){
380 this.fireEvent('check', this, true);
388 validate : function()
392 (this.inputType == 'radio' && this.validateRadio()) ||
393 (this.inputType == 'checkbox' && this.validateCheckbox())
403 validateRadio : function()
411 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
424 validateCheckbox : function()
427 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
428 //return (this.getValue() == this.inputValue) ? true : false;
431 var group = Roo.bootstrap.CheckBox.get(this.groupId);
444 r = (group[i].getValue() == group[i].inputValue) ? true : false;
451 * Mark this field as valid
453 markValid : function()
457 this.fireEvent('valid', this);
459 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
462 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
469 if(this.inputType == 'radio'){
470 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
471 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
472 e.findParent('.form-group', false, true).addClass(_this.validClass);
479 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
480 this.el.findParent('.form-group', false, true).addClass(this.validClass);
484 var group = Roo.bootstrap.CheckBox.get(this.groupId);
491 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
492 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
497 * Mark this field as invalid
498 * @param {String} msg The validation message
500 markInvalid : function(msg)
508 this.fireEvent('invalid', this, msg);
510 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
513 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
520 if(this.inputType == 'radio'){
521 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
522 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
523 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
530 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
531 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
535 var group = Roo.bootstrap.CheckBox.get(this.groupId);
542 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
543 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
548 clearInvalid : function()
550 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
552 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
554 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
557 label.iconEl.removeClass(label.validClass);
558 label.iconEl.removeClass(label.invalidClass);
564 if(this.inputType != 'radio'){
565 Roo.bootstrap.CheckBox.superclass.disable.call(this);
572 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
573 _this.getActionEl().addClass(this.disabledClass);
574 e.dom.disabled = true;
578 this.disabled = true;
579 this.fireEvent("disable", this);
585 if(this.inputType != 'radio'){
586 Roo.bootstrap.CheckBox.superclass.enable.call(this);
593 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
594 _this.getActionEl().removeClass(this.disabledClass);
595 e.dom.disabled = false;
599 this.disabled = false;
600 this.fireEvent("enable", this);
606 Roo.apply(Roo.bootstrap.CheckBox, {
611 * register a CheckBox Group
612 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
614 register : function(checkbox)
616 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
617 this.groups[checkbox.groupId] = {};
620 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
624 this.groups[checkbox.groupId][checkbox.name] = checkbox;
628 * fetch a CheckBox Group based on the group ID
629 * @param {string} the group ID
630 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
632 get: function(groupId) {
633 if (typeof(this.groups[groupId]) == 'undefined') {
637 return this.groups[groupId] ;