Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / widgets / form / RadioGroup.js
1 /*!
2  * Ext JS Library 3.0.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 {Boolean} allowBlank True to allow every item in the group to be blank (defaults to true).
19      * If allowBlank = false and no items are selected at validation time, {@link @blankText} will
20      * be used as the error text.
21      */
22     allowBlank : true,
23     /**
24      * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails
25      * (defaults to 'You must select one item in this group')
26      */
27     blankText : 'You must select one item in this group',
28     
29     // private
30     defaultType : 'radio',
31     
32     // private
33     groupCls : 'x-form-radio-group',
34     
35     /**
36      * @event change
37      * Fires when the state of a child radio changes.
38      * @param {Ext.form.RadioGroup} this
39      * @param {Ext.form.Radio} checked The checked radio
40      */
41     
42     /**
43      * Gets the selected {@link Ext.form.Radio} in the group, if it exists.
44      * @return {Ext.form.Radio} The selected radio.
45      */
46     getValue : function(){
47         var out = null;
48         this.eachItem(function(item){
49             if(item.checked){
50                 out = item;
51                 return false;
52             }
53         });
54         return out;
55     },
56     
57     /**
58      * Sets the checked radio in the group.
59      * @param {String/Ext.form.Radio} id The radio to check.
60      * @param {Boolean} value The value to set the radio.
61      * @return {Ext.form.RadioGroup} this
62      */
63     setValue : function(id, value){
64         if(this.rendered){
65             if(arguments.length > 1){
66                 var f = this.getBox(id);
67                 if(f){
68                     f.setValue(value);
69                     if(f.checked){
70                         this.eachItem(function(item){
71                             if (item !== f){
72                                 item.setValue(false);
73                             }
74                         });
75                     }
76                 }
77             }else{
78                 this.setValueForItem(id);
79             }
80         }else{
81             this.values = arguments;
82         }
83         return this;
84     },
85     
86     // private
87     fireChecked : function(){
88         if(!this.checkTask){
89             this.checkTask = new Ext.util.DelayedTask(this.bufferChecked, this);
90         }
91         this.checkTask.delay(10);
92     },
93     
94     // private
95     bufferChecked : function(){
96         var out = null;
97         this.eachItem(function(item){
98             if(item.checked){
99                 out = item;
100                 return false;
101             }
102         });
103         this.fireEvent('change', this, out);
104     },
105     
106     onDestroy : function(){
107         if(this.checkTask){
108             this.checkTask.cancel();
109             this.checkTask = null;
110         }
111         Ext.form.RadioGroup.superclass.onDestroy.call(this);
112     }
113
114 });
115
116 Ext.reg('radiogroup', Ext.form.RadioGroup);