GroupTab.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. Ext.ux.GroupTab = Ext.extend(Ext.Container, {
  8. mainItem: 0,
  9. expanded: true,
  10. deferredRender: true,
  11. activeTab: null,
  12. idDelimiter: '__',
  13. headerAsText: false,
  14. frame: false,
  15. hideBorders: true,
  16. initComponent: function(config){
  17. Ext.apply(this, config);
  18. this.frame = false;
  19. Ext.ux.GroupTab.superclass.initComponent.call(this);
  20. this.addEvents('activate', 'deactivate', 'changemainitem', 'beforetabchange', 'tabchange');
  21. this.setLayout(new Ext.layout.CardLayout({
  22. deferredRender: this.deferredRender
  23. }));
  24. if (!this.stack) {
  25. this.stack = Ext.TabPanel.AccessStack();
  26. }
  27. this.initItems();
  28. this.on('beforerender', function(){
  29. this.groupEl = this.ownerCt.getGroupEl(this);
  30. }, this);
  31. this.on('add', this.onAdd, this, {
  32. target: this
  33. });
  34. this.on('remove', this.onRemove, this, {
  35. target: this
  36. });
  37. if (this.mainItem !== undefined) {
  38. var item = (typeof this.mainItem == 'object') ? this.mainItem : this.items.get(this.mainItem);
  39. delete this.mainItem;
  40. this.setMainItem(item);
  41. }
  42. },
  43. /**
  44. * Sets the specified tab as the active tab. This method fires the {@link #beforetabchange} event which
  45. * can return false to cancel the tab change.
  46. * @param {String/Panel} tab The id or tab Panel to activate
  47. */
  48. setActiveTab : function(item){
  49. item = this.getComponent(item);
  50. if(!item){
  51. return false;
  52. }
  53. if(!this.rendered){
  54. this.activeTab = item;
  55. return true;
  56. }
  57. if(this.activeTab != item && this.fireEvent('beforetabchange', this, item, this.activeTab) !== false){
  58. if(this.activeTab && this.activeTab != this.mainItem){
  59. var oldEl = this.getTabEl(this.activeTab);
  60. if(oldEl){
  61. Ext.fly(oldEl).removeClass('x-grouptabs-strip-active');
  62. }
  63. }
  64. var el = this.getTabEl(item);
  65. Ext.fly(el).addClass('x-grouptabs-strip-active');
  66. this.activeTab = item;
  67. this.stack.add(item);
  68. this.layout.setActiveItem(item);
  69. if(this.layoutOnTabChange && item.doLayout){
  70. item.doLayout();
  71. }
  72. if(this.scrolling){
  73. this.scrollToTab(item, this.animScroll);
  74. }
  75. this.fireEvent('tabchange', this, item);
  76. return true;
  77. }
  78. return false;
  79. },
  80. getTabEl: function(item){
  81. if (item == this.mainItem) {
  82. return this.groupEl;
  83. }
  84. return Ext.TabPanel.prototype.getTabEl.call(this, item);
  85. },
  86. onRender: function(ct, position){
  87. Ext.ux.GroupTab.superclass.onRender.call(this, ct, position);
  88. this.strip = Ext.fly(this.groupEl).createChild({
  89. tag: 'ul',
  90. cls: 'x-grouptabs-sub'
  91. });
  92. this.tooltip = new Ext.ToolTip({
  93. target: this.groupEl,
  94. delegate: 'a.x-grouptabs-text',
  95. trackMouse: true,
  96. renderTo: document.body,
  97. listeners: {
  98. beforeshow: function(tip) {
  99. var item = (tip.triggerElement.parentNode === this.mainItem.tabEl)
  100. ? this.mainItem
  101. : this.findById(tip.triggerElement.parentNode.id.split(this.idDelimiter)[1]);
  102. if(!item.tabTip) {
  103. return false;
  104. }
  105. tip.body.dom.innerHTML = item.tabTip;
  106. },
  107. scope: this
  108. }
  109. });
  110. if (!this.itemTpl) {
  111. var tt = new Ext.Template('<li class="{cls}" id="{id}">', '<a onclick="return false;" class="x-grouptabs-text {iconCls}">{text}</a>', '</li>');
  112. tt.disableFormats = true;
  113. tt.compile();
  114. Ext.ux.GroupTab.prototype.itemTpl = tt;
  115. }
  116. this.items.each(this.initTab, this);
  117. },
  118. afterRender: function(){
  119. Ext.ux.GroupTab.superclass.afterRender.call(this);
  120. if (this.activeTab !== undefined) {
  121. var item = (typeof this.activeTab == 'object') ? this.activeTab : this.items.get(this.activeTab);
  122. delete this.activeTab;
  123. this.setActiveTab(item);
  124. }
  125. },
  126. // private
  127. initTab: function(item, index){
  128. var before = this.strip.dom.childNodes[index];
  129. var p = Ext.TabPanel.prototype.getTemplateArgs.call(this, item);
  130. if (item === this.mainItem) {
  131. item.tabEl = this.groupEl;
  132. p.cls += ' x-grouptabs-main-item';
  133. }
  134. var el = before ? this.itemTpl.insertBefore(before, p) : this.itemTpl.append(this.strip, p);
  135. item.tabEl = item.tabEl || el;
  136. item.on('disable', this.onItemDisabled, this);
  137. item.on('enable', this.onItemEnabled, this);
  138. item.on('titlechange', this.onItemTitleChanged, this);
  139. item.on('iconchange', this.onItemIconChanged, this);
  140. item.on('beforeshow', this.onBeforeShowItem, this);
  141. },
  142. setMainItem: function(item){
  143. item = this.getComponent(item);
  144. if (!item || this.fireEvent('changemainitem', this, item, this.mainItem) === false) {
  145. return;
  146. }
  147. this.mainItem = item;
  148. },
  149. getMainItem: function(){
  150. return this.mainItem || null;
  151. },
  152. // private
  153. onBeforeShowItem: function(item){
  154. if (item != this.activeTab) {
  155. this.setActiveTab(item);
  156. return false;
  157. }
  158. },
  159. // private
  160. onAdd: function(gt, item, index){
  161. if (this.rendered) {
  162. this.initTab.call(this, item, index);
  163. }
  164. },
  165. // private
  166. onRemove: function(tp, item){
  167. Ext.destroy(Ext.get(this.getTabEl(item)));
  168. this.stack.remove(item);
  169. item.un('disable', this.onItemDisabled, this);
  170. item.un('enable', this.onItemEnabled, this);
  171. item.un('titlechange', this.onItemTitleChanged, this);
  172. item.un('iconchange', this.onItemIconChanged, this);
  173. item.un('beforeshow', this.onBeforeShowItem, this);
  174. if (item == this.activeTab) {
  175. var next = this.stack.next();
  176. if (next) {
  177. this.setActiveTab(next);
  178. }
  179. else if (this.items.getCount() > 0) {
  180. this.setActiveTab(0);
  181. }
  182. else {
  183. this.activeTab = null;
  184. }
  185. }
  186. },
  187. // private
  188. onBeforeAdd: function(item){
  189. var existing = item.events ? (this.items.containsKey(item.getItemId()) ? item : null) : this.items.get(item);
  190. if (existing) {
  191. this.setActiveTab(item);
  192. return false;
  193. }
  194. Ext.TabPanel.superclass.onBeforeAdd.apply(this, arguments);
  195. var es = item.elements;
  196. item.elements = es ? es.replace(',header', '') : es;
  197. item.border = (item.border === true);
  198. },
  199. // private
  200. onItemDisabled: Ext.TabPanel.prototype.onItemDisabled,
  201. onItemEnabled: Ext.TabPanel.prototype.onItemEnabled,
  202. // private
  203. onItemTitleChanged: function(item){
  204. var el = this.getTabEl(item);
  205. if (el) {
  206. Ext.fly(el).child('a.x-grouptabs-text', true).innerHTML = item.title;
  207. }
  208. },
  209. //private
  210. onItemIconChanged: function(item, iconCls, oldCls){
  211. var el = this.getTabEl(item);
  212. if (el) {
  213. Ext.fly(el).child('a.x-grouptabs-text').replaceClass(oldCls, iconCls);
  214. }
  215. },
  216. beforeDestroy: function(){
  217. Ext.TabPanel.prototype.beforeDestroy.call(this);
  218. this.tooltip.destroy();
  219. }
  220. });
  221. Ext.reg('grouptab', Ext.ux.GroupTab);