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.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
70 cls : 'roo-' + this.inputType, //'form-box',
71 placeholder : this.placeholder || ''
75 if (this.weight) { // Validity check?
76 cfg.cls += " " + this.inputType + "-" + this.weight;
84 input.checked = this.checked;
88 input.name = this.name;
92 input.cls += ' input-' + this.size;
97 ['xs','sm','md','lg'].map(function(size){
99 cfg.cls += ' col-' + size + '-' + settings[size];
103 var inputblock = input;
105 if (this.before || this.after) {
115 cls : 'input-group-addon',
120 inputblock.cn.push(input);
125 cls : 'input-group-addon',
132 if (align ==='left' && this.fieldLabel.length) {
133 Roo.log("left and has label");
139 cls : 'control-label col-md-' + this.labelWidth,
140 html : this.fieldLabel
144 cls : "col-md-" + (12 - this.labelWidth),
151 } else if ( this.fieldLabel.length) {
156 tag: this.boxLabel ? 'span' : 'label',
158 cls: 'control-label box-input-label',
159 //cls : 'input-group-addon',
160 html : this.fieldLabel
170 Roo.log(" no label && no align");
171 cfg.cn = [ inputblock ] ;
178 //'for': id, // box label is handled by onclick - so no for...
184 boxLabelCfg.tooltip = this.tooltip;
187 cfg.cn.push(boxLabelCfg);
197 * return the real input element.
201 return this.el.select('input.roo-' + this.inputType,true).first();
206 return this.el.select('label.control-label',true).first();
212 return this.labelEl();
215 initEvents : function()
217 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
219 this.inputEl().on('click', this.onClick, this);
222 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
225 this.startValue = this.getValue();
228 Roo.bootstrap.CheckBox.register(this);
234 this.setChecked(!this.checked);
237 setChecked : function(state,suppressEvent)
239 this.startValue = this.getValue();
241 if(this.inputType == 'radio'){
243 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
244 e.dom.checked = false;
247 this.inputEl().dom.checked = true;
249 this.inputEl().dom.value = this.inputValue;
251 if(suppressEvent !== true){
252 this.fireEvent('check', this, true);
258 this.checked = state;
260 this.inputEl().dom.checked = state;
262 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
264 if(suppressEvent !== true){
265 this.fireEvent('check', this, state);
269 getValue : function()
271 if(this.inputType == 'radio'){
272 return this.getGroupValue();
275 return this.inputEl().getValue();
279 getGroupValue : function()
281 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
285 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
288 setValue : function(v,suppressEvent)
290 if(this.inputType == 'radio'){
291 this.setGroupValue(v, suppressEvent);
296 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
300 setGroupValue : function(v, suppressEvent)
302 this.startValue = this.getValue();
304 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
305 e.dom.checked = false;
307 if(e.dom.value == v){
308 e.dom.checked = true;
312 if(suppressEvent !== true){
313 this.fireEvent('check', this, true);
319 validate : function()
323 (this.inputType == 'radio' && this.getValue().length) ||
324 (this.inputType == 'checkbox' && this.validateGroup())
334 validateGroup : function()
337 return (this.getValue() == this.inputValue) ? true : false;
340 var group = Roo.bootstrap.CheckBox.get(this.groupId);
353 r = (group[i].getValue() == group[i].inputValue) ? true : false;
360 * Mark this field as valid
362 markValid : function()
364 if(this.inputType == 'radio'){
365 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
366 e.removeClass([this.invalidClass, this.validClass]);
367 e.addClass(this.validClass);
374 this.el.removeClass([this.invalidClass, this.validClass]);
375 this.el.addClass(this.validClass);
379 var group = Roo.bootstrap.CheckBox.get(this.groupId);
386 group[i].el.removeClass([this.invalidClass, this.validClass]);
387 group[i].el.addClass(this.validClass);
390 this.fireEvent('valid', this);
394 * Mark this field as invalid
395 * @param {String} msg The validation message
397 markInvalid : function(msg)
399 if(!this.el || this.preventMark){ // not rendered
403 this.el.removeClass([this.invalidClass, this.validClass]);
405 this.el.addClass(this.invalidClass);
407 this.fireEvent('invalid', this, msg);
412 Roo.apply(Roo.bootstrap.CheckBox, {
417 * register a CheckBox Group
418 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
420 register : function(checkbox)
422 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
423 this.groups[checkbox.groupId] = {};
426 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
430 this.groups[checkbox.groupId][checkbox.name] = checkbox;
434 * fetch a CheckBox Group based on the group ID
435 * @param {string} the group ID
436 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
438 get: function(groupId) {
439 if (typeof(this.groups[groupId]) == 'undefined') {
443 return this.groups[groupId] ;