GroupTabPanel.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ux-GroupTabPanel'>/**
  19. </span> * @author Nicolas Ferrero
  20. * @class Ext.ux.GroupTabPanel
  21. * @extends Ext.Container
  22. * A TabPanel with grouping support.
  23. */
  24. Ext.define('Ext.ux.GroupTabPanel', {
  25. extend: 'Ext.Container',
  26. alias: 'widget.grouptabpanel',
  27. requires:[
  28. 'Ext.data.*',
  29. 'Ext.tree.*',
  30. 'Ext.layout.*'
  31. ],
  32. baseCls : Ext.baseCSSPrefix + 'grouptabpanel',
  33. initComponent: function(config) {
  34. var me = this;
  35. Ext.apply(me, config);
  36. // Processes items to create the TreeStore and also set up
  37. // &quot;this.cards&quot; containing the actual card items.
  38. me.store = me.createTreeStore();
  39. me.layout = {
  40. type: 'hbox',
  41. align: 'stretch'
  42. };
  43. me.defaults = {
  44. border: false
  45. };
  46. me.items = [{
  47. xtype: 'treepanel',
  48. cls: 'x-tree-panel x-grouptabbar',
  49. width: 150,
  50. rootVisible: false,
  51. store: me.store,
  52. hideHeaders: true,
  53. animate: false,
  54. processEvent: Ext.emptyFn,
  55. viewConfig: {
  56. overItemCls: '',
  57. getRowClass: me.getRowClass,
  58. itemSelector: 'div.' + Ext.baseCSSPrefix + 'grouptab-row',
  59. cellSelector: 'div.' + Ext.baseCSSPrefix + 'grouptab',
  60. getTableChunker: function() {
  61. return Ext.ux.GroupTreeChunker;
  62. },
  63. onHeaderResize: function(header, w, suppressFocus) {
  64. var me = this,
  65. el = me.el;
  66. if (el) {
  67. el.select('div.' + Ext.baseCSSPrefix + 'grid-table-resizer').setWidth(me.headerCt.getFullWidth());
  68. if (!me.ignoreTemplate) {
  69. me.setNewTemplate();
  70. }
  71. if (!suppressFocus) {
  72. me.el.focus();
  73. }
  74. me.forceReflow();
  75. }
  76. }
  77. },
  78. columns: [{
  79. xtype: 'treecolumn',
  80. sortable: false,
  81. dataIndex: 'text',
  82. flex: 1,
  83. renderer: function (value, cell, node, idx1, idx2, store, tree) {
  84. var cls = '';
  85. if (node.parentNode &amp;&amp; node.parentNode.parentNode === null) {
  86. cls += ' x-grouptab-first';
  87. if (node.previousSibling) {
  88. cls += ' x-grouptab-prev';
  89. }
  90. if (!node.get('expanded') || node.firstChild == null) {
  91. cls += ' x-grouptab-last';
  92. }
  93. } else if (node.nextSibling === null) {
  94. cls += ' x-grouptab-last';
  95. } else {
  96. cls += ' x-grouptab-center';
  97. }
  98. if (node.data.activeTab) {
  99. cls += ' x-active-tab';
  100. }
  101. cell.tdCls= 'x-grouptab'+ cls;
  102. return value;
  103. }
  104. }]
  105. },{
  106. xtype: 'container',
  107. flex: 1,
  108. layout: 'card',
  109. activeItem: me.mainItem,
  110. baseCls: Ext.baseCSSPrefix + 'grouptabcontainer',
  111. items: me.cards
  112. }];
  113. me.addEvents(
  114. <span id='Ext-ux-GroupTabPanel-event-beforetabchange'> /**
  115. </span> * @event beforetabchange
  116. * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
  117. * the tabchange
  118. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  119. * @param {Ext.Component} newCard The card that is about to be activated
  120. * @param {Ext.Component} oldCard The card that is currently active
  121. */
  122. 'beforetabchange',
  123. <span id='Ext-ux-GroupTabPanel-event-tabchange'> /**
  124. </span> * @event tabchange
  125. * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
  126. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  127. * @param {Ext.Component} newCard The newly activated item
  128. * @param {Ext.Component} oldCard The previously active item
  129. */
  130. 'tabchange',
  131. <span id='Ext-ux-GroupTabPanel-event-beforegroupchange'> /**
  132. </span> * @event beforegroupchange
  133. * Fires before a group change (activated by {@link #setActiveGroup}). Return false in any listener to cancel
  134. * the groupchange
  135. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  136. * @param {Ext.Component} newGroup The root group card that is about to be activated
  137. * @param {Ext.Component} oldGroup The root group card that is currently active
  138. */
  139. 'beforegroupchange',
  140. <span id='Ext-ux-GroupTabPanel-event-groupchange'> /**
  141. </span> * @event groupchange
  142. * Fires when a new group has been activated (activated by {@link #setActiveGroup}).
  143. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  144. * @param {Ext.Component} newGroup The newly activated root group item
  145. * @param {Ext.Component} oldGroup The previously active root group item
  146. */
  147. 'groupchange'
  148. );
  149. me.callParent(arguments);
  150. me.setActiveTab(me.activeTab);
  151. me.setActiveGroup(me.activeGroup);
  152. me.mon(me.down('treepanel').getSelectionModel(), 'select', me.onNodeSelect, me);
  153. },
  154. getRowClass: function(node, rowIndex, rowParams, store) {
  155. var cls = '';
  156. if (node.data.activeGroup) {
  157. cls += ' x-active-group';
  158. }
  159. return cls;
  160. },
  161. <span id='Ext-ux-GroupTabPanel-method-onNodeSelect'> /**
  162. </span> * @private
  163. * Node selection listener.
  164. */
  165. onNodeSelect: function (selModel, node) {
  166. var me = this,
  167. currentNode = me.store.getRootNode(),
  168. parent;
  169. if (node.parentNode &amp;&amp; node.parentNode.parentNode === null) {
  170. parent = node;
  171. } else {
  172. parent = node.parentNode;
  173. }
  174. if (me.setActiveGroup(parent.get('id')) === false || me.setActiveTab(node.get('id')) === false) {
  175. return false;
  176. }
  177. while (currentNode) {
  178. currentNode.set('activeTab', false);
  179. currentNode.set('activeGroup', false);
  180. currentNode = currentNode.firstChild || currentNode.nextSibling || currentNode.parentNode.nextSibling;
  181. }
  182. parent.set('activeGroup', true);
  183. parent.eachChild(function(child) {
  184. child.set('activeGroup', true);
  185. });
  186. node.set('activeTab', true);
  187. selModel.view.refresh();
  188. },
  189. <span id='Ext-ux-GroupTabPanel-method-setActiveTab'> /**
  190. </span> * Makes the given component active (makes it the visible card in the GroupTabPanel's CardLayout)
  191. * @param {Ext.Component} cmp The component to make active
  192. */
  193. setActiveTab: function(cmp) {
  194. var me = this,
  195. newTab = cmp,
  196. oldTab;
  197. if(Ext.isString(cmp)) {
  198. newTab = Ext.getCmp(newTab);
  199. }
  200. if (newTab === me.activeTab) {
  201. return false;
  202. }
  203. oldTab = me.activeTab;
  204. if (me.fireEvent('beforetabchange', me, newTab, oldTab) !== false) {
  205. me.activeTab = newTab;
  206. if (me.rendered) {
  207. me.down('container[baseCls=' + Ext.baseCSSPrefix + 'grouptabcontainer' + ']').getLayout().setActiveItem(newTab);
  208. }
  209. me.fireEvent('tabchange', me, newTab, oldTab);
  210. }
  211. return true;
  212. },
  213. <span id='Ext-ux-GroupTabPanel-method-setActiveGroup'> /**
  214. </span> * Makes the given group active
  215. * @param {Ext.Component} cmp The root component to make active.
  216. */
  217. setActiveGroup: function(cmp) {
  218. var me = this,
  219. newGroup = cmp,
  220. oldGroup;
  221. if(Ext.isString(cmp)) {
  222. newGroup = Ext.getCmp(newGroup);
  223. }
  224. if (newGroup === me.activeGroup) {
  225. return true;
  226. }
  227. oldGroup = me.activeGroup;
  228. if (me.fireEvent('beforegroupchange', me, newGroup, oldGroup) !== false) {
  229. me.activeGroup = newGroup;
  230. me.fireEvent('groupchange', me, newGroup, oldGroup);
  231. } else {
  232. return false;
  233. }
  234. return true;
  235. },
  236. <span id='Ext-ux-GroupTabPanel-method-createTreeStore'> /**
  237. </span> * @private
  238. * Creates the TreeStore used by the GroupTabBar.
  239. */
  240. createTreeStore: function() {
  241. var me = this,
  242. groups = me.prepareItems(me.items),
  243. data = {
  244. text: '.',
  245. children: []
  246. },
  247. cards = me.cards = [];
  248. me.activeGroup = me.activeGroup || 0;
  249. Ext.each(groups, function(groupItem, idx) {
  250. var leafItems = groupItem.items.items,
  251. rootItem = (leafItems[groupItem.mainItem] || leafItems[0]),
  252. groupRoot = {
  253. children: []
  254. };
  255. // Create the root node of the group
  256. groupRoot.id = rootItem.id;
  257. groupRoot.text = rootItem.title;
  258. groupRoot.iconCls = rootItem.iconCls;
  259. groupRoot.expanded = true;
  260. groupRoot.activeGroup = (me.activeGroup === idx);
  261. groupRoot.activeTab = groupRoot.activeGroup ? true : false;
  262. if (groupRoot.activeTab) {
  263. me.activeTab = groupRoot.id;
  264. }
  265. if (groupRoot.activeGroup) {
  266. me.mainItem = groupItem.mainItem || 0;
  267. me.activeGroup = groupRoot.id;
  268. }
  269. Ext.each(leafItems, function(leafItem) {
  270. // First node has been done
  271. if (leafItem.id !== groupRoot.id) {
  272. var child = {
  273. id: leafItem.id,
  274. leaf: true,
  275. text: leafItem.title,
  276. iconCls: leafItem.iconCls,
  277. activeGroup: groupRoot.activeGroup,
  278. activeTab: false
  279. };
  280. groupRoot.children.push(child);
  281. }
  282. // Ensure the items do not get headers
  283. delete leafItem.title;
  284. delete leafItem.iconCls;
  285. cards.push(leafItem);
  286. });
  287. data.children.push(groupRoot);
  288. });
  289. return Ext.create('Ext.data.TreeStore', {
  290. fields: ['id', 'text', 'activeGroup', 'activeTab'],
  291. root: {
  292. expanded: true
  293. },
  294. proxy: {
  295. type: 'memory',
  296. data: data
  297. }
  298. });
  299. },
  300. <span id='Ext-ux-GroupTabPanel-method-getActiveTab'> /**
  301. </span> * Returns the item that is currently active inside this GroupTabPanel.
  302. * @return {Ext.Component/Number} The currently active item
  303. */
  304. getActiveTab: function() {
  305. return this.activeTab;
  306. },
  307. <span id='Ext-ux-GroupTabPanel-method-getActiveGroup'> /**
  308. </span> * Returns the root group item that is currently active inside this GroupTabPanel.
  309. * @return {Ext.Component/Number} The currently active root group item
  310. */
  311. getActiveGroup: function() {
  312. return this.activeGroup;
  313. }
  314. });
  315. <span id='Ext-ux-GroupTreeChunker'>/**
  316. </span> * Allows GroupTab to render a table structure.
  317. */
  318. Ext.define('Ext.ux.GroupTreeChunker', {
  319. singleton: true,
  320. requires: ['Ext.XTemplate'],
  321. metaTableTpl: [
  322. '{%if (this.openTableWrap)out.push(this.openTableWrap())%}',
  323. '&lt;table class=&quot;' + Ext.baseCSSPrefix + 'grid-table-resizer&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; {[this.embedFullWidth(values)]}&gt;&lt;tr&gt;&lt;td&gt;',
  324. '{[this.openRows()]}',
  325. '{row}',
  326. '{[this.closeRows()]}',
  327. '&lt;/td&gt;&lt;/tr&gt;&lt;table&gt;',
  328. '{%if (this.closeTableWrap)out.push(this.closeTableWrap())%}'
  329. ],
  330. constructor: function() {
  331. Ext.XTemplate.prototype.recurse = function(values, reference) {
  332. return this.apply(reference ? values[reference] : values);
  333. };
  334. },
  335. embedFullWidth: function(values) {
  336. var result = 'style=&quot;width:{fullWidth}px;';
  337. // If there are no records, we need to give the table a height so that it
  338. // is displayed and causes q scrollbar if the width exceeds the View's width.
  339. if (!values.rowCount) {
  340. result += 'height:1px;';
  341. }
  342. return result + '&quot;';
  343. },
  344. openRows: function() {
  345. return '&lt;tpl for=&quot;rows&quot;&gt;';
  346. },
  347. closeRows: function() {
  348. return '&lt;/tpl&gt;';
  349. },
  350. metaRowTpl: [
  351. '&lt;div class=&quot;' + Ext.baseCSSPrefix + 'grouptab-row {[this.embedRowCls()]}&quot; {[this.embedRowAttr()]}&gt;',
  352. '&lt;tpl for=&quot;columns&quot;&gt;',
  353. '&lt;div class=&quot;{cls} ' + Ext.baseCSSPrefix + 'grouptab-cell ' + Ext.baseCSSPrefix + 'grid-cell-{columnId} {{id}-modified} {{id}-tdCls} {[this.firstOrLastCls(xindex, xcount)]}&quot; {{id}-tdAttr}&gt;',
  354. '&lt;div {unselectableAttr} class=&quot;' + Ext.baseCSSPrefix + 'grid-cell-inner {unselectableCls}&quot; style=&quot;text-align: {align}; {{id}-style};&quot;&gt;{{id}}&lt;/div&gt;',
  355. '&lt;div class=&quot;x-grouptabs-corner x-grouptabs-corner-top-left&quot; id=&quot;ext-gen25&quot;&gt;&lt;/div&gt;',
  356. '&lt;div class=&quot;x-grouptabs-corner x-grouptabs-corner-bottom-left&quot; id=&quot;ext-gen26&quot;&gt;&lt;/div&gt;',
  357. '&lt;/div&gt;',
  358. '&lt;/tpl&gt;',
  359. '&lt;/div&gt;'
  360. ],
  361. firstOrLastCls: function(xindex, xcount) {
  362. if (xindex === 1) {
  363. return Ext.view.Table.prototype.firstCls;
  364. } else if (xindex === xcount) {
  365. return Ext.view.Table.prototype.lastCls;
  366. }
  367. },
  368. embedRowCls: function() {
  369. return '{rowCls}';
  370. },
  371. embedRowAttr: function() {
  372. return '{rowAttr}';
  373. },
  374. getTableTpl: function(cfg, textOnly) {
  375. var tpl,
  376. tableTplMemberFns = {
  377. openRows: this.openRows,
  378. closeRows: this.closeRows,
  379. embedFullWidth: this.embedFullWidth
  380. },
  381. tplMemberFns = {},
  382. features = cfg.features || [],
  383. ln = features.length,
  384. i = 0,
  385. memberFns = {
  386. embedRowCls: this.embedRowCls,
  387. embedRowAttr: this.embedRowAttr,
  388. firstOrLastCls: this.firstOrLastCls,
  389. unselectableAttr: cfg.enableTextSelection ? '' : 'unselectable=&quot;on&quot;',
  390. unselectableCls: cfg.enableTextSelection ? '' : Ext.baseCSSPrefix + 'unselectable'
  391. },
  392. // copy the default
  393. metaRowTpl = Array.prototype.slice.call(this.metaRowTpl, 0),
  394. metaTableTpl;
  395. for (; i &lt; ln; i++) {
  396. if (!features[i].disabled) {
  397. features[i].mutateMetaRowTpl(metaRowTpl);
  398. Ext.apply(memberFns, features[i].getMetaRowTplFragments());
  399. Ext.apply(tplMemberFns, features[i].getFragmentTpl());
  400. Ext.apply(tableTplMemberFns, features[i].getTableFragments());
  401. }
  402. }
  403. metaRowTpl = new Ext.XTemplate(metaRowTpl.join(''), memberFns);
  404. cfg.row = metaRowTpl.applyTemplate(cfg);
  405. metaTableTpl = new Ext.XTemplate(this.metaTableTpl.join(''), tableTplMemberFns);
  406. tpl = metaTableTpl.applyTemplate(cfg);
  407. // TODO: Investigate eliminating.
  408. if (!textOnly) {
  409. tpl = new Ext.XTemplate(tpl, tplMemberFns);
  410. }
  411. return tpl;
  412. }
  413. });
  414. </pre>
  415. </body>
  416. </html>