GroupTabPanel.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. Ext.define('erp.view.sys.hr.GroupTabPanel', {
  2. extend: 'Ext.Container',
  3. alias: 'widget.grouptabpanel',
  4. requires:[
  5. 'Ext.tree.Panel',
  6. 'erp.view.sys.hr.GroupTabRenderer'
  7. ],
  8. baseCls : Ext.baseCSSPrefix + 'grouptabpanel',
  9. initComponent: function(config) {
  10. var me = this;
  11. Ext.apply(me, config);
  12. me.store = me.createTreeStore();
  13. me.layout = {
  14. type: 'hbox',
  15. align: 'stretch'
  16. };
  17. me.defaults = {
  18. border: false
  19. };
  20. me.items = [{
  21. xtype: 'treepanel',
  22. cls: 'x-tree-panel x-grouptabbar',
  23. width: 150,
  24. rootVisible: false,
  25. store: me.store,
  26. hideHeaders: true,
  27. animate: false,
  28. processEvent: Ext.emptyFn,
  29. border: false,
  30. plugins: [{
  31. ptype: 'grouptabrenderer'
  32. }],
  33. viewConfig: {
  34. overItemCls: '',
  35. getRowClass: me.getRowClass
  36. },
  37. columns: [{
  38. xtype: 'treecolumn',
  39. sortable: false,
  40. dataIndex: 'text',
  41. flex: 1,
  42. renderer: function (value, cell, node, idx1, idx2, store, tree) {
  43. var cls = '';
  44. if (node.parentNode && node.parentNode.parentNode === null) {
  45. cls += ' x-grouptab-first';
  46. if (node.previousSibling) {
  47. cls += ' x-grouptab-prev';
  48. }
  49. if (!node.get('expanded') || node.firstChild == null) {
  50. cls += ' x-grouptab-last';
  51. }
  52. } else if (node.nextSibling === null) {
  53. cls += ' x-grouptab-last';
  54. } else {
  55. cls += ' x-grouptab-center';
  56. }
  57. if (node.data.activeTab) {
  58. cls += ' x-active-tab';
  59. }
  60. cell.tdCls= 'x-grouptab'+ cls;
  61. return value;
  62. }
  63. }]
  64. }, {
  65. xtype: 'container',
  66. flex: 1,
  67. layout: 'card',
  68. // activeItem: me.activeItem || me.mainItem,
  69. baseCls: Ext.baseCSSPrefix + 'grouptabcontainer',
  70. items: me.cards
  71. }];
  72. me.addEvents(
  73. /**
  74. * @event beforetabchange
  75. * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
  76. * the tabchange
  77. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  78. * @param {Ext.Component} newCard The card that is about to be activated
  79. * @param {Ext.Component} oldCard The card that is currently active
  80. */
  81. 'beforetabchange',
  82. /**
  83. * @event tabchange
  84. * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
  85. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  86. * @param {Ext.Component} newCard The newly activated item
  87. * @param {Ext.Component} oldCard The previously active item
  88. */
  89. 'tabchange',
  90. /**
  91. * @event beforegroupchange
  92. * Fires before a group change (activated by {@link #setActiveGroup}). Return false in any listener to cancel
  93. * the groupchange
  94. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  95. * @param {Ext.Component} newGroup The root group card that is about to be activated
  96. * @param {Ext.Component} oldGroup The root group card that is currently active
  97. */
  98. 'beforegroupchange',
  99. /**
  100. * @event groupchange
  101. * Fires when a new group has been activated (activated by {@link #setActiveGroup}).
  102. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  103. * @param {Ext.Component} newGroup The newly activated root group item
  104. * @param {Ext.Component} oldGroup The previously active root group item
  105. */
  106. 'groupchange'
  107. );
  108. me.callParent(arguments);
  109. me.setActiveTab(me.activeTab);
  110. me.setActiveGroup(me.activeGroup);
  111. me.mon(me.down('treepanel').getSelectionModel(), 'select', me.onNodeSelect, me);
  112. },
  113. getRowClass: function(node, rowIndex, rowParams, store) {
  114. var cls = '';
  115. if (node.data.activeGroup) {
  116. cls += ' x-active-group';
  117. }
  118. return cls;
  119. },
  120. /**
  121. * @private
  122. * Node selection listener.
  123. */
  124. onNodeSelect: function (selModel, node) {
  125. var me = this,
  126. currentNode = me.store.getRootNode(),
  127. parent;
  128. if (node.parentNode && node.parentNode.parentNode === null) {
  129. parent = node;
  130. } else {
  131. parent = node.parentNode;
  132. }
  133. if (me.setActiveGroup(parent.get('id')) === false || me.setActiveTab(node.get('id')) === false) {
  134. return false;
  135. }
  136. while (currentNode) {
  137. currentNode.set('activeTab', false);
  138. currentNode.set('activeGroup', false);
  139. currentNode = currentNode.firstChild || currentNode.nextSibling || currentNode.parentNode.nextSibling;
  140. }
  141. parent.set('activeGroup', true);
  142. parent.eachChild(function(child) {
  143. child.set('activeGroup', true);
  144. });
  145. node.set('activeTab', true);
  146. selModel.view.refresh();
  147. },
  148. /**
  149. * Makes the given component active (makes it the visible card in the GroupTabPanel's CardLayout)
  150. * @param {Ext.Component} cmp The component to make active
  151. */
  152. setActiveTab: function(cmp) {
  153. var me = this,
  154. newTab = cmp,
  155. oldTab;
  156. if(Ext.isString(cmp)) {
  157. newTab = Ext.getCmp(newTab);
  158. }
  159. if (newTab === me.activeTab) {
  160. return false;
  161. }
  162. oldTab = me.activeTab;
  163. if (me.fireEvent('beforetabchange', me, newTab, oldTab) !== false) {
  164. me.activeTab = newTab;
  165. if (me.rendered) {
  166. me.down('container[baseCls=' + Ext.baseCSSPrefix + 'grouptabcontainer' + ']').getLayout().setActiveItem(newTab);
  167. }
  168. me.fireEvent('tabchange', me, newTab, oldTab);
  169. }
  170. return true;
  171. },
  172. /**
  173. * Makes the given group active
  174. * @param {Ext.Component} cmp The root component to make active.
  175. */
  176. setActiveGroup: function(cmp) {
  177. var me = this,
  178. newGroup = cmp,
  179. oldGroup;
  180. if(Ext.isString(cmp)) {
  181. newGroup = Ext.getCmp(newGroup);
  182. }
  183. if (newGroup === me.activeGroup) {
  184. return true;
  185. }
  186. oldGroup = me.activeGroup;
  187. if (me.fireEvent('beforegroupchange', me, newGroup, oldGroup) !== false) {
  188. me.activeGroup = newGroup;
  189. me.fireEvent('groupchange', me, newGroup, oldGroup);
  190. } else {
  191. return false;
  192. }
  193. return true;
  194. },
  195. /**
  196. * @private
  197. * Creates the TreeStore used by the GroupTabBar.
  198. */
  199. createTreeStore: function() {
  200. var me = this,
  201. groups = me.prepareItems(me.items),
  202. data = {
  203. text: '.',
  204. children: []
  205. },
  206. cards = me.cards = [];
  207. me.activeGroup = me.activeGroup || 0;
  208. Ext.each(groups, function(groupItem, idx) {
  209. var leafItems = groupItem.items.items,
  210. rootItem = (leafItems[groupItem.mainItem] || leafItems[0]),
  211. groupRoot = {
  212. children: []
  213. };
  214. // Create the root node of the group
  215. groupRoot.id = rootItem.id;
  216. groupRoot.text = rootItem.title;
  217. groupRoot.iconCls = rootItem.iconCls;
  218. groupRoot.expanded = true;
  219. groupRoot.activeGroup = (me.activeGroup === idx);
  220. groupRoot.activeTab = groupRoot.activeGroup ? true : false;
  221. if (groupRoot.activeTab) {
  222. me.activeTab = groupRoot.id;
  223. }
  224. if (groupRoot.activeGroup) {
  225. me.mainItem = groupItem.mainItem || 0;
  226. me.activeGroup = groupRoot.id;
  227. }
  228. Ext.each(leafItems, function(leafItem) {
  229. // First node has been done
  230. if (leafItem.id !== groupRoot.id) {
  231. var child = {
  232. id: leafItem.id,
  233. leaf: true,
  234. text: leafItem.title,
  235. iconCls: leafItem.iconCls,
  236. activeGroup: groupRoot.activeGroup,
  237. activeTab: false
  238. };
  239. groupRoot.children.push(child);
  240. }
  241. // Ensure the items do not get headers
  242. delete leafItem.title;
  243. delete leafItem.iconCls;
  244. cards.push(leafItem);
  245. });
  246. data.children.push(groupRoot);
  247. });
  248. return Ext.create('Ext.data.TreeStore', {
  249. fields: ['id', 'text', 'activeGroup', 'activeTab'],
  250. root: {
  251. expanded: true
  252. },
  253. proxy: {
  254. type: 'memory',
  255. data: data
  256. }
  257. });
  258. },
  259. /**
  260. * Returns the item that is currently active inside this GroupTabPanel.
  261. * @return {Ext.Component/Number} The currently active item
  262. */
  263. getActiveTab: function() {
  264. return this.activeTab;
  265. },
  266. /**
  267. * Returns the root group item that is currently active inside this GroupTabPanel.
  268. * @return {Ext.Component/Number} The currently active root group item
  269. */
  270. getActiveGroup: function() {
  271. return this.activeGroup;
  272. }
  273. });