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 || ''
79 cls : 'roo-hidden-value',
80 value : this.checked ? this.valueOff : this.inputValue
83 if (this.weight) { // Validity check?
84 cfg.cls += " " + this.inputType + "-" + this.weight;
92 input.checked = this.checked;
98 hidden.name = this.name;
99 input.name = '_hidden_' + this.name;
103 input.cls += ' input-' + this.size;
108 ['xs','sm','md','lg'].map(function(size){
109 if (settings[size]) {
110 cfg.cls += ' col-' + size + '-' + settings[size];
122 if (this.before || this.after) {
132 cls : 'input-group-addon',
137 inputblock.cn.push(input);
138 inputblock.cn.push(hidden);
143 cls : 'input-group-addon',
150 if (align ==='left' && this.fieldLabel.length) {
151 // Roo.log("left and has label");
157 cls : 'control-label col-md-' + this.labelWidth,
158 html : this.fieldLabel
162 cls : "col-md-" + (12 - this.labelWidth),
169 } else if ( this.fieldLabel.length) {
170 // Roo.log(" label");
174 tag: this.boxLabel ? 'span' : 'label',
176 cls: 'control-label box-input-label',
177 //cls : 'input-group-addon',
178 html : this.fieldLabel
188 // Roo.log(" no label && no align");
189 cfg.cn = [ inputblock ] ;
197 //'for': id, // box label is handled by onclick - so no for...
203 boxLabelCfg.tooltip = this.tooltip;
206 cfg.cn.push(boxLabelCfg);
216 * return the real input element.
220 return this.el.select('input.roo-' + this.inputType,true).first();
222 hiddenEl: function ()
224 return this.el.select('input.hidden-value',true).first();
229 return this.el.select('label.control-label',true).first();
235 return this.labelEl();
238 boxLabelEl: function()
240 return this.el.select('label.box-label',true).first();
243 initEvents : function()
245 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
247 this.inputEl().on('click', this.onClick, this);
250 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
253 this.startValue = this.getValue();
256 Roo.bootstrap.CheckBox.register(this);
262 this.setChecked(!this.checked);
265 setChecked : function(state,suppressEvent)
267 this.startValue = this.getValue();
269 if(this.inputType == 'radio'){
271 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
272 e.dom.checked = false;
275 this.inputEl().dom.checked = true;
277 this.inputEl().dom.value = this.inputValue;
279 if(suppressEvent !== true){
280 this.fireEvent('check', this, true);
288 this.checked = state;
290 this.inputEl().dom.checked = state;
293 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
295 if(suppressEvent !== true){
296 this.fireEvent('check', this, state);
302 getValue : function()
304 if(this.inputType == 'radio'){
305 return this.getGroupValue();
308 return this.hiddenEl() ? this.hiddenEl().dom.value : this.value;
312 getGroupValue : function()
314 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
318 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
321 setValue : function(v,suppressEvent)
323 if(this.inputType == 'radio'){
324 this.setGroupValue(v, suppressEvent);
328 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
333 setGroupValue : function(v, suppressEvent)
335 this.startValue = this.getValue();
337 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
338 e.dom.checked = false;
340 if(e.dom.value == v){
341 e.dom.checked = true;
345 if(suppressEvent !== true){
346 this.fireEvent('check', this, true);
354 validate : function()
358 (this.inputType == 'radio' && this.validateRadio()) ||
359 (this.inputType == 'checkbox' && this.validateCheckbox())
369 validateRadio : function()
377 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
390 validateCheckbox : function()
393 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
396 var group = Roo.bootstrap.CheckBox.get(this.groupId);
409 r = (group[i].getValue() == group[i].inputValue) ? true : false;
416 * Mark this field as valid
418 markValid : function()
426 this.fireEvent('valid', this);
428 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
431 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
438 if(this.inputType == 'radio'){
439 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
440 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
441 e.findParent('.form-group', false, true).addClass(_this.validClass);
448 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
449 this.el.findParent('.form-group', false, true).addClass(this.validClass);
453 var group = Roo.bootstrap.CheckBox.get(this.groupId);
460 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
461 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
466 * Mark this field as invalid
467 * @param {String} msg The validation message
469 markInvalid : function(msg)
477 this.fireEvent('invalid', this, msg);
479 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
482 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
489 if(this.inputType == 'radio'){
490 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
491 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
492 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
499 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
500 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
504 var group = Roo.bootstrap.CheckBox.get(this.groupId);
511 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
512 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
519 if(this.inputType != 'radio'){
520 Roo.bootstrap.CheckBox.superclass.disable.call(this);
527 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
528 _this.getActionEl().addClass(this.disabledClass);
529 e.dom.disabled = true;
533 this.disabled = true;
534 this.fireEvent("disable", this);
540 if(this.inputType != 'radio'){
541 Roo.bootstrap.CheckBox.superclass.enable.call(this);
548 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
549 _this.getActionEl().removeClass(this.disabledClass);
550 e.dom.disabled = false;
554 this.disabled = false;
555 this.fireEvent("enable", this);
562 Roo.apply(Roo.bootstrap.CheckBox, {
567 * register a CheckBox Group
568 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
570 register : function(checkbox)
572 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
573 this.groups[checkbox.groupId] = {};
576 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
580 this.groups[checkbox.groupId][checkbox.name] = checkbox;
584 * fetch a CheckBox Group based on the group ID
585 * @param {string} the group ID
586 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
588 get: function(groupId) {
589 if (typeof(this.groups[groupId]) == 'undefined') {
593 return this.groups[groupId] ;