Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / history / history.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.require([
16     'Ext.util.History',
17     'Ext.tab.Panel'
18 ]);
19
20 Ext.onReady(function() {
21     // The only requirement for this to work is that you must have a hidden field and
22     // an iframe available in the page with ids corresponding to Ext.History.fieldId
23     // and Ext.History.iframeId.  See history.html for an example.
24     Ext.History.init();
25
26     // Needed if you want to handle history for multiple components in the same page.
27     // Should be something that won't be in component ids.
28     var tokenDelimiter = ':';
29     
30     function onTabChange(tabPanel, tab) {
31         var tabs = [],
32             ownerCt = tabPanel.ownerCt, 
33             oldToken, newToken;
34
35         tabs.push(tab.id);
36         tabs.push(tabPanel.id);
37
38         while (ownerCt && ownerCt.is('tabpanel')) {
39             tabs.push(ownerCt.id);
40             ownerCt = ownerCt.ownerCt;
41         }
42         
43         newToken = tabs.reverse().join(tokenDelimiter);
44         
45         oldToken = Ext.History.getToken();
46        
47         if (oldToken === null || oldToken.search(newToken) === -1) {
48             Ext.History.add(newToken);
49         }
50     }
51     
52     // Handle this change event in order to restore the UI to the appropriate history state
53     function onAfterRender() {
54         Ext.History.on('change', function(token) {
55             var parts, tabPanel, length, i;
56             
57             if (token) {
58                 parts = token.split(tokenDelimiter);
59                 length = parts.length;
60                 
61                 // setActiveTab in all nested tabs
62                 for (i = 0; i < length - 1; i++) {
63                     Ext.getCmp(parts[i]).setActiveTab(Ext.getCmp(parts[i + 1]));
64                 }
65             }
66         });
67         
68         // This is the initial default state.  Necessary if you navigate starting from the
69         // page without any existing history token params and go back to the start state.
70         var activeTab1 = Ext.getCmp('main-tabs').getActiveTab(),
71             activeTab2 = activeTab1.getActiveTab();
72             
73         onTabChange(activeTab1, activeTab2);
74     }
75     
76     Ext.create('Ext.TabPanel', {
77         renderTo: Ext.getBody(),
78         id: 'main-tabs',
79         height: 300,
80         width: 600,
81         activeTab: 0,
82         defaults: {
83             padding: 10
84         },
85         
86         items: [{
87             xtype: 'tabpanel',
88             title: 'Tab 1',
89             id: 'tab1',
90             activeTab: 0,
91             padding: 5,
92             border: true,
93             plain: true,
94             
95             defaults: {
96                 padding: 10
97             },
98
99             items: [{
100                 title: 'Sub-tab 1',
101                 id: 'subtab1',
102                 html: 'Sub-tab 1 content'
103             },{
104                 title: 'Sub-tab 2',
105                 id: 'subtab2',
106                 html: 'Sub-tab 2 content'
107             },{
108                 title: 'Sub-tab 3',
109                 id: 'subtab3',
110                 html: 'Sub-tab 3 content'
111             }],
112             
113             listeners: {
114                 tabchange: onTabChange
115             }
116         },{
117             title: 'Tab 2',
118             id: 'tab2',
119             html: 'Tab 2 content'
120         },{
121             title: 'Tab 3',
122             id: 'tab3',
123             html: 'Tab 3 content'
124         },{
125             title: 'Tab 4',
126             id: 'tab4',
127             html: 'Tab 4 content'
128         },{
129             title: 'Tab 5',
130             id: 'tab5',
131             html: 'Tab 5 content'
132         }],
133         listeners: {
134             tabchange: onTabChange,
135             afterrender: onAfterRender 
136         }
137     });
138 });