Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / resources / TabCloseMenu.js
1 // Very simple plugin for adding a close context menu to tabs
2 Ext.ux.TabCloseMenu = function(){
3     var tabs, menu, ctxItem;
4     this.init = function(tp){
5         tabs = tp;
6         tabs.on('contextmenu', onContextMenu);
7     }
8
9     function onContextMenu(ts, item, e){
10         if(!menu){ // create context menu on first right click
11             menu = new Ext.menu.Menu([{
12                 id: tabs.id + '-close',
13                 text: 'Close Tab',
14                 handler : function(){
15                     tabs.remove(ctxItem);
16                 }
17             },{
18                 id: tabs.id + '-close-others',
19                 text: 'Close Other Tabs',
20                 handler : function(){
21                     tabs.items.each(function(item){
22                         if(item.closable && item != ctxItem){
23                             tabs.remove(item);
24                         }
25                     });
26                 }
27             }]);
28         }
29         ctxItem = item;
30         var items = menu.items;
31         items.get(tabs.id + '-close').setDisabled(!item.closable);
32         var disableOthers = true;
33         tabs.items.each(function(){
34             if(this != item && this.closable){
35                 disableOthers = false;
36                 return false;
37             }
38         });
39         items.get(tabs.id + '-close-others').setDisabled(disableOthers);
40         menu.showAt(e.getPoint());
41     }
42 };