[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.History |
---|
| 9 | * @extends Ext.util.Observable |
---|
| 10 | * History management component that allows you to register arbitrary tokens that signify application |
---|
| 11 | * history state on navigation actions. You can then handle the history {@link #change} event in order |
---|
| 12 | * to reset your application UI to the appropriate state when the user navigates forward or backward through |
---|
| 13 | * the browser history stack. |
---|
| 14 | * @singleton |
---|
| 15 | */ |
---|
| 16 | Ext.History = (function () { |
---|
| 17 | var iframe, hiddenField; |
---|
| 18 | var ready = false; |
---|
| 19 | var currentToken; |
---|
| 20 | |
---|
| 21 | function getHash() { |
---|
| 22 | var href = location.href, i = href.indexOf("#"), |
---|
| 23 | hash = i >= 0 ? href.substr(i + 1) : null; |
---|
| 24 | |
---|
| 25 | if (Ext.isGecko) { |
---|
| 26 | hash = decodeURIComponent(hash); |
---|
| 27 | } |
---|
| 28 | return hash; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | function doSave() { |
---|
| 32 | hiddenField.value = currentToken; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | function handleStateChange(token) { |
---|
| 36 | currentToken = token; |
---|
| 37 | Ext.History.fireEvent('change', token); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | function updateIFrame (token) { |
---|
| 41 | var html = ['<html><body><div id="state">',Ext.util.Format.htmlEncode(token),'</div></body></html>'].join(''); |
---|
| 42 | try { |
---|
| 43 | var doc = iframe.contentWindow.document; |
---|
| 44 | doc.open(); |
---|
| 45 | doc.write(html); |
---|
| 46 | doc.close(); |
---|
| 47 | return true; |
---|
| 48 | } catch (e) { |
---|
| 49 | return false; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | function checkIFrame() { |
---|
| 54 | if (!iframe.contentWindow || !iframe.contentWindow.document) { |
---|
| 55 | setTimeout(checkIFrame, 10); |
---|
| 56 | return; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | var doc = iframe.contentWindow.document; |
---|
| 60 | var elem = doc.getElementById("state"); |
---|
| 61 | var token = elem ? elem.innerText : null; |
---|
| 62 | |
---|
| 63 | var hash = getHash(); |
---|
| 64 | |
---|
| 65 | setInterval(function () { |
---|
| 66 | |
---|
| 67 | doc = iframe.contentWindow.document; |
---|
| 68 | elem = doc.getElementById("state"); |
---|
| 69 | |
---|
| 70 | var newtoken = elem ? elem.innerText : null; |
---|
| 71 | |
---|
| 72 | var newHash = getHash(); |
---|
| 73 | |
---|
| 74 | if (newtoken !== token) { |
---|
| 75 | token = newtoken; |
---|
| 76 | handleStateChange(token); |
---|
| 77 | location.hash = token; |
---|
| 78 | hash = token; |
---|
| 79 | doSave(); |
---|
| 80 | } else if (newHash !== hash) { |
---|
| 81 | hash = newHash; |
---|
| 82 | updateIFrame(newHash); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | }, 50); |
---|
| 86 | |
---|
| 87 | ready = true; |
---|
| 88 | |
---|
| 89 | Ext.History.fireEvent('ready', Ext.History); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | function startUp() { |
---|
| 93 | currentToken = hiddenField.value ? hiddenField.value : getHash(); |
---|
| 94 | |
---|
| 95 | if (Ext.isIE) { |
---|
| 96 | checkIFrame(); |
---|
| 97 | } else { |
---|
| 98 | var hash = getHash(); |
---|
| 99 | setInterval(function () { |
---|
| 100 | var newHash = getHash(); |
---|
| 101 | if (newHash !== hash) { |
---|
| 102 | hash = newHash; |
---|
| 103 | handleStateChange(hash); |
---|
| 104 | doSave(); |
---|
| 105 | } |
---|
| 106 | }, 50); |
---|
| 107 | ready = true; |
---|
| 108 | Ext.History.fireEvent('ready', Ext.History); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | return { |
---|
| 113 | /** |
---|
| 114 | * The id of the hidden field required for storing the current history token. |
---|
| 115 | * @type String |
---|
| 116 | * @property |
---|
| 117 | */ |
---|
| 118 | fieldId: 'x-history-field', |
---|
| 119 | /** |
---|
| 120 | * The id of the iframe required by IE to manage the history stack. |
---|
| 121 | * @type String |
---|
| 122 | * @property |
---|
| 123 | */ |
---|
| 124 | iframeId: 'x-history-frame', |
---|
| 125 | |
---|
| 126 | events:{}, |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Initialize the global History instance. |
---|
| 130 | * @param {Boolean} onReady (optional) A callback function that will be called once the history |
---|
| 131 | * component is fully initialized. |
---|
| 132 | * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser window. |
---|
| 133 | */ |
---|
| 134 | init: function (onReady, scope) { |
---|
| 135 | if(ready) { |
---|
| 136 | Ext.callback(onReady, scope, [this]); |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | if(!Ext.isReady){ |
---|
| 140 | Ext.onReady(function(){ |
---|
| 141 | Ext.History.init(onReady, scope); |
---|
| 142 | }); |
---|
| 143 | return; |
---|
| 144 | } |
---|
| 145 | hiddenField = Ext.getDom(Ext.History.fieldId); |
---|
| 146 | if (Ext.isIE) { |
---|
| 147 | iframe = Ext.getDom(Ext.History.iframeId); |
---|
| 148 | } |
---|
| 149 | this.addEvents( |
---|
| 150 | /** |
---|
| 151 | * @event ready |
---|
| 152 | * Fires when the Ext.History singleton has been initialized and is ready for use. |
---|
| 153 | * @param {Ext.History} The Ext.History singleton. |
---|
| 154 | */ |
---|
| 155 | 'ready', |
---|
| 156 | /** |
---|
| 157 | * @event change |
---|
| 158 | * Fires when navigation back or forwards within the local page's history occurs. |
---|
| 159 | * @param {String} token An identifier associated with the page state at that point in its history. |
---|
| 160 | */ |
---|
| 161 | 'change' |
---|
| 162 | ); |
---|
| 163 | if(onReady){ |
---|
| 164 | this.on('ready', onReady, scope, {single:true}); |
---|
| 165 | } |
---|
| 166 | startUp(); |
---|
| 167 | }, |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Add a new token to the history stack. This can be any arbitrary value, although it would |
---|
| 171 | * commonly be the concatenation of a component id and another id marking the specifc history |
---|
| 172 | * state of that component. Example usage: |
---|
| 173 | * <pre><code> |
---|
| 174 | // Handle tab changes on a TabPanel |
---|
| 175 | tabPanel.on('tabchange', function(tabPanel, tab){ |
---|
| 176 | Ext.History.add(tabPanel.id + ':' + tab.id); |
---|
| 177 | }); |
---|
| 178 | </code></pre> |
---|
| 179 | * @param {String} token The value that defines a particular application-specific history state |
---|
| 180 | * @param {Boolean} preventDuplicates When true, if the passed token matches the current token |
---|
| 181 | * it will not save a new history step. Set to false if the same state can be saved more than once |
---|
| 182 | * at the same history stack location (defaults to true). |
---|
| 183 | */ |
---|
| 184 | add: function (token, preventDup) { |
---|
| 185 | if(preventDup !== false){ |
---|
| 186 | if(this.getToken() == token){ |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | if (Ext.isIE) { |
---|
| 191 | return updateIFrame(token); |
---|
| 192 | } else { |
---|
| 193 | location.hash = token; |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | }, |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button). |
---|
| 200 | */ |
---|
| 201 | back: function(){ |
---|
| 202 | history.go(-1); |
---|
| 203 | }, |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button). |
---|
| 207 | */ |
---|
| 208 | forward: function(){ |
---|
| 209 | history.go(1); |
---|
| 210 | }, |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * Retrieves the currently-active history token. |
---|
| 214 | * @return {String} The token |
---|
| 215 | */ |
---|
| 216 | getToken: function() { |
---|
| 217 | return ready ? currentToken : getHash(); |
---|
| 218 | } |
---|
| 219 | }; |
---|
| 220 | })(); |
---|
| 221 | Ext.apply(Ext.History, new Ext.util.Observable()); |
---|