TabScrollerMenu.html
7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js">Ext.ns('Ext.ux');
<span id='Ext-ux-TabScrollerMenu-method-constructor'><span id='Ext-ux-TabScrollerMenu'>/**
</span></span> * @class Ext.ux.TabScrollerMenu
* @extends Object
* Plugin (ptype = 'tabscrollermenu') for adding a tab menu to a TabBar is the Tabs overflow.
* @constructor
* @param {Object} config Configuration options
* @ptype tabscrollermenu
*/
Ext.define('Ext.ux.TabScrollerMenu', {
alias: 'plugin.tabscrollermenu',
requires: ['Ext.menu.Menu'],
<span id='Ext-ux-TabScrollerMenu-cfg-pageSize'> /**
</span> * @cfg {Number} pageSize How many items to allow per submenu.
*/
pageSize: 10,
<span id='Ext-ux-TabScrollerMenu-cfg-maxText'> /**
</span> * @cfg {Number} maxText How long should the title of each {@link Ext.menu.Item} be.
*/
maxText: 15,
<span id='Ext-ux-TabScrollerMenu-cfg-menuPrefixText'> /**
</span> * @cfg {String} menuPrefixText Text to prefix the submenus.
*/
menuPrefixText: 'Items',
constructor: function(config) {
Ext.apply(this, config);
},
//private
init: function(tabPanel) {
var me = this;
me.tabPanel = tabPanel;
tabPanel.on({
render: function() {
me.tabBar = tabPanel.tabBar;
me.layout = me.tabBar.layout;
me.layout.overflowHandler.handleOverflow = Ext.Function.bind(me.showButton, me);
me.layout.overflowHandler.clearOverflow = Ext.Function.createSequence(me.layout.overflowHandler.clearOverflow, me.hideButton, me);
},
single: true
});
},
showButton: function() {
var me = this,
result = Ext.getClass(me.layout.overflowHandler).prototype.handleOverflow.apply(me.layout.overflowHandler, arguments);
if (!me.menuButton) {
me.menuButton = me.tabBar.body.createChild({
cls: Ext.baseCSSPrefix + 'tab-tabmenu-right'
}, me.tabBar.body.child('.' + Ext.baseCSSPrefix + 'box-scroller-right'));
me.menuButton.addClsOnOver(Ext.baseCSSPrefix + 'tab-tabmenu-over');
me.menuButton.on('click', me.showTabsMenu, me);
}
me.menuButton.show();
result.reservedSpace += me.menuButton.getWidth();
return result;
},
hideButton: function() {
var me = this;
if (me.menuButton) {
me.menuButton.hide();
}
},
<span id='Ext-ux-TabScrollerMenu-method-getPageSize'> /**
</span> * Returns an the current page size (this.pageSize);
* @return {Number} this.pageSize The current page size.
*/
getPageSize: function() {
return this.pageSize;
},
<span id='Ext-ux-TabScrollerMenu-method-setPageSize'> /**
</span> * Sets the number of menu items per submenu "page size".
* @param {Number} pageSize The page size
*/
setPageSize: function(pageSize) {
this.pageSize = pageSize;
},
<span id='Ext-ux-TabScrollerMenu-method-getMaxText'> /**
</span> * Returns the current maxText length;
* @return {Number} this.maxText The current max text length.
*/
getMaxText: function() {
return this.maxText;
},
<span id='Ext-ux-TabScrollerMenu-method-setMaxText'> /**
</span> * Sets the maximum text size for each menu item.
* @param {Number} t The max text per each menu item.
*/
setMaxText: function(t) {
this.maxText = t;
},
<span id='Ext-ux-TabScrollerMenu-method-getMenuPrefixText'> /**
</span> * Returns the current menu prefix text String.;
* @return {String} this.menuPrefixText The current menu prefix text.
*/
getMenuPrefixText: function() {
return this.menuPrefixText;
},
<span id='Ext-ux-TabScrollerMenu-method-setMenuPrefixText'> /**
</span> * Sets the menu prefix text String.
* @param {String} t The menu prefix text.
*/
setMenuPrefixText: function(t) {
this.menuPrefixText = t;
},
showTabsMenu: function(e) {
var me = this;
if (me.tabsMenu) {
me.tabsMenu.removeAll();
} else {
me.tabsMenu = new Ext.menu.Menu();
me.tabPanel.on('destroy', me.tabsMenu.destroy, me.tabsMenu);
}
me.generateTabMenuItems();
var target = Ext.get(e.getTarget()),
xy = target.getXY();
//Y param + 24 pixels
xy[1] += 24;
me.tabsMenu.showAt(xy);
},
// private
generateTabMenuItems: function() {
var me = this,
tabPanel = me.tabPanel,
curActive = tabPanel.getActiveTab(),
allItems = tabPanel.items.getRange(),
pageSize = me.getPageSize(),
tabsMenu = me.tabsMenu,
totalItems, numSubMenus, remainder,
i, curPage, menuItems, x, item, start, index;
tabsMenu.suspendLayouts();
allItems = Ext.Array.filter(allItems, function(item){
if (item.id == curActive.id) {
return false;
}
return item.hidden ? !!item.hiddenByLayout : true;
});
totalItems = allItems.length;
numSubMenus = Math.floor(totalItems / pageSize);
remainder = totalItems % pageSize;
if (totalItems > pageSize) {
// Loop through all of the items and create submenus in chunks of 10
for (i = 0; i < numSubMenus; i++) {
curPage = (i + 1) * pageSize;
menuItems = [];
for (x = 0; x < pageSize; x++) {
index = x + curPage - pageSize;
item = allItems[index];
menuItems.push(me.autoGenMenuItem(item));
}
tabsMenu.add({
text: me.getMenuPrefixText() + ' ' + (curPage - pageSize + 1) + ' - ' + curPage,
menu: menuItems
});
}
// remaining items
if (remainder > 0) {
start = numSubMenus * pageSize;
menuItems = [];
for (i = start; i < totalItems; i++) {
item = allItems[i];
menuItems.push(me.autoGenMenuItem(item));
}
me.tabsMenu.add({
text: me.menuPrefixText + ' ' + (start + 1) + ' - ' + (start + menuItems.length),
menu: menuItems
});
}
} else {
for (i = 0; i < totalItems; ++i) {
tabsMenu.add(me.autoGenMenuItem(allItems[i]));
}
}
tabsMenu.resumeLayouts(true);
},
// private
autoGenMenuItem: function(item) {
var maxText = this.getMaxText(),
text = Ext.util.Format.ellipsis(item.title, maxText);
return {
text: text,
handler: this.showTabFromMenu,
scope: this,
disabled: item.disabled,
tabToShow: item,
iconCls: item.iconCls
};
},
// private
showTabFromMenu: function(menuItem) {
this.tabPanel.setActiveTab(menuItem.tabToShow);
}
});
</pre>
</body>
</html>