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);
295 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
298 setGroupValue : function(v, suppressEvent)
300 this.startValue = this.getValue();
302 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
303 e.dom.checked = false;
305 if(e.dom.value == v){
306 e.dom.checked = true;
310 if(suppressEvent !== true){
311 this.fireEvent('check', this, true);
317 validate : function()
321 (this.inputType == 'radio' && this.getValue().length) ||
322 (this.inputType == 'checkbox' && this.validateGroup())
332 validateGroup : function()
335 return (this.getValue() == this.inputValue) ? true : false;
338 var group = Roo.bootstrap.CheckBox.get(this.groupId);
351 r = (group[i].getValue() == group[i].inputValue) ? true : false;
358 * Mark this field as valid
360 markValid : function(){
361 Roo.log('run marValid on checkbox');
362 if(!this.el || this.preventMark){ // not rendered
366 this.el.removeClass([this.invalidClass, this.validClass]);
368 this.el.addClass(this.validClass);
370 this.fireEvent('valid', this);
374 * Mark this field as invalid
375 * @param {String} msg The validation message
377 markInvalid : function(){
378 if(!this.el || this.preventMark){ // not rendered
382 this.el.removeClass([this.invalidClass, this.validClass]);
384 this.el.addClass(this.invalidClass);
386 this.fireEvent('invalid', this, msg);
391 Roo.apply(Roo.bootstrap.CheckBox, {
396 * register a CheckBox Group
397 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
399 register : function(checkbox)
401 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
402 this.groups[checkbox.groupId] = {};
405 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
409 this.groups[checkbox.groupId][checkbox.name] = checkbox;
413 * fetch a CheckBox Group based on the group ID
414 * @param {string} the group ID
415 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
417 get: function(groupId) {
418 if (typeof(this.groups[groupId]) == 'undefined') {
422 return this.groups[groupId] ;