4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-History'>/**
19 </span> * @class Ext.util.History
21 History management component that allows you to register arbitrary tokens that signify application
22 history state on navigation actions. You can then handle the history {@link #change} event in order
23 to reset your application UI to the appropriate state when the user navigates forward or backward through
24 the browser history stack.
27 The {@link #init} method of the History object must be called before using History. This sets up the internal
28 state and must be the first thing called before using History.
31 The History objects requires elements on the page to keep track of the browser history. For older versions of IE,
32 an IFrame is required to do the tracking. For other browsers, a hidden field can be used. The history objects expects
33 these to be on the page before the {@link #init} method is called. The following markup is suggested in order
34 to support all browsers:
36 <form id="history-form" class="x-hide-display">
37 <input type="hidden" id="x-history-field" />
38 <iframe id="x-history-frame"></iframe>
44 Ext.define('Ext.util.History', {
46 alternateClassName: 'Ext.History',
48 observable: 'Ext.util.Observable'
51 constructor: function() {
53 me.oldIEMode = Ext.isIE6 || Ext.isIE7 || !Ext.isStrict && Ext.isIE8;
55 me.hiddenField = null;
57 me.currentToken = null;
61 var href = window.location.href,
62 i = href.indexOf("#");
64 return i >= 0 ? href.substr(i + 1) : null;
68 this.hiddenField.value = this.currentToken;
72 handleStateChange: function(token) {
73 this.currentToken = token;
74 this.fireEvent('change', token);
77 updateIFrame: function(token) {
78 var html = '<html><body><div id="state">' +
79 Ext.util.Format.htmlEncode(token) +
80 '</div></body></html>';
83 var doc = this.iframe.contentWindow.document;
93 checkIFrame: function () {
95 contentWindow = me.iframe.contentWindow;
97 if (!contentWindow || !contentWindow.document) {
98 Ext.Function.defer(this.checkIFrame, 10, this);
102 var doc = contentWindow.document,
103 elem = doc.getElementById("state"),
104 oldToken = elem ? elem.innerText : null,
105 oldHash = me.getHash();
107 Ext.TaskManager.start({
109 var doc = contentWindow.document,
110 elem = doc.getElementById("state"),
111 newToken = elem ? elem.innerText : null,
112 newHash = me.getHash();
114 if (newToken !== oldToken) {
116 me.handleStateChange(newToken);
117 window.top.location.hash = newToken;
120 } else if (newHash !== oldHash) {
122 me.updateIFrame(newHash);
129 me.fireEvent('ready', me);
132 startUp: function () {
135 me.currentToken = me.hiddenField.value || this.getHash();
140 var hash = me.getHash();
141 Ext.TaskManager.start({
143 var newHash = me.getHash();
144 if (newHash !== hash) {
146 me.handleStateChange(hash);
154 me.fireEvent('ready', me);
159 <span id='Ext-util-History-property-fieldId'> /**
160 </span> * The id of the hidden field required for storing the current history token.
164 fieldId: Ext.baseCSSPrefix + 'history-field',
165 <span id='Ext-util-History-property-iframeId'> /**
166 </span> * The id of the iframe required by IE to manage the history stack.
170 iframeId: Ext.baseCSSPrefix + 'history-frame',
172 <span id='Ext-util-History-method-init'> /**
173 </span> * Initialize the global History instance.
174 * @param {Boolean} onReady (optional) A callback function that will be called once the history
175 * component is fully initialized.
176 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser window.
178 init: function (onReady, scope) {
182 Ext.callback(onReady, scope, [me]);
187 Ext.onReady(function() {
188 me.init(onReady, scope);
193 me.hiddenField = Ext.getDom(me.fieldId);
196 me.iframe = Ext.getDom(me.iframeId);
200 <span id='Ext-util-History-event-ready'> /**
201 </span> * @event ready
202 * Fires when the Ext.util.History singleton has been initialized and is ready for use.
203 * @param {Ext.util.History} The Ext.util.History singleton.
206 <span id='Ext-util-History-event-change'> /**
207 </span> * @event change
208 * Fires when navigation back or forwards within the local page's history occurs.
209 * @param {String} token An identifier associated with the page state at that point in its history.
215 me.on('ready', onReady, scope, {single: true});
220 <span id='Ext-util-History-method-add'> /**
221 </span> * Add a new token to the history stack. This can be any arbitrary value, although it would
222 * commonly be the concatenation of a component id and another id marking the specifc history
223 * state of that component. Example usage:
224 * <pre><code>
225 // Handle tab changes on a TabPanel
226 tabPanel.on('tabchange', function(tabPanel, tab){
227 Ext.History.add(tabPanel.id + ':' + tab.id);
229 </code></pre>
230 * @param {String} token The value that defines a particular application-specific history state
231 * @param {Boolean} preventDuplicates When true, if the passed token matches the current token
232 * it will not save a new history step. Set to false if the same state can be saved more than once
233 * at the same history stack location (defaults to true).
235 add: function (token, preventDup) {
238 if (preventDup !== false) {
239 if (me.getToken() === token) {
245 return me.updateIFrame(token);
247 window.top.location.hash = token;
252 <span id='Ext-util-History-method-back'> /**
253 </span> * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
256 window.history.go(-1);
259 <span id='Ext-util-History-method-forward'> /**
260 </span> * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
263 window.history.go(1);
266 <span id='Ext-util-History-method-getToken'> /**
267 </span> * Retrieves the currently-active history token.
268 * @return {String} The token
270 getToken: function() {
271 return this.ready ? this.currentToken : this.getHash();