3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.Action"></div>/**
\r
16 * <p>An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it
\r
17 * can be usefully shared among multiple components. Actions let you share handlers, configuration options and UI
\r
18 * updates across any components that support the Action interface (primarily {@link Ext.Toolbar}, {@link Ext.Button}
\r
19 * and {@link Ext.menu.Menu} components).</p>
\r
20 * <p>Aside from supporting the config object interface, any component that needs to use Actions must also support
\r
21 * the following method list, as these will be called as needed by the Action class: setText(string), setIconCls(string),
\r
22 * setDisabled(boolean), setVisible(boolean) and setHandler(function).</p>
\r
23 * Example usage:<br>
\r
25 // Define the shared action. Each component below will have the same
\r
26 // display text and icon, and will display the same message on click.
\r
27 var action = new Ext.Action({
\r
28 {@link #text}: 'Do something',
\r
29 {@link #handler}: function(){
\r
30 Ext.Msg.alert('Click', 'You did something.');
\r
32 {@link #iconCls}: 'do-something',
\r
33 {@link #itemId}: 'myAction'
\r
36 var panel = new Ext.Panel({
\r
41 // Add the action directly to a toolbar as a menu button
\r
44 text: 'Action Menu',
\r
45 // Add the action to a menu as a text item
\r
50 // Add the action to the panel body as a standard button
\r
51 new Ext.Button(action)
\r
53 renderTo: Ext.getBody()
\r
56 // Change the text for all components using the action
\r
57 action.setText('Something else');
\r
59 // Reference an action through a container using the itemId
\r
60 var btn = panel.getComponent('myAction');
\r
61 var aRef = btn.baseAction;
\r
62 aRef.setText('New text');
\r
65 * @param {Object} config The configuration options
\r
67 Ext.Action = function(config){
\r
68 this.initialConfig = config;
\r
69 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
\r
73 Ext.Action.prototype = {
\r
74 <div id="cfg-Ext.Action-text"></div>/**
\r
75 * @cfg {String} text The text to set for all components using this action (defaults to '').
\r
77 <div id="cfg-Ext.Action-iconCls"></div>/**
\r
78 * @cfg {String} iconCls
\r
79 * The CSS class selector that specifies a background image to be used as the header icon for
\r
80 * all components using this action (defaults to '').
\r
81 * <p>An example of specifying a custom icon class would be something like:
\r
83 // specify the property in the config for the class:
\r
85 iconCls: 'do-something'
\r
87 // css class that specifies background image to be used as the icon image:
\r
88 .do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
\r
91 <div id="cfg-Ext.Action-disabled"></div>/**
\r
92 * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).
\r
94 <div id="cfg-Ext.Action-hidden"></div>/**
\r
95 * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).
\r
97 <div id="cfg-Ext.Action-handler"></div>/**
\r
98 * @cfg {Function} handler The function that will be invoked by each component tied to this action
\r
99 * when the component's primary event is triggered (defaults to undefined).
\r
101 <div id="cfg-Ext.Action-itemId"></div>/**
\r
102 * @cfg {String} itemId
\r
103 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
\r
105 <div id="cfg-Ext.Action-scope"></div>/**
\r
106 * @cfg {Object} scope The scope in which the {@link #handler} function will execute.
\r
112 <div id="method-Ext.Action-setText"></div>/**
\r
113 * Sets the text to be displayed by all components using this action.
\r
114 * @param {String} text The text to display
\r
116 setText : function(text){
\r
117 this.initialConfig.text = text;
\r
118 this.callEach('setText', [text]);
\r
121 <div id="method-Ext.Action-getText"></div>/**
\r
122 * Gets the text currently displayed by all components using this action.
\r
124 getText : function(){
\r
125 return this.initialConfig.text;
\r
128 <div id="method-Ext.Action-setIconClass"></div>/**
\r
129 * Sets the icon CSS class for all components using this action. The class should supply
\r
130 * a background image that will be used as the icon image.
\r
131 * @param {String} cls The CSS class supplying the icon image
\r
133 setIconClass : function(cls){
\r
134 this.initialConfig.iconCls = cls;
\r
135 this.callEach('setIconClass', [cls]);
\r
138 <div id="method-Ext.Action-getIconClass"></div>/**
\r
139 * Gets the icon CSS class currently used by all components using this action.
\r
141 getIconClass : function(){
\r
142 return this.initialConfig.iconCls;
\r
145 <div id="method-Ext.Action-setDisabled"></div>/**
\r
146 * Sets the disabled state of all components using this action. Shortcut method
\r
147 * for {@link #enable} and {@link #disable}.
\r
148 * @param {Boolean} disabled True to disable the component, false to enable it
\r
150 setDisabled : function(v){
\r
151 this.initialConfig.disabled = v;
\r
152 this.callEach('setDisabled', [v]);
\r
155 <div id="method-Ext.Action-enable"></div>/**
\r
156 * Enables all components using this action.
\r
158 enable : function(){
\r
159 this.setDisabled(false);
\r
162 <div id="method-Ext.Action-disable"></div>/**
\r
163 * Disables all components using this action.
\r
165 disable : function(){
\r
166 this.setDisabled(true);
\r
169 <div id="method-Ext.Action-isDisabled"></div>/**
\r
170 * Returns true if the components using this action are currently disabled, else returns false.
\r
172 isDisabled : function(){
\r
173 return this.initialConfig.disabled;
\r
176 <div id="method-Ext.Action-setHidden"></div>/**
\r
177 * Sets the hidden state of all components using this action. Shortcut method
\r
178 * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
\r
179 * @param {Boolean} hidden True to hide the component, false to show it
\r
181 setHidden : function(v){
\r
182 this.initialConfig.hidden = v;
\r
183 this.callEach('setVisible', [!v]);
\r
186 <div id="method-Ext.Action-show"></div>/**
\r
187 * Shows all components using this action.
\r
190 this.setHidden(false);
\r
193 <div id="method-Ext.Action-hide"></div>/**
\r
194 * Hides all components using this action.
\r
197 this.setHidden(true);
\r
200 <div id="method-Ext.Action-isHidden"></div>/**
\r
201 * Returns true if the components using this action are currently hidden, else returns false.
\r
203 isHidden : function(){
\r
204 return this.initialConfig.hidden;
\r
207 <div id="method-Ext.Action-setHandler"></div>/**
\r
208 * Sets the function that will be called by each component using this action when its primary event is triggered.
\r
209 * @param {Function} fn The function that will be invoked by the action's components. The function
\r
210 * will be called with no arguments.
\r
211 * @param {Object} scope The scope in which the function will execute
\r
213 setHandler : function(fn, scope){
\r
214 this.initialConfig.handler = fn;
\r
215 this.initialConfig.scope = scope;
\r
216 this.callEach('setHandler', [fn, scope]);
\r
219 <div id="method-Ext.Action-each"></div>/**
\r
220 * Executes the specified function once for each component currently tied to this action. The function passed
\r
221 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
\r
222 * @param {Function} fn The function to execute for each component
\r
223 * @param {Object} scope The scope in which the function will execute
\r
225 each : function(fn, scope){
\r
226 Ext.each(this.items, fn, scope);
\r
230 callEach : function(fnName, args){
\r
231 var cs = this.items;
\r
232 for(var i = 0, len = cs.length; i < len; i++){
\r
233 cs[i][fnName].apply(cs[i], args);
\r
238 addComponent : function(comp){
\r
239 this.items.push(comp);
\r
240 comp.on('destroy', this.removeComponent, this);
\r
244 removeComponent : function(comp){
\r
245 this.items.remove(comp);
\r
248 <div id="method-Ext.Action-execute"></div>/**
\r
249 * Executes this action manually using the handler function specified in the original config object
\r
250 * or the handler function set with <code>{@link #setHandler}</code>. Any arguments passed to this
\r
251 * function will be passed on to the handler function.
\r
252 * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
\r
253 * @param {Mixed} arg2 (optional)
\r
254 * @param {Mixed} etc... (optional)
\r
256 execute : function(){
\r
257 this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);
\r