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