Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / menu / CheckItem.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.menu.CheckItem
17  * @extends Ext.menu.Item
18  *
19  * A menu item that contains a togglable checkbox by default, but that can also be a part of a radio group.
20  *
21  * {@img Ext.menu.CheckItem/Ext.menu.CheckItem.png Ext.menu.CheckItem component}
22  *
23  * __Example Usage__
24  *
25  *     Ext.create('Ext.menu.Menu', {
26  *         width: 100,
27  *         height: 110,
28  *         floating: false,  // usually you want this set to True (default)
29  *         renderTo: Ext.getBody(),  // usually rendered by it's containing component
30  *         items: [{
31  *             xtype: 'menucheckitem',
32  *             text: 'select all'
33  *         },{
34  *             xtype: 'menucheckitem',
35  *             text: 'select specific',
36  *         },{
37  *             iconCls: 'add16',
38  *             text: 'icon item'
39  *         },{
40  *             text: 'regular item'
41  *         }]
42  *     }); 
43  *     
44  */
45 Ext.define('Ext.menu.CheckItem', {
46     extend: 'Ext.menu.Item',
47     alias: 'widget.menucheckitem',
48
49     /**
50      * @cfg {String} checkedCls
51      * The CSS class used by {@link #cls} to show the checked state.
52      * Defaults to `Ext.baseCSSPrefix + 'menu-item-checked'`.
53      * @markdown
54      */
55     checkedCls: Ext.baseCSSPrefix + 'menu-item-checked',
56     /**
57      * @cfg {String} uncheckedCls
58      * The CSS class used by {@link #cls} to show the unchecked state.
59      * Defaults to `Ext.baseCSSPrefix + 'menu-item-unchecked'`.
60      * @markdown
61      */
62     uncheckedCls: Ext.baseCSSPrefix + 'menu-item-unchecked',
63     /**
64      * @cfg {String} groupCls
65      * The CSS class applied to this item's icon image to denote being a part of a radio group.
66      * Defaults to `Ext.baseCSSClass + 'menu-group-icon'`.
67      * Any specified {@link #iconCls} overrides this.
68      * @markdown
69      */
70     groupCls: Ext.baseCSSPrefix + 'menu-group-icon',
71
72     /**
73      * @cfg {Boolean} hideOnClick
74      * Whether to not to hide the owning menu when this item is clicked.
75      * Defaults to `false` for checkbox items, and to `true` for radio group items.
76      * @markdown
77      */
78     hideOnClick: false,
79
80     afterRender: function() {
81         var me = this;
82         this.callParent();
83         me.checked = !me.checked;
84         me.setChecked(!me.checked, true);
85     },
86
87     initComponent: function() {
88         var me = this;
89         me.addEvents(
90             /**
91              * @event beforecheckchange
92              * Fires before a change event. Return false to cancel.
93              * @param {Ext.menu.CheckItem} this
94              * @param {Boolean} checked
95              */
96             'beforecheckchange',
97
98             /**
99              * @event checkchange
100              * Fires after a change event.
101              * @param {Ext.menu.CheckItem} this
102              * @param {Boolean} checked
103              */
104             'checkchange'
105         );
106
107         me.callParent(arguments);
108
109         Ext.menu.Manager.registerCheckable(me);
110
111         if (me.group) {
112             if (!me.iconCls) {
113                 me.iconCls = me.groupCls;
114             }
115             if (me.initialConfig.hideOnClick !== false) {
116                 me.hideOnClick = true;
117             }
118         }
119     },
120
121     /**
122      * Disables just the checkbox functionality of this menu Item. If this menu item has a submenu, that submenu
123      * will still be accessible
124      */
125     disableCheckChange: function() {
126         var me = this;
127
128         if (me.iconEl) {
129             me.iconEl.addCls(me.disabledCls);
130         }
131         me.checkChangeDisabled = true;
132     },
133
134     /**
135      * Reenables the checkbox functionality of this menu item after having been disabled by {@link #disableCheckChange}
136      */
137     enableCheckChange: function() {
138         var me = this;
139
140         me.iconEl.removeCls(me.disabledCls);
141         me.checkChangeDisabled = false;
142     },
143
144     onClick: function(e) {
145         var me = this;
146         if(!me.disabled && !me.checkChangeDisabled && !(me.checked && me.group)) {
147             me.setChecked(!me.checked);
148         }
149         this.callParent([e]);
150     },
151
152     onDestroy: function() {
153         Ext.menu.Manager.unregisterCheckable(this);
154         this.callParent(arguments);
155     },
156
157     /**
158      * Sets the checked state of the item
159      * @param {Boolean} checked True to check, false to uncheck
160      * @param {Boolean} suppressEvents (optional) True to prevent firing the checkchange events. Defaults to `false`.
161      * @markdown
162      */
163     setChecked: function(checked, suppressEvents) {
164         var me = this;
165         if (me.checked !== checked && (suppressEvents || me.fireEvent('beforecheckchange', me, checked) !== false)) {
166             if (me.el) {
167                 me.el[checked  ? 'addCls' : 'removeCls'](me.checkedCls)[!checked ? 'addCls' : 'removeCls'](me.uncheckedCls);
168             }
169             me.checked = checked;
170             Ext.menu.Manager.onCheckChange(me, checked);
171             if (!suppressEvents) {
172                 Ext.callback(me.checkHandler, me.scope, [me, checked]);
173                 me.fireEvent('checkchange', me, checked);
174             }
175         }
176     }
177 });
178