/
platform_location.ts
210 lines (182 loc) · 5.8 KB
/
platform_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
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Inject, Injectable, InjectionToken, ɵɵinject} from '@angular/core';
import {getDOM} from '../dom_adapter';
import {DOCUMENT} from '../dom_tokens';
/**
* This class should not be used directly by an application developer. Instead, use
* {@link Location}.
*
* `PlatformLocation` encapsulates all calls to DOM APIs, which allows the Router to be
* platform-agnostic.
* This means that we can have different implementation of `PlatformLocation` for the different
* platforms that Angular supports. For example, `@angular/platform-browser` provides an
* implementation specific to the browser environment, while `@angular/platform-server` provides
* one suitable for use with server-side rendering.
*
* The `PlatformLocation` class is used directly by all implementations of {@link LocationStrategy}
* when they need to interact with the DOM APIs like pushState, popState, etc.
*
* {@link LocationStrategy} in turn is used by the {@link Location} service which is used directly
* by the {@link Router} in order to navigate between routes. Since all interactions between {@link
* Router} /
* {@link Location} / {@link LocationStrategy} and DOM APIs flow through the `PlatformLocation`
* class, they are all platform-agnostic.
*
* @publicApi
*/
@Injectable({
providedIn: 'platform',
// See #23917
useFactory: useBrowserPlatformLocation
})
export abstract class PlatformLocation {
abstract getBaseHrefFromDOM(): string;
abstract getState(): unknown;
/**
* Returns a function that, when executed, removes the `popstate` event handler.
*/
abstract onPopState(fn: LocationChangeListener): VoidFunction;
/**
* Returns a function that, when executed, removes the `hashchange` event handler.
*/
abstract onHashChange(fn: LocationChangeListener): VoidFunction;
abstract get href(): string;
abstract get protocol(): string;
abstract get hostname(): string;
abstract get port(): string;
abstract get pathname(): string;
abstract get search(): string;
abstract get hash(): string;
abstract replaceState(state: any, title: string, url: string): void;
abstract pushState(state: any, title: string, url: string): void;
abstract forward(): void;
abstract back(): void;
historyGo?(relativePosition: number): void {
throw new Error('Not implemented');
}
}
export function useBrowserPlatformLocation() {
return ɵɵinject(BrowserPlatformLocation);
}
/**
* @description
* Indicates when a location is initialized.
*
* @publicApi
*/
export const LOCATION_INITIALIZED = new InjectionToken<Promise<any>>('Location Initialized');
/**
* @description
* A serializable version of the event from `onPopState` or `onHashChange`
*
* @publicApi
*/
export interface LocationChangeEvent {
type: string;
state: any;
}
/**
* @publicApi
*/
export interface LocationChangeListener {
(event: LocationChangeEvent): any;
}
/**
* `PlatformLocation` encapsulates all of the direct calls to platform APIs.
* This class should not be used directly by an application developer. Instead, use
* {@link Location}.
*/
@Injectable({
providedIn: 'platform',
// See #23917
useFactory: createBrowserPlatformLocation,
})
export class BrowserPlatformLocation extends PlatformLocation {
public readonly location!: Location;
private _history!: History;
constructor(@Inject(DOCUMENT) private _doc: any) {
super();
this._init();
}
// This is moved to its own method so that `MockPlatformLocationStrategy` can overwrite it
/** @internal */
_init() {
(this as {location: Location}).location = window.location;
this._history = window.history;
}
override getBaseHrefFromDOM(): string {
return getDOM().getBaseHref(this._doc)!;
}
override onPopState(fn: LocationChangeListener): VoidFunction {
const window = getDOM().getGlobalEventTarget(this._doc, 'window');
window.addEventListener('popstate', fn, false);
return () => window.removeEventListener('popstate', fn);
}
override onHashChange(fn: LocationChangeListener): VoidFunction {
const window = getDOM().getGlobalEventTarget(this._doc, 'window');
window.addEventListener('hashchange', fn, false);
return () => window.removeEventListener('hashchange', fn);
}
override get href(): string {
return this.location.href;
}
override get protocol(): string {
return this.location.protocol;
}
override get hostname(): string {
return this.location.hostname;
}
override get port(): string {
return this.location.port;
}
override get pathname(): string {
return this.location.pathname;
}
override get search(): string {
return this.location.search;
}
override get hash(): string {
return this.location.hash;
}
override set pathname(newPath: string) {
this.location.pathname = newPath;
}
override pushState(state: any, title: string, url: string): void {
if (supportsState()) {
this._history.pushState(state, title, url);
} else {
this.location.hash = url;
}
}
override replaceState(state: any, title: string, url: string): void {
if (supportsState()) {
this._history.replaceState(state, title, url);
} else {
this.location.hash = url;
}
}
override forward(): void {
this._history.forward();
}
override back(): void {
this._history.back();
}
override historyGo(relativePosition: number = 0): void {
this._history.go(relativePosition);
}
override getState(): unknown {
return this._history.state;
}
}
export function supportsState(): boolean {
return !!window.history.pushState;
}
export function createBrowserPlatformLocation() {
return new BrowserPlatformLocation(ɵɵinject(DOCUMENT));
}