forked from ngrx/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.ts
181 lines (160 loc) · 5.38 KB
/
location.ts
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
/**
* This is a fork of the Location service in Angular 2's Component Router.
* It exposes location updates as a BehaviorSubject, letting the router
* observe location changes.
*/
import { ReplaySubject } from 'rxjs/subject/ReplaySubject';
import { Subscriber } from 'rxjs/Subscriber';
import { LocationStrategy } from 'angular2/src/router/location/location_strategy';
import { UrlChangeEvent, PlatformLocation } from 'angular2/src/router/location/platform_location';
import { BrowserPlatformLocation } from 'angular2/src/router/location/browser_platform_location';
import { Injectable, Inject, Provider, provide } from 'angular2/core';
import { stringify as stringifyQueryParams } from 'query-string';
export interface LocationChange {
path: string;
type: 'push' | 'pop' | 'replace';
}
/**
* `Location` is a service that applications can use to interact with a browser's URL.
* Depending on which {@link LocationStrategy} is used, `Location` will either persist
* to the URL's path or the URL's hash segment.
*
* Note: it's better to use {@link Router#navigate} service to trigger route changes. Use
* `Location` only if you need to interact with or create normalized URLs outside of
* routing.
*
* `Location` is responsible for normalizing the URL against the application's base href.
* A normalized URL is absolute from the URL host, includes the application's base href, and has no
* trailing slash:
* - `/my/app/user/123` is normalized
* - `my/app/user/123` **is not** normalized
* - `/my/app/user/123/` **is not** normalized
*
* ### Example
*
* ```
* import {Component} from 'angular2/core';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig,
* Location
* } from 'angular2/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {...},
* ])
* class AppCmp {
* constructor(location: Location) {
* location.go('/foo');
* }
* }
*
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
* ```
*/
@Injectable()
export class Location extends ReplaySubject<LocationChange> {
private _baseHref: string;
constructor(public platformStrategy: LocationStrategy) {
super(1);
platformStrategy.onPopState(event => this._update('pop'));
const browserBaseHref = this.platformStrategy.getBaseHref();
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
this._update('push');
}
private _update(type: 'push' | 'pop' | 'replace') {
this.next({ path: this.path(), type });
}
/**
* Returns the normalized URL path.
*/
path(): string {
return this.normalize(this.platformStrategy.path());
}
/**
* Given a string representing a URL, returns the normalized URL path without leading or
* trailing slashes
*/
normalize(url: string): string {
return stripTrailingSlash(_stripBaseHref(this._baseHref, stripIndexHtml(url)));
}
/**
* Given a string representing a URL, returns the platform-specific external URL path.
* If the given URL doesn't begin with a leading slash (`'/'`), this method adds one
* before normalizing. This method will also add a hash if `HashLocationStrategy` is
* used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
*/
prepareExternalUrl(url: string, query: any = ''): string {
if (url.length > 0 && !url.startsWith('/')) {
url = '/' + url;
}
return this.platformStrategy.prepareExternalUrl(url + normalizeQueryParams(normalizeQuery(query)));
}
/**
* Changes the browsers URL to the normalized version of the given URL, and pushes a
* new item onto the platform's history.
*/
go(path: string, query: any = ''): void {
this.platformStrategy.pushState(null, '', path, normalizeQuery(query));
this._update('push');
}
/**
* Changes the browsers URL to the normalized version of the given URL, and replaces
* the top item on the platform's history stack.
*/
replaceState(path: string, query: any = ''): void {
this.platformStrategy.replaceState(null, '', path, normalizeQuery(query));
this._update('replace');
}
/**
* Changes the browsers query parameters. Replaces teh top item on the platform's
* history stack
*/
search(query: any = ''): void {
const [ pathname ] = this.path().split('?');
this.replaceState(pathname, query);
}
/**
* Navigates forward in the platform's history.
*/
forward(): void {
this.platformStrategy.forward();
}
/**
* Navigates back in the platform's history.
*/
back(): void {
this.platformStrategy.back();
}
}
function _stripBaseHref(baseHref: string, url: string): string {
if (baseHref.length > 0 && url.startsWith(baseHref)) {
return url.substring(baseHref.length);
}
return url;
}
function stripIndexHtml(url: string): string {
if (/\/index.html$/g.test(url)) {
// '/index.html'.length == 11
return url.substring(0, url.length - 11);
}
return url;
}
function stripTrailingSlash(url: string): string {
if (/\/$/g.test(url)) {
url = url.substring(0, url.length - 1);
}
return url;
}
function normalizeQuery(query: any) {
return typeof query === 'string' ? query : stringifyQueryParams(query);
}
function normalizeQueryParams(params: string): string {
return (params.length > 0 && params.substring(0, 1) !== '?') ? ('?' + params) : params;
}
export const LOCATION_PROVIDERS = [
provide(Location, { useClass: Location }),
provide(PlatformLocation, { useClass: BrowserPlatformLocation })
];