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'>/**
19 </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 Ext.define('Ext.Action', {
79 /* Begin Definitions */
83 <span id='Ext-Action-cfg-text'> /**
84 </span> * @cfg {String} text The text to set for all components configured by this Action (defaults to '').
86 <span id='Ext-Action-cfg-iconCls'> /**
87 </span> * @cfg {String} iconCls
88 * The CSS class selector that specifies a background image to be used as the header icon for
89 * all components configured by this Action (defaults to '').
90 * <p>An example of specifying a custom icon class would be something like:
91 * </p><pre><code>
92 // specify the property in the config for the class:
94 iconCls: 'do-something'
96 // css class that specifies background image to be used as the icon image:
97 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
98 </code></pre>
100 <span id='Ext-Action-cfg-disabled'> /**
101 </span> * @cfg {Boolean} disabled True to disable all components configured by this Action, false to enable them (defaults to false).
103 <span id='Ext-Action-cfg-hidden'> /**
104 </span> * @cfg {Boolean} hidden True to hide all components configured by this Action, false to show them (defaults to false).
106 <span id='Ext-Action-cfg-handler'> /**
107 </span> * @cfg {Function} handler The function that will be invoked by each component tied to this Action
108 * when the component's primary event is triggered (defaults to undefined).
110 <span id='Ext-Action-cfg-itemId'> /**
111 </span> * @cfg {String} itemId
112 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
114 <span id='Ext-Action-cfg-scope'> /**
115 </span> * @cfg {Object} scope The scope (<code><b>this</b></code> reference) in which the
116 * <code>{@link #handler}</code> is executed. Defaults to the browser window.
119 <span id='Ext-Action-method-constructor'> /**
120 </span> * Creates new Action.
121 * @param {Object} config Config object.
123 constructor : function(config){
124 this.initialConfig = config;
125 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
132 <span id='Ext-Action-method-setText'> /**
133 </span> * Sets the text to be displayed by all components configured by this Action.
134 * @param {String} text The text to display
136 setText : function(text){
137 this.initialConfig.text = text;
138 this.callEach('setText', [text]);
141 <span id='Ext-Action-method-getText'> /**
142 </span> * Gets the text currently displayed by all components configured by this Action.
144 getText : function(){
145 return this.initialConfig.text;
148 <span id='Ext-Action-method-setIconCls'> /**
149 </span> * Sets the icon CSS class for all components configured by this Action. The class should supply
150 * a background image that will be used as the icon image.
151 * @param {String} cls The CSS class supplying the icon image
153 setIconCls : function(cls){
154 this.initialConfig.iconCls = cls;
155 this.callEach('setIconCls', [cls]);
158 <span id='Ext-Action-method-getIconCls'> /**
159 </span> * Gets the icon CSS class currently used by all components configured by this Action.
161 getIconCls : function(){
162 return this.initialConfig.iconCls;
165 <span id='Ext-Action-method-setDisabled'> /**
166 </span> * Sets the disabled state of all components configured by this Action. Shortcut method
167 * for {@link #enable} and {@link #disable}.
168 * @param {Boolean} disabled True to disable the component, false to enable it
170 setDisabled : function(v){
171 this.initialConfig.disabled = v;
172 this.callEach('setDisabled', [v]);
175 <span id='Ext-Action-method-enable'> /**
176 </span> * Enables all components configured by this Action.
179 this.setDisabled(false);
182 <span id='Ext-Action-method-disable'> /**
183 </span> * Disables all components configured by this Action.
185 disable : function(){
186 this.setDisabled(true);
189 <span id='Ext-Action-method-isDisabled'> /**
190 </span> * Returns true if the components using this Action are currently disabled, else returns false.
192 isDisabled : function(){
193 return this.initialConfig.disabled;
196 <span id='Ext-Action-method-setHidden'> /**
197 </span> * Sets the hidden state of all components configured by this Action. Shortcut method
198 * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
199 * @param {Boolean} hidden True to hide the component, false to show it
201 setHidden : function(v){
202 this.initialConfig.hidden = v;
203 this.callEach('setVisible', [!v]);
206 <span id='Ext-Action-method-show'> /**
207 </span> * Shows all components configured by this Action.
210 this.setHidden(false);
213 <span id='Ext-Action-method-hide'> /**
214 </span> * Hides all components configured by this Action.
217 this.setHidden(true);
220 <span id='Ext-Action-method-isHidden'> /**
221 </span> * Returns true if the components configured by this Action are currently hidden, else returns false.
223 isHidden : function(){
224 return this.initialConfig.hidden;
227 <span id='Ext-Action-method-setHandler'> /**
228 </span> * Sets the function that will be called by each Component using this action when its primary event is triggered.
229 * @param {Function} fn The function that will be invoked by the action's components. The function
230 * will be called with no arguments.
231 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.
233 setHandler : function(fn, scope){
234 this.initialConfig.handler = fn;
235 this.initialConfig.scope = scope;
236 this.callEach('setHandler', [fn, scope]);
239 <span id='Ext-Action-method-each'> /**
240 </span> * Executes the specified function once for each Component currently tied to this Action. The function passed
241 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
242 * @param {Function} fn The function to execute for each component
243 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component.
245 each : function(fn, scope){
246 Ext.each(this.items, fn, scope);
250 callEach : function(fnName, args){
251 var items = this.items,
255 for(; i < len; i++){
256 items[i][fnName].apply(items[i], args);
261 addComponent : function(comp){
262 this.items.push(comp);
263 comp.on('destroy', this.removeComponent, this);
267 removeComponent : function(comp){
268 Ext.Array.remove(this.items, comp);
271 <span id='Ext-Action-method-execute'> /**
272 </span> * Executes this Action manually using the handler function specified in the original config object
273 * or the handler function set with <code>{@link #setHandler}</code>. Any arguments passed to this
274 * function will be passed on to the handler function.
275 * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
276 * @param {Mixed} arg2 (optional)
277 * @param {Mixed} etc... (optional)
279 execute : function(){
280 this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);