Skip to content

Commit e632fc5

Browse files
authored
fix(router): handle nested primary p-r-o (#1645)
* fix(router): handle nested primary p-r-o * tests(e2e): test nested primary p-r-o navigation * chore: fix getFrameToBack return displacement
1 parent 5eb3a7e commit e632fc5

13 files changed

+63
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<GridLayout rows="auto">
2+
<Label text="Nested About Component"></Label>
3+
</GridLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from "@angular/core";
2+
3+
@Component({
4+
moduleId: module.id,
5+
selector: "about-nested-page",
6+
templateUrl: "./about-nested.component.html"
7+
})
8+
export class AboutNestedComponent {
9+
}

Diff for: e2e/nested-router-tab-view/app/about/about.component.html

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
<GridLayout rows="auto, *">
66
<Button text="Back(ActivatedRoute)" automationText="Back(ActivatedRoute)" (tap)="backActivatedRoute()"></Button>
7+
<GridLayout row="1">
8+
<page-router-outlet actionBarVisibility="never"></page-router-outlet>
9+
</GridLayout>
710
</GridLayout>

Diff for: e2e/nested-router-tab-view/app/about/about.component.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Component, ViewContainerRef } from "@angular/core";
2-
import { ModalDialogService, ModalDialogOptions } from "nativescript-angular/directives/dialogs";
1+
import { Component } from "@angular/core";
32
import { RouterExtensions } from "nativescript-angular/router";
43
import { ActivatedRoute } from "@angular/router";
54

@@ -10,8 +9,6 @@ import { ActivatedRoute } from "@angular/router";
109
})
1110
export class AboutComponent {
1211
constructor(
13-
private modal: ModalDialogService,
14-
private vcRef: ViewContainerRef,
1512
private activeRoute: ActivatedRoute,
1613
private routerExtension: RouterExtensions) { }
1714

Diff for: e2e/nested-router-tab-view/app/app.component.html

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
<page-router-outlet tag="mainPRO"></page-router-outlet>
2-
3-
<!-- <TabView androidTabsPosition="bottom">
4-
<page-router-outlet
5-
*tabItem="{title: 'Players'}"
6-
name="playerTab">
7-
</page-router-outlet>
8-
9-
<page-router-outlet
10-
*tabItem="{title: 'Teams'}"
11-
name="teamTab">
12-
</page-router-outlet>
13-
</TabView> -->
1+
<page-router-outlet></page-router-outlet>

Diff for: e2e/nested-router-tab-view/app/app.routing.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { LoginComponent } from "./login/login.component";
1010
import { TabsComponent } from "./tabs/tabs.component";
1111
import { HomeComponent } from "./home/home.component";
1212
import { AboutComponent } from "./about/about.component";
13+
import { AboutNestedComponent } from "./about/about-nested.component";
1314

1415
import { ModalComponent } from "./modal/modal.component";
1516
import { NestedModalComponent } from "./modal-nested/modal-nested.component";
@@ -56,7 +57,11 @@ const routes: Routes = [
5657
{ path: "team/:id", component: TeamDetailComponent, outlet: "teamTab" },
5758
]
5859
},
59-
{ path: "about", component: AboutComponent }
60+
{
61+
path: "about", component: AboutComponent, children: [
62+
{ path: "about-nested", component: AboutNestedComponent },
63+
]
64+
}
6065
];
6166

6267
@NgModule({

Diff for: e2e/nested-router-tab-view/app/home/home.component.html

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<GridLayout row="1" columns="*,*">
2929
<GridLayout col="0">
3030
<page-router-outlet name="playerTab"></page-router-outlet>
31-
<!-- <page-router-outlet></page-router-outlet> -->
3231
</GridLayout>
3332
<GridLayout col="1">
3433
<page-router-outlet name="teamTab"></page-router-outlet>

Diff for: e2e/nested-router-tab-view/app/shared.module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DataService } from "./data.service";
88

99
import { HomeComponent } from "./home/home.component";
1010
import { AboutComponent } from "./about/about.component";
11+
import { AboutNestedComponent } from "./about/about-nested.component";
1112
import { PlayerComponent } from "./player/players.component";
1213
import { PlayerDetailComponent } from "./player/player-detail.component";
1314
import { TeamsComponent } from "./team/teams.component";
@@ -21,6 +22,7 @@ import { TeamDetailComponent } from "./team/team-detail.component";
2122
declarations: [
2223
HomeComponent,
2324
AboutComponent,
25+
AboutNestedComponent,
2426
PlayerComponent,
2527
PlayerDetailComponent,
2628
TeamsComponent,
@@ -29,6 +31,7 @@ import { TeamDetailComponent } from "./team/team-detail.component";
2931
exports: [
3032
HomeComponent,
3133
AboutComponent,
34+
AboutNestedComponent,
3235
PlayerComponent,
3336
PlayerDetailComponent,
3437
TeamsComponent,

Diff for: e2e/nested-router-tab-view/app/tabs/tabs.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<StackLayout>
44
<GridLayout rows="auto,*">
55
<WrapLayout row="0">
6-
<Button text="Go To About Page" [nsRouterLink]="['../about']"></Button>
6+
<Button text="Go To About Page" [nsRouterLink]="['../about/about-nested']"></Button>
77
<Button text="Back()" (tap)="back()"></Button>
88
<Button text="CanGoBack(ActivatedRoute)" (tap)="canGoBack()"></Button>
99
<Button text="Back(ActivatedRoute)" (tap)="backActivatedRoute()"></Button>

Diff for: e2e/nested-router-tab-view/e2e/home-tabs.e2e-spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe("home-tabs:", () => {
4848
await screen.loadedPlayersList();
4949
await screen.navigateToAboutPage();
5050
await screen.loadedAbout();
51+
await screen.loadedNestedAbout();
5152
});
5253

5354
it("should go back to Tabs and then back to Home", async () => {

Diff for: e2e/nested-router-tab-view/e2e/screen.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert } from "chai";
33

44
const home = "Home Component";
55
const about = "About Component";
6+
const aboutNested = "Nested About Component";
67
const login = "Login Component";
78
const tabs = "Tabs Component";
89

@@ -70,7 +71,13 @@ export class Screen {
7071
loadedAbout= async () => {
7172
const lblAbout = await this._driver.findElementByAutomationText(about);
7273
assert.isTrue(await lblAbout.isDisplayed());
73-
console.log(home + " loaded!");
74+
console.log(about + " loaded!");
75+
}
76+
77+
loadedNestedAbout= async () => {
78+
const lblAboutNested = await this._driver.findElementByAutomationText(aboutNested);
79+
assert.isTrue(await lblAboutNested.isDisplayed());
80+
console.log(aboutNested + " loaded!");
7481
}
7582

7683
loadedTabs = async () => {

Diff for: nativescript-angular/router/ns-location-strategy.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export class Outlet {
5353
const lastState = this.peekState();
5454
return lastState && lastState.segmentGroup.toString() === stateUrl;
5555
}
56+
57+
// Search for frame that can go back.
58+
// Nested 'primary' outlets could result in Outlet with multiple navigatable frames.
59+
getFrameToBack(): Frame {
60+
let frame = this.frames[this.frames.length - 1];
61+
62+
if (!this.isNSEmptyOutlet) {
63+
for (let index = this.frames.length - 1; index >= 0; index--) {
64+
const currentFrame = this.frames[index];
65+
if (currentFrame.canGoBack()) {
66+
frame = currentFrame;
67+
break;
68+
}
69+
}
70+
}
71+
72+
return frame;
73+
}
5674
}
5775

5876
export interface NavigationOptions {
@@ -251,7 +269,7 @@ export class NSLocationStrategy extends LocationStrategy {
251269
const topmostFrame = this.frameService.getFrame();
252270
this.currentOutlet = this.getOutletByFrame(topmostFrame);
253271
}
254-
this.currentOutlet.frames[this.currentOutlet.frames.length - 1].goBack();
272+
this.currentOutlet.getFrameToBack().goBack();
255273
} else {
256274
// Nested navigation - just pop the state
257275
if (isLogEnabled()) {
@@ -448,6 +466,12 @@ export class NSLocationStrategy extends LocationStrategy {
448466
if (currentOutlet.containsFrame(frame) && !isEqualToCurrent) {
449467
this.callPopState(null, true, currentOutlet);
450468
}
469+
470+
if (!currentOutlet.isNSEmptyOutlet) {
471+
currentOutlet.frames = currentOutlet.frames.filter(currentFrame => currentFrame !== frame);
472+
return currentOutlet.frames.length;
473+
}
474+
451475
return !currentOutlet.containsFrame(frame);
452476
});
453477
}

Diff for: nativescript-angular/router/page-router-outlet.ts

+2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
423423
if (modalNavigation > 0) { // Modal with 'primary' p-r-o
424424
outlet = this.locationStrategy.findOutletByModal(modalNavigation);
425425
} else {
426+
const pathByOutlets = this.locationStrategy.getPathByOutlets(topActivatedRoute);
426427
outlet = this.locationStrategy.findOutletByKey(outletKey);
428+
outlet = outlet || this.locationStrategy.findOutletByOutletPath(pathByOutlets);
427429
}
428430

429431
// Named lazy loaded outlet.

0 commit comments

Comments
 (0)