]>
git.proxmox.com Git - extjs.git/blob - extjs/classic/classic/src/Action.js
2 * An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it
3 * can be usefully shared among multiple components. Actions let you share handlers, configuration options and UI
4 * updates across any components that support the Action interface (primarily {@link Ext.toolbar.Toolbar},
5 * {@link Ext.button.Button} and {@link Ext.menu.Menu} components).
7 * Use a single Action instance as the config object for any number of UI Components which share the same configuration. The
8 * Action not only supplies the configuration, but allows all Components based upon it to have a common set of methods
9 * called at once through a single call to the Action.
11 * Any Component that is to be configured with an Action must also support
12 * the following methods:
15 * - setIconCls(string)
16 * - setDisabled(boolean)
17 * - setVisible(boolean)
18 * - setHandler(function)
20 * This allows the Action to control its associated Components.
24 * // Define the shared Action. Each Component below will have the same
25 * // display text and icon, and will display the same message on click.
26 * var action = new Ext.Action({
27 * {@link #text}: 'Do something',
28 * {@link #handler}: function(){
29 * Ext.Msg.alert('Click', 'You did something.');
31 * {@link #iconCls}: 'do-something',
32 * {@link #itemId}: 'myAction'
35 * var panel = new Ext.panel.Panel({
40 * // Add the Action directly to a toolbar as a menu button
43 * text: 'Action Menu',
44 * // Add the Action to a menu as a text item
49 * // Add the Action to the panel body as a standard button
50 * new Ext.button.Button(action)
52 * renderTo: Ext.getBody()
55 * // Change the text for all components using the Action
56 * action.setText('Something else');
58 * // Reference an Action through a container using the itemId
59 * var btn = panel.getComponent('myAction');
60 * var aRef = btn.baseAction;
61 * aRef.setText('New text');
63 Ext
.define('Ext.Action', {
65 /* Begin Definitions */
70 * @cfg {String} [text='']
71 * The text to set for all components configured by this Action.
74 * @cfg {String} [iconCls='']
75 * @localdoc **Note:** The CSS class(es) specifying the background image will apply
76 * to all components configured by this Action.
77 * @inheritdoc Ext.panel.Header#cfg-iconCls
80 * @cfg {Boolean} [disabled=false]
81 * True to disable all components configured by this Action, false to enable them.
84 * @cfg {Boolean} [hidden=false]
85 * True to hide all components configured by this Action, false to show them.
88 * @cfg {Function} handler
89 * The function that will be invoked by each component tied to this Action
90 * when the component's primary event is triggered.
93 * @cfg {String} itemId
94 * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
98 * The scope (this reference) in which the {@link #handler} is executed.
99 * Defaults to the browser window.
103 * Creates new Action.
104 * @param {Object} config Config object.
106 constructor : function(config
){
107 this.initialConfig
= config
;
108 this.itemId
= config
.itemId
= (config
.itemId
|| config
.id
|| Ext
.id());
113 * @property {Boolean} isAction
114 * `true` in this class to identify an object as an instantiated Action, or subclass thereof.
119 * Sets the text to be displayed by all components configured by this Action.
120 * @param {String} text The text to display
122 setText : function(text
){
123 this.initialConfig
.text
= text
;
124 this.callEach('setText', [text
]);
128 * Gets the text currently displayed by all components configured by this Action.
130 getText : function(){
131 return this.initialConfig
.text
;
135 * Sets the {@link #iconCls icon CSS class} for all components configured by this
136 * Action. The class should supply a background image that will be used as the icon
138 * @param {String} cls The CSS class supplying the icon image
140 setIconCls : function(cls
){
141 this.initialConfig
.iconCls
= cls
;
142 this.callEach('setIconCls', [cls
]);
146 * Gets the icon CSS class currently used by all components configured by this Action.
148 getIconCls : function(){
149 return this.initialConfig
.iconCls
;
153 * Sets the disabled state of all components configured by this Action. Shortcut method
154 * for {@link #enable} and {@link #disable}.
155 * @param {Boolean} disabled True to disable the component, false to enable it
157 setDisabled : function(v
){
158 this.initialConfig
.disabled
= v
;
159 this.callEach('setDisabled', [v
]);
163 * Enables all components configured by this Action.
166 this.setDisabled(false);
170 * Disables all components configured by this Action.
172 disable : function(){
173 this.setDisabled(true);
177 * Returns true if the components using this Action are currently disabled, else returns false.
179 isDisabled : function(){
180 return this.initialConfig
.disabled
;
184 * Sets the hidden state of all components configured by this Action. Shortcut method
185 * for `{@link #hide}` and `{@link #show}`.
186 * @param {Boolean} hidden True to hide the component, false to show it.
188 setHidden : function(v
){
189 this.initialConfig
.hidden
= v
;
190 this.callEach('setVisible', [!v
]);
194 * Shows all components configured by this Action.
197 this.setHidden(false);
201 * Hides all components configured by this Action.
204 this.setHidden(true);
208 * Returns true if the components configured by this Action are currently hidden, else returns false.
210 isHidden : function(){
211 return this.initialConfig
.hidden
;
215 * Sets the function that will be called by each Component using this action when its primary event is triggered.
216 * @param {Function} fn The function that will be invoked by the action's components. The function
217 * will be called with no arguments.
218 * @param {Object} scope The scope (this reference) in which the function is executed. Defaults to the Component
221 setHandler : function(fn
, scope
){
222 this.initialConfig
.handler
= fn
;
223 this.initialConfig
.scope
= scope
;
224 this.callEach('setHandler', [fn
, scope
]);
228 * Executes the specified function once for each Component currently tied to this Action. The function passed
229 * in should accept a single argument that will be an object that supports the basic Action config/method interface.
230 * @param {Function} fn The function to execute for each component
231 * @param {Object} scope The scope (this reference) in which the function is executed.
232 * Defaults to the Component.
234 each : function(fn
, scope
){
235 Ext
.each(this.items
, fn
, scope
);
241 callEach : function(fnName
, args
){
242 var items
= this.items
,
247 Ext
.suspendLayouts();
250 item
[fnName
].apply(item
, args
);
252 Ext
.resumeLayouts(true);
258 addComponent : function(comp
){
259 this.items
.push(comp
);
260 comp
.on('destroy', this.removeComponent
, this);
266 removeComponent : function(comp
){
267 Ext
.Array
.remove(this.items
, comp
);
271 * Executes this Action manually using the handler function specified in the original config object
272 * or the handler function set with {@link #setHandler}. Any arguments passed to this
273 * function will be passed on to the handler function.
274 * @param {Object...} args Variable number of arguments passed to the handler function
276 execute : function(){
277 this.initialConfig
.handler
.apply(this.initialConfig
.scope
|| Ext
.global
, arguments
);