GroupTabPanel.html
16.6 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!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"><span id='Ext-ux-GroupTabPanel'>/**
</span> * @author Nicolas Ferrero
* @class Ext.ux.GroupTabPanel
* @extends Ext.Container
* A TabPanel with grouping support.
*/
Ext.define('Ext.ux.GroupTabPanel', {
extend: 'Ext.Container',
alias: 'widget.grouptabpanel',
requires:[
'Ext.data.*',
'Ext.tree.*',
'Ext.layout.*'
],
baseCls : Ext.baseCSSPrefix + 'grouptabpanel',
initComponent: function(config) {
var me = this;
Ext.apply(me, config);
// Processes items to create the TreeStore and also set up
// "this.cards" containing the actual card items.
me.store = me.createTreeStore();
me.layout = {
type: 'hbox',
align: 'stretch'
};
me.defaults = {
border: false
};
me.items = [{
xtype: 'treepanel',
cls: 'x-tree-panel x-grouptabbar',
width: 150,
rootVisible: false,
store: me.store,
hideHeaders: true,
animate: false,
processEvent: Ext.emptyFn,
viewConfig: {
overItemCls: '',
getRowClass: me.getRowClass,
itemSelector: 'div.' + Ext.baseCSSPrefix + 'grouptab-row',
cellSelector: 'div.' + Ext.baseCSSPrefix + 'grouptab',
getTableChunker: function() {
return Ext.ux.GroupTreeChunker;
},
onHeaderResize: function(header, w, suppressFocus) {
var me = this,
el = me.el;
if (el) {
el.select('div.' + Ext.baseCSSPrefix + 'grid-table-resizer').setWidth(me.headerCt.getFullWidth());
if (!me.ignoreTemplate) {
me.setNewTemplate();
}
if (!suppressFocus) {
me.el.focus();
}
me.forceReflow();
}
}
},
columns: [{
xtype: 'treecolumn',
sortable: false,
dataIndex: 'text',
flex: 1,
renderer: function (value, cell, node, idx1, idx2, store, tree) {
var cls = '';
if (node.parentNode && node.parentNode.parentNode === null) {
cls += ' x-grouptab-first';
if (node.previousSibling) {
cls += ' x-grouptab-prev';
}
if (!node.get('expanded') || node.firstChild == null) {
cls += ' x-grouptab-last';
}
} else if (node.nextSibling === null) {
cls += ' x-grouptab-last';
} else {
cls += ' x-grouptab-center';
}
if (node.data.activeTab) {
cls += ' x-active-tab';
}
cell.tdCls= 'x-grouptab'+ cls;
return value;
}
}]
},{
xtype: 'container',
flex: 1,
layout: 'card',
activeItem: me.mainItem,
baseCls: Ext.baseCSSPrefix + 'grouptabcontainer',
items: me.cards
}];
me.addEvents(
<span id='Ext-ux-GroupTabPanel-event-beforetabchange'> /**
</span> * @event beforetabchange
* Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
* the tabchange
* @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
* @param {Ext.Component} newCard The card that is about to be activated
* @param {Ext.Component} oldCard The card that is currently active
*/
'beforetabchange',
<span id='Ext-ux-GroupTabPanel-event-tabchange'> /**
</span> * @event tabchange
* Fires when a new tab has been activated (activated by {@link #setActiveTab}).
* @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
* @param {Ext.Component} newCard The newly activated item
* @param {Ext.Component} oldCard The previously active item
*/
'tabchange',
<span id='Ext-ux-GroupTabPanel-event-beforegroupchange'> /**
</span> * @event beforegroupchange
* Fires before a group change (activated by {@link #setActiveGroup}). Return false in any listener to cancel
* the groupchange
* @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
* @param {Ext.Component} newGroup The root group card that is about to be activated
* @param {Ext.Component} oldGroup The root group card that is currently active
*/
'beforegroupchange',
<span id='Ext-ux-GroupTabPanel-event-groupchange'> /**
</span> * @event groupchange
* Fires when a new group has been activated (activated by {@link #setActiveGroup}).
* @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
* @param {Ext.Component} newGroup The newly activated root group item
* @param {Ext.Component} oldGroup The previously active root group item
*/
'groupchange'
);
me.callParent(arguments);
me.setActiveTab(me.activeTab);
me.setActiveGroup(me.activeGroup);
me.mon(me.down('treepanel').getSelectionModel(), 'select', me.onNodeSelect, me);
},
getRowClass: function(node, rowIndex, rowParams, store) {
var cls = '';
if (node.data.activeGroup) {
cls += ' x-active-group';
}
return cls;
},
<span id='Ext-ux-GroupTabPanel-method-onNodeSelect'> /**
</span> * @private
* Node selection listener.
*/
onNodeSelect: function (selModel, node) {
var me = this,
currentNode = me.store.getRootNode(),
parent;
if (node.parentNode && node.parentNode.parentNode === null) {
parent = node;
} else {
parent = node.parentNode;
}
if (me.setActiveGroup(parent.get('id')) === false || me.setActiveTab(node.get('id')) === false) {
return false;
}
while (currentNode) {
currentNode.set('activeTab', false);
currentNode.set('activeGroup', false);
currentNode = currentNode.firstChild || currentNode.nextSibling || currentNode.parentNode.nextSibling;
}
parent.set('activeGroup', true);
parent.eachChild(function(child) {
child.set('activeGroup', true);
});
node.set('activeTab', true);
selModel.view.refresh();
},
<span id='Ext-ux-GroupTabPanel-method-setActiveTab'> /**
</span> * Makes the given component active (makes it the visible card in the GroupTabPanel's CardLayout)
* @param {Ext.Component} cmp The component to make active
*/
setActiveTab: function(cmp) {
var me = this,
newTab = cmp,
oldTab;
if(Ext.isString(cmp)) {
newTab = Ext.getCmp(newTab);
}
if (newTab === me.activeTab) {
return false;
}
oldTab = me.activeTab;
if (me.fireEvent('beforetabchange', me, newTab, oldTab) !== false) {
me.activeTab = newTab;
if (me.rendered) {
me.down('container[baseCls=' + Ext.baseCSSPrefix + 'grouptabcontainer' + ']').getLayout().setActiveItem(newTab);
}
me.fireEvent('tabchange', me, newTab, oldTab);
}
return true;
},
<span id='Ext-ux-GroupTabPanel-method-setActiveGroup'> /**
</span> * Makes the given group active
* @param {Ext.Component} cmp The root component to make active.
*/
setActiveGroup: function(cmp) {
var me = this,
newGroup = cmp,
oldGroup;
if(Ext.isString(cmp)) {
newGroup = Ext.getCmp(newGroup);
}
if (newGroup === me.activeGroup) {
return true;
}
oldGroup = me.activeGroup;
if (me.fireEvent('beforegroupchange', me, newGroup, oldGroup) !== false) {
me.activeGroup = newGroup;
me.fireEvent('groupchange', me, newGroup, oldGroup);
} else {
return false;
}
return true;
},
<span id='Ext-ux-GroupTabPanel-method-createTreeStore'> /**
</span> * @private
* Creates the TreeStore used by the GroupTabBar.
*/
createTreeStore: function() {
var me = this,
groups = me.prepareItems(me.items),
data = {
text: '.',
children: []
},
cards = me.cards = [];
me.activeGroup = me.activeGroup || 0;
Ext.each(groups, function(groupItem, idx) {
var leafItems = groupItem.items.items,
rootItem = (leafItems[groupItem.mainItem] || leafItems[0]),
groupRoot = {
children: []
};
// Create the root node of the group
groupRoot.id = rootItem.id;
groupRoot.text = rootItem.title;
groupRoot.iconCls = rootItem.iconCls;
groupRoot.expanded = true;
groupRoot.activeGroup = (me.activeGroup === idx);
groupRoot.activeTab = groupRoot.activeGroup ? true : false;
if (groupRoot.activeTab) {
me.activeTab = groupRoot.id;
}
if (groupRoot.activeGroup) {
me.mainItem = groupItem.mainItem || 0;
me.activeGroup = groupRoot.id;
}
Ext.each(leafItems, function(leafItem) {
// First node has been done
if (leafItem.id !== groupRoot.id) {
var child = {
id: leafItem.id,
leaf: true,
text: leafItem.title,
iconCls: leafItem.iconCls,
activeGroup: groupRoot.activeGroup,
activeTab: false
};
groupRoot.children.push(child);
}
// Ensure the items do not get headers
delete leafItem.title;
delete leafItem.iconCls;
cards.push(leafItem);
});
data.children.push(groupRoot);
});
return Ext.create('Ext.data.TreeStore', {
fields: ['id', 'text', 'activeGroup', 'activeTab'],
root: {
expanded: true
},
proxy: {
type: 'memory',
data: data
}
});
},
<span id='Ext-ux-GroupTabPanel-method-getActiveTab'> /**
</span> * Returns the item that is currently active inside this GroupTabPanel.
* @return {Ext.Component/Number} The currently active item
*/
getActiveTab: function() {
return this.activeTab;
},
<span id='Ext-ux-GroupTabPanel-method-getActiveGroup'> /**
</span> * Returns the root group item that is currently active inside this GroupTabPanel.
* @return {Ext.Component/Number} The currently active root group item
*/
getActiveGroup: function() {
return this.activeGroup;
}
});
<span id='Ext-ux-GroupTreeChunker'>/**
</span> * Allows GroupTab to render a table structure.
*/
Ext.define('Ext.ux.GroupTreeChunker', {
singleton: true,
requires: ['Ext.XTemplate'],
metaTableTpl: [
'{%if (this.openTableWrap)out.push(this.openTableWrap())%}',
'<table class="' + Ext.baseCSSPrefix + 'grid-table-resizer" border="0" cellspacing="0" cellpadding="0" {[this.embedFullWidth(values)]}><tr><td>',
'{[this.openRows()]}',
'{row}',
'{[this.closeRows()]}',
'</td></tr><table>',
'{%if (this.closeTableWrap)out.push(this.closeTableWrap())%}'
],
constructor: function() {
Ext.XTemplate.prototype.recurse = function(values, reference) {
return this.apply(reference ? values[reference] : values);
};
},
embedFullWidth: function(values) {
var result = 'style="width:{fullWidth}px;';
// If there are no records, we need to give the table a height so that it
// is displayed and causes q scrollbar if the width exceeds the View's width.
if (!values.rowCount) {
result += 'height:1px;';
}
return result + '"';
},
openRows: function() {
return '<tpl for="rows">';
},
closeRows: function() {
return '</tpl>';
},
metaRowTpl: [
'<div class="' + Ext.baseCSSPrefix + 'grouptab-row {[this.embedRowCls()]}" {[this.embedRowAttr()]}>',
'<tpl for="columns">',
'<div class="{cls} ' + Ext.baseCSSPrefix + 'grouptab-cell ' + Ext.baseCSSPrefix + 'grid-cell-{columnId} {{id}-modified} {{id}-tdCls} {[this.firstOrLastCls(xindex, xcount)]}" {{id}-tdAttr}>',
'<div {unselectableAttr} class="' + Ext.baseCSSPrefix + 'grid-cell-inner {unselectableCls}" style="text-align: {align}; {{id}-style};">{{id}}</div>',
'<div class="x-grouptabs-corner x-grouptabs-corner-top-left" id="ext-gen25"></div>',
'<div class="x-grouptabs-corner x-grouptabs-corner-bottom-left" id="ext-gen26"></div>',
'</div>',
'</tpl>',
'</div>'
],
firstOrLastCls: function(xindex, xcount) {
if (xindex === 1) {
return Ext.view.Table.prototype.firstCls;
} else if (xindex === xcount) {
return Ext.view.Table.prototype.lastCls;
}
},
embedRowCls: function() {
return '{rowCls}';
},
embedRowAttr: function() {
return '{rowAttr}';
},
getTableTpl: function(cfg, textOnly) {
var tpl,
tableTplMemberFns = {
openRows: this.openRows,
closeRows: this.closeRows,
embedFullWidth: this.embedFullWidth
},
tplMemberFns = {},
features = cfg.features || [],
ln = features.length,
i = 0,
memberFns = {
embedRowCls: this.embedRowCls,
embedRowAttr: this.embedRowAttr,
firstOrLastCls: this.firstOrLastCls,
unselectableAttr: cfg.enableTextSelection ? '' : 'unselectable="on"',
unselectableCls: cfg.enableTextSelection ? '' : Ext.baseCSSPrefix + 'unselectable'
},
// copy the default
metaRowTpl = Array.prototype.slice.call(this.metaRowTpl, 0),
metaTableTpl;
for (; i < ln; i++) {
if (!features[i].disabled) {
features[i].mutateMetaRowTpl(metaRowTpl);
Ext.apply(memberFns, features[i].getMetaRowTplFragments());
Ext.apply(tplMemberFns, features[i].getFragmentTpl());
Ext.apply(tableTplMemberFns, features[i].getTableFragments());
}
}
metaRowTpl = new Ext.XTemplate(metaRowTpl.join(''), memberFns);
cfg.row = metaRowTpl.applyTemplate(cfg);
metaTableTpl = new Ext.XTemplate(this.metaTableTpl.join(''), tableTplMemberFns);
tpl = metaTableTpl.applyTemplate(cfg);
// TODO: Investigate eliminating.
if (!textOnly) {
tpl = new Ext.XTemplate(tpl, tplMemberFns);
}
return tpl;
}
});
</pre>
</body>
</html>