Skip to content

Commit 5a88bc2

Browse files
authored
fix: restrict nested outlet back navigation scope (#54)
1 parent 26f3da8 commit 5a88bc2

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# @nativescript/angular 12+
1+
# @nativescript/angular
22

3-
For usage with NativeScript for Angular 12+ projects.
3+
For usage with NativeScript for Angular 12+ (13, etc.) projects.
44

55
Clean and setup workspace:
66

packages/angular/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# @nativescript/angular
22

3-
NativeScript for Angular v12+
3+
For usage with NativeScript for Angular 12+ (13, etc.) projects.

packages/angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/angular",
3-
"version": "13.0.1",
3+
"version": "13.0.2-alpha.0",
44
"homepage": "https://nativescript.org/",
55
"repository": {
66
"type": "git",

packages/angular/src/lib/legacy/router/ns-location-strategy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export class NSLocationStrategy extends LocationStrategy {
266266
}
267267

268268
// Methods for syncing with page navigation in PageRouterOutlet
269-
public _beginBackPageNavigation(frame: Frame) {
269+
public _beginBackPageNavigation(frame: Frame, outletKey: string) {
270270
const outlet: Outlet = this.getOutletByFrame(frame);
271271

272272
if (!outlet || outlet.isPageNavigationBack) {
@@ -279,7 +279,8 @@ export class NSLocationStrategy extends LocationStrategy {
279279
if (NativeScriptDebug.isLogEnabled()) {
280280
NativeScriptDebug.routerLog('NSLocationStrategy.startGoBack()');
281281
}
282-
outlet.isPageNavigationBack = true;
282+
outlet.setOutletKeyNavigatingBack(outletKey);
283+
// outlet.isPageNavigationBack = true;
283284
// we find all the children and also set their isPageNavigationBack
284285
this.outlets
285286
.filter((o) => {

packages/angular/src/lib/legacy/router/ns-location-utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export class Outlet {
6161
this.path = path;
6262
}
6363

64+
setOutletKeyNavigatingBack(key: string) {
65+
const nests = key.split('/');
66+
this.outletKeys
67+
.filter((k) => k.split('/').length >= nests.length)
68+
.forEach((k) => {
69+
this._navigatingBackOutlets.add(k);
70+
});
71+
}
72+
6473
containsFrame(frame: Frame): boolean {
6574
return this.frames.indexOf(frame) > -1;
6675
}

packages/angular/src/lib/legacy/router/page-router-outlet.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,12 @@ export class PageRouterOutlet implements OnDestroy {
327327
// Add it to the new page
328328
this.viewUtil.appendChild(page, componentView);
329329

330+
const topActivatedRoute = findTopActivatedRouteNodeForOutlet(this._activatedRoute.snapshot);
331+
const outletKey = this.locationStrategy.getRouteFullPath(topActivatedRoute);
332+
330333
const navigatedFromCallback = (<any>global).Zone.current.wrap((args: NavigatedData) => {
331334
if (args.isBackNavigation) {
332-
this.locationStrategy._beginBackPageNavigation(this.frame);
335+
this.locationStrategy._beginBackPageNavigation(this.frame, outletKey);
333336
this.locationStrategy.back(null, this.frame);
334337
}
335338
});

packages/angular/src/lib/legacy/router/router-extensions.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export class RouterExtensions {
8888

8989
// tslint:disable-next-line:max-line-length
9090
private findOutletsToBack(options?: BackNavigationOptions): { outletsToBack: Array<Outlet>; outlets: Array<string> } {
91-
const outletsToBack: Array<Outlet> = [];
9291
const rootRoute: ActivatedRoute = this.router.routerState.root;
9392
let outlets = options.outlets;
9493
let relativeRoute = options.relativeTo || rootRoute;
@@ -102,21 +101,37 @@ export class RouterExtensions {
102101
relativeRoute = relativeRoute.parent || relativeRoute;
103102
}
104103

105-
const routesToMatch = outlets ? relativeRoute.children : [relativeRoute];
106104
outlets = outlets || [relativeRoute.outlet];
107105

108-
for (let index = 0; index < routesToMatch.length; index++) {
109-
const currentRoute = routesToMatch[index];
110-
if (outlets.some((currentOutlet) => currentOutlet === currentRoute.outlet)) {
111-
const outlet = this.findOutletByRoute(currentRoute);
106+
const outletsToBack = this.findOutletsRecursive([...outlets], relativeRoute);
112107

113-
if (outlet) {
114-
outletsToBack.push(outlet);
115-
}
108+
return { outletsToBack: outletsToBack, outlets: outlets };
109+
}
110+
111+
// warning, outlets is mutable!
112+
private findOutletsRecursive(outlets: string[], route?: ActivatedRoute) {
113+
if (!route || outlets.length === 0) {
114+
return [];
115+
}
116+
const outletsToBack = [];
117+
if (outlets.some((currentOutlet) => currentOutlet === route.outlet)) {
118+
const outlet = this.findOutletByRoute(route);
119+
if (outlet) {
120+
outlets.splice(outlets.indexOf(route.outlet), 1);
121+
outletsToBack.push(outlet);
116122
}
117123
}
118-
119-
return { outletsToBack: outletsToBack, outlets: outlets };
124+
if (!route.children) {
125+
return outletsToBack;
126+
}
127+
for (let index = 0; index < route.children.length; index++) {
128+
if (outlets.length === 0) {
129+
break;
130+
}
131+
const currentRoute = route.children[index];
132+
outletsToBack.push(...this.findOutletsRecursive(outlets, currentRoute));
133+
}
134+
return outletsToBack;
120135
}
121136

122137
private findOutletByRoute(currentRoute: ActivatedRoute): Outlet {

0 commit comments

Comments
 (0)