4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Action-method-constructor'><span id='Ext-Action'>/**
19 </span></span> * @class Ext.Action
20 * <p>An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it
21 * can be usefully shared among multiple components. Actions let you share handlers, configuration options and UI
22 * updates across any components that support the Action interface (primarily {@link Ext.toolbar.Toolbar}, {@link Ext.button.Button}
23 * and {@link Ext.menu.Menu} components).</p>
24 * <p>Use a single Action instance as the config object for any number of UI Components which share the same configuration. The
25 * Action not only supplies the configuration, but allows all Components based upon it to have a common set of methods
26 * called at once through a single call to the Action.</p>
27 * <p>Any Component that is to be configured with an Action must also support
28 * the following methods:<ul>
29 * <li><code>setText(string)</code></li>
30 * <li><code>setIconCls(string)</code></li>
31 * <li><code>setDisabled(boolean)</code></li>
32 * <li><code>setVisible(boolean)</code></li>
33 * <li><code>setHandler(function)</code></li></ul>.</p>
34 * <p>This allows the Action to control its associated Components.</p>
35 * Example usage:<br>
36 * <pre><code>
37 // Define the shared Action. Each Component below will have the same
38 // display text and icon, and will display the same message on click.
39 var action = new Ext.Action({
40 {@link #text}: 'Do something',
41 {@link #handler}: function(){
42 Ext.Msg.alert('Click', 'You did something.');
44 {@link #iconCls}: 'do-something',
45 {@link #itemId}: 'myAction'
48 var panel = new Ext.panel.Panel({
53 // Add the Action directly to a toolbar as a menu button
57 // Add the Action to a menu as a text item
62 // Add the Action to the panel body as a standard button
63 new Ext.button.Button(action)
65 renderTo: Ext.getBody()
68 // Change the text for all components using the Action
69 action.setText('Something else');
71 // Reference an Action through a container using the itemId
72 var btn = panel.getComponent('myAction');
73 var aRef = btn.baseAction;
74 aRef.setText('New text');
75 </code></pre>
77 * @param {Object} config The configuration options
79 Ext.define('Ext.Action', {
81 /* Begin Definitions */
85 <span id='Ext-Action-cfg-text'> /**
86 </span> * @cfg {String} text The text to set for all components configured by this Action (defaults to '').
88 <span id='Ext-Action-cfg-iconCls'> /**
89 </span> * @cfg {String} iconCls
90 * The CSS class selector that specifies a background image to be used as the header icon for
91 * all components configured by this Action (defaults to '').
92 * <p>An example of specifying a custom icon class would be something like:
93 * </p><pre><code>
94 // specify the property in the config for the class:
96 iconCls: 'do-something'
98 // css class that specifies background image to be used as the icon image:
99 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
100 </code></pre>
102 <span id='Ext-Action-cfg-disabled'> /**
103 </span> * @cfg {Boolean} disabled True to disable all components configured by this Action, false to enable them (defaults to false).
105 <span id='Ext-Action-cfg-hidden'> /**
106 </span> * @cfg {Boolean} hidden True to hide all components configured by this Action, false to show them (defaults to false).
108 <span id='Ext-Action-cfg-handler'> /**
109 </span> * @cfg {Function} handler The function that will be invoked by each component tied to this Action
110 * when the component's primary event is triggered (defaults to undefined).
112 <span id='Ext-Action-cfg-itemId'> /**
113 </span> * @cfg {String} itemId
114 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
116 <span id='Ext-Action-cfg-scope'> /**
117 </span> * @cfg {Object} scope The scope (<code><b>this</b></code> reference) in which the
118 * <code>{@link #handler}</code> is executed. Defaults to the browser window.
121 constructor : function(config){
122 this.initialConfig = config;
123 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
130 <span id='Ext-Action-method-setText'> /**
131 </span> * Sets the text to be displayed by all components configured by this Action.
132 * @param {String} text The text to display
134 setText : function(text){
135 this.initialConfig.text = text;
136 this.callEach('setText', [text]);
139 <span id='Ext-Action-method-getText'> /**
140 </span> * Gets the text currently displayed by all components configured by this Action.
142 getText : function(){
143 return this.initialConfig.text;
146 <span id='Ext-Action-method-setIconCls'> /**
147 </span> * Sets the icon CSS class for all components configured by this Action. The class should supply
148 * a background image that will be used as the icon image.
149 * @param {String} cls The CSS class supplying the icon image
151 setIconCls : function(cls){
152 this.initialConfig.iconCls = cls;
153 this.callEach('setIconCls', [cls]);
156 <span id='Ext-Action-method-getIconCls'> /**
157 </span> * Gets the icon CSS class currently used by all components configured by this Action.
159 getIconCls : function(){
160 return this.initialConfig.iconCls;
163 <span id='Ext-Action-method-setDisabled'> /**
164 </span> * Sets the disabled state of all components configured by this Action. Shortcut method
165 * for {@link #enable} and {@link #disable}.
166 * @param {Boolean} disabled True to disable the component, false to enable it
168 setDisabled : function(v){
169 this.initialConfig.disabled = v;
170 this.callEach('setDisabled', [v]);
173 <span id='Ext-Action-method-enable'> /**
174 </span> * Enables all components configured by this Action.
177 this.setDisabled(false);
180 <span id='Ext-Action-method-disable'> /**
181 </span> * Disables all components configured by this Action.
183 disable : function(){
184 this.setDisabled(true);
187 <span id='Ext-Action-method-isDisabled'> /**
188 </span> * Returns true if the components using this Action are currently disabled, else returns false.
190 isDisabled : function(){
191 return this.initialConfig.disabled;
194 <span id='Ext-Action-method-setHidden'> /**
195 </span> * Sets the hidden state of all components configured by this Action. Shortcut method
196 * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
197 * @param {Boolean} hidden True to hide the component, false to show it
199 setHidden : function(v){
200 this.initialConfig.hidden = v;
201 this.callEach('setVisible', [!v]);
204 <span id='Ext-Action-method-show'> /**
205 </span> * Shows all components configured by this Action.
208 this.setHidden(false);
211 <span id='Ext-Action-method-hide'> /**
212 </span> * Hides all components configured by this Action.
215 this.setHidden(true);
218 <span id='Ext-Action-method-isHidden'> /**
219 </span> * Returns true if the components configured by this Action are currently hidden, else returns false.
221 isHidden : function(){
222 return this.initialConfig.hidden;
225 <span id='Ext-Action-method-setHandler'> /**
226 </span> * Sets the function that will be called by each Component using this action when its primary event is triggered.
227 * @param {Function} fn The function that will be invoked by the action's components. The function
228 * will be called with no arguments.
229 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.
231 setHandler : function(fn, scope){
232 this.initialConfig.handler = fn;
233 this.initialConfig.scope = scope;
234 this.callEach('setHandler', [fn, scope]);
237 <span id='Ext-Action-method-each'> /**
238 </span> * Executes the specified function once for each Component currently tied to this Action. The function passed
239 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
240 * @param {Function} fn The function to execute for each component
241 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component.
243 each : function(fn, scope){
244 Ext.each(this.items, fn, scope);
248 callEach : function(fnName, args){
249 var items = this.items,
253 for(; i < len; i++){
254 items[i][fnName].apply(items[i], args);
259 addComponent : function(comp){
260 this.items.push(comp);
261 comp.on('destroy', this.removeComponent, this);
265 removeComponent : function(comp){
266 Ext.Array.remove(this.items, comp);
269 <span id='Ext-Action-method-execute'> /**
270 </span> * Executes this Action manually using the handler function specified in the original config object
271 * or the handler function set with <code>{@link #setHandler}</code>. Any arguments passed to this
272 * function will be passed on to the handler function.
273 * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
274 * @param {Mixed} arg2 (optional)
275 * @param {Mixed} etc... (optional)
277 execute : function(){
278 this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);