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