38933685674e25c007e4db2ac140ee0cc4955d2a
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / extjs / ux / ext.ux.tabclosemenu.js
1 /*!
2 * Ext JS Library 3.2.1
3 * Copyright(c) 2006-2010 Ext JS, Inc.
4 * licensing@extjs.com
5 * http://www.extjs.com/license
6 */
7 /**
8 * @class Ext.ux.TabCloseMenu
9 * @extends Object
10 * Plugin (ptype = 'tabclosemenu') for adding a close context menu to tabs. Note that the menu respects
11 * the closable configuration on the tab. As such, commands like remove others and remove all will not
12 * remove items that are not closable.
13 *
14 * @constructor
15 * @param {Object} config The configuration options
16 * @ptype tabclosemenu
17 */
18 Ext.ux.TabCloseMenu = Ext.extend(Object, {
19 /**
20 * @cfg {String} closeTabText
21 * The text for closing the current tab. Defaults to <tt>'Close Tab'</tt>.
22 */
23 closeTabText: 'Close Tab',
24
25 /**
26 * @cfg {String} closeOtherTabsText
27 * The text for closing all tabs except the current one. Defaults to <tt>'Close Other Tabs'</tt>.
28 */
29 closeOtherTabsText: 'Close Other Tabs',
30
31 /**
32 * @cfg {Boolean} showCloseAll
33 * Indicates whether to show the 'Close All' option. Defaults to <tt>true</tt>.
34 */
35 showCloseAll: true,
36
37 /**
38 * @cfg {String} closeAllTabsText
39 * <p>The text for closing all tabs. Defaults to <tt>'Close All Tabs'</tt>.
40 */
41 closeAllTabsText: 'Close All Tabs',
42
43 /**
44 * @cfg {Array} custom menu entries
45 */
46 customMenuEntries: [],
47
48
49 constructor : function(config){
50 Ext.apply(this, config || {});
51 },
52
53 //public
54 init : function(tabs){
55 this.tabs = tabs;
56 tabs.on({
57 scope: this,
58 contextmenu: this.onContextMenu,
59 destroy: this.destroy
60 });
61 },
62
63 destroy : function(){
64 Ext.destroy(this.menu);
65 delete this.menu;
66 delete this.tabs;
67 delete this.active;
68 },
69
70 // private
71 onContextMenu : function(tabs, item, e){
72 this.active = item;
73 var m = this.createMenu(),
74 disableAll = true,
75 disableOthers = true,
76 closeAll = m.getComponent('closeall');
77
78 m.getComponent('close').setDisabled(!item.closable);
79 tabs.items.each(function(){
80 if(this.closable){
81 disableAll = false;
82 if(this != item){
83 disableOthers = false;
84 return false;
85 }
86 }
87 return true;
88 });
89 m.getComponent('closeothers').setDisabled(disableOthers);
90 if(closeAll){
91 closeAll.setDisabled(disableAll);
92 }
93
94 e.stopEvent();
95 m.showAt(e.getPoint());
96 },
97
98 createMenu : function(){
99 if(!this.menu){
100 var items = [{
101 itemId: 'close',
102 text: this.closeTabText,
103 scope: this,
104 handler: this.onClose
105 }];
106 if(this.showCloseAll){
107 items.push('-');
108 }
109 items.push({
110 itemId: 'closeothers',
111 text: this.closeOtherTabsText,
112 scope: this,
113 handler: this.onCloseOthers
114 });
115 if(this.showCloseAll){
116 items.push({
117 itemId: 'closeall',
118 text: this.closeAllTabsText,
119 scope: this,
120 handler: this.onCloseAll
121 });
122 }
123 if (this.customMenuEntries.length) {
124 this.customMenuEntries.each(function(item) {
125 items.push(item);
126 });
127 }
128
129 this.menu = new Ext.menu.Menu({
130 items: items
131 });
132 }
133 return this.menu;
134 },
135
136 onClose : function(){
137 this.tabs.remove(this.active);
138 },
139
140 onCloseOthers : function(){
141 this.doClose(true);
142 },
143
144 onCloseAll : function(){
145 this.doClose(false);
146 },
147
148 doClose : function(excludeActive){
149 var items = [];
150 this.tabs.items.each(function(item){
151 if(item.closable){
152 if(!excludeActive || item != this.active){
153 items.push(item);
154 }
155 }
156 }, this);
157 Ext.each(items, function(item){
158 this.tabs.remove(item);
159 }, this);
160 }
161 });
162
163 Ext.preg('tabclosemenu', Ext.ux.TabCloseMenu);