TabCloseMenu.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Ext JS Library 3.3.0
  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. * @cfg {String} closeOtherTabsText
  26. * The text for closing all tabs except the current one. Defaults to <tt>'Close Other Tabs'</tt>.
  27. */
  28. closeOtherTabsText: 'Close Other Tabs',
  29. /**
  30. * @cfg {Boolean} showCloseAll
  31. * Indicates whether to show the 'Close All' option. Defaults to <tt>true</tt>.
  32. */
  33. showCloseAll: true,
  34. /**
  35. * @cfg {String} closeAllTabsText
  36. * <p>The text for closing all tabs. Defaults to <tt>'Close All Tabs'</tt>.
  37. */
  38. closeAllTabsText: 'Close All Tabs',
  39. constructor : function(config){
  40. Ext.apply(this, config || {});
  41. },
  42. //public
  43. init : function(tabs){
  44. this.tabs = tabs;
  45. tabs.on({
  46. scope: this,
  47. contextmenu: this.onContextMenu,
  48. destroy: this.destroy
  49. });
  50. },
  51. destroy : function(){
  52. Ext.destroy(this.menu);
  53. delete this.menu;
  54. delete this.tabs;
  55. delete this.active;
  56. },
  57. // private
  58. onContextMenu : function(tabs, item, e){
  59. this.active = item;
  60. var m = this.createMenu(),
  61. disableAll = true,
  62. disableOthers = true,
  63. closeAll = m.getComponent('closeall');
  64. m.getComponent('close').setDisabled(!item.closable);
  65. tabs.items.each(function(){
  66. if(this.closable){
  67. disableAll = false;
  68. if(this != item){
  69. disableOthers = false;
  70. return false;
  71. }
  72. }
  73. });
  74. m.getComponent('closeothers').setDisabled(disableOthers);
  75. if(closeAll){
  76. closeAll.setDisabled(disableAll);
  77. }
  78. e.stopEvent();
  79. m.showAt(e.getPoint());
  80. },
  81. createMenu : function(){
  82. if(!this.menu){
  83. var items = [{
  84. itemId: 'close',
  85. text: this.closeTabText,
  86. scope: this,
  87. handler: this.onClose
  88. }];
  89. if(this.showCloseAll){
  90. items.push('-');
  91. }
  92. items.push({
  93. itemId: 'closeothers',
  94. text: this.closeOtherTabsText,
  95. scope: this,
  96. handler: this.onCloseOthers
  97. });
  98. if(this.showCloseAll){
  99. items.push({
  100. itemId: 'closeall',
  101. text: this.closeAllTabsText,
  102. scope: this,
  103. handler: this.onCloseAll
  104. });
  105. }
  106. this.menu = new Ext.menu.Menu({
  107. items: items
  108. });
  109. }
  110. return this.menu;
  111. },
  112. onClose : function(){
  113. this.tabs.remove(this.active);
  114. },
  115. onCloseOthers : function(){
  116. this.doClose(true);
  117. },
  118. onCloseAll : function(){
  119. this.doClose(false);
  120. },
  121. doClose : function(excludeActive){
  122. var items = [];
  123. this.tabs.items.each(function(item){
  124. if(item.closable){
  125. if(!excludeActive || item != this.active){
  126. items.push(item);
  127. }
  128. }
  129. }, this);
  130. Ext.each(items, function(item){
  131. this.tabs.remove(item);
  132. }, this);
  133. }
  134. });
  135. Ext.preg('tabclosemenu', Ext.ux.TabCloseMenu);