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>
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.');
27 {@link #iconCls}: 'do-something',
28 {@link #itemId}: 'myAction'
31 var panel = new Ext.panel.Panel({
36 // Add the Action directly to a toolbar as a menu button
40 // Add the Action to a menu as a text item
45 // Add the Action to the panel body as a standard button
46 new Ext.button.Button(action)
48 renderTo: Ext.getBody()
51 // Change the text for all components using the Action
52 action.setText('Something else');
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');
60 * @param {Object} config The configuration options
62 Ext.define('Ext.Action', {
64 /* Begin Definitions */
69 * @cfg {String} text The text to set for all components configured by this Action (defaults to '').
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:
77 // specify the property in the config for the class:
79 iconCls: 'do-something'
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; }
86 * @cfg {Boolean} disabled True to disable all components configured by this Action, false to enable them (defaults to false).
89 * @cfg {Boolean} hidden True to hide all components configured by this Action, false to show them (defaults to false).
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).
96 * @cfg {String} itemId
97 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
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.
104 constructor : function(config){
105 this.initialConfig = config;
106 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
114 * Sets the text to be displayed by all components configured by this Action.
115 * @param {String} text The text to display
117 setText : function(text){
118 this.initialConfig.text = text;
119 this.callEach('setText', [text]);
123 * Gets the text currently displayed by all components configured by this Action.
125 getText : function(){
126 return this.initialConfig.text;
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
134 setIconCls : function(cls){
135 this.initialConfig.iconCls = cls;
136 this.callEach('setIconCls', [cls]);
140 * Gets the icon CSS class currently used by all components configured by this Action.
142 getIconCls : function(){
143 return this.initialConfig.iconCls;
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
151 setDisabled : function(v){
152 this.initialConfig.disabled = v;
153 this.callEach('setDisabled', [v]);
157 * Enables all components configured by this Action.
160 this.setDisabled(false);
164 * Disables all components configured by this Action.
166 disable : function(){
167 this.setDisabled(true);
171 * Returns true if the components using this Action are currently disabled, else returns false.
173 isDisabled : function(){
174 return this.initialConfig.disabled;
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
182 setHidden : function(v){
183 this.initialConfig.hidden = v;
184 this.callEach('setVisible', [!v]);
188 * Shows all components configured by this Action.
191 this.setHidden(false);
195 * Hides all components configured by this Action.
198 this.setHidden(true);
202 * Returns true if the components configured by this Action are currently hidden, else returns false.
204 isHidden : function(){
205 return this.initialConfig.hidden;
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.
214 setHandler : function(fn, scope){
215 this.initialConfig.handler = fn;
216 this.initialConfig.scope = scope;
217 this.callEach('setHandler', [fn, scope]);
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.
226 each : function(fn, scope){
227 Ext.each(this.items, fn, scope);
231 callEach : function(fnName, args){
232 var items = this.items,
237 items[i][fnName].apply(items[i], args);
242 addComponent : function(comp){
243 this.items.push(comp);
244 comp.on('destroy', this.removeComponent, this);
248 removeComponent : function(comp){
249 Ext.Array.remove(this.items, comp);
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)
260 execute : function(){
261 this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);