Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / widgets / Action.js
index 4ccb5d5..803bf08 100644 (file)
 /*!
- * Ext JS Library 3.1.1
- * Copyright(c) 2006-2010 Ext JS, LLC
+ * Ext JS Library 3.2.0
+ * Copyright(c) 2006-2010 Ext JS, Inc.
  * licensing@extjs.com
  * http://www.extjs.com/license
  */
-/**\r
- * @class Ext.Action\r
- * <p>An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it\r
- * can be usefully shared among multiple components.  Actions let you share handlers, configuration options and UI\r
- * updates across any components that support the Action interface (primarily {@link Ext.Toolbar}, {@link Ext.Button}\r
- * and {@link Ext.menu.Menu} components).</p>\r
- * <p>Aside from supporting the config object interface, any component that needs to use Actions must also support\r
- * the following method list, as these will be called as needed by the Action class: setText(string), setIconCls(string),\r
- * setDisabled(boolean), setVisible(boolean) and setHandler(function).</p>\r
- * Example usage:<br>\r
- * <pre><code>\r
-// Define the shared action.  Each component below will have the same\r
-// display text and icon, and will display the same message on click.\r
-var action = new Ext.Action({\r
-    {@link #text}: 'Do something',\r
-    {@link #handler}: function(){\r
-        Ext.Msg.alert('Click', 'You did something.');\r
-    },\r
-    {@link #iconCls}: 'do-something',\r
-    {@link #itemId}: 'myAction'\r
-});\r
-\r
-var panel = new Ext.Panel({\r
-    title: 'Actions',\r
-    width: 500,\r
-    height: 300,\r
-    tbar: [\r
-        // Add the action directly to a toolbar as a menu button\r
-        action,\r
-        {\r
-            text: 'Action Menu',\r
-            // Add the action to a menu as a text item\r
-            menu: [action]\r
-        }\r
-    ],\r
-    items: [\r
-        // Add the action to the panel body as a standard button\r
-        new Ext.Button(action)\r
-    ],\r
-    renderTo: Ext.getBody()\r
-});\r
-\r
-// Change the text for all components using the action\r
-action.setText('Something else');\r
-\r
-// Reference an action through a container using the itemId\r
-var btn = panel.getComponent('myAction');\r
-var aRef = btn.baseAction;\r
-aRef.setText('New text');\r
-</code></pre>\r
- * @constructor\r
- * @param {Object} config The configuration options\r
- */\r
-Ext.Action = Ext.extend(Object, {\r
-    /**\r
-     * @cfg {String} text The text to set for all components using this action (defaults to '').\r
-     */\r
-    /**\r
-     * @cfg {String} iconCls\r
-     * The CSS class selector that specifies a background image to be used as the header icon for\r
-     * all components using this action (defaults to '').\r
-     * <p>An example of specifying a custom icon class would be something like:\r
-     * </p><pre><code>\r
-// specify the property in the config for the class:\r
-     ...\r
-     iconCls: 'do-something'\r
-\r
-// css class that specifies background image to be used as the icon image:\r
-.do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }\r
-</code></pre>\r
-     */\r
-    /**\r
-     * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).\r
-     */\r
-    /**\r
-     * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).\r
-     */\r
-    /**\r
-     * @cfg {Function} handler The function that will be invoked by each component tied to this action\r
-     * when the component's primary event is triggered (defaults to undefined).\r
-     */\r
-    /**\r
-     * @cfg {String} itemId\r
-     * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.\r
-     */\r
-    /**\r
-     * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the\r
-     * <code>{@link #handler}</code> is executed. Defaults to this Button.\r
-     */\r
-\r
-    constructor : function(config){\r
-        this.initialConfig = config;\r
-        this.itemId = config.itemId = (config.itemId || config.id || Ext.id());\r
-        this.items = [];\r
-    },\r
-    \r
-    // private\r
-    isAction : true,\r
-\r
-    /**\r
-     * Sets the text to be displayed by all components using this action.\r
-     * @param {String} text The text to display\r
-     */\r
-    setText : function(text){\r
-        this.initialConfig.text = text;\r
-        this.callEach('setText', [text]);\r
-    },\r
-\r
-    /**\r
-     * Gets the text currently displayed by all components using this action.\r
-     */\r
-    getText : function(){\r
-        return this.initialConfig.text;\r
-    },\r
-\r
-    /**\r
-     * Sets the icon CSS class for all components using this action.  The class should supply\r
-     * a background image that will be used as the icon image.\r
-     * @param {String} cls The CSS class supplying the icon image\r
-     */\r
-    setIconClass : function(cls){\r
-        this.initialConfig.iconCls = cls;\r
-        this.callEach('setIconClass', [cls]);\r
-    },\r
-\r
-    /**\r
-     * Gets the icon CSS class currently used by all components using this action.\r
-     */\r
-    getIconClass : function(){\r
-        return this.initialConfig.iconCls;\r
-    },\r
-\r
-    /**\r
-     * Sets the disabled state of all components using this action.  Shortcut method\r
-     * for {@link #enable} and {@link #disable}.\r
-     * @param {Boolean} disabled True to disable the component, false to enable it\r
-     */\r
-    setDisabled : function(v){\r
-        this.initialConfig.disabled = v;\r
-        this.callEach('setDisabled', [v]);\r
-    },\r
-\r
-    /**\r
-     * Enables all components using this action.\r
-     */\r
-    enable : function(){\r
-        this.setDisabled(false);\r
-    },\r
-\r
-    /**\r
-     * Disables all components using this action.\r
-     */\r
-    disable : function(){\r
-        this.setDisabled(true);\r
-    },\r
-\r
-    /**\r
-     * Returns true if the components using this action are currently disabled, else returns false.  \r
-     */\r
-    isDisabled : function(){\r
-        return this.initialConfig.disabled;\r
-    },\r
-\r
-    /**\r
-     * Sets the hidden state of all components using this action.  Shortcut method\r
-     * for <code>{@link #hide}</code> and <code>{@link #show}</code>.\r
-     * @param {Boolean} hidden True to hide the component, false to show it\r
-     */\r
-    setHidden : function(v){\r
-        this.initialConfig.hidden = v;\r
-        this.callEach('setVisible', [!v]);\r
-    },\r
-\r
-    /**\r
-     * Shows all components using this action.\r
-     */\r
-    show : function(){\r
-        this.setHidden(false);\r
-    },\r
-\r
-    /**\r
-     * Hides all components using this action.\r
-     */\r
-    hide : function(){\r
-        this.setHidden(true);\r
-    },\r
-\r
-    /**\r
-     * Returns true if the components using this action are currently hidden, else returns false.  \r
-     */\r
-    isHidden : function(){\r
-        return this.initialConfig.hidden;\r
-    },\r
-\r
-    /**\r
-     * Sets the function that will be called by each Component using this action when its primary event is triggered.\r
-     * @param {Function} fn The function that will be invoked by the action's components.  The function\r
-     * will be called with no arguments.\r
-     * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed. Defaults to the Component firing the event.\r
-     */\r
-    setHandler : function(fn, scope){\r
-        this.initialConfig.handler = fn;\r
-        this.initialConfig.scope = scope;\r
-        this.callEach('setHandler', [fn, scope]);\r
-    },\r
-\r
-    /**\r
-     * Executes the specified function once for each Component currently tied to this action.  The function passed\r
-     * in should accept a single argument that will be an object that supports the basic Action config/method interface.\r
-     * @param {Function} fn The function to execute for each component\r
-     * @param {Object} scope The scope (<code>this</code> reference) in which the function is executed.  Defaults to the Component.\r
-     */\r
-    each : function(fn, scope){\r
-        Ext.each(this.items, fn, scope);\r
-    },\r
-\r
-    // private\r
-    callEach : function(fnName, args){\r
-        var cs = this.items;\r
-        for(var i = 0, len = cs.length; i < len; i++){\r
-            cs[i][fnName].apply(cs[i], args);\r
-        }\r
-    },\r
-\r
-    // private\r
-    addComponent : function(comp){\r
-        this.items.push(comp);\r
-        comp.on('destroy', this.removeComponent, this);\r
-    },\r
-\r
-    // private\r
-    removeComponent : function(comp){\r
-        this.items.remove(comp);\r
-    },\r
-\r
-    /**\r
-     * Executes this action manually using the handler function specified in the original config object\r
-     * or the handler function set with <code>{@link #setHandler}</code>.  Any arguments passed to this\r
-     * function will be passed on to the handler function.\r
-     * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function\r
-     * @param {Mixed} arg2 (optional)\r
-     * @param {Mixed} etc... (optional)\r
-     */\r
-    execute : function(){\r
-        this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);\r
-    }\r
-});\r
+/**
+ * @class Ext.Action
+ * <p>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}, {@link Ext.Button}
+ * and {@link Ext.menu.Menu} components).</p>
+ * <p>Aside from supporting the config object interface, any component that needs to use Actions must also support
+ * the following method list, as these will be called as needed by the Action class: setText(string), setIconCls(string),
+ * setDisabled(boolean), setVisible(boolean) and setHandler(function).</p>
+ * Example usage:<br>
+ * <pre><code>
+// 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({
+    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(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');
+</code></pre>
+ * @constructor
+ * @param {Object} config The configuration options
+ */
+Ext.Action = Ext.extend(Object, {
+    /**
+     * @cfg {String} text The text to set for all components using this action (defaults to '').
+     */
+    /**
+     * @cfg {String} iconCls
+     * The CSS class selector that specifies a background image to be used as the header icon for
+     * all components using this action (defaults to '').
+     * <p>An example of specifying a custom icon class would be something like:
+     * </p><pre><code>
+// 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; }
+</code></pre>
+     */
+    /**
+     * @cfg {Boolean} disabled True to disable all components using this action, false to enable them (defaults to false).
+     */
+    /**
+     * @cfg {Boolean} hidden True to hide all components using this action, false to show them (defaults to false).
+     */
+    /**
+     * @cfg {Function} handler The function that will be invoked by each component tied to this action
+     * when the component's primary event is triggered (defaults to undefined).
+     */
+    /**
+     * @cfg {String} itemId
+     * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
+     */
+    /**
+     * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
+     * <code>{@link #handler}</code> is executed. Defaults to this Button.
+     */
+
+    constructor : function(config){
+        this.initialConfig = config;
+        this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
+        this.items = [];
+    },
+    
+    // private
+    isAction : true,
+
+    /**
+     * Sets the text to be displayed by all components using 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 using this action.
+     */
+    getText : function(){
+        return this.initialConfig.text;
+    },
+
+    /**
+     * Sets the icon CSS class for all components using 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
+     */
+    setIconClass : function(cls){
+        this.initialConfig.iconCls = cls;
+        this.callEach('setIconClass', [cls]);
+    },
+
+    /**
+     * Gets the icon CSS class currently used by all components using this action.
+     */
+    getIconClass : function(){
+        return this.initialConfig.iconCls;
+    },
+
+    /**
+     * Sets the disabled state of all components using 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 using this action.
+     */
+    enable : function(){
+        this.setDisabled(false);
+    },
+
+    /**
+     * Disables all components using 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 using this action.  Shortcut method
+     * for <code>{@link #hide}</code> and <code>{@link #show}</code>.
+     * @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 using this action.
+     */
+    show : function(){
+        this.setHidden(false);
+    },
+
+    /**
+     * Hides all components using this action.
+     */
+    hide : function(){
+        this.setHidden(true);
+    },
+
+    /**
+     * Returns true if the components using 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 (<code>this</code> 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 (<code>this</code> 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 cs = this.items;
+        for(var i = 0, len = cs.length; i < len; i++){
+            cs[i][fnName].apply(cs[i], args);
+        }
+    },
+
+    // private
+    addComponent : function(comp){
+        this.items.push(comp);
+        comp.on('destroy', this.removeComponent, this);
+    },
+
+    // private
+    removeComponent : function(comp){
+        this.items.remove(comp);
+    },
+
+    /**
+     * Executes this action manually using the handler function specified in the original config object
+     * or the handler function set with <code>{@link #setHandler}</code>.  Any arguments passed to this
+     * function will be passed on to the handler function.
+     * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
+     * @param {Mixed} arg2 (optional)
+     * @param {Mixed} etc... (optional)
+     */
+    execute : function(){
+        this.initialConfig.handler.apply(this.initialConfig.scope || window, arguments);
+    }
+});