3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.History"></div>/**
\r
15 * @class Ext.History
\r
16 * @extends Ext.util.Observable
\r
17 * History management component that allows you to register arbitrary tokens that signify application
\r
18 * history state on navigation actions. You can then handle the history {@link #change} event in order
\r
19 * to reset your application UI to the appropriate state when the user navigates forward or backward through
\r
20 * the browser history stack.
\r
23 Ext.History = (function () {
\r
24 var iframe, hiddenField;
\r
28 function getHash() {
\r
29 var href = top.location.href, i = href.indexOf("#");
\r
30 return i >= 0 ? href.substr(i + 1) : null;
\r
34 hiddenField.value = currentToken;
\r
37 function handleStateChange(token) {
\r
38 currentToken = token;
\r
39 Ext.History.fireEvent('change', token);
\r
42 function updateIFrame (token) {
\r
43 var html = ['<html><body><div id="state">',token,'</div></body></html>'].join('');
\r
45 var doc = iframe.contentWindow.document;
\r
55 function checkIFrame() {
\r
56 if (!iframe.contentWindow || !iframe.contentWindow.document) {
\r
57 setTimeout(checkIFrame, 10);
\r
61 var doc = iframe.contentWindow.document;
\r
62 var elem = doc.getElementById("state");
\r
63 var token = elem ? elem.innerText : null;
\r
65 var hash = getHash();
\r
67 setInterval(function () {
\r
69 doc = iframe.contentWindow.document;
\r
70 elem = doc.getElementById("state");
\r
72 var newtoken = elem ? elem.innerText : null;
\r
74 var newHash = getHash();
\r
76 if (newtoken !== token) {
\r
78 handleStateChange(token);
\r
79 top.location.hash = token;
\r
82 } else if (newHash !== hash) {
\r
84 updateIFrame(newHash);
\r
91 Ext.History.fireEvent('ready', Ext.History);
\r
94 function startUp() {
\r
95 currentToken = hiddenField.value ? hiddenField.value : getHash();
\r
100 var hash = getHash();
\r
101 setInterval(function () {
\r
102 var newHash = getHash();
\r
103 if (newHash !== hash) {
\r
105 handleStateChange(hash);
\r
110 Ext.History.fireEvent('ready', Ext.History);
\r
115 <div id="prop-Ext.History-fieldId"></div>/**
\r
116 * The id of the hidden field required for storing the current history token.
\r
120 fieldId: 'x-history-field',
\r
121 <div id="prop-Ext.History-iframeId"></div>/**
\r
122 * The id of the iframe required by IE to manage the history stack.
\r
126 iframeId: 'x-history-frame',
\r
130 <div id="method-Ext.History-init"></div>/**
\r
131 * Initialize the global History instance.
\r
132 * @param {Boolean} onReady (optional) A callback function that will be called once the history
\r
133 * component is fully initialized.
\r
134 * @param {Object} scope (optional) The callback scope
\r
136 init: function (onReady, scope) {
\r
138 Ext.callback(onReady, scope, [this]);
\r
142 Ext.onReady(function(){
\r
143 Ext.History.init(onReady, scope);
\r
147 hiddenField = Ext.getDom(Ext.History.fieldId);
\r
149 iframe = Ext.getDom(Ext.History.iframeId);
\r
151 this.addEvents('ready', 'change');
\r
153 this.on('ready', onReady, scope, {single:true});
\r
158 <div id="method-Ext.History-add"></div>/**
\r
159 * Add a new token to the history stack. This can be any arbitrary value, although it would
\r
160 * commonly be the concatenation of a component id and another id marking the specifc history
\r
161 * state of that component. Example usage:
\r
163 // Handle tab changes on a TabPanel
\r
164 tabPanel.on('tabchange', function(tabPanel, tab){
\r
165 Ext.History.add(tabPanel.id + ':' + tab.id);
\r
168 * @param {String} token The value that defines a particular application-specific history state
\r
169 * @param {Boolean} preventDuplicates When true, if the passed token matches the current token
\r
170 * it will not save a new history step. Set to false if the same state can be saved more than once
\r
171 * at the same history stack location (defaults to true).
\r
173 add: function (token, preventDup) {
\r
174 if(preventDup !== false){
\r
175 if(this.getToken() == token){
\r
180 return updateIFrame(token);
\r
182 top.location.hash = token;
\r
187 <div id="method-Ext.History-back"></div>/**
\r
188 * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
\r
194 <div id="method-Ext.History-forward"></div>/**
\r
195 * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
\r
197 forward: function(){
\r
201 <div id="method-Ext.History-getToken"></div>/**
\r
202 * Retrieves the currently-active history token.
\r
203 * @return {String} The token
\r
205 getToken: function() {
\r
206 return ready ? currentToken : getHash();
\r
210 Ext.apply(Ext.History, new Ext.util.Observable());</pre>