Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / docs / source / Action1.html
1 <html>\r
2 <head>\r
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    \r
4   <title>The source code</title>\r
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
7 </head>\r
8 <body  onload="prettyPrint();">\r
9     <pre class="prettyprint lang-js"><div id="cls-Ext.Action"></div>/**\r
10  * @class Ext.Action\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
19  * <pre><code>\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     {@link #text}: 'Do something',\r
24     {@link #handler}: function(){\r
25         Ext.Msg.alert('Click', 'You did something.');\r
26     },\r
27     {@link #iconCls}: 'do-something',\r
28     {@link #itemId}: 'myAction'\r
29 });\r
30 \r
31 var panel = new Ext.Panel({\r
32     title: 'Actions',\r
33     width: 500,\r
34     height: 300,\r
35     tbar: [\r
36         // Add the action directly to a toolbar as a menu button\r
37         action,\r
38         {\r
39             text: 'Action Menu',\r
40             // Add the action to a menu as a text item\r
41             menu: [action]\r
42         }\r
43     ],\r
44     items: [\r
45         // Add the action to the panel body as a standard button\r
46         new Ext.Button(action)\r
47     ],\r
48     renderTo: Ext.getBody()\r
49 });\r
50 \r
51 // Change the text for all components using the action\r
52 action.setText('Something else');\r
53 \r
54 // Reference an action through a container using the itemId\r
55 var btn = panel.getComponent('myAction');\r
56 var aRef = btn.baseAction;\r
57 aRef.setText('New text');\r
58 </code></pre>\r
59  * @constructor\r
60  * @param {Object} config The configuration options\r
61  */\r
62 Ext.Action = Ext.extend(Object, {\r
63     <div id="cfg-Ext.Action-text"></div>/**\r
64      * @cfg {String} text The text to set for all components using this action (defaults to '').\r
65      */\r
66     <div id="cfg-Ext.Action-iconCls"></div>/**\r
67      * @cfg {String} iconCls\r
68      * The CSS class selector that specifies a background image to be used as the header icon for\r
69      * all components using this action (defaults to '').\r
70      * <p>An example of specifying a custom icon class would be something like:\r
71      * </p><pre><code>\r
72 // specify the property in the config for the class:\r
73      ...\r
74      iconCls: 'do-something'\r
75 \r
76 // css class that specifies background image to be used as the icon image:\r
77 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }\r
78 </code></pre>\r
79      */\r
80     <div id="cfg-Ext.Action-disabled"></div>/**\r
81      * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).\r
82      */\r
83     <div id="cfg-Ext.Action-hidden"></div>/**\r
84      * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).\r
85      */\r
86     <div id="cfg-Ext.Action-handler"></div>/**\r
87      * @cfg {Function} handler The function that will be invoked by each component tied to this action\r
88      * when the component's primary event is triggered (defaults to undefined).\r
89      */\r
90     <div id="cfg-Ext.Action-itemId"></div>/**\r
91      * @cfg {String} itemId\r
92      * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.\r
93      */\r
94     <div id="cfg-Ext.Action-scope"></div>/**\r
95      * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the\r
96      * <code>{@link #handler}</code> is executed. Defaults to this Button.\r
97      */\r
98 \r
99     constructor : function(config){\r
100         this.initialConfig = config;\r
101         this.itemId = config.itemId = (config.itemId || config.id || Ext.id());\r
102         this.items = [];\r
103     },\r
104     \r
105     // private\r
106     isAction : true,\r
107 \r
108     <div id="method-Ext.Action-setText"></div>/**\r
109      * Sets the text to be displayed by all components using this action.\r
110      * @param {String} text The text to display\r
111      */\r
112     setText : function(text){\r
113         this.initialConfig.text = text;\r
114         this.callEach('setText', [text]);\r
115     },\r
116 \r
117     <div id="method-Ext.Action-getText"></div>/**\r
118      * Gets the text currently displayed by all components using this action.\r
119      */\r
120     getText : function(){\r
121         return this.initialConfig.text;\r
122     },\r
123 \r
124     <div id="method-Ext.Action-setIconClass"></div>/**\r
125      * Sets the icon CSS class for all components using this action.  The class should supply\r
126      * a background image that will be used as the icon image.\r
127      * @param {String} cls The CSS class supplying the icon image\r
128      */\r
129     setIconClass : function(cls){\r
130         this.initialConfig.iconCls = cls;\r
131         this.callEach('setIconClass', [cls]);\r
132     },\r
133 \r
134     <div id="method-Ext.Action-getIconClass"></div>/**\r
135      * Gets the icon CSS class currently used by all components using this action.\r
136      */\r
137     getIconClass : function(){\r
138         return this.initialConfig.iconCls;\r
139     },\r
140 \r
141     <div id="method-Ext.Action-setDisabled"></div>/**\r
142      * Sets the disabled state of all components using this action.  Shortcut method\r
143      * for {@link #enable} and {@link #disable}.\r
144      * @param {Boolean} disabled True to disable the component, false to enable it\r
145      */\r
146     setDisabled : function(v){\r
147         this.initialConfig.disabled = v;\r
148         this.callEach('setDisabled', [v]);\r
149     },\r
150 \r
151     <div id="method-Ext.Action-enable"></div>/**\r
152      * Enables all components using this action.\r
153      */\r
154     enable : function(){\r
155         this.setDisabled(false);\r
156     },\r
157 \r
158     <div id="method-Ext.Action-disable"></div>/**\r
159      * Disables all components using this action.\r
160      */\r
161     disable : function(){\r
162         this.setDisabled(true);\r
163     },\r
164 \r
165     <div id="method-Ext.Action-isDisabled"></div>/**\r
166      * Returns true if the components using this action are currently disabled, else returns false.  \r
167      */\r
168     isDisabled : function(){\r
169         return this.initialConfig.disabled;\r
170     },\r
171 \r
172     <div id="method-Ext.Action-setHidden"></div>/**\r
173      * Sets the hidden state of all components using this action.  Shortcut method\r
174      * for <code>{@link #hide}</code> and <code>{@link #show}</code>.\r
175      * @param {Boolean} hidden True to hide the component, false to show it\r
176      */\r
177     setHidden : function(v){\r
178         this.initialConfig.hidden = v;\r
179         this.callEach('setVisible', [!v]);\r
180     },\r
181 \r
182     <div id="method-Ext.Action-show"></div>/**\r
183      * Shows all components using this action.\r
184      */\r
185     show : function(){\r
186         this.setHidden(false);\r
187     },\r
188 \r
189     <div id="method-Ext.Action-hide"></div>/**\r
190      * Hides all components using this action.\r
191      */\r
192     hide : function(){\r
193         this.setHidden(true);\r
194     },\r
195 \r
196     <div id="method-Ext.Action-isHidden"></div>/**\r
197      * Returns true if the components using this action are currently hidden, else returns false.  \r
198      */\r
199     isHidden : function(){\r
200         return this.initialConfig.hidden;\r
201     },\r
202 \r
203     <div id="method-Ext.Action-setHandler"></div>/**\r
204      * Sets the function that will be called by each Component using this action when its primary event is triggered.\r
205      * @param {Function} fn The function that will be invoked by the action's components.  The function\r
206      * will be called with no arguments.\r
207      * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.\r
208      */\r
209     setHandler : function(fn, scope){\r
210         this.initialConfig.handler = fn;\r
211         this.initialConfig.scope = scope;\r
212         this.callEach('setHandler', [fn, scope]);\r
213     },\r
214 \r
215     <div id="method-Ext.Action-each"></div>/**\r
216      * Executes the specified function once for each Component currently tied to this action.  The function passed\r
217      * in should accept a single argument that will be an object that supports the basic Action config/method interface.\r
218      * @param {Function} fn The function to execute for each component\r
219      * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed.  Defaults to the Component.\r
220      */\r
221     each : function(fn, scope){\r
222         Ext.each(this.items, fn, scope);\r
223     },\r
224 \r
225     // private\r
226     callEach : function(fnName, args){\r
227         var cs = this.items;\r
228         for(var i = 0, len = cs.length; i < len; i++){\r
229             cs[i][fnName].apply(cs[i], args);\r
230         }\r
231     },\r
232 \r
233     // private\r
234     addComponent : function(comp){\r
235         this.items.push(comp);\r
236         comp.on('destroy', this.removeComponent, this);\r
237     },\r
238 \r
239     // private\r
240     removeComponent : function(comp){\r
241         this.items.remove(comp);\r
242     },\r
243 \r
244     <div id="method-Ext.Action-execute"></div>/**\r
245      * Executes this action manually using the handler function specified in the original config object\r
246      * or the handler function set with <code>{@link #setHandler}</code>.  Any arguments passed to this\r
247      * function will be passed on to the handler function.\r
248      * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function\r
249      * @param {Mixed} arg2 (optional)\r
250      * @param {Mixed} etc... (optional)\r
251      */\r
252     execute : function(){\r
253         this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);\r
254     }\r
255 });\r
256 </pre>    \r
257 </body>\r
258 </html>