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