TabCloseMenu.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.TabCloseMenu
  11. * Plugin (ptype = 'tabclosemenu') for adding a close context menu to tabs. Note that the menu respects
  12. * the closable configuration on the tab. As such, commands like remove others and remove all will not
  13. * remove items that are not closable.
  14. *
  15. * @constructor
  16. * @param {Object} config The configuration options
  17. * @ptype tabclosemenu
  18. */
  19. Ext.define('Ext.tab.TabCloseMenu', {
  20. alias: 'plugin.tabclosemenu',
  21. alternateClassName: 'Ext.ux.TabCloseMenu',
  22. mixins: {
  23. observable: 'Ext.util.Observable'
  24. },
  25. /**
  26. * @cfg {String} closeTabText
  27. * The text for closing the current tab. Defaults to <tt>'Close Tab'</tt>.
  28. */
  29. closeTabText: 'Close Tab',
  30. /**
  31. * @cfg {Boolean} showCloseOthers
  32. * Indicates whether to show the 'Close Others' option. Defaults to <tt>true</tt>.
  33. */
  34. showCloseOthers: true,
  35. /**
  36. * @cfg {String} closeOtherTabsText
  37. * The text for closing all tabs except the current one. Defaults to <tt>'Close Other Tabs'</tt>.
  38. */
  39. closeOthersTabsText: 'Close Other Tabs',
  40. /**
  41. * @cfg {Boolean} showCloseAll
  42. * Indicates whether to show the 'Close All' option. Defaults to <tt>true</tt>.
  43. */
  44. showCloseAll: true,
  45. /**
  46. * @cfg {String} closeAllTabsText
  47. * <p>The text for closing all tabs. Defaults to <tt>'Close All Tabs'</tt>.
  48. */
  49. closeAllTabsText: 'Close All Tabs',
  50. /**
  51. * @cfg {Array} extraItemsHead
  52. * An array of additional context menu items to add to the front of the context menu.
  53. */
  54. extraItemsHead: null,
  55. /**
  56. * @cfg {Array} extraItemsTail
  57. * An array of additional context menu items to add to the end of the context menu.
  58. */
  59. extraItemsTail: null,
  60. //public
  61. constructor: function (config) {
  62. this.addEvents(
  63. 'aftermenu',
  64. 'beforemenu');
  65. this.mixins.observable.constructor.call(this, config);
  66. },
  67. init : function(tabpanel){
  68. this.tabPanel = tabpanel;
  69. this.tabBar = tabpanel.down("tabbar");
  70. this.mon(this.tabPanel, {
  71. scope: this,
  72. afterlayout: this.onAfterLayout,
  73. single: true
  74. });
  75. },
  76. onAfterLayout: function() {
  77. this.mon(this.tabBar.el, {
  78. scope: this,
  79. contextmenu: this.onContextMenu,
  80. delegate: 'div.x-tab'
  81. });
  82. },
  83. onBeforeDestroy : function(){
  84. Ext.destroy(this.menu);
  85. this.callParent(arguments);
  86. },
  87. // private
  88. onContextMenu : function(event, target){
  89. var me = this,
  90. menu = me.createMenu(),
  91. disableAll = true,
  92. disableOthers = true,
  93. tab = me.tabBar.getChildByElement(target),
  94. index = me.tabBar.items.indexOf(tab);
  95. me.item = me.tabPanel.getComponent(index);
  96. menu.child('*[text="' + me.closeTabText + '"]').setDisabled(!me.item.closable);
  97. if (me.showCloseAll || me.showCloseOthers) {
  98. me.tabPanel.items.each(function(item) {
  99. if (item.closable) {
  100. disableAll = false;
  101. if (item != me.item) {
  102. disableOthers = false;
  103. return false;
  104. }
  105. }
  106. return true;
  107. });
  108. if (me.showCloseAll) {
  109. menu.child('*[text="' + me.closeAllTabsText + '"]').setDisabled(disableAll);
  110. }
  111. if (me.showCloseOthers) {
  112. menu.child('*[text="' + me.closeOthersTabsText + '"]').setDisabled(disableOthers);
  113. }
  114. }
  115. event.preventDefault();
  116. me.fireEvent('beforemenu', menu, me.item, me);
  117. menu.showAt(event.getXY());
  118. },
  119. createMenu : function() {
  120. var me = this;
  121. if (!me.menu) {
  122. var items = [{
  123. text: me.closeTabText,
  124. scope: me,
  125. handler: me.onClose
  126. }];
  127. if (me.showCloseAll || me.showCloseOthers) {
  128. items.push('-');
  129. }
  130. if (me.showCloseOthers) {
  131. items.push({
  132. text: me.closeOthersTabsText,
  133. scope: me,
  134. handler: me.onCloseOthers
  135. });
  136. }
  137. if (me.showCloseAll) {
  138. items.push({
  139. text: me.closeAllTabsText,
  140. scope: me,
  141. handler: me.onCloseAll
  142. });
  143. }
  144. if (me.extraItemsHead) {
  145. items = me.extraItemsHead.concat(items);
  146. }
  147. if (me.extraItemsTail) {
  148. items = items.concat(me.extraItemsTail);
  149. }
  150. me.menu = Ext.create('Ext.menu.Menu', {
  151. items: items,
  152. listeners: {
  153. hide: me.onHideMenu,
  154. scope: me
  155. }
  156. });
  157. }
  158. return me.menu;
  159. },
  160. onHideMenu: function () {
  161. var me = this;
  162. me.item = null;
  163. me.fireEvent('aftermenu', me.menu, me);
  164. },
  165. onClose : function(){
  166. this.tabPanel.remove(this.item);
  167. },
  168. onCloseOthers : function(){
  169. this.doClose(true);
  170. },
  171. onCloseAll : function(){
  172. this.doClose(false);
  173. },
  174. doClose : function(excludeActive){
  175. var items = [];
  176. this.tabPanel.items.each(function(item){
  177. if(item.closable){
  178. if(!excludeActive || item != this.item){
  179. items.push(item);
  180. }
  181. }
  182. }, this);
  183. Ext.each(items, function(item){
  184. this.tabPanel.remove(item);
  185. }, this);
  186. }
  187. });