-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
history_location.js
272 lines (214 loc) · 5.81 KB
/
history_location.js
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
import {
get,
set
} from 'ember-metal';
import { Object as EmberObject } from 'ember-runtime';
import EmberLocation from './api';
/**
@module ember
@submodule ember-routing
*/
let popstateFired = false;
function _uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = c === 'x' ? r : r & 3 | 8;
return v.toString(16);
});
}
/**
Ember.HistoryLocation implements the location API using the browser's
history.pushState API.
@class HistoryLocation
@namespace Ember
@extends Ember.Object
@private
*/
export default EmberObject.extend({
implementation: 'history',
init() {
this._super(...arguments);
let base = document.querySelector('base');
let baseURL = '';
if (base) {
baseURL = base.getAttribute('href');
}
set(this, 'baseURL', baseURL);
set(this, 'location', get(this, 'location') || window.location);
this._popstateHandler = undefined;
},
/**
Used to set state on first call to setURL
@private
@method initState
*/
initState() {
let history = get(this, 'history') || window.history;
set(this, 'history', history);
if (history && 'state' in history) {
this.supportsHistory = true;
}
this.replaceState(this.formatURL(this.getURL()));
},
/**
Will be pre-pended to path upon state change
@property rootURL
@default '/'
@private
*/
rootURL: '/',
/**
Returns the current `location.pathname` without `rootURL` or `baseURL`
@private
@method getURL
@return url {String}
*/
getURL() {
let location = get(this, 'location');
let path = location.pathname;
let rootURL = get(this, 'rootURL');
let baseURL = get(this, 'baseURL');
// remove trailing slashes if they exists
rootURL = rootURL.replace(/\/$/, '');
baseURL = baseURL.replace(/\/$/, '');
// remove baseURL and rootURL from start of path
let url = path
.replace(new RegExp(`^${baseURL}(?=/|$)`), '')
.replace(new RegExp(`^${rootURL}(?=/|$)`), '')
.replace(/\/\/$/g,'/'); // remove extra slashes
let search = location.search || '';
url += search + this.getHash();
return url;
},
/**
Uses `history.pushState` to update the url without a page reload.
@private
@method setURL
@param path {String}
*/
setURL(path) {
let state = this.getState();
path = this.formatURL(path);
if (!state || state.path !== path) {
this.pushState(path);
}
},
/**
Uses `history.replaceState` to update the url without a page reload
or history modification.
@private
@method replaceURL
@param path {String}
*/
replaceURL(path) {
let state = this.getState();
path = this.formatURL(path);
if (!state || state.path !== path) {
this.replaceState(path);
}
},
/**
Get the current `history.state`. Checks for if a polyfill is
required and if so fetches this._historyState. The state returned
from getState may be null if an iframe has changed a window's
history.
The object returned will contain a `path` for the given state as well
as a unique state `id`. The state index will allow the app to distinguish
between two states with similar paths but should be unique from one another.
@private
@method getState
@return state {Object}
*/
getState() {
if (this.supportsHistory) {
return get(this, 'history').state;
}
return this._historyState;
},
/**
Pushes a new state.
@private
@method pushState
@param path {String}
*/
pushState(path) {
let state = { path, uuid: _uuid() };
get(this, 'history').pushState(state, null, path);
this._historyState = state;
// used for webkit workaround
this._previousURL = this.getURL();
},
/**
Replaces the current state.
@private
@method replaceState
@param path {String}
*/
replaceState(path) {
let state = { path, uuid: _uuid() };
get(this, 'history').replaceState(state, null, path);
this._historyState = state;
// used for webkit workaround
this._previousURL = this.getURL();
},
/**
Register a callback to be invoked whenever the browser
history changes, including using forward and back buttons.
@private
@method onUpdateURL
@param callback {Function}
*/
onUpdateURL(callback) {
this._removeEventListener();
this._popstateHandler = () => {
// Ignore initial page load popstate event in Chrome
if (!popstateFired) {
popstateFired = true;
if (this.getURL() === this._previousURL) { return; }
}
callback(this.getURL());
};
window.addEventListener('popstate', this._popstateHandler);
},
/**
Used when using `{{action}}` helper. The url is always appended to the rootURL.
@private
@method formatURL
@param url {String}
@return formatted url {String}
*/
formatURL(url) {
let rootURL = get(this, 'rootURL');
let baseURL = get(this, 'baseURL');
if (url !== '') {
// remove trailing slashes if they exists
rootURL = rootURL.replace(/\/$/, '');
baseURL = baseURL.replace(/\/$/, '');
} else if (baseURL[0] === '/' && rootURL[0] === '/') {
// if baseURL and rootURL both start with a slash
// ... remove trailing slash from baseURL if it exists
baseURL = baseURL.replace(/\/$/, '');
}
return baseURL + rootURL + url;
},
/**
Cleans up the HistoryLocation event listener.
@private
@method willDestroy
*/
willDestroy() {
this._removeEventListener();
},
/**
@private
Returns normalized location.hash
@method getHash
*/
getHash: EmberLocation._getHash,
_removeEventListener() {
if (this._popstateHandler) {
window.removeEventListener('popstate', this._popstateHandler);
}
}
});