4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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='']
85 * The text to set for all components configured by this Action.
87 <span id='Ext-Action-cfg-iconCls'> /**
88 </span> * @cfg {String} [iconCls='']
89 * The CSS class selector that specifies a background image to be used as the header icon for
90 * all components configured by this Action.
91 * <p>An example of specifying a custom icon class would be something like:
92 * </p><pre><code>
93 // specify the property in the config for the class:
95 iconCls: 'do-something'
97 // css class that specifies background image to be used as the icon image:
98 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
99 </code></pre>
101 <span id='Ext-Action-cfg-disabled'> /**
102 </span> * @cfg {Boolean} [disabled=false]
103 * True to disable all components configured by this Action, false to enable them.
105 <span id='Ext-Action-cfg-hidden'> /**
106 </span> * @cfg {Boolean} [hidden=false]
107 * True to hide all components configured by this Action, false to show them.
109 <span id='Ext-Action-cfg-handler'> /**
110 </span> * @cfg {Function} handler
111 * The function that will be invoked by each component tied to this Action
112 * when the component's primary event is triggered.
114 <span id='Ext-Action-cfg-itemId'> /**
115 </span> * @cfg {String} itemId
116 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
118 <span id='Ext-Action-cfg-scope'> /**
119 </span> * @cfg {Object} scope
120 * The scope (this reference) in which the {@link #handler} is executed.
121 * Defaults to the browser window.
124 <span id='Ext-Action-method-constructor'> /**
125 </span> * Creates new Action.
126 * @param {Object} config Config object.
128 constructor : function(config){
129 this.initialConfig = config;
130 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
137 <span id='Ext-Action-method-setText'> /**
138 </span> * Sets the text to be displayed by all components configured by this Action.
139 * @param {String} text The text to display
141 setText : function(text){
142 this.initialConfig.text = text;
143 this.callEach('setText', [text]);
146 <span id='Ext-Action-method-getText'> /**
147 </span> * Gets the text currently displayed by all components configured by this Action.
149 getText : function(){
150 return this.initialConfig.text;
153 <span id='Ext-Action-method-setIconCls'> /**
154 </span> * Sets the icon CSS class for all components configured by this Action. The class should supply
155 * a background image that will be used as the icon image.
156 * @param {String} cls The CSS class supplying the icon image
158 setIconCls : function(cls){
159 this.initialConfig.iconCls = cls;
160 this.callEach('setIconCls', [cls]);
163 <span id='Ext-Action-method-getIconCls'> /**
164 </span> * Gets the icon CSS class currently used by all components configured by this Action.
166 getIconCls : function(){
167 return this.initialConfig.iconCls;
170 <span id='Ext-Action-method-setDisabled'> /**
171 </span> * Sets the disabled state of all components configured by this Action. Shortcut method
172 * for {@link #enable} and {@link #disable}.
173 * @param {Boolean} disabled True to disable the component, false to enable it
175 setDisabled : function(v){
176 this.initialConfig.disabled = v;
177 this.callEach('setDisabled', [v]);
180 <span id='Ext-Action-method-enable'> /**
181 </span> * Enables all components configured by this Action.
184 this.setDisabled(false);
187 <span id='Ext-Action-method-disable'> /**
188 </span> * Disables all components configured by this Action.
190 disable : function(){
191 this.setDisabled(true);
194 <span id='Ext-Action-method-isDisabled'> /**
195 </span> * Returns true if the components using this Action are currently disabled, else returns false.
197 isDisabled : function(){
198 return this.initialConfig.disabled;
201 <span id='Ext-Action-method-setHidden'> /**
202 </span> * Sets the hidden state of all components configured by this Action. Shortcut method
203 * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
204 * @param {Boolean} hidden True to hide the component, false to show it
206 setHidden : function(v){
207 this.initialConfig.hidden = v;
208 this.callEach('setVisible', [!v]);
211 <span id='Ext-Action-method-show'> /**
212 </span> * Shows all components configured by this Action.
215 this.setHidden(false);
218 <span id='Ext-Action-method-hide'> /**
219 </span> * Hides all components configured by this Action.
222 this.setHidden(true);
225 <span id='Ext-Action-method-isHidden'> /**
226 </span> * Returns true if the components configured by this Action are currently hidden, else returns false.
228 isHidden : function(){
229 return this.initialConfig.hidden;
232 <span id='Ext-Action-method-setHandler'> /**
233 </span> * Sets the function that will be called by each Component using this action when its primary event is triggered.
234 * @param {Function} fn The function that will be invoked by the action's components. The function
235 * will be called with no arguments.
236 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.
238 setHandler : function(fn, scope){
239 this.initialConfig.handler = fn;
240 this.initialConfig.scope = scope;
241 this.callEach('setHandler', [fn, scope]);
244 <span id='Ext-Action-method-each'> /**
245 </span> * Executes the specified function once for each Component currently tied to this Action. The function passed
246 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
247 * @param {Function} fn The function to execute for each component
248 * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component.
250 each : function(fn, scope){
251 Ext.each(this.items, fn, scope);
255 callEach : function(fnName, args){
256 var items = this.items,
260 for(; i < len; i++){
261 items[i][fnName].apply(items[i], args);
266 addComponent : function(comp){
267 this.items.push(comp);
268 comp.on('destroy', this.removeComponent, this);
272 removeComponent : function(comp){
273 Ext.Array.remove(this.items, comp);
276 <span id='Ext-Action-method-execute'> /**
277 </span> * Executes this Action manually using the handler function specified in the original config object
278 * or the handler function set with <code>{@link #setHandler}</code>. Any arguments passed to this
279 * function will be passed on to the handler function.
280 * @param {Object...} args (optional) Variable number of arguments passed to the handler function
282 execute : function(){
283 this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);