3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.util.History
18 History management component that allows you to register arbitrary tokens that signify application
19 history state on navigation actions. You can then handle the history {@link #change} event in order
20 to reset your application UI to the appropriate state when the user navigates forward or backward through
21 the browser history stack.
24 The {@link #init} method of the History object must be called before using History. This sets up the internal
25 state and must be the first thing called before using History.
28 The History objects requires elements on the page to keep track of the browser history. For older versions of IE,
29 an IFrame is required to do the tracking. For other browsers, a hidden field can be used. The history objects expects
30 these to be on the page before the {@link #init} method is called. The following markup is suggested in order
31 to support all browsers:
33 <form id="history-form" class="x-hide-display">
34 <input type="hidden" id="x-history-field" />
35 <iframe id="x-history-frame"></iframe>
41 Ext.define('Ext.util.History', {
43 alternateClassName: 'Ext.History',
45 observable: 'Ext.util.Observable'
48 constructor: function() {
50 me.oldIEMode = Ext.isIE6 || Ext.isIE7 || !Ext.isStrict && Ext.isIE8;
52 me.hiddenField = null;
54 me.currentToken = null;
58 var href = window.location.href,
59 i = href.indexOf("#");
61 return i >= 0 ? href.substr(i + 1) : null;
65 this.hiddenField.value = this.currentToken;
69 handleStateChange: function(token) {
70 this.currentToken = token;
71 this.fireEvent('change', token);
74 updateIFrame: function(token) {
75 var html = '<html><body><div id="state">' +
76 Ext.util.Format.htmlEncode(token) +
77 '</div></body></html>';
80 var doc = this.iframe.contentWindow.document;
90 checkIFrame: function () {
92 contentWindow = me.iframe.contentWindow;
94 if (!contentWindow || !contentWindow.document) {
95 Ext.Function.defer(this.checkIFrame, 10, this);
99 var doc = contentWindow.document,
100 elem = doc.getElementById("state"),
101 oldToken = elem ? elem.innerText : null,
102 oldHash = me.getHash();
104 Ext.TaskManager.start({
106 var doc = contentWindow.document,
107 elem = doc.getElementById("state"),
108 newToken = elem ? elem.innerText : null,
109 newHash = me.getHash();
111 if (newToken !== oldToken) {
113 me.handleStateChange(newToken);
114 window.top.location.hash = newToken;
117 } else if (newHash !== oldHash) {
119 me.updateIFrame(newHash);
126 me.fireEvent('ready', me);
129 startUp: function () {
132 me.currentToken = me.hiddenField.value || this.getHash();
137 var hash = me.getHash();
138 Ext.TaskManager.start({
140 var newHash = me.getHash();
141 if (newHash !== hash) {
143 me.handleStateChange(hash);
151 me.fireEvent('ready', me);
157 * The id of the hidden field required for storing the current history token.
161 fieldId: Ext.baseCSSPrefix + 'history-field',
163 * The id of the iframe required by IE to manage the history stack.
167 iframeId: Ext.baseCSSPrefix + 'history-frame',
170 * Initialize the global History instance.
171 * @param {Boolean} onReady (optional) A callback function that will be called once the history
172 * component is fully initialized.
173 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser window.
175 init: function (onReady, scope) {
179 Ext.callback(onReady, scope, [me]);
184 Ext.onReady(function() {
185 me.init(onReady, scope);
190 me.hiddenField = Ext.getDom(me.fieldId);
193 me.iframe = Ext.getDom(me.iframeId);
199 * Fires when the Ext.util.History singleton has been initialized and is ready for use.
200 * @param {Ext.util.History} The Ext.util.History singleton.
205 * Fires when navigation back or forwards within the local page's history occurs.
206 * @param {String} token An identifier associated with the page state at that point in its history.
212 me.on('ready', onReady, scope, {single: true});
218 * Add a new token to the history stack. This can be any arbitrary value, although it would
219 * commonly be the concatenation of a component id and another id marking the specifc history
220 * state of that component. Example usage:
222 // Handle tab changes on a TabPanel
223 tabPanel.on('tabchange', function(tabPanel, tab){
224 Ext.History.add(tabPanel.id + ':' + tab.id);
227 * @param {String} token The value that defines a particular application-specific history state
228 * @param {Boolean} preventDuplicates When true, if the passed token matches the current token
229 * it will not save a new history step. Set to false if the same state can be saved more than once
230 * at the same history stack location (defaults to true).
232 add: function (token, preventDup) {
235 if (preventDup !== false) {
236 if (me.getToken() === token) {
242 return me.updateIFrame(token);
244 window.top.location.hash = token;
250 * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
253 window.history.go(-1);
257 * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
260 window.history.go(1);
264 * Retrieves the currently-active history token.
265 * @return {String} The token
267 getToken: function() {
268 return this.ready ? this.currentToken : this.getHash();