commit extjs-2.2.1
[extjs.git] / source / widgets / menu / CheckItem.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.menu.CheckItem\r
11  * @extends Ext.menu.Item\r
12  * Adds a menu item that contains a checkbox by default, but can also be part of a radio group.\r
13  * @constructor\r
14  * Creates a new CheckItem\r
15  * @param {Object} config Configuration options\r
16  */\r
17 Ext.menu.CheckItem = function(config){\r
18     Ext.menu.CheckItem.superclass.constructor.call(this, config);\r
19     this.addEvents(\r
20         /**\r
21          * @event beforecheckchange\r
22          * Fires before the checked value is set, providing an opportunity to cancel if needed\r
23          * @param {Ext.menu.CheckItem} this\r
24          * @param {Boolean} checked The new checked value that will be set\r
25          */\r
26         "beforecheckchange" ,\r
27         /**\r
28          * @event checkchange\r
29          * Fires after the checked value has been set\r
30          * @param {Ext.menu.CheckItem} this\r
31          * @param {Boolean} checked The checked value that was set\r
32          */\r
33         "checkchange"\r
34     );\r
35     /**\r
36      * A function that handles the checkchange event.  The function is undefined by default, but if an implementation\r
37      * is provided, it will be called automatically when the checkchange event fires.\r
38      * @param {Ext.menu.CheckItem} this\r
39      * @param {Boolean} checked The checked value that was set\r
40      * @method checkHandler\r
41      */\r
42     if(this.checkHandler){\r
43         this.on('checkchange', this.checkHandler, this.scope);\r
44     }\r
45     Ext.menu.MenuMgr.registerCheckable(this);\r
46 };\r
47 Ext.extend(Ext.menu.CheckItem, Ext.menu.Item, {\r
48     /**\r
49      * @cfg {String} group\r
50      * All check items with the same group name will automatically be grouped into a single-select\r
51      * radio button group (defaults to '')\r
52      */\r
53     /**\r
54      * @cfg {String} itemCls The default CSS class to use for check items (defaults to "x-menu-item x-menu-check-item")\r
55      */\r
56     itemCls : "x-menu-item x-menu-check-item",\r
57     /**\r
58      * @cfg {String} groupClass The default CSS class to use for radio group check items (defaults to "x-menu-group-item")\r
59      */\r
60     groupClass : "x-menu-group-item",\r
61 \r
62     /**\r
63      * @cfg {Boolean} checked True to initialize this checkbox as checked (defaults to false).  Note that\r
64      * if this checkbox is part of a radio group (group = true) only the last item in the group that is\r
65      * initialized with checked = true will be rendered as checked.\r
66      */\r
67     checked: false,\r
68 \r
69     // private\r
70     ctype: "Ext.menu.CheckItem",\r
71 \r
72     // private\r
73     onRender : function(c){\r
74         Ext.menu.CheckItem.superclass.onRender.apply(this, arguments);\r
75         if(this.group){\r
76             this.el.addClass(this.groupClass);\r
77         }\r
78         if(this.checked){\r
79             this.checked = false;\r
80             this.setChecked(true, true);\r
81         }\r
82     },\r
83 \r
84     // private\r
85     destroy : function(){\r
86         Ext.menu.MenuMgr.unregisterCheckable(this);\r
87         Ext.menu.CheckItem.superclass.destroy.apply(this, arguments);\r
88     },\r
89 \r
90     /**\r
91      * Set the checked state of this item\r
92      * @param {Boolean} checked The new checked value\r
93      * @param {Boolean} suppressEvent (optional) True to prevent the checkchange event from firing (defaults to false)\r
94      */\r
95     setChecked : function(state, suppressEvent){\r
96         if(this.checked != state && this.fireEvent("beforecheckchange", this, state) !== false){\r
97             if(this.container){\r
98                 this.container[state ? "addClass" : "removeClass"]("x-menu-item-checked");\r
99             }\r
100             this.checked = state;\r
101             if(suppressEvent !== true){\r
102                 this.fireEvent("checkchange", this, state);\r
103             }\r
104         }\r
105     },\r
106 \r
107     // private\r
108     handleClick : function(e){\r
109        if(!this.disabled && !(this.checked && this.group)){// disable unselect on radio item\r
110            this.setChecked(!this.checked);\r
111        }\r
112        Ext.menu.CheckItem.superclass.handleClick.apply(this, arguments);\r
113     }\r
114 });