Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / docs / resources / TabCloseMenu.js
index 4831a3c..6725fc1 100644 (file)
-// Very simple plugin for adding a close context menu to tabs\r
-Ext.ux.TabCloseMenu = function(){\r
-    var tabs, menu, ctxItem;\r
-    this.init = function(tp){\r
-        tabs = tp;\r
-        tabs.on('contextmenu', onContextMenu);\r
-    }\r
-\r
-    function onContextMenu(ts, item, e){\r
-        if(!menu){ // create context menu on first right click\r
-            menu = new Ext.menu.Menu([{\r
-                id: tabs.id + '-close',\r
-                text: 'Close Tab',\r
-                handler : function(){\r
-                    tabs.remove(ctxItem);\r
-                }\r
-            },{\r
-                id: tabs.id + '-close-others',\r
-                text: 'Close Other Tabs',\r
-                handler : function(){\r
-                    tabs.items.each(function(item){\r
-                        if(item.closable && item != ctxItem){\r
-                            tabs.remove(item);\r
-                        }\r
-                    });\r
-                }\r
-            }]);\r
-        }\r
-        ctxItem = item;\r
-        var items = menu.items;\r
-        items.get(tabs.id + '-close').setDisabled(!item.closable);\r
-        var disableOthers = true;\r
-        tabs.items.each(function(){\r
-            if(this != item && this.closable){\r
-                disableOthers = false;\r
-                return false;\r
-            }\r
-        });\r
-        items.get(tabs.id + '-close-others').setDisabled(disableOthers);\r
-       e.stopEvent();\r
-        menu.showAt(e.getPoint());\r
-    }\r
-};\r
+/*!
+ * Ext JS Library 3.2.1
+ * Copyright(c) 2006-2010 Ext JS, Inc.
+ * licensing@extjs.com
+ * http://www.extjs.com/license
+ */
+/**
+ * @class Ext.ux.TabCloseMenu
+ * @extends Object 
+ * Plugin (ptype = 'tabclosemenu') for adding a close context menu to tabs. Note that the menu respects
+ * the closable configuration on the tab. As such, commands like remove others and remove all will not
+ * remove items that are not closable.
+ * 
+ * @constructor
+ * @param {Object} config The configuration options
+ * @ptype tabclosemenu
+ */
+Ext.ux.TabCloseMenu = Ext.extend(Object, {
+    /**
+     * @cfg {String} closeTabText
+     * The text for closing the current tab. Defaults to <tt>'Close Tab'</tt>.
+     */
+    closeTabText: 'Close Tab',
+
+    /**
+     * @cfg {String} closeOtherTabsText
+     * The text for closing all tabs except the current one. Defaults to <tt>'Close Other Tabs'</tt>.
+     */
+    closeOtherTabsText: 'Close Other Tabs',
+    
+    /**
+     * @cfg {Boolean} showCloseAll
+     * Indicates whether to show the 'Close All' option. Defaults to <tt>true</tt>. 
+     */
+    showCloseAll: true,
+
+    /**
+     * @cfg {String} closeAllTabsText
+     * <p>The text for closing all tabs. Defaults to <tt>'Close All Tabs'</tt>.
+     */
+    closeAllTabsText: 'Close All Tabs',
+    
+    constructor : function(config){
+        Ext.apply(this, config || {});
+    },
+
+    //public
+    init : function(tabs){
+        this.tabs = tabs;
+        tabs.on({
+            scope: this,
+            contextmenu: this.onContextMenu,
+            destroy: this.destroy
+        });
+    },
+    
+    destroy : function(){
+        Ext.destroy(this.menu);
+        delete this.menu;
+        delete this.tabs;
+        delete this.active;    
+    },
+
+    // private
+    onContextMenu : function(tabs, item, e){
+        this.active = item;
+        var m = this.createMenu(),
+            disableAll = true,
+            disableOthers = true,
+            closeAll = m.getComponent('closeall');
+        
+        m.getComponent('close').setDisabled(!item.closable);
+        tabs.items.each(function(){
+            if(this.closable){
+                disableAll = false;
+                if(this != item){
+                    disableOthers = false;
+                    return false;
+                }
+            }
+        });
+        m.getComponent('closeothers').setDisabled(disableOthers);
+        if(closeAll){
+            closeAll.setDisabled(disableAll);
+        }
+        
+        e.stopEvent();
+        m.showAt(e.getPoint());
+    },
+    
+    createMenu : function(){
+        if(!this.menu){
+            var items = [{
+                itemId: 'close',
+                text: this.closeTabText,
+                scope: this,
+                handler: this.onClose
+            }];
+            if(this.showCloseAll){
+                items.push('-');
+            }
+            items.push({
+                itemId: 'closeothers',
+                text: this.closeOtherTabsText,
+                scope: this,
+                handler: this.onCloseOthers
+            });
+            if(this.showCloseAll){
+                items.push({
+                    itemId: 'closeall',
+                    text: this.closeAllTabsText,
+                    scope: this,
+                    handler: this.onCloseAll
+                });
+            }
+            this.menu = new Ext.menu.Menu({
+                items: items
+            });
+        }
+        return this.menu;
+    },
+    
+    onClose : function(){
+        this.tabs.remove(this.active);
+    },
+    
+    onCloseOthers : function(){
+        this.doClose(true);
+    },
+    
+    onCloseAll : function(){
+        this.doClose(false);
+    },
+    
+    doClose : function(excludeActive){
+        var items = [];
+        this.tabs.items.each(function(item){
+            if(item.closable){
+                if(!excludeActive || item != this.active){
+                    items.push(item);
+                }    
+            }
+        }, this);
+        Ext.each(items, function(item){
+            this.tabs.remove(item);
+        }, this);
+    }
+});
+
+Ext.preg('tabclosemenu', Ext.ux.TabCloseMenu);
\ No newline at end of file