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