TabScrollerMenu.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. Ext.ns('Ext.ux');
  10. /**
  11. * @class Ext.ux.TabScrollerMenu
  12. * @extends Object
  13. * Plugin (ptype = 'tabscrollermenu') for adding a tab menu to a TabBar is the Tabs overflow.
  14. * @constructor
  15. * @param {Object} config Configuration options
  16. * @ptype tabscrollermenu
  17. */
  18. Ext.define('Ext.ux.TabScrollerMenu', {
  19. alias: 'plugin.tabscrollermenu',
  20. uses: ['Ext.menu.Menu'],
  21. /**
  22. * @cfg {Number} pageSize How many items to allow per submenu.
  23. */
  24. pageSize: 10,
  25. /**
  26. * @cfg {Number} maxText How long should the title of each {@link Ext.menu.Item} be.
  27. */
  28. maxText: 15,
  29. /**
  30. * @cfg {String} menuPrefixText Text to prefix the submenus.
  31. */
  32. menuPrefixText: 'Items',
  33. constructor: function(config) {
  34. config = config || {};
  35. Ext.apply(this, config);
  36. },
  37. //private
  38. init: function(tabPanel) {
  39. var me = this;
  40. Ext.apply(tabPanel, me.parentOverrides);
  41. me.tabPanel = tabPanel;
  42. tabPanel.on({
  43. render: function() {
  44. me.tabBar = tabPanel.tabBar;
  45. me.layout = me.tabBar.layout;
  46. me.layout.overflowHandler.handleOverflow = Ext.Function.bind(me.showButton, me);
  47. me.layout.overflowHandler.clearOverflow = Ext.Function.createSequence(me.layout.overflowHandler.clearOverflow, me.hideButton, me);
  48. },
  49. single: true
  50. });
  51. },
  52. showButton: function() {
  53. var me = this,
  54. result = Ext.getClass(me.layout.overflowHandler).prototype.handleOverflow.apply(me.layout.overflowHandler, arguments);
  55. if (!me.menuButton) {
  56. me.menuButton = me.tabBar.body.createChild({
  57. cls: Ext.baseCSSPrefix + 'tab-tabmenu-right'
  58. }, me.tabBar.body.child('.' + Ext.baseCSSPrefix + 'box-scroller-right'));
  59. me.menuButton.addClsOnOver(Ext.baseCSSPrefix + 'tab-tabmenu-over');
  60. me.menuButton.on('click', me.showTabsMenu, me);
  61. }
  62. me.menuButton.show();
  63. result.targetSize.width -= me.menuButton.getWidth();
  64. return result;
  65. },
  66. hideButton: function() {
  67. var me = this;
  68. if (me.menuButton) {
  69. me.menuButton.hide();
  70. }
  71. },
  72. /**
  73. * Returns an the current page size (this.pageSize);
  74. * @return {Number} this.pageSize The current page size.
  75. */
  76. getPageSize: function() {
  77. return this.pageSize;
  78. },
  79. /**
  80. * Sets the number of menu items per submenu "page size".
  81. * @param {Number} pageSize The page size
  82. */
  83. setPageSize: function(pageSize) {
  84. this.pageSize = pageSize;
  85. },
  86. /**
  87. * Returns the current maxText length;
  88. * @return {Number} this.maxText The current max text length.
  89. */
  90. getMaxText: function() {
  91. return this.maxText;
  92. },
  93. /**
  94. * Sets the maximum text size for each menu item.
  95. * @param {Number} t The max text per each menu item.
  96. */
  97. setMaxText: function(t) {
  98. this.maxText = t;
  99. },
  100. /**
  101. * Returns the current menu prefix text String.;
  102. * @return {String} this.menuPrefixText The current menu prefix text.
  103. */
  104. getMenuPrefixText: function() {
  105. return this.menuPrefixText;
  106. },
  107. /**
  108. * Sets the menu prefix text String.
  109. * @param {String} t The menu prefix text.
  110. */
  111. setMenuPrefixText: function(t) {
  112. this.menuPrefixText = t;
  113. },
  114. showTabsMenu: function(e) {
  115. var me = this;
  116. if (me.tabsMenu) {
  117. me.tabsMenu.removeAll();
  118. } else {
  119. me.tabsMenu = Ext.create('Ext.menu.Menu');
  120. me.tabPanel.on('destroy', me.tabsMenu.destroy, me.tabsMenu);
  121. }
  122. me.generateTabMenuItems();
  123. var target = Ext.get(e.getTarget());
  124. var xy = target.getXY();
  125. //Y param + 24 pixels
  126. xy[1] += 24;
  127. me.tabsMenu.showAt(xy);
  128. },
  129. // private
  130. generateTabMenuItems: function() {
  131. var me = this,
  132. tabPanel = me.tabPanel,
  133. curActive = tabPanel.getActiveTab(),
  134. totalItems = tabPanel.items.getCount(),
  135. pageSize = me.getPageSize(),
  136. numSubMenus = Math.floor(totalItems / pageSize),
  137. remainder = totalItems % pageSize,
  138. i, curPage, menuItems, x, item, start, index;
  139. // if (totalItems > pageSize) {
  140. //
  141. // // Loop through all of the items and create submenus in chunks of 10
  142. // for (i = 0; i < numSubMenus; i++) {
  143. // curPage = (i + 1) * pageSize;
  144. // menuItems = [];
  145. //
  146. // for (x = 0; x < pageSize; x++) {
  147. // index = x + curPage - pageSize;
  148. // item = tabPanel.items.get(index);
  149. // menuItems.push(me.autoGenMenuItem(item));
  150. // }
  151. //
  152. // me.tabsMenu.add({
  153. // text: me.getMenuPrefixText() + ' ' + (curPage - pageSize + 1) + ' - ' + curPage,
  154. // menu: menuItems
  155. // });
  156. // }
  157. //
  158. // // remaining items
  159. // if (remainder > 0) {
  160. // start = numSubMenus * pageSize;
  161. // menuItems = [];
  162. // for (i = start; i < totalItems; i++) {
  163. // item = tabPanel.items.get(i);
  164. // menuItems.push(me.autoGenMenuItem(item));
  165. // }
  166. // me.tabsMenu.add({
  167. // text: me.menuPrefixText + ' ' + (start + 1) + ' - ' + (start + menuItems.length),
  168. // menu: menuItems
  169. // });
  170. //
  171. // }
  172. // }
  173. // else {
  174. tabPanel.items.each(function(item) {
  175. if(item.id!=curActive.id){
  176. me.tabsMenu.add(me.autoGenMenuItem(item));
  177. }
  178. });
  179. me.tabsMenu.add({
  180. text:'关闭所有面板' ,
  181. style: {
  182. background: '#E5E5E5'
  183. },
  184. handler : function(){
  185. // tabPanel.removeAll();
  186. tabPanel.items.each(function(item) {
  187. if(item.closable){
  188. tabPanel.remove(item);
  189. }
  190. });
  191. }
  192. });
  193. },
  194. // private
  195. autoGenMenuItem: function(item) {
  196. var maxText = this.getMaxText(),
  197. text = Ext.util.Format.ellipsis(item.title, maxText);
  198. return {
  199. text: text,
  200. handler: this.showTabFromMenu,
  201. scope: this,
  202. disabled: item.disabled,
  203. tabToShow: item,
  204. iconCls: item.iconCls
  205. };
  206. },
  207. // private
  208. showTabFromMenu: function(menuItem) {
  209. this.tabPanel.setActiveTab(menuItem.tabToShow);
  210. }
  211. });