Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / form / RadioGroup.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.form.RadioGroup
9  * @extends Ext.form.CheckboxGroup
10  * A grouping container for {@link Ext.form.Radio} controls.
11  * @constructor
12  * Creates a new RadioGroup
13  * @param {Object} config Configuration options
14  * @xtype radiogroup
15  */
16 Ext.form.RadioGroup = Ext.extend(Ext.form.CheckboxGroup, {
17     /**
18      * @cfg {Array} items An Array of {@link Ext.form.Radio Radio}s or Radio config objects
19      * to arrange in the group.
20      */
21     /**
22      * @cfg {Boolean} allowBlank True to allow every item in the group to be blank (defaults to true).
23      * If allowBlank = false and no items are selected at validation time, {@link @blankText} will
24      * be used as the error text.
25      */
26     allowBlank : true,
27     /**
28      * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails
29      * (defaults to 'You must select one item in this group')
30      */
31     blankText : 'You must select one item in this group',
32     
33     // private
34     defaultType : 'radio',
35     
36     // private
37     groupCls : 'x-form-radio-group',
38     
39     /**
40      * @event change
41      * Fires when the state of a child radio changes.
42      * @param {Ext.form.RadioGroup} this
43      * @param {Ext.form.Radio} checked The checked radio
44      */
45     
46     /**
47      * Gets the selected {@link Ext.form.Radio} in the group, if it exists.
48      * @return {Ext.form.Radio} The selected radio.
49      */
50     getValue : function(){
51         var out = null;
52         this.eachItem(function(item){
53             if(item.checked){
54                 out = item;
55                 return false;
56             }
57         });
58         return out;
59     },
60     
61     /**
62      * Sets the checked radio in the group.
63      * @param {String/Ext.form.Radio} id The radio to check.
64      * @param {Boolean} value The value to set the radio.
65      * @return {Ext.form.RadioGroup} this
66      */
67     onSetValue : function(id, value){
68         if(arguments.length > 1){
69             var f = this.getBox(id);
70             if(f){
71                 f.setValue(value);
72                 if(f.checked){
73                     this.eachItem(function(item){
74                         if (item !== f){
75                             item.setValue(false);
76                         }
77                     });
78                 }
79             }
80         }else{
81             this.setValueForItem(id);
82         }
83     },
84     
85     setValueForItem : function(val){
86         val = String(val).split(',')[0];
87         this.eachItem(function(item){
88             item.setValue(val == item.inputValue);
89         });
90     },
91     
92     // private
93     fireChecked : function(){
94         if(!this.checkTask){
95             this.checkTask = new Ext.util.DelayedTask(this.bufferChecked, this);
96         }
97         this.checkTask.delay(10);
98     },
99     
100     // private
101     bufferChecked : function(){
102         var out = null;
103         this.eachItem(function(item){
104             if(item.checked){
105                 out = item;
106                 return false;
107             }
108         });
109         this.fireEvent('change', this, out);
110     },
111     
112     onDestroy : function(){
113         if(this.checkTask){
114             this.checkTask.cancel();
115             this.checkTask = null;
116         }
117         Ext.form.RadioGroup.superclass.onDestroy.call(this);
118     }
119
120 });
121
122 Ext.reg('radiogroup', Ext.form.RadioGroup);