History.html
9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-util-History'>/**
</span> * History management component that allows you to register arbitrary tokens that signify application
* history state on navigation actions. You can then handle the history {@link #change} event in order
* to reset your application UI to the appropriate state when the user navigates forward or backward through
* the browser history stack.
*
* ## Initializing
*
* The {@link #init} method of the History object must be called before using History. This sets up the internal
* state and must be the first thing called before using History.
*/
Ext.define('Ext.util.History', {
singleton: true,
alternateClassName: 'Ext.History',
mixins: {
observable: 'Ext.util.Observable'
},
<span id='Ext-util-History-property-useTopWindow'> /**
</span> * @property
* True to use `window.top.location.hash` or false to use `window.location.hash`.
*/
useTopWindow: true,
<span id='Ext-util-History-property-fieldId'> /**
</span> * @property
* The id of the hidden field required for storing the current history token.
*/
fieldId: Ext.baseCSSPrefix + 'history-field',
<span id='Ext-util-History-property-iframeId'> /**
</span> * @property
* The id of the iframe required by IE to manage the history stack.
*/
iframeId: Ext.baseCSSPrefix + 'history-frame',
constructor: function() {
var me = this;
me.oldIEMode = Ext.isIE6 || Ext.isIE7 || !Ext.isStrict && Ext.isIE8;
me.iframe = null;
me.hiddenField = null;
me.ready = false;
me.currentToken = null;
me.mixins.observable.constructor.call(me);
},
getHash: function() {
var href = window.location.href,
i = href.indexOf("#");
return i >= 0 ? href.substr(i + 1) : null;
},
setHash: function (hash) {
var me = this,
win = me.useTopWindow ? window.top : window;
try {
win.location.hash = hash;
} catch (e) {
// IE can give Access Denied (esp. in popup windows)
}
},
doSave: function() {
this.hiddenField.value = this.currentToken;
},
handleStateChange: function(token) {
this.currentToken = token;
this.fireEvent('change', token);
},
updateIFrame: function(token) {
var html = '<html><body><div id="state">' +
Ext.util.Format.htmlEncode(token) +
'</div></body></html>',
doc;
try {
doc = this.iframe.contentWindow.document;
doc.open();
doc.write(html);
doc.close();
return true;
} catch (e) {
return false;
}
},
checkIFrame: function () {
var me = this,
contentWindow = me.iframe.contentWindow,
doc, elem, oldToken, oldHash;
if (!contentWindow || !contentWindow.document) {
Ext.Function.defer(this.checkIFrame, 10, this);
return;
}
doc = contentWindow.document;
elem = doc.getElementById("state");
oldToken = elem ? elem.innerText : null;
oldHash = me.getHash();
Ext.TaskManager.start({
run: function () {
var doc = contentWindow.document,
elem = doc.getElementById("state"),
newToken = elem ? elem.innerText : null,
newHash = me.getHash();
if (newToken !== oldToken) {
oldToken = newToken;
me.handleStateChange(newToken);
me.setHash(newToken);
oldHash = newToken;
me.doSave();
} else if (newHash !== oldHash) {
oldHash = newHash;
me.updateIFrame(newHash);
}
},
interval: 50,
scope: me
});
me.ready = true;
me.fireEvent('ready', me);
},
startUp: function () {
var me = this,
hash;
me.currentToken = me.hiddenField.value || this.getHash();
if (me.oldIEMode) {
me.checkIFrame();
} else {
hash = me.getHash();
Ext.TaskManager.start({
run: function () {
var newHash = me.getHash();
if (newHash !== hash) {
hash = newHash;
me.handleStateChange(hash);
me.doSave();
}
},
interval: 50,
scope: me
});
me.ready = true;
me.fireEvent('ready', me);
}
},
<span id='Ext-util-History-method-init'> /**
</span> * Initializes the global History instance.
* @param {Function} [onReady] A callback function that will be called once the history
* component is fully initialized.
* @param {Object} [scope] The scope (`this` reference) in which the callback is executed.
* Defaults to the browser window.
*/
init: function (onReady, scope) {
var me = this,
DomHelper = Ext.DomHelper;
if (me.ready) {
Ext.callback(onReady, scope, [me]);
return;
}
if (!Ext.isReady) {
Ext.onReady(function() {
me.init(onReady, scope);
});
return;
}
/*
<form id="history-form" class="x-hide-display">
<input type="hidden" id="x-history-field" />
<iframe id="x-history-frame"></iframe>
</form>
*/
me.hiddenField = Ext.getDom(me.fieldId);
if (!me.hiddenField) {
me.hiddenField = Ext.getBody().createChild({
id: Ext.id(),
tag: 'form',
cls: Ext.baseCSSPrefix + 'hide-display',
children: [{
tag: 'input',
type: 'hidden',
id: me.fieldId
}]
}, false, true).firstChild;
}
if (me.oldIEMode) {
me.iframe = Ext.getDom(me.iframeId);
if (!me.iframe) {
me.iframe = DomHelper.append(me.hiddenField.parentNode, {
tag: 'iframe',
id: me.iframeId,
src: Ext.SSL_SECURE_URL
});
}
}
me.addEvents(
<span id='Ext-util-History-event-ready'> /**
</span> * @event ready
* Fires when the Ext.util.History singleton has been initialized and is ready for use.
* @param {Ext.util.History} The Ext.util.History singleton.
*/
'ready',
<span id='Ext-util-History-event-change'> /**
</span> * @event change
* Fires when navigation back or forwards within the local page's history occurs.
* @param {String} token An identifier associated with the page state at that point in its history.
*/
'change'
);
if (onReady) {
me.on('ready', onReady, scope, {single: true});
}
me.startUp();
},
<span id='Ext-util-History-method-add'> /**
</span> * Add a new token to the history stack. This can be any arbitrary value, although it would
* commonly be the concatenation of a component id and another id marking the specific history
* state of that component. Example usage:
*
* // Handle tab changes on a TabPanel
* tabPanel.on('tabchange', function(tabPanel, tab){
* Ext.History.add(tabPanel.id + ':' + tab.id);
* });
*
* @param {String} token The value that defines a particular application-specific history state
* @param {Boolean} [preventDuplicates=true] When true, if the passed token matches the current token
* it will not save a new history step. Set to false if the same state can be saved more than once
* at the same history stack location.
*/
add: function (token, preventDup) {
var me = this;
if (preventDup !== false) {
if (me.getToken() === token) {
return true;
}
}
if (me.oldIEMode) {
return me.updateIFrame(token);
} else {
me.setHash(token);
return true;
}
},
<span id='Ext-util-History-method-back'> /**
</span> * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
*/
back: function() {
window.history.go(-1);
},
<span id='Ext-util-History-method-forward'> /**
</span> * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
*/
forward: function(){
window.history.go(1);
},
<span id='Ext-util-History-method-getToken'> /**
</span> * Retrieves the currently-active history token.
* @return {String} The token
*/
getToken: function() {
return this.ready ? this.currentToken : this.getHash();
}
});</pre>
</body>
</html>