GroupTabPanel.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @author Nicolas Ferrero
  3. * @class Ext.ux.GroupTabPanel
  4. * @extends Ext.Container
  5. * A TabPanel with grouping support.
  6. */
  7. Ext.define('Ext.ux.GroupTabPanel', {
  8. extend: 'Ext.Container',
  9. alias: 'widget.grouptabpanel',
  10. requires:[
  11. 'Ext.data.*',
  12. 'Ext.tree.*',
  13. 'Ext.layout.*'
  14. ],
  15. baseCls : Ext.baseCSSPrefix + 'grouptabpanel',
  16. initComponent: function(config) {
  17. var me = this,
  18. items = [];
  19. Ext.apply(me, config);
  20. me.store = me.createItemsStore();
  21. me.layout = {
  22. type: 'hbox',
  23. pack: 'start',
  24. align: 'stretch'
  25. };
  26. me.defaults = {
  27. border: false
  28. };
  29. me.items = Ext.each(me.items, function(item) {
  30. items.push(item.items);
  31. });
  32. me.items = [{
  33. xtype: 'treepanel',
  34. cls: 'x-tree-panel x-grouptabbar',
  35. width: 150,
  36. rootVisible: false,
  37. height: 400,
  38. store: me.store,
  39. hideHeaders: true,
  40. useArrows: true,
  41. animate: false,
  42. viewConfig: {
  43. overItemCls: ''
  44. },
  45. columns: [{
  46. xtype: 'treecolumn',
  47. sortable: false,
  48. dataIndex: 'text',
  49. flex: 1,
  50. renderer: function (value, cell, node, idx1, idx2, store, tree) {
  51. var cls = '';
  52. if (!node.data.activeGroup) {
  53. cls += ' x-inactive-group';
  54. } else if (node.parentNode && node.parentNode.parentNode === null) {
  55. cls += ' x-grouptab-first';
  56. if (node.previousSibling) {
  57. cls += ' x-grouptab-prev';
  58. }
  59. } else if (node.nextSibling === null) {
  60. cls += ' x-grouptab-last';
  61. } else {
  62. cls += ' x-grouptab-center';
  63. }
  64. if (node.data.activeTab) {
  65. cls += ' x-active-tab';
  66. }
  67. cell.tdCls= 'x-grouptab'+ cls;
  68. return value;
  69. }
  70. }]
  71. },{
  72. xtype: 'container',
  73. flex: 1,
  74. layout: 'card',
  75. activeItem: me.mainItem,
  76. baseCls: Ext.baseCSSPrefix + 'grouptabcontainer',
  77. items: items
  78. }];
  79. me.addEvents(
  80. /**
  81. * @event beforetabchange
  82. * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
  83. * the tabchange
  84. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  85. * @param {Ext.Component} newCard The card that is about to be activated
  86. * @param {Ext.Component} oldCard The card that is currently active
  87. */
  88. 'beforetabchange',
  89. /**
  90. * @event tabchange
  91. * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
  92. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  93. * @param {Ext.Component} newCard The newly activated item
  94. * @param {Ext.Component} oldCard The previously active item
  95. */
  96. 'tabchange',
  97. /**
  98. * @event beforegroupchange
  99. * Fires before a group change (activated by {@link #setActiveGroup}). Return false in any listener to cancel
  100. * the groupchange
  101. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  102. * @param {Ext.Component} newGroup The root group card that is about to be activated
  103. * @param {Ext.Component} oldGroup The root group card that is currently active
  104. */
  105. 'beforegroupchange',
  106. /**
  107. * @event groupchange
  108. * Fires when a new group has been activated (activated by {@link #setActiveGroup}).
  109. * @param {Ext.ux.GroupTabPanel} grouptabPanel The GroupTabPanel
  110. * @param {Ext.Component} newGroup The newly activated root group item
  111. * @param {Ext.Component} oldGroup The previously active root group item
  112. */
  113. 'groupchange'
  114. );
  115. me.callParent(arguments);
  116. me.setActiveTab(me.activeTab);
  117. me.setActiveGroup(me.activeGroup);
  118. me.mon(me.down('treepanel').getSelectionModel(), 'select', me.onNodeSelect, me);
  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. createItemsStore: function() {
  200. var me = this,
  201. data = {
  202. text:'.',
  203. children: []
  204. };
  205. me.activeGroup = me.activeGroup || 0;
  206. Ext.each(me.items, function(item, idx){
  207. var items = item.items,
  208. rootItem = (items[item.mainItem] || items[0]),
  209. root = {
  210. children: []
  211. };
  212. if (!rootItem.id) {
  213. rootItem.id = Ext.id();
  214. }
  215. root.id = rootItem.id;
  216. root.text = rootItem.title;
  217. root.iconCls = rootItem.iconCls;
  218. delete rootItem.iconCls;
  219. delete rootItem.title;
  220. root.expanded = true;
  221. root.activeGroup = (me.activeGroup === idx);
  222. root.activeTab = root.activeGroup ? true : false;
  223. if (root.activeTab) {
  224. me.activeTab = root.id;
  225. }
  226. if (root.activeGroup) {
  227. me.mainItem = item.mainItem || 0;
  228. me.activeGroup = root.id;
  229. }
  230. Ext.each(item.items, function(childItem) {
  231. if (!childItem.id) {
  232. childItem.id = Ext.id();
  233. }
  234. if(childItem.id !== root.id) {
  235. var child = {
  236. id: childItem.id,
  237. leaf: true,
  238. text: childItem.title,
  239. iconCls: childItem.iconCls,
  240. activeTab: false
  241. };
  242. delete childItem.title;
  243. delete childItem.iconCls;
  244. child.activeGroup = root.activeGroup;
  245. root.children.push(child);
  246. }
  247. }, me);
  248. data.children.push(root);
  249. }, me);
  250. return Ext.create('Ext.data.TreeStore', {
  251. fields: ['id', 'text', 'activeGroup', 'activeTab'],
  252. root: {expanded: true},
  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/Integer} 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/Integer} The currently active root group item
  269. */
  270. getActiveGroup: function() {
  271. return this.activeGroup;
  272. }
  273. });