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
20 * @cfg {String} tooltip label tooltip
23 * Create a new CheckBox
24 * @param {Object} config The config object
27 Roo.bootstrap.CheckBox = function(config){
28 Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
33 * Fires when the element is checked or unchecked.
34 * @param {Roo.bootstrap.CheckBox} this This input
35 * @param {Boolean} checked The new checked value
40 * Fires when the element is click.
41 * @param {Roo.bootstrap.CheckBox} this This input
48 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
50 inputType: 'checkbox',
59 getAutoCreate : function()
61 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
67 cfg.cls = 'form-group ' + this.inputType; //input-group
70 cfg.cls += ' ' + this.inputType + '-inline';
76 type : this.inputType,
77 value : this.inputValue,
78 cls : 'roo-' + this.inputType, //'form-box',
79 placeholder : this.placeholder || ''
83 if(this.inputType != 'radio'){
87 cls : 'roo-hidden-value',
88 value : this.checked ? this.inputValue : this.valueOff
93 if (this.weight) { // Validity check?
94 cfg.cls += " " + this.inputType + "-" + this.weight;
102 input.checked = this.checked;
107 input.name = this.name;
109 if(this.inputType != 'radio'){
110 hidden.name = this.name;
111 input.name = '_hidden_' + this.name;
116 input.cls += ' input-' + this.size;
121 ['xs','sm','md','lg'].map(function(size){
122 if (settings[size]) {
123 cfg.cls += ' col-' + size + '-' + settings[size];
127 var inputblock = input;
129 if (this.before || this.after) {
139 cls : 'input-group-addon',
144 inputblock.cn.push(input);
146 if(this.inputType != 'radio'){
147 inputblock.cn.push(hidden);
153 cls : 'input-group-addon',
159 var boxLabelCfg = false;
165 //'for': id, // box label is handled by onclick - so no for...
170 boxLabelCfg.tooltip = this.tooltip;
176 if (align ==='left' && this.fieldLabel.length) {
177 // Roo.log("left and has label");
182 cls : 'control-label',
183 html : this.fieldLabel
194 cfg.cn[1].cn.push(boxLabelCfg);
197 if(this.labelWidth > 12){
198 cfg.cn[0].style = "width: " + this.labelWidth + 'px';
201 if(this.labelWidth < 13 && this.labelmd == 0){
202 this.labelmd = this.labelWidth;
205 if(this.labellg > 0){
206 cfg.cn[0].cls += ' col-lg-' + this.labellg;
207 cfg.cn[1].cls += ' col-lg-' + (12 - this.labellg);
210 if(this.labelmd > 0){
211 cfg.cn[0].cls += ' col-md-' + this.labelmd;
212 cfg.cn[1].cls += ' col-md-' + (12 - this.labelmd);
215 if(this.labelsm > 0){
216 cfg.cn[0].cls += ' col-sm-' + this.labelsm;
217 cfg.cn[1].cls += ' col-sm-' + (12 - this.labelsm);
220 if(this.labelxs > 0){
221 cfg.cn[0].cls += ' col-xs-' + this.labelxs;
222 cfg.cn[1].cls += ' col-xs-' + (12 - this.labelxs);
225 } else if ( this.fieldLabel.length) {
226 // Roo.log(" label");
230 tag: this.boxLabel ? 'span' : 'label',
232 cls: 'control-label box-input-label',
233 //cls : 'input-group-addon',
234 html : this.fieldLabel
241 cfg.cn.push(boxLabelCfg);
246 // Roo.log(" no label && no align");
247 cfg.cn = [ inputblock ] ;
249 cfg.cn.push(boxLabelCfg);
257 if(this.inputType != 'radio'){
266 * return the real input element.
270 return this.el.select('input.roo-' + this.inputType,true).first();
272 hiddenEl: function ()
274 return this.el.select('input.roo-hidden-value',true).first();
279 return this.el.select('label.control-label',true).first();
285 return this.labelEl();
288 boxLabelEl: function()
290 return this.el.select('label.box-label',true).first();
293 initEvents : function()
295 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
297 this.inputEl().on('click', this.onClick, this);
300 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
303 this.startValue = this.getValue();
306 Roo.bootstrap.CheckBox.register(this);
310 onClick : function(e)
312 if(this.fireEvent('click', this, e) !== false){
313 this.setChecked(!this.checked);
318 setChecked : function(state,suppressEvent)
320 this.startValue = this.getValue();
322 if(this.inputType == 'radio'){
324 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
325 e.dom.checked = false;
328 this.inputEl().dom.checked = true;
330 this.inputEl().dom.value = this.inputValue;
332 if(suppressEvent !== true){
333 this.fireEvent('check', this, true);
341 this.checked = state;
343 this.inputEl().dom.checked = state;
346 this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
348 if(suppressEvent !== true){
349 this.fireEvent('check', this, state);
355 getValue : function()
357 if(this.inputType == 'radio'){
358 return this.getGroupValue();
361 return this.hiddenEl().dom.value;
365 getGroupValue : function()
367 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
371 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
374 setValue : function(v,suppressEvent)
376 if(this.inputType == 'radio'){
377 this.setGroupValue(v, suppressEvent);
381 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
386 setGroupValue : function(v, suppressEvent)
388 this.startValue = this.getValue();
390 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
391 e.dom.checked = false;
393 if(e.dom.value == v){
394 e.dom.checked = true;
398 if(suppressEvent !== true){
399 this.fireEvent('check', this, true);
407 validate : function()
409 if(this.getVisibilityEl().hasClass('hidden')){
415 (this.inputType == 'radio' && this.validateRadio()) ||
416 (this.inputType == 'checkbox' && this.validateCheckbox())
426 validateRadio : function()
428 if(this.getVisibilityEl().hasClass('hidden')){
438 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
451 validateCheckbox : function()
454 return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
455 //return (this.getValue() == this.inputValue) ? true : false;
458 var group = Roo.bootstrap.CheckBox.get(this.groupId);
467 if(group[i].el.isVisible(true)){
480 r = (group[i].getValue() == group[i].inputValue) ? true : false;
487 * Mark this field as valid
489 markValid : function()
493 this.fireEvent('valid', this);
495 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
498 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
505 if(this.inputType == 'radio'){
506 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
507 var fg = e.findParent('.form-group', false, true);
508 if (Roo.bootstrap.version == 3) {
509 fg.removeClass([_this.invalidClass, _this.validClass]);
510 fg.addClass(_this.validClass);
512 fg.removeClass(['is-valid', 'is-invalid']);
513 fg.addClass('is-valid');
521 var fg = this.el.findParent('.form-group', false, true);
522 if (Roo.bootstrap.version == 3) {
523 fg.removeClass([this.invalidClass, this.validClass]);
524 fg.addClass(this.validClass);
526 fg.removeClass(['is-valid', 'is-invalid']);
527 fg.addClass('is-valid');
532 var group = Roo.bootstrap.CheckBox.get(this.groupId);
539 var fg = group[i].el.findParent('.form-group', false, true);
540 if (Roo.bootstrap.version == 3) {
541 fg.removeClass([this.invalidClass, this.validClass]);
542 fg.addClass(this.validClass);
544 fg.removeClass(['is-valid', 'is-invalid']);
545 fg.addClass('is-valid');
551 * Mark this field as invalid
552 * @param {String} msg The validation message
554 markInvalid : function(msg)
562 this.fireEvent('invalid', this, msg);
564 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
567 label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
574 if(this.inputType == 'radio'){
576 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
577 var fg = e.findParent('.form-group', false, true);
578 if (Roo.bootstrap.version == 3) {
579 fg.removeClass([_this.invalidClass, _this.validClass]);
580 fg.addClass(_this.invalidClass);
582 fg.removeClass(['is-invalid', 'is-valid']);
583 fg.addClass('is-invalid');
591 var fg = this.el.findParent('.form-group', false, true);
592 if (Roo.bootstrap.version == 3) {
593 fg.removeClass([_this.invalidClass, _this.validClass]);
594 fg.addClass(_this.invalidClass);
596 fg.removeClass(['is-invalid', 'is-valid']);
597 fg.addClass('is-invalid');
602 var group = Roo.bootstrap.CheckBox.get(this.groupId);
609 var fg = group[i].el.findParent('.form-group', false, true);
610 if (Roo.bootstrap.version == 3) {
611 fg.removeClass([_this.invalidClass, _this.validClass]);
612 fg.addClass(_this.invalidClass);
614 fg.removeClass(['is-invalid', 'is-valid']);
615 fg.addClass('is-invalid');
621 clearInvalid : function()
623 Roo.bootstrap.Input.prototype.clearInvalid.call(this);
625 // this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
627 var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
629 if (label && label.iconEl) {
630 label.iconEl.removeClass([ label.validClass, label.invalidClass ]);
631 label.iconEl.removeClass(['is-invalid', 'is-valid']);
637 if(this.inputType != 'radio'){
638 Roo.bootstrap.CheckBox.superclass.disable.call(this);
645 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
646 _this.getActionEl().addClass(this.disabledClass);
647 e.dom.disabled = true;
651 this.disabled = true;
652 this.fireEvent("disable", this);
658 if(this.inputType != 'radio'){
659 Roo.bootstrap.CheckBox.superclass.enable.call(this);
666 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
667 _this.getActionEl().removeClass(this.disabledClass);
668 e.dom.disabled = false;
672 this.disabled = false;
673 this.fireEvent("enable", this);
677 setBoxLabel : function(v)
682 this.el.select('label.box-label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
688 Roo.apply(Roo.bootstrap.CheckBox, {
693 * register a CheckBox Group
694 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
696 register : function(checkbox)
698 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
699 this.groups[checkbox.groupId] = {};
702 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
706 this.groups[checkbox.groupId][checkbox.name] = checkbox;
710 * fetch a CheckBox Group based on the group ID
711 * @param {string} the group ID
712 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
714 get: function(groupId) {
715 if (typeof(this.groups[groupId]) == 'undefined') {
719 return this.groups[groupId] ;