Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / Action.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.Action\r
9  * <p>An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it\r
10  * can be usefully shared among multiple components.  Actions let you share handlers, configuration options and UI\r
11  * updates across any components that support the Action interface (primarily {@link Ext.Toolbar}, {@link Ext.Button}\r
12  * and {@link Ext.menu.Menu} components).</p>\r
13  * <p>Aside from supporting the config object interface, any component that needs to use Actions must also support\r
14  * the following method list, as these will be called as needed by the Action class: setText(string), setIconCls(string),\r
15  * setDisabled(boolean), setVisible(boolean) and setHandler(function).</p>\r
16  * Example usage:<br>\r
17  * <pre><code>\r
18 // Define the shared action.  Each component below will have the same\r
19 // display text and icon, and will display the same message on click.\r
20 var action = new Ext.Action({\r
21     {@link #text}: 'Do something',\r
22     {@link #handler}: function(){\r
23         Ext.Msg.alert('Click', 'You did something.');\r
24     },\r
25     {@link #iconCls}: 'do-something',\r
26     {@link #itemId}: 'myAction'\r
27 });\r
28 \r
29 var panel = new Ext.Panel({\r
30     title: 'Actions',\r
31     width: 500,\r
32     height: 300,\r
33     tbar: [\r
34         // Add the action directly to a toolbar as a menu button\r
35         action,\r
36         {\r
37             text: 'Action Menu',\r
38             // Add the action to a menu as a text item\r
39             menu: [action]\r
40         }\r
41     ],\r
42     items: [\r
43         // Add the action to the panel body as a standard button\r
44         new Ext.Button(action)\r
45     ],\r
46     renderTo: Ext.getBody()\r
47 });\r
48 \r
49 // Change the text for all components using the action\r
50 action.setText('Something else');\r
51 \r
52 // Reference an action through a container using the itemId\r
53 var btn = panel.getComponent('myAction');\r
54 var aRef = btn.baseAction;\r
55 aRef.setText('New text');\r
56 </code></pre>\r
57  * @constructor\r
58  * @param {Object} config The configuration options\r
59  */\r
60 Ext.Action = Ext.extend(Object, {\r
61     /**\r
62      * @cfg {String} text The text to set for all components using this action (defaults to '').\r
63      */\r
64     /**\r
65      * @cfg {String} iconCls\r
66      * The CSS class selector that specifies a background image to be used as the header icon for\r
67      * all components using this action (defaults to '').\r
68      * <p>An example of specifying a custom icon class would be something like:\r
69      * </p><pre><code>\r
70 // specify the property in the config for the class:\r
71      ...\r
72      iconCls: 'do-something'\r
73 \r
74 // css class that specifies background image to be used as the icon image:\r
75 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }\r
76 </code></pre>\r
77      */\r
78     /**\r
79      * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).\r
80      */\r
81     /**\r
82      * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).\r
83      */\r
84     /**\r
85      * @cfg {Function} handler The function that will be invoked by each component tied to this action\r
86      * when the component's primary event is triggered (defaults to undefined).\r
87      */\r
88     /**\r
89      * @cfg {String} itemId\r
90      * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.\r
91      */\r
92     /**\r
93      * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the\r
94      * <code>{@link #handler}</code> is executed. Defaults to this Button.\r
95      */\r
96 \r
97     constructor : function(config){\r
98         this.initialConfig = config;\r
99         this.itemId = config.itemId = (config.itemId || config.id || Ext.id());\r
100         this.items = [];\r
101     },\r
102     \r
103     // private\r
104     isAction : true,\r
105 \r
106     /**\r
107      * Sets the text to be displayed by all components using this action.\r
108      * @param {String} text The text to display\r
109      */\r
110     setText : function(text){\r
111         this.initialConfig.text = text;\r
112         this.callEach('setText', [text]);\r
113     },\r
114 \r
115     /**\r
116      * Gets the text currently displayed by all components using this action.\r
117      */\r
118     getText : function(){\r
119         return this.initialConfig.text;\r
120     },\r
121 \r
122     /**\r
123      * Sets the icon CSS class for all components using this action.  The class should supply\r
124      * a background image that will be used as the icon image.\r
125      * @param {String} cls The CSS class supplying the icon image\r
126      */\r
127     setIconClass : function(cls){\r
128         this.initialConfig.iconCls = cls;\r
129         this.callEach('setIconClass', [cls]);\r
130     },\r
131 \r
132     /**\r
133      * Gets the icon CSS class currently used by all components using this action.\r
134      */\r
135     getIconClass : function(){\r
136         return this.initialConfig.iconCls;\r
137     },\r
138 \r
139     /**\r
140      * Sets the disabled state of all components using this action.  Shortcut method\r
141      * for {@link #enable} and {@link #disable}.\r
142      * @param {Boolean} disabled True to disable the component, false to enable it\r
143      */\r
144     setDisabled : function(v){\r
145         this.initialConfig.disabled = v;\r
146         this.callEach('setDisabled', [v]);\r
147     },\r
148 \r
149     /**\r
150      * Enables all components using this action.\r
151      */\r
152     enable : function(){\r
153         this.setDisabled(false);\r
154     },\r
155 \r
156     /**\r
157      * Disables all components using this action.\r
158      */\r
159     disable : function(){\r
160         this.setDisabled(true);\r
161     },\r
162 \r
163     /**\r
164      * Returns true if the components using this action are currently disabled, else returns false.  \r
165      */\r
166     isDisabled : function(){\r
167         return this.initialConfig.disabled;\r
168     },\r
169 \r
170     /**\r
171      * Sets the hidden state of all components using this action.  Shortcut method\r
172      * for <code>{@link #hide}</code> and <code>{@link #show}</code>.\r
173      * @param {Boolean} hidden True to hide the component, false to show it\r
174      */\r
175     setHidden : function(v){\r
176         this.initialConfig.hidden = v;\r
177         this.callEach('setVisible', [!v]);\r
178     },\r
179 \r
180     /**\r
181      * Shows all components using this action.\r
182      */\r
183     show : function(){\r
184         this.setHidden(false);\r
185     },\r
186 \r
187     /**\r
188      * Hides all components using this action.\r
189      */\r
190     hide : function(){\r
191         this.setHidden(true);\r
192     },\r
193 \r
194     /**\r
195      * Returns true if the components using this action are currently hidden, else returns false.  \r
196      */\r
197     isHidden : function(){\r
198         return this.initialConfig.hidden;\r
199     },\r
200 \r
201     /**\r
202      * Sets the function that will be called by each Component using this action when its primary event is triggered.\r
203      * @param {Function} fn The function that will be invoked by the action's components.  The function\r
204      * will be called with no arguments.\r
205      * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.\r
206      */\r
207     setHandler : function(fn, scope){\r
208         this.initialConfig.handler = fn;\r
209         this.initialConfig.scope = scope;\r
210         this.callEach('setHandler', [fn, scope]);\r
211     },\r
212 \r
213     /**\r
214      * Executes the specified function once for each Component currently tied to this action.  The function passed\r
215      * in should accept a single argument that will be an object that supports the basic Action config/method interface.\r
216      * @param {Function} fn The function to execute for each component\r
217      * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed.  Defaults to the Component.\r
218      */\r
219     each : function(fn, scope){\r
220         Ext.each(this.items, fn, scope);\r
221     },\r
222 \r
223     // private\r
224     callEach : function(fnName, args){\r
225         var cs = this.items;\r
226         for(var i = 0, len = cs.length; i < len; i++){\r
227             cs[i][fnName].apply(cs[i], args);\r
228         }\r
229     },\r
230 \r
231     // private\r
232     addComponent : function(comp){\r
233         this.items.push(comp);\r
234         comp.on('destroy', this.removeComponent, this);\r
235     },\r
236 \r
237     // private\r
238     removeComponent : function(comp){\r
239         this.items.remove(comp);\r
240     },\r
241 \r
242     /**\r
243      * Executes this action manually using the handler function specified in the original config object\r
244      * or the handler function set with <code>{@link #setHandler}</code>.  Any arguments passed to this\r
245      * function will be passed on to the handler function.\r
246      * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function\r
247      * @param {Mixed} arg2 (optional)\r
248      * @param {Mixed} etc... (optional)\r
249      */\r
250     execute : function(){\r
251         this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);\r
252     }\r
253 });\r