| 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 | 1
1
1
1
1
2
2
2
2
2
10
10
10
1
1
1
1
1
| import {History} from 'aurelia-history';
import {LinkHandler, DefaultLinkHandler} from './link-handler';
import {DOM, PLATFORM} from 'aurelia-pal';
/**
* Configures the plugin by registering BrowserHistory as the implementation of History in the DI container.
* @param config The FrameworkConfiguration object provided by Aurelia.
*/
export function configure(config: Object): void {
config.singleton(History, BrowserHistory);
config.transient(LinkHandler, DefaultLinkHandler);
}
/**
* An implementation of the basic history API.
*/
export class BrowserHistory extends History {
static inject = [LinkHandler];
/**
* Creates an instance of BrowserHistory
* @param linkHandler An instance of LinkHandler.
*/
constructor(linkHandler: LinkHandler) {
super();
this._isActive = false;
this._checkUrlCallback = this._checkUrl.bind(this);
this.location = PLATFORM.location;
this.history = PLATFORM.history;
this.linkHandler = linkHandler;
}
/**
* Activates the history object.
*
* @param options The set of options to activate history with.
*/
activate(options?: Object): boolean {
if (this._isActive) {
throw new Error('History has already been activated.');
}
let wantsPushState = !!options.pushState;
this._isActive = true;
this.options = Object.assign({}, { root: '/' }, this.options, options);
// Normalize root to always include a leading and trailing slash.
this.root = ('/' + this.options.root + '/').replace(rootStripper, '/');
this._wantsHashChange = this.options.hashChange !== false;
this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState);
// Determine how we check the URL state.
let eventName;
if (this._hasPushState) {
eventName = 'popstate';
} else if (this._wantsHashChange) {
eventName = 'hashchange';
}
PLATFORM.addEventListener(eventName, this._checkUrlCallback);
// Determine if we need to change the base url, for a pushState link
// opened by a non-pushState browser.
if (this._wantsHashChange && wantsPushState) {
// Transition from hashChange to pushState or vice versa if both are requested.
let loc = this.location;
let atRoot = loc.pathname.replace(/[^\/]$/, '$&/') === this.root;
// If we've started off with a route from a `pushState`-enabled
// browser, but we're currently in a browser that doesn't support it...
if (!this._hasPushState && !atRoot) {
this.fragment = this._getFragment(null, true);
this.location.replace(this.root + this.location.search + '#' + this.fragment);
// Return immediately as browser will do redirect to new url
return true;
// Or if we've started out with a hash-based route, but we're currently
// in a browser where it could be `pushState`-based instead...
} else if (this._hasPushState && atRoot && loc.hash) {
this.fragment = this._getHash().replace(routeStripper, '');
this.history.replaceState({}, DOM.title, this.root + this.fragment + loc.search);
}
}
if (!this.fragment) {
this.fragment = this._getFragment();
}
this.linkHandler.activate(this);
if (!this.options.silent) {
return this._loadUrl();
}
}
/**
* Deactivates the history object.
*/
deactivate(): void {
PLATFORM.removeEventListener('popstate', this._checkUrlCallback);
PLATFORM.removeEventListener('hashchange', this._checkUrlCallback);
this._isActive = false;
this.linkHandler.deactivate();
}
/**
* Causes a history navigation to occur.
*
* @param fragment The history fragment to navigate to.
* @param options The set of options that specify how the navigation should occur.
* @return True if navigation occurred/false otherwise.
*/
navigate(fragment?: string, {trigger = true, replace = false} = {}): boolean {
if (fragment && absoluteUrl.test(fragment)) {
this.location.href = fragment;
return true;
}
if (!this._isActive) {
return false;
}
fragment = this._getFragment(fragment || '');
if (this.fragment === fragment && !replace) {
return false;
}
this.fragment = fragment;
let url = this.root + fragment;
// Don't include a trailing slash on the root.
if (fragment === '' && url !== '/') {
url = url.slice(0, -1);
}
// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
url = url.replace('//', '/');
this.history[replace ? 'replaceState' : 'pushState']({}, DOM.title, url);
} else if (this._wantsHashChange) {
// If hash changes haven't been explicitly disabled, update the hash
// fragment to store history.
updateHash(this.location, fragment, replace);
} else {
// If you've told us that you explicitly don't want fallback hashchange-
// based history, then `navigate` becomes a page refresh.
return this.location.assign(url);
}
if (trigger) {
return this._loadUrl(fragment);
}
}
/**
* Causes the history state to navigate back.
*/
navigateBack(): void {
this.history.back();
}
/**
* Sets the document title.
*/
setTitle(title: string): void {
DOM.title = title;
}
_getHash(): string {
return this.location.hash.substr(1);
}
_getFragment(fragment: string, forcePushState?: boolean): string {
let root;
Iif (!fragment) {
if (this._hasPushState || !this._wantsHashChange || forcePushState) {
fragment = this.location.pathname + this.location.search;
root = this.root.replace(trailingSlash, '');
if (!fragment.indexOf(root)) {
fragment = fragment.substr(root.length);
}
} else {
fragment = this._getHash();
}
}
return '/' + fragment.replace(routeStripper, '');
}
_checkUrl(): boolean {
let current = this._getFragment();
if (current !== this.fragment) {
this._loadUrl();
}
}
_loadUrl(fragmentOverride: string): boolean {
let fragment = this.fragment = this._getFragment(fragmentOverride);
return this.options.routeHandler ?
this.options.routeHandler(fragment) :
false;
}
}
// Cached regex for stripping a leading hash/slash and trailing space.
const routeStripper = /^#?\/*|\s+$/g;
// Cached regex for stripping leading and trailing slashes.
const rootStripper = /^\/+|\/+$/g;
// Cached regex for removing a trailing slash.
const trailingSlash = /\/$/;
// Cached regex for detecting if a URL is absolute,
// i.e., starts with a scheme or is scheme-relative.
// See http://www.ietf.org/rfc/rfc2396.txt section 3.1 for valid scheme format
const absoluteUrl = /^([a-z][a-z0-9+\-.]*:)?\/\//i;
// Update the hash location, either replacing the current entry, or adding
// a new one to the browser history.
function updateHash(location, fragment, replace) {
if (replace) {
let href = location.href.replace(/(javascript:|#).*$/, '');
location.replace(href + '#' + fragment);
} else {
// Some browsers require that `hash` contains a leading #.
location.hash = '#' + fragment;
}
}
|