-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
router_link.ts
441 lines (415 loc) Β· 16 KB
/
router_link.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* @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 {LocationStrategy} from '@angular/common';
import {Attribute, Directive, ElementRef, HostBinding, HostListener, Input, OnChanges, OnDestroy, Renderer2, SimpleChanges} from '@angular/core';
import {Subject, Subscription} from 'rxjs';
import {QueryParamsHandling} from '../config';
import {Event, NavigationEnd} from '../events';
import {Router} from '../router';
import {ActivatedRoute} from '../router_state';
import {Params} from '../shared';
import {UrlTree} from '../url_tree';
/**
* @description
*
* When applied to an element in a template, makes that element a link
* that initiates navigation to a route. Navigation opens one or more routed components
* in one or more `<router-outlet>` locations on the page.
*
* Given a route configuration `[{ path: 'user/:name', component: UserCmp }]`,
* the following creates a static link to the route:
* `<a routerLink="/user/bob">link to user component</a>`
*
* You can use dynamic values to generate the link.
* For a dynamic link, pass an array of path segments,
* followed by the params for each segment.
* For example, `['/team', teamId, 'user', userName, {details: true}]`
* generates a link to `/team/11/user/bob;details=true`.
*
* Multiple static segments can be merged into one term and combined with dynamic segments.
* For example, `['/team/11/user', userName, {details: true}]`
*
* The input that you provide to the link is treated as a delta to the current URL.
* For instance, suppose the current URL is `/user/(box//aux:team)`.
* The link `<a [routerLink]="['/user/jim']">Jim</a>` creates the URL
* `/user/(jim//aux:team)`.
* See {@link Router#createUrlTree createUrlTree} for more information.
*
* @usageNotes
*
* You can use absolute or relative paths in a link, set query parameters,
* control how parameters are handled, and keep a history of navigation states.
*
* ### Relative link paths
*
* The first segment name can be prepended with `/`, `./`, or `../`.
* * If the first segment begins with `/`, the router looks up the route from the root of the
* app.
* * If the first segment begins with `./`, or doesn't begin with a slash, the router
* looks in the children of the current activated route.
* * If the first segment begins with `../`, the router goes up one level in the route tree.
*
* ### Setting and handling query params and fragments
*
* The following link adds a query parameter and a fragment to the generated URL:
*
* ```
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
* link to user component
* </a>
* ```
* By default, the directive constructs the new URL using the given query parameters.
* The example generates the link: `/user/bob?debug=true#education`.
*
* You can instruct the directive to handle query parameters differently
* by specifying the `queryParamsHandling` option in the link.
* Allowed values are:
*
* - `'merge'`: Merge the given `queryParams` into the current query params.
* - `'preserve'`: Preserve the current query params.
*
* For example:
*
* ```
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" queryParamsHandling="merge">
* link to user component
* </a>
* ```
*
* See {@link UrlCreationOptions.queryParamsHandling UrlCreationOptions#queryParamsHandling}.
*
* ### Preserving navigation history
*
* You can provide a `state` value to be persisted to the browser's
* [`History.state` property](https://developer.mozilla.org/en-US/docs/Web/API/History#Properties).
* For example:
*
* ```
* <a [routerLink]="['/user/bob']" [state]="{tracingId: 123}">
* link to user component
* </a>
* ```
*
* Use {@link Router.getCurrentNavigation() Router#getCurrentNavigation} to retrieve a saved
* navigation-state value. For example, to capture the `tracingId` during the `NavigationStart`
* event:
*
* ```
* // Get NavigationStart events
* router.events.pipe(filter(e => e instanceof NavigationStart)).subscribe(e => {
* const navigation = router.getCurrentNavigation();
* tracingService.trace({id: navigation.extras.state.tracingId});
* });
* ```
*
* @ngModule RouterModule
*
* @publicApi
*/
@Directive({selector: ':not(a):not(area)[routerLink]'})
export class RouterLink implements OnChanges {
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#queryParams UrlCreationOptions#queryParams}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() queryParams?: Params|null;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#fragment UrlCreationOptions#fragment}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() fragment?: string;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#queryParamsHandling UrlCreationOptions#queryParamsHandling}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() queryParamsHandling?: QueryParamsHandling|null;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#preserveFragment UrlCreationOptions#preserveFragment}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
// TODO(issue/24571): remove '!'.
@Input() preserveFragment!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#skipLocationChange NavigationBehaviorOptions#skipLocationChange}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
// TODO(issue/24571): remove '!'.
@Input() skipLocationChange!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#replaceUrl NavigationBehaviorOptions#replaceUrl}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
// TODO(issue/24571): remove '!'.
@Input() replaceUrl!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#state NavigationBehaviorOptions#state}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
@Input() state?: {[k: string]: any};
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* Specify a value here when you do not want to use the default value
* for `routerLink`, which is the current activated route.
* Note that a value of `undefined` here will use the `routerLink` default.
* @see {@link UrlCreationOptions#relativeTo UrlCreationOptions#relativeTo}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() relativeTo?: ActivatedRoute|null;
private commands: any[]|null = null;
/** @internal */
onChanges = new Subject<RouterLink>();
constructor(
private router: Router, private route: ActivatedRoute,
@Attribute('tabindex') private readonly tabIndexAttribute: string|null|undefined,
private readonly renderer: Renderer2, private readonly el: ElementRef) {
this.setTabIndexIfNotOnNativeEl('0');
}
/**
* Modifies the tab index if there was not a tabindex attribute on the element during
* instantiation.
*/
private setTabIndexIfNotOnNativeEl(newTabIndex: string|null) {
if (this.tabIndexAttribute != null /* both `null` and `undefined` */) {
return;
}
const renderer = this.renderer;
const nativeElement = this.el.nativeElement;
if (newTabIndex !== null) {
renderer.setAttribute(nativeElement, 'tabindex', newTabIndex);
} else {
renderer.removeAttribute(nativeElement, 'tabindex');
}
}
/** @nodoc */
ngOnChanges(changes: SimpleChanges) {
// This is subscribed to by `RouterLinkActive` so that it knows to update when there are changes
// to the RouterLinks it's tracking.
this.onChanges.next(this);
}
/**
* Commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
* - **array**: commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
* - **string**: shorthand for array of commands with just the string, i.e. `['/route']`
* - **null|undefined**: effectively disables the `routerLink`
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input()
set routerLink(commands: any[]|string|null|undefined) {
if (commands != null) {
this.commands = Array.isArray(commands) ? commands : [commands];
this.setTabIndexIfNotOnNativeEl('0');
} else {
this.commands = null;
this.setTabIndexIfNotOnNativeEl(null);
}
}
/** @nodoc */
@HostListener('click')
onClick(): boolean {
if (this.urlTree === null) {
return true;
}
const extras = {
skipLocationChange: attrBoolValue(this.skipLocationChange),
replaceUrl: attrBoolValue(this.replaceUrl),
state: this.state,
};
this.router.navigateByUrl(this.urlTree, extras);
return true;
}
get urlTree(): UrlTree|null {
if (this.commands === null) {
return null;
}
return this.router.createUrlTree(this.commands, {
// If the `relativeTo` input is not defined, we want to use `this.route` by default.
// Otherwise, we should use the value provided by the user in the input.
relativeTo: this.relativeTo !== undefined ? this.relativeTo : this.route,
queryParams: this.queryParams,
fragment: this.fragment,
queryParamsHandling: this.queryParamsHandling,
preserveFragment: attrBoolValue(this.preserveFragment),
});
}
}
/**
* @description
*
* Lets you link to specific routes in your app.
*
* See `RouterLink` for more information.
*
* @ngModule RouterModule
*
* @publicApi
*/
@Directive({selector: 'a[routerLink],area[routerLink]'})
export class RouterLinkWithHref implements OnChanges, OnDestroy {
// TODO(issue/24571): remove '!'.
@HostBinding('attr.target') @Input() target!: string;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#queryParams UrlCreationOptions#queryParams}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() queryParams?: Params|null;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#fragment UrlCreationOptions#fragment}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() fragment?: string;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#queryParamsHandling UrlCreationOptions#queryParamsHandling}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() queryParamsHandling?: QueryParamsHandling|null;
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* @see {@link UrlCreationOptions#preserveFragment UrlCreationOptions#preserveFragment}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
// TODO(issue/24571): remove '!'.
@Input() preserveFragment!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#skipLocationChange NavigationBehaviorOptions#skipLocationChange}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
// TODO(issue/24571): remove '!'.
@Input() skipLocationChange!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#replaceUrl NavigationBehaviorOptions#replaceUrl}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
// TODO(issue/24571): remove '!'.
@Input() replaceUrl!: boolean;
/**
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
* `NavigationBehaviorOptions`.
* @see {@link NavigationBehaviorOptions#state NavigationBehaviorOptions#state}
* @see {@link Router#navigateByUrl Router#navigateByUrl}
*/
@Input() state?: {[k: string]: any};
/**
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the
* `UrlCreationOptions`.
* Specify a value here when you do not want to use the default value
* for `routerLink`, which is the current activated route.
* Note that a value of `undefined` here will use the `routerLink` default.
* @see {@link UrlCreationOptions#relativeTo UrlCreationOptions#relativeTo}
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input() relativeTo?: ActivatedRoute|null;
private commands: any[]|null = null;
private subscription: Subscription;
// the url displayed on the anchor element.
// @HostBinding('attr.href') is used rather than @HostBinding() because it removes the
// href attribute when it becomes `null`.
@HostBinding('attr.href') href: string|null = null;
/** @internal */
onChanges = new Subject<RouterLinkWithHref>();
constructor(
private router: Router, private route: ActivatedRoute,
private locationStrategy: LocationStrategy) {
this.subscription = router.events.subscribe((s: Event) => {
if (s instanceof NavigationEnd) {
this.updateTargetUrlAndHref();
}
});
}
/**
* Commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
* - **array**: commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
* - **string**: shorthand for array of commands with just the string, i.e. `['/route']`
* - **null|undefined**: Disables the link by removing the `href`
* @see {@link Router#createUrlTree Router#createUrlTree}
*/
@Input()
set routerLink(commands: any[]|string|null|undefined) {
if (commands != null) {
this.commands = Array.isArray(commands) ? commands : [commands];
} else {
this.commands = null;
}
}
/** @nodoc */
ngOnChanges(changes: SimpleChanges): any {
this.updateTargetUrlAndHref();
this.onChanges.next(this);
}
/** @nodoc */
ngOnDestroy(): any {
this.subscription.unsubscribe();
}
/** @nodoc */
@HostListener(
'click',
['$event.button', '$event.ctrlKey', '$event.shiftKey', '$event.altKey', '$event.metaKey'])
onClick(button: number, ctrlKey: boolean, shiftKey: boolean, altKey: boolean, metaKey: boolean):
boolean {
if (button !== 0 || ctrlKey || shiftKey || altKey || metaKey) {
return true;
}
if (typeof this.target === 'string' && this.target != '_self' || this.urlTree === null) {
return true;
}
const extras = {
skipLocationChange: attrBoolValue(this.skipLocationChange),
replaceUrl: attrBoolValue(this.replaceUrl),
state: this.state
};
this.router.navigateByUrl(this.urlTree, extras);
return false;
}
private updateTargetUrlAndHref(): void {
this.href = this.urlTree !== null ?
this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.urlTree)) :
null;
}
get urlTree(): UrlTree|null {
if (this.commands === null) {
return null;
}
return this.router.createUrlTree(this.commands, {
// If the `relativeTo` input is not defined, we want to use `this.route` by default.
// Otherwise, we should use the value provided by the user in the input.
relativeTo: this.relativeTo !== undefined ? this.relativeTo : this.route,
queryParams: this.queryParams,
fragment: this.fragment,
queryParamsHandling: this.queryParamsHandling,
preserveFragment: attrBoolValue(this.preserveFragment),
});
}
}
function attrBoolValue(s: any): boolean {
return s === '' || !!s;
}