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