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
40 Roo.bootstrap.CheckBox.register(this);
45 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
47 inputType: 'checkbox',
55 getAutoCreate : function()
57 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
63 cfg.cls = 'form-group ' + this.inputType; //input-group
66 cfg.cls += ' ' + this.inputType + '-inline';
72 type : this.inputType,
73 value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
74 cls : 'roo-' + this.inputType, //'form-box',
75 placeholder : this.placeholder || ''
79 if (this.weight) { // Validity check?
80 cfg.cls += " " + this.inputType + "-" + this.weight;
88 input.checked = this.checked;
92 input.name = this.name;
96 input.cls += ' input-' + this.size;
101 ['xs','sm','md','lg'].map(function(size){
102 if (settings[size]) {
103 cfg.cls += ' col-' + size + '-' + settings[size];
109 var inputblock = input;
114 if (this.before || this.after) {
124 cls : 'input-group-addon',
129 inputblock.cn.push(input);
134 cls : 'input-group-addon',
141 if (align ==='left' && this.fieldLabel.length) {
142 Roo.log("left and has label");
148 cls : 'control-label col-md-' + this.labelWidth,
149 html : this.fieldLabel
153 cls : "col-md-" + (12 - this.labelWidth),
160 } else if ( this.fieldLabel.length) {
165 tag: this.boxLabel ? 'span' : 'label',
167 cls: 'control-label box-input-label',
168 //cls : 'input-group-addon',
169 html : this.fieldLabel
179 Roo.log(" no label && no align");
180 cfg.cn = [ inputblock ] ;
187 //'for': id, // box label is handled by onclick - so no for...
193 boxLabelCfg.tooltip = this.tooltip;
196 cfg.cn.push(boxLabelCfg);
206 * return the real input element.
210 return this.el.select('input.roo-' + this.inputType,true).first();
215 return this.el.select('label.control-label',true).first();
221 return this.labelEl();
224 initEvents : function()
226 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
228 this.inputEl().on('click', this.onClick, this);
231 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
234 this.startValue = this.getValue();
240 this.setChecked(!this.checked);
243 setChecked : function(state,suppressEvent)
245 this.startValue = this.getValue();
247 if(this.inputType == 'radio'){
249 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
250 e.dom.checked = false;
253 this.inputEl().dom.checked = true;
255 this.inputEl().dom.value = this.inputValue;
257 if(suppressEvent !== true){
258 this.fireEvent('check', this, true);
264 this.checked = state;
266 this.inputEl().dom.checked = state;
268 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
270 if(suppressEvent !== true){
271 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);
304 setGroupValue : function(v, suppressEvent)
306 this.startValue = this.getValue();
308 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
309 e.dom.checked = false;
311 if(e.dom.value == v){
312 e.dom.checked = true;
316 if(suppressEvent !== true){
317 this.fireEvent('check', this, true);
323 validate : function()
327 (this.inputType == 'radio' && this.getValue().length) ||
328 (this.inputType == 'checkbox' && this.getValue() == this.inputValue)
338 Roo.apply(Roo.bootstrap.CheckBox, {
342 * register a CheckBox Group
343 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
345 register : function(checkbox)
347 this.groups[checkbox.groupId].push(checkbox);
351 * fetch a CheckBox Group based on the group ID
352 * if one does not exist , it will get created.
353 * @param {string} the group ID
354 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
356 get: function(groupId) {
357 if (typeof(this.groups[groupId]) == 'undefined') {
358 this.register(new Roo.bootstrap.CheckBox({ groupId : groupId }));
361 return this.groups[groupId] ;