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 invalidClass : "has-warning",
44 validClass : "has-success",
45 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.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
72 cls : 'roo-' + this.inputType, //'form-box',
73 placeholder : this.placeholder || ''
77 if (this.weight) { // Validity check?
78 cfg.cls += " " + this.inputType + "-" + this.weight;
86 input.checked = this.checked;
90 input.name = this.name;
94 input.cls += ' input-' + this.size;
99 ['xs','sm','md','lg'].map(function(size){
100 if (settings[size]) {
101 cfg.cls += ' col-' + size + '-' + settings[size];
105 var inputblock = input;
107 if (this.before || this.after) {
117 cls : 'input-group-addon',
122 inputblock.cn.push(input);
127 cls : 'input-group-addon',
134 if (align ==='left' && this.fieldLabel.length) {
135 Roo.log("left and has label");
141 cls : 'control-label col-md-' + this.labelWidth,
142 html : this.fieldLabel
146 cls : "col-md-" + (12 - this.labelWidth),
153 } else if ( this.fieldLabel.length) {
158 tag: this.boxLabel ? 'span' : 'label',
160 cls: 'control-label box-input-label',
161 //cls : 'input-group-addon',
162 html : this.fieldLabel
172 Roo.log(" no label && no align");
173 cfg.cn = [ inputblock ] ;
180 //'for': id, // box label is handled by onclick - so no for...
186 boxLabelCfg.tooltip = this.tooltip;
189 cfg.cn.push(boxLabelCfg);
199 * return the real input element.
203 return this.el.select('input.roo-' + this.inputType,true).first();
208 return this.el.select('label.control-label',true).first();
214 return this.labelEl();
217 initEvents : function()
219 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
221 this.inputEl().on('click', this.onClick, this);
224 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
227 this.startValue = this.getValue();
230 Roo.bootstrap.CheckBox.register(this);
236 this.setChecked(!this.checked);
239 setChecked : function(state,suppressEvent)
241 this.startValue = this.getValue();
243 if(this.inputType == 'radio'){
245 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
246 e.dom.checked = false;
249 this.inputEl().dom.checked = true;
251 this.inputEl().dom.value = this.inputValue;
253 if(suppressEvent !== true){
254 this.fireEvent('check', this, true);
262 this.checked = state;
264 this.inputEl().dom.checked = state;
266 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
268 if(suppressEvent !== true){
269 this.fireEvent('check', this, state);
275 getValue : function()
277 if(this.inputType == 'radio'){
278 return this.getGroupValue();
281 return this.inputEl().getValue();
285 getGroupValue : function()
287 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
291 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
294 setValue : function(v,suppressEvent)
296 if(this.inputType == 'radio'){
297 this.setGroupValue(v, suppressEvent);
301 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
306 setGroupValue : function(v, suppressEvent)
308 this.startValue = this.getValue();
310 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
311 e.dom.checked = false;
313 if(e.dom.value == v){
314 e.dom.checked = true;
318 if(suppressEvent !== true){
319 this.fireEvent('check', this, true);
327 validate : function()
331 (this.inputType == 'radio' && this.getValue().length) ||
332 (this.inputType == 'checkbox' && this.validateGroup())
342 validateGroup : function()
345 return (this.getValue() == this.inputValue) ? true : false;
348 var group = Roo.bootstrap.CheckBox.get(this.groupId);
361 r = (group[i].getValue() == group[i].inputValue) ? true : false;
368 * Mark this field as valid
370 markValid : function()
374 this.fireEvent('valid', this);
376 if(this.inputType == 'radio'){
377 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
378 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
379 e.findParent('.form-group', false, true).addClass(_this.validClass);
386 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
387 this.el.findParent('.form-group', false, true).addClass(this.validClass);
391 var group = Roo.bootstrap.CheckBox.get(this.groupId);
398 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
399 group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
404 * Mark this field as invalid
405 * @param {String} msg The validation message
407 markInvalid : function(msg)
411 this.fireEvent('invalid', this, msg);
413 if(this.inputType == 'radio'){
414 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
415 e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
416 e.findParent('.form-group', false, true).addClass(_this.invalidClass);
423 this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
424 this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
428 var group = Roo.bootstrap.CheckBox.get(this.groupId);
435 group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
436 group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
443 Roo.apply(Roo.bootstrap.CheckBox, {
448 * register a CheckBox Group
449 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
451 register : function(checkbox)
453 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
454 this.groups[checkbox.groupId] = {};
457 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
461 this.groups[checkbox.groupId][checkbox.name] = checkbox;
465 * fetch a CheckBox Group based on the group ID
466 * @param {string} the group ID
467 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
469 get: function(groupId) {
470 if (typeof(this.groups[groupId]) == 'undefined') {
474 return this.groups[groupId] ;