3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
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>
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.');
41 {@link #iconCls}: 'do-something',
42 {@link #itemId}: 'myAction'
45 var panel = new Ext.panel.Panel({
50 // Add the Action directly to a toolbar as a menu button
54 // Add the Action to a menu as a text item
59 // Add the Action to the panel body as a standard button
60 new Ext.button.Button(action)
62 renderTo: Ext.getBody()
65 // Change the text for all components using the Action
66 action.setText('Something else');
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');
74 Ext.define('Ext.Action', {
76 /* Begin Definitions */
81 * @cfg {String} [text='']
82 * The text to set for all components configured by this Action.
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:
90 // specify the property in the config for the class:
92 iconCls: 'do-something'
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; }
99 * @cfg {Boolean} [disabled=false]
100 * True to disable all components configured by this Action, false to enable them.
103 * @cfg {Boolean} [hidden=false]
104 * True to hide all components configured by this Action, false to show them.
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.
112 * @cfg {String} itemId
113 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
116 * @cfg {Object} scope
117 * The scope (this reference) in which the {@link #handler} is executed.
118 * Defaults to the browser window.
122 * Creates new Action.
123 * @param {Object} config Config object.
125 constructor : function(config){
126 this.initialConfig = config;
127 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
135 * Sets the text to be displayed by all components configured by this Action.
136 * @param {String} text The text to display
138 setText : function(text){
139 this.initialConfig.text = text;
140 this.callEach('setText', [text]);
144 * Gets the text currently displayed by all components configured by this Action.
146 getText : function(){
147 return this.initialConfig.text;
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
155 setIconCls : function(cls){
156 this.initialConfig.iconCls = cls;
157 this.callEach('setIconCls', [cls]);
161 * Gets the icon CSS class currently used by all components configured by this Action.
163 getIconCls : function(){
164 return this.initialConfig.iconCls;
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
172 setDisabled : function(v){
173 this.initialConfig.disabled = v;
174 this.callEach('setDisabled', [v]);
178 * Enables all components configured by this Action.
181 this.setDisabled(false);
185 * Disables all components configured by this Action.
187 disable : function(){
188 this.setDisabled(true);
192 * Returns true if the components using this Action are currently disabled, else returns false.
194 isDisabled : function(){
195 return this.initialConfig.disabled;
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
203 setHidden : function(v){
204 this.initialConfig.hidden = v;
205 this.callEach('setVisible', [!v]);
209 * Shows all components configured by this Action.
212 this.setHidden(false);
216 * Hides all components configured by this Action.
219 this.setHidden(true);
223 * Returns true if the components configured by this Action are currently hidden, else returns false.
225 isHidden : function(){
226 return this.initialConfig.hidden;
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.
235 setHandler : function(fn, scope){
236 this.initialConfig.handler = fn;
237 this.initialConfig.scope = scope;
238 this.callEach('setHandler', [fn, scope]);
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.
247 each : function(fn, scope){
248 Ext.each(this.items, fn, scope);
252 callEach : function(fnName, args){
253 var items = this.items,
258 items[i][fnName].apply(items[i], args);
263 addComponent : function(comp){
264 this.items.push(comp);
265 comp.on('destroy', this.removeComponent, this);
269 removeComponent : function(comp){
270 Ext.Array.remove(this.items, comp);
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
279 execute : function(){
280 this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);