commit extjs-2.2.1
[extjs.git] / source / widgets / form / Radio.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.form.Radio\r
11  * @extends Ext.form.Checkbox\r
12  * Single radio field.  Same as Checkbox, but provided as a convenience for automatically setting the input type.\r
13  * Radio grouping is handled automatically by the browser if you give each radio in a group the same name.\r
14  * @constructor\r
15  * Creates a new Radio\r
16  * @param {Object} config Configuration options\r
17  */\r
18 Ext.form.Radio = Ext.extend(Ext.form.Checkbox, {\r
19     // private\r
20     inputType: 'radio',\r
21     // private\r
22     baseCls: 'x-form-radio',\r
23     \r
24     /**\r
25      * If this radio is part of a group, it will return the selected value\r
26      * @return {String}\r
27      */\r
28     getGroupValue : function(){\r
29         var c = this.getParent().child('input[name='+this.el.dom.name+']:checked', true);\r
30         return c ? c.value : null;\r
31     },\r
32     \r
33     // private\r
34     getParent : function(){\r
35         return this.el.up('form') || Ext.getBody();\r
36     },\r
37 \r
38     // private\r
39     toggleValue : function() {\r
40         if(!this.checked){\r
41             var els = this.getParent().select('input[name='+this.el.dom.name+']');\r
42             els.each(function(el){\r
43                 if(el.dom.id == this.id){\r
44                     this.setValue(true);\r
45                 }else{\r
46                     Ext.getCmp(el.dom.id).setValue(false);\r
47                 }\r
48             }, this);\r
49         }\r
50     },\r
51     \r
52     /**\r
53      * Sets either the checked/unchecked status of this Radio, or, if a string value\r
54      * is passed, checks a sibling Radio of the same name whose value is the value specified.\r
55      * @param value {String/Boolean} Checked value, or the value of the sibling radio button to check.\r
56      */\r
57     setValue : function(v){\r
58         if(typeof v=='boolean') {\r
59             Ext.form.Radio.superclass.setValue.call(this, v);\r
60         }else{\r
61             var r = this.getParent().child('input[name='+this.el.dom.name+'][value='+v+']', true);\r
62             if(r && !r.checked){\r
63                 Ext.getCmp(r.id).toggleValue();\r
64             };\r
65         }\r
66     },\r
67     \r
68     /**\r
69      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide\r
70      * @method\r
71      */\r
72     markInvalid : Ext.emptyFn,\r
73     /**\r
74      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide\r
75      * @method\r
76      */\r
77     clearInvalid : Ext.emptyFn\r
78     \r
79 });\r
80 Ext.reg('radio', Ext.form.Radio);\r