-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathrouter-extensions.ts
133 lines (109 loc) · 4.8 KB
/
router-extensions.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
import { Injectable } from "@angular/core";
import { Router, UrlTree, NavigationExtras, ActivatedRoute } from "@angular/router";
import { NSLocationStrategy, NavigationOptions, Outlet } from "./ns-location-strategy";
import { FrameService } from "../platform-providers";
import { routerError } from "../trace";
import { findTopActivatedRouteNodeForOutlet } from "./page-router-outlet";
export type ExtendedNavigationExtras = NavigationExtras & NavigationOptions;
export interface BackNavigationOptions {
outlets?: Array<string>;
relativeTo?: ActivatedRoute | null;
}
@Injectable()
export class RouterExtensions {
constructor(
public router: Router,
public locationStrategy: NSLocationStrategy,
public frameService: FrameService
) { }
public navigate(commands: any[], extras?: ExtendedNavigationExtras): Promise<boolean> {
if (extras) {
this.locationStrategy._setNavigationOptions(extras);
}
return this.router.navigate(commands, extras);
}
public navigateByUrl(url: string | UrlTree, options?: NavigationOptions): Promise<boolean> {
if (options) {
this.locationStrategy._setNavigationOptions(options);
}
return this.router.navigateByUrl(url);
}
public back(backNavigationOptions?: BackNavigationOptions) {
if (backNavigationOptions) {
this.backOutlets(backNavigationOptions);
} else {
this.locationStrategy.back();
}
}
public canGoBack(backNavigationOptions?: BackNavigationOptions) {
let canGoBack = true;
if (backNavigationOptions) {
const { outletsToBack, outlets } = this.findOutletsToBack(backNavigationOptions);
if (outletsToBack.length !== outlets.length) {
routerError("No outlet found relative to activated route");
} else {
outletsToBack.forEach(outletToBack => {
if (!this.locationStrategy.canGoBack(outletToBack)) {
canGoBack = false;
}
});
}
} else {
canGoBack = this.locationStrategy.canGoBack();
}
return canGoBack;
}
public backToPreviousPage() {
this.frameService.getFrame().goBack();
}
public canGoBackToPreviousPage(): boolean {
return this.frameService.getFrame().canGoBack();
}
private backOutlets(options: BackNavigationOptions) {
const { outletsToBack, outlets } = this.findOutletsToBack(options);
if (outletsToBack.length !== outlets.length) {
routerError("No outlet found relative to activated route");
} else {
outletsToBack.forEach(outletToBack => {
if (outletToBack.isPageNavigationBack) {
routerError("Attempted to call startGoBack while going back:");
} else {
this.locationStrategy.back(outletToBack);
}
});
}
}
// tslint:disable-next-line:max-line-length
private findOutletsToBack(options?: BackNavigationOptions): { outletsToBack: Array<Outlet>, outlets: Array<string> } {
const outletsToBack: Array<Outlet> = [];
const rootRoute: ActivatedRoute = this.router.routerState.root;
let outlets = options.outlets;
let relativeRoute = options.relativeTo || rootRoute;
const relativeRouteOutlet = this.findOutletByRoute(relativeRoute);
const isNSEmptyOutlet = relativeRouteOutlet && relativeRouteOutlet.isNSEmptyOutlet();
// Lazy named outlet has added 'primary' inner NSEmptyOutlet child.
// Take parent route when `relativeTo` option points to the outer named outlet.
if (isNSEmptyOutlet && relativeRoute.outlet !== "primary") {
relativeRoute = relativeRoute.parent || relativeRoute;
}
const routesToMatch = outlets ? relativeRoute.children : [relativeRoute];
outlets = outlets || [relativeRoute.outlet];
for (let index = 0; index < routesToMatch.length; index++) {
const currentRoute = routesToMatch[index];
if (outlets.some(currentOutlet => currentOutlet === currentRoute.outlet)) {
let outlet = this.findOutletByRoute(currentRoute);
if (outlet) {
outletsToBack.push(outlet);
}
}
}
return { outletsToBack: outletsToBack, outlets: outlets };
}
private findOutletByRoute(currentRoute: ActivatedRoute): Outlet {
let outlet;
const currentRouteSnapshop = findTopActivatedRouteNodeForOutlet(currentRoute.snapshot);
const outletKey = this.locationStrategy.getRouteFullPath(currentRouteSnapshop);
outlet = this.locationStrategy.findOutletByKey(outletKey);
return outlet;
}
}