3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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
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
25 {@link #iconCls}: 'do-something',
\r
26 {@link #itemId}: 'myAction'
\r
29 var panel = new Ext.Panel({
\r
34 // Add the action directly to a toolbar as a menu button
\r
37 text: 'Action Menu',
\r
38 // Add the action to a menu as a text item
\r
43 // Add the action to the panel body as a standard button
\r
44 new Ext.Button(action)
\r
46 renderTo: Ext.getBody()
\r
49 // Change the text for all components using the action
\r
50 action.setText('Something else');
\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
58 * @param {Object} config The configuration options
\r
60 Ext.Action = function(config){
\r
61 this.initialConfig = config;
\r
62 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
\r
66 Ext.Action.prototype = {
\r
68 * @cfg {String} text The text to set for all components using this action (defaults to '').
\r
71 * @cfg {String} iconCls
\r
72 * The CSS class selector that specifies a background image to be used as the header icon for
\r
73 * all components using this action (defaults to '').
\r
74 * <p>An example of specifying a custom icon class would be something like:
\r
76 // specify the property in the config for the class:
\r
78 iconCls: 'do-something'
\r
80 // css class that specifies background image to be used as the icon image:
\r
81 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
\r
85 * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).
\r
88 * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).
\r
91 * @cfg {Function} handler The function that will be invoked by each component tied to this action
\r
92 * when the component's primary event is triggered (defaults to undefined).
\r
95 * @cfg {String} itemId
\r
96 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
\r
99 * @cfg {Object} scope The scope in which the {@link #handler} function will execute.
\r
106 * Sets the text to be displayed by all components using this action.
\r
107 * @param {String} text The text to display
\r
109 setText : function(text){
\r
110 this.initialConfig.text = text;
\r
111 this.callEach('setText', [text]);
\r
115 * Gets the text currently displayed by all components using this action.
\r
117 getText : function(){
\r
118 return this.initialConfig.text;
\r
122 * Sets the icon CSS class for all components using this action. The class should supply
\r
123 * a background image that will be used as the icon image.
\r
124 * @param {String} cls The CSS class supplying the icon image
\r
126 setIconClass : function(cls){
\r
127 this.initialConfig.iconCls = cls;
\r
128 this.callEach('setIconClass', [cls]);
\r
132 * Gets the icon CSS class currently used by all components using this action.
\r
134 getIconClass : function(){
\r
135 return this.initialConfig.iconCls;
\r
139 * Sets the disabled state of all components using this action. Shortcut method
\r
140 * for {@link #enable} and {@link #disable}.
\r
141 * @param {Boolean} disabled True to disable the component, false to enable it
\r
143 setDisabled : function(v){
\r
144 this.initialConfig.disabled = v;
\r
145 this.callEach('setDisabled', [v]);
\r
149 * Enables all components using this action.
\r
151 enable : function(){
\r
152 this.setDisabled(false);
\r
156 * Disables all components using this action.
\r
158 disable : function(){
\r
159 this.setDisabled(true);
\r
163 * Returns true if the components using this action are currently disabled, else returns false.
\r
165 isDisabled : function(){
\r
166 return this.initialConfig.disabled;
\r
170 * Sets the hidden state of all components using this action. Shortcut method
\r
171 * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
\r
172 * @param {Boolean} hidden True to hide the component, false to show it
\r
174 setHidden : function(v){
\r
175 this.initialConfig.hidden = v;
\r
176 this.callEach('setVisible', [!v]);
\r
180 * Shows all components using this action.
\r
183 this.setHidden(false);
\r
187 * Hides all components using this action.
\r
190 this.setHidden(true);
\r
194 * Returns true if the components using this action are currently hidden, else returns false.
\r
196 isHidden : function(){
\r
197 return this.initialConfig.hidden;
\r
201 * Sets the function that will be called by each component using this action when its primary event is triggered.
\r
202 * @param {Function} fn The function that will be invoked by the action's components. The function
\r
203 * will be called with no arguments.
\r
204 * @param {Object} scope The scope in which the function will execute
\r
206 setHandler : function(fn, scope){
\r
207 this.initialConfig.handler = fn;
\r
208 this.initialConfig.scope = scope;
\r
209 this.callEach('setHandler', [fn, scope]);
\r
213 * Executes the specified function once for each component currently tied to this action. The function passed
\r
214 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
\r
215 * @param {Function} fn The function to execute for each component
\r
216 * @param {Object} scope The scope in which the function will execute
\r
218 each : function(fn, scope){
\r
219 Ext.each(this.items, fn, scope);
\r
223 callEach : function(fnName, args){
\r
224 var cs = this.items;
\r
225 for(var i = 0, len = cs.length; i < len; i++){
\r
226 cs[i][fnName].apply(cs[i], args);
\r
231 addComponent : function(comp){
\r
232 this.items.push(comp);
\r
233 comp.on('destroy', this.removeComponent, this);
\r
237 removeComponent : function(comp){
\r
238 this.items.remove(comp);
\r
242 * Executes this action manually using the handler function specified in the original config object
\r
243 * or the handler function set with <code>{@link #setHandler}</code>. Any arguments passed to this
\r
244 * function will be passed on to the handler function.
\r
245 * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
\r
246 * @param {Mixed} arg2 (optional)
\r
247 * @param {Mixed} etc... (optional)
\r
249 execute : function(){
\r
250 this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);
\r