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