2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
11 * <p>An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it
\r
12 * can be usefully shared among multiple components. Actions let you share handlers, configuration options and UI
\r
13 * updates across any components that support the Action interface (primarily {@link Ext.Toolbar}, {@link Ext.Button}
\r
14 * and {@link Ext.menu.Menu} components).</p>
\r
15 * <p>Aside from supporting the config object interface, any component that needs to use Actions must also support
\r
16 * the following method list, as these will be called as needed by the Action class: setText(string), setIconCls(string),
\r
17 * setDisabled(boolean), setVisible(boolean) and setHandler(function).</p>
\r
18 * Example usage:<br>
\r
20 // Define the shared action. Each component below will have the same
\r
21 // display text and icon, and will display the same message on click.
\r
22 var action = new Ext.Action({
\r
23 text: 'Do something',
\r
24 handler: function(){
\r
25 Ext.Msg.alert('Click', 'You did something.');
\r
27 iconCls: 'do-something'
\r
30 var panel = new Ext.Panel({
\r
35 // 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
53 * @param {Object} config The configuration options
\r
55 Ext.Action = function(config){
\r
56 this.initialConfig = config;
\r
60 Ext.Action.prototype = {
\r
62 * @cfg {String} text The text to set for all components using this action (defaults to '').
\r
65 * @cfg {String} iconCls The icon CSS class for all components using this action (defaults to '').
\r
66 * The class should supply a background image that will be used as the icon image.
\r
69 * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).
\r
72 * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).
\r
75 * @cfg {Function} handler The function that will be invoked by each component tied to this action
\r
76 * when the component's primary event is triggered (defaults to undefined).
\r
79 * @cfg {Object} scope The scope in which the {@link #handler} function will execute.
\r
86 * Sets the text to be displayed by all components using this action.
\r
87 * @param {String} text The text to display
\r
89 setText : function(text){
\r
90 this.initialConfig.text = text;
\r
91 this.callEach('setText', [text]);
\r
95 * Gets the text currently displayed by all components using this action.
\r
97 getText : function(){
\r
98 return this.initialConfig.text;
\r
102 * Sets the icon CSS class for all components using this action. The class should supply
\r
103 * a background image that will be used as the icon image.
\r
104 * @param {String} cls The CSS class supplying the icon image
\r
106 setIconClass : function(cls){
\r
107 this.initialConfig.iconCls = cls;
\r
108 this.callEach('setIconClass', [cls]);
\r
112 * Gets the icon CSS class currently used by all components using this action.
\r
114 getIconClass : function(){
\r
115 return this.initialConfig.iconCls;
\r
119 * Sets the disabled state of all components using this action. Shortcut method
\r
120 * for {@link #enable} and {@link #disable}.
\r
121 * @param {Boolean} disabled True to disable the component, false to enable it
\r
123 setDisabled : function(v){
\r
124 this.initialConfig.disabled = v;
\r
125 this.callEach('setDisabled', [v]);
\r
129 * Enables all components using this action.
\r
131 enable : function(){
\r
132 this.setDisabled(false);
\r
136 * Disables all components using this action.
\r
138 disable : function(){
\r
139 this.setDisabled(true);
\r
143 * Returns true if the components using this action are currently disabled, else returns false. Read-only.
\r
146 isDisabled : function(){
\r
147 return this.initialConfig.disabled;
\r
151 * Sets the hidden state of all components using this action. Shortcut method
\r
152 * for {@link #hide} and {@link #show}.
\r
153 * @param {Boolean} hidden True to hide the component, false to show it
\r
155 setHidden : function(v){
\r
156 this.initialConfig.hidden = v;
\r
157 this.callEach('setVisible', [!v]);
\r
161 * Shows all components using this action.
\r
164 this.setHidden(false);
\r
168 * Hides all components using this action.
\r
171 this.setHidden(true);
\r
175 * Returns true if the components using this action are currently hidden, else returns false. Read-only.
\r
178 isHidden : function(){
\r
179 return this.initialConfig.hidden;
\r
183 * Sets the function that will be called by each component using this action when its primary event is triggered.
\r
184 * @param {Function} fn The function that will be invoked by the action's components. The function
\r
185 * will be called with no arguments.
\r
186 * @param {Object} scope The scope in which the function will execute
\r
188 setHandler : function(fn, scope){
\r
189 this.initialConfig.handler = fn;
\r
190 this.initialConfig.scope = scope;
\r
191 this.callEach('setHandler', [fn, scope]);
\r
195 * Executes the specified function once for each component currently tied to this action. The function passed
\r
196 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
\r
197 * @param {Function} fn The function to execute for each component
\r
198 * @param {Object} scope The scope in which the function will execute
\r
200 each : function(fn, scope){
\r
201 Ext.each(this.items, fn, scope);
\r
205 callEach : function(fnName, args){
\r
206 var cs = this.items;
\r
207 for(var i = 0, len = cs.length; i < len; i++){
\r
208 cs[i][fnName].apply(cs[i], args);
\r
213 addComponent : function(comp){
\r
214 this.items.push(comp);
\r
215 comp.on('destroy', this.removeComponent, this);
\r
219 removeComponent : function(comp){
\r
220 this.items.remove(comp);
\r
224 * Executes this action manually using the default handler specified in the original config object. Any arguments
\r
225 * passed to this function will be passed on to the handler function.
\r
226 * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
\r
227 * @param {Mixed} arg2 (optional)
\r
228 * @param {Mixed} etc... (optional)
\r
230 execute : function(){
\r
231 this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);
\r