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
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.Action"></div>/**
\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
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
27 {@link #iconCls}: 'do-something',
\r
28 {@link #itemId}: 'myAction'
\r
31 var panel = new Ext.Panel({
\r
36 // Add the action directly to a toolbar as a menu button
\r
39 text: 'Action Menu',
\r
40 // Add the action to a menu as a text item
\r
45 // Add the action to the panel body as a standard button
\r
46 new Ext.Button(action)
\r
48 renderTo: Ext.getBody()
\r
51 // Change the text for all components using the action
\r
52 action.setText('Something else');
\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
60 * @param {Object} config The configuration options
\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
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
72 // specify the property in the config for the class:
\r
74 iconCls: 'do-something'
\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
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
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
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
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
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
99 constructor : function(config){
\r
100 this.initialConfig = config;
\r
101 this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
\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
112 setText : function(text){
\r
113 this.initialConfig.text = text;
\r
114 this.callEach('setText', [text]);
\r
117 <div id="method-Ext.Action-getText"></div>/**
\r
118 * Gets the text currently displayed by all components using this action.
\r
120 getText : function(){
\r
121 return this.initialConfig.text;
\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
129 setIconClass : function(cls){
\r
130 this.initialConfig.iconCls = cls;
\r
131 this.callEach('setIconClass', [cls]);
\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
137 getIconClass : function(){
\r
138 return this.initialConfig.iconCls;
\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
146 setDisabled : function(v){
\r
147 this.initialConfig.disabled = v;
\r
148 this.callEach('setDisabled', [v]);
\r
151 <div id="method-Ext.Action-enable"></div>/**
\r
152 * Enables all components using this action.
\r
154 enable : function(){
\r
155 this.setDisabled(false);
\r
158 <div id="method-Ext.Action-disable"></div>/**
\r
159 * Disables all components using this action.
\r
161 disable : function(){
\r
162 this.setDisabled(true);
\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
168 isDisabled : function(){
\r
169 return this.initialConfig.disabled;
\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
177 setHidden : function(v){
\r
178 this.initialConfig.hidden = v;
\r
179 this.callEach('setVisible', [!v]);
\r
182 <div id="method-Ext.Action-show"></div>/**
\r
183 * Shows all components using this action.
\r
186 this.setHidden(false);
\r
189 <div id="method-Ext.Action-hide"></div>/**
\r
190 * Hides all components using this action.
\r
193 this.setHidden(true);
\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
199 isHidden : function(){
\r
200 return this.initialConfig.hidden;
\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
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
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
221 each : function(fn, scope){
\r
222 Ext.each(this.items, fn, scope);
\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
234 addComponent : function(comp){
\r
235 this.items.push(comp);
\r
236 comp.on('destroy', this.removeComponent, this);
\r
240 removeComponent : function(comp){
\r
241 this.items.remove(comp);
\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
252 execute : function(){
\r
253 this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);
\r