history.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Ext.require([
  2. 'Ext.util.History',
  3. 'Ext.tab.Panel'
  4. ]);
  5. Ext.onReady(function() {
  6. // The only requirement for this to work is that you must have a hidden field and
  7. // an iframe available in the page with ids corresponding to Ext.History.fieldId
  8. // and Ext.History.iframeId. See history.html for an example.
  9. Ext.History.init();
  10. // Needed if you want to handle history for multiple components in the same page.
  11. // Should be something that won't be in component ids.
  12. var tokenDelimiter = ':';
  13. function onTabChange(tabPanel, tab) {
  14. var tabs = [],
  15. ownerCt = tabPanel.ownerCt,
  16. oldToken, newToken;
  17. tabs.push(tab.id);
  18. tabs.push(tabPanel.id);
  19. while (ownerCt && ownerCt.is('tabpanel')) {
  20. tabs.push(ownerCt.id);
  21. ownerCt = ownerCt.ownerCt;
  22. }
  23. newToken = tabs.reverse().join(tokenDelimiter);
  24. oldToken = Ext.History.getToken();
  25. if (oldToken === null || oldToken.search(newToken) === -1) {
  26. Ext.History.add(newToken);
  27. }
  28. }
  29. // Handle this change event in order to restore the UI to the appropriate history state
  30. function onAfterRender() {
  31. Ext.History.on('change', function(token) {
  32. var parts, tabPanel, length, i;
  33. if (token) {
  34. parts = token.split(tokenDelimiter);
  35. length = parts.length;
  36. // setActiveTab in all nested tabs
  37. for (i = 0; i < length - 1; i++) {
  38. Ext.getCmp(parts[i]).setActiveTab(Ext.getCmp(parts[i + 1]));
  39. }
  40. }
  41. });
  42. // This is the initial default state. Necessary if you navigate starting from the
  43. // page without any existing history token params and go back to the start state.
  44. var activeTab1 = Ext.getCmp('main-tabs').getActiveTab(),
  45. activeTab2 = activeTab1.getActiveTab();
  46. onTabChange(activeTab1, activeTab2);
  47. }
  48. Ext.create('Ext.TabPanel', {
  49. renderTo: Ext.getBody(),
  50. id: 'main-tabs',
  51. height: 300,
  52. width: 600,
  53. activeTab: 0,
  54. defaults: {
  55. padding: 10
  56. },
  57. items: [{
  58. xtype: 'tabpanel',
  59. title: 'Tab 1',
  60. id: 'tab1',
  61. activeTab: 0,
  62. padding: 5,
  63. border: true,
  64. plain: true,
  65. defaults: {
  66. padding: 10
  67. },
  68. items: [{
  69. title: 'Sub-tab 1',
  70. id: 'subtab1',
  71. html: 'Sub-tab 1 content'
  72. },{
  73. title: 'Sub-tab 2',
  74. id: 'subtab2',
  75. html: 'Sub-tab 2 content'
  76. },{
  77. title: 'Sub-tab 3',
  78. id: 'subtab3',
  79. html: 'Sub-tab 3 content'
  80. }],
  81. listeners: {
  82. tabchange: onTabChange
  83. }
  84. },{
  85. title: 'Tab 2',
  86. id: 'tab2',
  87. html: 'Tab 2 content'
  88. },{
  89. title: 'Tab 3',
  90. id: 'tab3',
  91. html: 'Tab 3 content'
  92. },{
  93. title: 'Tab 4',
  94. id: 'tab4',
  95. html: 'Tab 4 content'
  96. },{
  97. title: 'Tab 5',
  98. id: 'tab5',
  99. html: 'Tab 5 content'
  100. }],
  101. listeners: {
  102. tabchange: onTabChange,
  103. afterrender: onAfterRender
  104. }
  105. });
  106. });