Skip to content

Commit 6a28263

Browse files
committed
fix: clean up properly shared modal page router outlets
1 parent 62919b1 commit 6a28263

14 files changed

+382
-87
lines changed

Diff for: e2e/modal-navigation-ng/app/app.component.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import { Component } from "@angular/core";
1+
import { Component, ViewContainerRef } from "@angular/core";
22
import { Router, NavigationEnd } from "@angular/router";
33
import { NSLocationStrategy } from "nativescript-angular/router/ns-location-strategy";
44

5+
import { ViewContainerRefService } from "./shared/ViewContainerRefService";
6+
57
@Component({
68
selector: "ns-app",
79
templateUrl: "app.component.html",
810
})
911

1012
export class AppComponent {
11-
constructor(router: Router, location: NSLocationStrategy) {
13+
constructor(
14+
router: Router,
15+
location: NSLocationStrategy,
16+
private _vcRef: ViewContainerRef,
17+
private _viewContainerRefService: ViewContainerRefService) {
1218
router.events.subscribe(e => {
13-
// console.log("[ROUTER]: " + e.toString());
14-
1519
if (e instanceof NavigationEnd) {
1620
console.log("[ROUTER]: " + e.toString());
1721
console.log(location.toString());
1822
}
1923
});
24+
25+
this._viewContainerRefService.root = this._vcRef;
2026
}
2127
}

Diff for: e2e/modal-navigation-ng/app/app.module.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { ItemsComponent } from "./item/items.component";
88
import { ItemDetailComponent } from "./item/item-detail.component";
99
import { HomeComponent } from "./home/home.component";
1010
import { ModalTest } from "./modal/modal-test";
11+
import { ModalViewComponent } from "./modal-shared/modal-view.component";
12+
import { ModalViewContentComponent } from "./modal-shared/modal-view-content.component";
13+
import { ModalSharedSecondComponent } from "./modal-shared/modal-shared-second.component";
14+
import { ViewContainerRefService } from "./shared/ViewContainerRefService";
1115

1216
// Uncomment and add to NgModule imports if you need to use two-way binding
1317
// import { NativeScriptFormsModule } from "nativescript-angular/forms";
@@ -23,17 +27,24 @@ import { ModalTest } from "./modal/modal-test";
2327
NativeScriptModule,
2428
AppRoutingModule
2529
],
26-
entryComponents: [...ModalTest.entries],
30+
entryComponents: [
31+
ModalViewComponent,
32+
...ModalTest.entries
33+
],
2734
declarations: [
2835
AppComponent,
2936
ItemsComponent,
3037
ItemDetailComponent,
3138
HomeComponent,
39+
ModalViewComponent,
40+
ModalViewContentComponent,
3241
ModalTest,
42+
ModalSharedSecondComponent,
3343
...ModalTest.entries
3444
],
3545
providers: [
36-
ItemService
46+
ItemService,
47+
ViewContainerRefService
3748
],
3849
schemas: [
3950
NO_ERRORS_SCHEMA

Diff for: e2e/modal-navigation-ng/app/app.routing.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,29 @@ import { HomeComponent } from "./home/home.component";
66
import { ItemsComponent } from "./item/items.component";
77
import { ItemDetailComponent } from "./item/item-detail.component";
88
import { ModalTest } from "./modal/modal-test";
9+
import { ModalViewContentComponent } from "./modal-shared/modal-view-content.component";
10+
import { ModalSharedSecondComponent } from "./modal-shared/modal-shared-second.component";
911

1012
const routes: Routes = [
1113
{ path: "", redirectTo: "/home", pathMatch: "full" },
1214

1315
{ path: "home", component: HomeComponent },
1416

15-
{ path: "modal", component: ModalTest , children: [
16-
{ path: "items", component: ItemsComponent },
17-
{ path: "item/:id", component: ItemDetailComponent },
18-
] },
17+
{
18+
path: "modal", component: ModalTest,
19+
children: [
20+
{ path: "items", component: ItemsComponent },
21+
{ path: "item/:id", component: ItemDetailComponent },
22+
]
23+
},
24+
25+
{
26+
path: "modal-shared", component: ModalViewContentComponent, outlet: "modalOutlet"
27+
},
28+
29+
{
30+
path: "modal-shared-second-host", component: ModalSharedSecondComponent
31+
}
1932
];
2033

2134
@NgModule({

Diff for: e2e/modal-navigation-ng/app/home/home.component.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import { Component, ViewContainerRef } from "@angular/core";
2-
import { RouterExtensions } from "nativescript-angular/router";
1+
import { Component } from "@angular/core";
2+
import { ModalDialogService, ModalDialogOptions } from "nativescript-angular/modal-dialog";
3+
4+
import { ViewContainerRefService } from "../shared/ViewContainerRefService";
5+
import { ModalViewComponent } from "../modal-shared/modal-view.component";
36

47
@Component({
58
selector: "modal-test",
69
template: `
7-
<GridLayout rows="*, auto">
8-
<Button text="got to modal page" (tap)="gotToModal()"></Button>
10+
<GridLayout rows="auto, auto, auto, *">
11+
<Button text="go to modal test page" [nsRouterLink]="['/modal']"></Button>
12+
<Button row="1" text="show shared modal" (tap)="onRootModalTap()"></Button>
13+
<Button row="2" text="go to second (to open shared modal)" [nsRouterLink]="['/modal-shared-second-host']"></Button>
14+
<Label row="3" text="home component" verticalAlignment="bottom"></Label>
915
</GridLayout>
1016
`
1117
})
1218
export class HomeComponent {
19+
constructor(
20+
private _modalService: ModalDialogService,
21+
private _viewContainerRefService: ViewContainerRefService
22+
) { }
1323

14-
public result: string = "result";
15-
16-
constructor(private _routerExtensions: RouterExtensions, private vcRef: ViewContainerRef) { }
24+
onRootModalTap(): void {
25+
const options: ModalDialogOptions = {
26+
viewContainerRef: this._viewContainerRefService.root,
27+
context: {},
28+
fullscreen: true
29+
};
1730

18-
public gotToModal() {
19-
this._routerExtensions.navigate(["/modal"]); //{clearHistory: true}
31+
this._modalService.showModal(ModalViewComponent, options)
32+
.then((result: string) => {
33+
console.log(result);
34+
});
2035
}
2136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Component } from "@angular/core";
2+
import { ModalDialogOptions, ModalDialogService } from "nativescript-angular/modal-dialog";
3+
4+
import { ViewContainerRefService } from "../shared/ViewContainerRefService";
5+
import { ModalViewComponent } from "../modal-shared/modal-view.component";
6+
import { RouterExtensions } from "nativescript-angular/router";
7+
8+
@Component({
9+
selector: "ns-second",
10+
moduleId: module.id,
11+
template: `
12+
<StackLayout class="page">
13+
<Button text="show shared modal" (tap)="onRootModalTap()"></Button>
14+
<Button text="go back" (tap)="onBackTap()"></Button>
15+
<Label text="second component"></Label>
16+
</StackLayout>
17+
`
18+
})
19+
export class ModalSharedSecondComponent {
20+
constructor(
21+
private _modalService: ModalDialogService,
22+
private _viewContainerRefService: ViewContainerRefService,
23+
private _routerExtensions: RouterExtensions
24+
) { }
25+
26+
onRootModalTap(): void {
27+
const options: ModalDialogOptions = {
28+
viewContainerRef: this._viewContainerRefService.root,
29+
context: {},
30+
fullscreen: true
31+
};
32+
33+
this._modalService.showModal(ModalViewComponent, options)
34+
.then((result: string) => {
35+
console.log(result);
36+
});
37+
}
38+
39+
onBackTap() {
40+
if (this._routerExtensions.canGoBack()) {
41+
this._routerExtensions.back();
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Component } from "@angular/core";
2+
import { ModalDialogParams } from "nativescript-angular/modal-dialog";
3+
4+
@Component({
5+
selector: "ModalViewContent",
6+
moduleId: module.id,
7+
template: `
8+
<ActionBar title="SHARED MODAL VIEW" class="action-bar">
9+
</ActionBar>
10+
11+
<StackLayout class="page">
12+
<Button text="close modal" (tap)="onTap()"></Button>
13+
</StackLayout>
14+
`,
15+
styles: [`
16+
.action-bar, .page {
17+
background-color: chocolate;
18+
}
19+
`]
20+
})
21+
export class ModalViewContentComponent {
22+
constructor(private _params: ModalDialogParams) { }
23+
24+
onTap(): void {
25+
this._params.closeCallback("return value");
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, OnInit } from "@angular/core";
2+
import { RouterExtensions } from "nativescript-angular/router";
3+
4+
@Component({
5+
selector: "ModalView",
6+
moduleId: module.id,
7+
template:`
8+
<page-router-outlet name="modalOutlet"></page-router-outlet>
9+
`
10+
})
11+
export class ModalViewComponent implements OnInit {
12+
constructor(private _routerExtensions: RouterExtensions) {}
13+
14+
ngOnInit(): void {
15+
this._routerExtensions.navigate([{ outlets: { modalOutlet: ["modal-shared"]}}]);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Injectable, ViewContainerRef } from "@angular/core";
2+
3+
@Injectable()
4+
export class ViewContainerRefService {
5+
private _rootViewContainerRef: ViewContainerRef;
6+
7+
get root():ViewContainerRef {
8+
return this._rootViewContainerRef;
9+
}
10+
11+
set root(viewContainerRef: ViewContainerRef) {
12+
this._rootViewContainerRef = viewContainerRef;
13+
}
14+
}

0 commit comments

Comments
 (0)