3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
9 * @extends Ext.util.Observable
\r
10 * History management component that allows you to register arbitrary tokens that signify application
\r
11 * history state on navigation actions. You can then handle the history {@link #change} event in order
\r
12 * to reset your application UI to the appropriate state when the user navigates forward or backward through
\r
13 * the browser history stack.
\r
16 Ext.History = (function () {
\r
17 var iframe, hiddenField;
\r
21 function getHash() {
\r
22 var href = top.location.href, i = href.indexOf("#");
\r
23 return i >= 0 ? href.substr(i + 1) : null;
\r
27 hiddenField.value = currentToken;
\r
30 function handleStateChange(token) {
\r
31 currentToken = token;
\r
32 Ext.History.fireEvent('change', token);
\r
35 function updateIFrame (token) {
\r
36 var html = ['<html><body><div id="state">',Ext.util.Format.htmlEncode(token),'</div></body></html>'].join('');
\r
38 var doc = iframe.contentWindow.document;
\r
48 function checkIFrame() {
\r
49 if (!iframe.contentWindow || !iframe.contentWindow.document) {
\r
50 setTimeout(checkIFrame, 10);
\r
54 var doc = iframe.contentWindow.document;
\r
55 var elem = doc.getElementById("state");
\r
56 var token = elem ? elem.innerText : null;
\r
58 var hash = getHash();
\r
60 setInterval(function () {
\r
62 doc = iframe.contentWindow.document;
\r
63 elem = doc.getElementById("state");
\r
65 var newtoken = elem ? elem.innerText : null;
\r
67 var newHash = getHash();
\r
69 if (newtoken !== token) {
\r
71 handleStateChange(token);
\r
72 top.location.hash = token;
\r
75 } else if (newHash !== hash) {
\r
77 updateIFrame(newHash);
\r
84 Ext.History.fireEvent('ready', Ext.History);
\r
87 function startUp() {
\r
88 currentToken = hiddenField.value ? hiddenField.value : getHash();
\r
93 var hash = getHash();
\r
94 setInterval(function () {
\r
95 var newHash = getHash();
\r
96 if (newHash !== hash) {
\r
98 handleStateChange(hash);
\r
103 Ext.History.fireEvent('ready', Ext.History);
\r
109 * The id of the hidden field required for storing the current history token.
\r
113 fieldId: 'x-history-field',
\r
115 * The id of the iframe required by IE to manage the history stack.
\r
119 iframeId: 'x-history-frame',
\r
124 * Initialize the global History instance.
\r
125 * @param {Boolean} onReady (optional) A callback function that will be called once the history
\r
126 * component is fully initialized.
\r
127 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser window.
\r
129 init: function (onReady, scope) {
\r
131 Ext.callback(onReady, scope, [this]);
\r
135 Ext.onReady(function(){
\r
136 Ext.History.init(onReady, scope);
\r
140 hiddenField = Ext.getDom(Ext.History.fieldId);
\r
142 iframe = Ext.getDom(Ext.History.iframeId);
\r
147 * Fires when the Ext.History singleton has been initialized and is ready for use.
\r
148 * @param {Ext.History} The Ext.History singleton.
\r
153 * Fires when navigation back or forwards within the local page's history occurs.
\r
154 * @param {String} token An identifier associated with the page state at that point in its history.
\r
159 this.on('ready', onReady, scope, {single:true});
\r
165 * Add a new token to the history stack. This can be any arbitrary value, although it would
\r
166 * commonly be the concatenation of a component id and another id marking the specifc history
\r
167 * state of that component. Example usage:
\r
169 // Handle tab changes on a TabPanel
\r
170 tabPanel.on('tabchange', function(tabPanel, tab){
\r
171 Ext.History.add(tabPanel.id + ':' + tab.id);
\r
174 * @param {String} token The value that defines a particular application-specific history state
\r
175 * @param {Boolean} preventDuplicates When true, if the passed token matches the current token
\r
176 * it will not save a new history step. Set to false if the same state can be saved more than once
\r
177 * at the same history stack location (defaults to true).
\r
179 add: function (token, preventDup) {
\r
180 if(preventDup !== false){
\r
181 if(this.getToken() == token){
\r
186 return updateIFrame(token);
\r
188 top.location.hash = token;
\r
194 * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
\r
201 * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
\r
203 forward: function(){
\r
208 * Retrieves the currently-active history token.
\r
209 * @return {String} The token
\r
211 getToken: function() {
\r
212 return ready ? currentToken : getHash();
\r
216 Ext.apply(Ext.History, new Ext.util.Observable());