4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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>
43 Ext.define('Ext.util.History', {
45 alternateClassName: 'Ext.History',
47 observable: 'Ext.util.Observable'
50 constructor: function() {
52 me.oldIEMode = Ext.isIE6 || Ext.isIE7 || !Ext.isStrict && Ext.isIE8;
54 me.hiddenField = null;
56 me.currentToken = null;
60 var href = window.location.href,
61 i = href.indexOf("#");
63 return i >= 0 ? href.substr(i + 1) : null;
67 this.hiddenField.value = this.currentToken;
71 handleStateChange: function(token) {
72 this.currentToken = token;
73 this.fireEvent('change', token);
76 updateIFrame: function(token) {
77 var html = '<html><body><div id="state">' +
78 Ext.util.Format.htmlEncode(token) +
79 '</div></body></html>';
82 var doc = this.iframe.contentWindow.document;
92 checkIFrame: function () {
94 contentWindow = me.iframe.contentWindow;
96 if (!contentWindow || !contentWindow.document) {
97 Ext.Function.defer(this.checkIFrame, 10, this);
101 var doc = contentWindow.document,
102 elem = doc.getElementById("state"),
103 oldToken = elem ? elem.innerText : null,
104 oldHash = me.getHash();
106 Ext.TaskManager.start({
108 var doc = contentWindow.document,
109 elem = doc.getElementById("state"),
110 newToken = elem ? elem.innerText : null,
111 newHash = me.getHash();
113 if (newToken !== oldToken) {
115 me.handleStateChange(newToken);
116 window.top.location.hash = newToken;
119 } else if (newHash !== oldHash) {
121 me.updateIFrame(newHash);
128 me.fireEvent('ready', me);
131 startUp: function () {
134 me.currentToken = me.hiddenField.value || this.getHash();
139 var hash = me.getHash();
140 Ext.TaskManager.start({
142 var newHash = me.getHash();
143 if (newHash !== hash) {
145 me.handleStateChange(hash);
153 me.fireEvent('ready', me);
158 <span id='Ext-util-History-property-fieldId'> /**
159 </span> * The id of the hidden field required for storing the current history token.
163 fieldId: Ext.baseCSSPrefix + 'history-field',
164 <span id='Ext-util-History-property-iframeId'> /**
165 </span> * The id of the iframe required by IE to manage the history stack.
169 iframeId: Ext.baseCSSPrefix + 'history-frame',
171 <span id='Ext-util-History-method-init'> /**
172 </span> * Initialize the global History instance.
173 * @param {Boolean} onReady (optional) A callback function that will be called once the history
174 * component is fully initialized.
175 * @param {Object} scope (optional) The scope (`this` reference) in which the callback is executed. Defaults to the browser window.
177 init: function (onReady, scope) {
181 Ext.callback(onReady, scope, [me]);
186 Ext.onReady(function() {
187 me.init(onReady, scope);
192 me.hiddenField = Ext.getDom(me.fieldId);
195 me.iframe = Ext.getDom(me.iframeId);
199 <span id='Ext-util-History-event-ready'> /**
200 </span> * @event ready
201 * Fires when the Ext.util.History singleton has been initialized and is ready for use.
202 * @param {Ext.util.History} The Ext.util.History singleton.
205 <span id='Ext-util-History-event-change'> /**
206 </span> * @event change
207 * Fires when navigation back or forwards within the local page's history occurs.
208 * @param {String} token An identifier associated with the page state at that point in its history.
214 me.on('ready', onReady, scope, {single: true});
219 <span id='Ext-util-History-method-add'> /**
220 </span> * Add a new token to the history stack. This can be any arbitrary value, although it would
221 * commonly be the concatenation of a component id and another id marking the specific history
222 * state of that component. Example usage:
224 * // Handle tab changes on a TabPanel
225 * tabPanel.on('tabchange', function(tabPanel, tab){
226 * Ext.History.add(tabPanel.id + ':' + tab.id);
229 * @param {String} token The value that defines a particular application-specific history state
230 * @param {Boolean} [preventDuplicates=true] When true, if the passed token matches the current token
231 * it will not save a new history step. Set to false if the same state can be saved more than once
232 * at the same history stack location.
234 add: function (token, preventDup) {
237 if (preventDup !== false) {
238 if (me.getToken() === token) {
244 return me.updateIFrame(token);
246 window.top.location.hash = token;
251 <span id='Ext-util-History-method-back'> /**
252 </span> * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
255 window.history.go(-1);
258 <span id='Ext-util-History-method-forward'> /**
259 </span> * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
262 window.history.go(1);
265 <span id='Ext-util-History-method-getToken'> /**
266 </span> * Retrieves the currently-active history token.
267 * @return {String} The token
269 getToken: function() {
270 return this.ready ? this.currentToken : this.getHash();