Skip to content

Commit 99f359c

Browse files
committed
feat: add lazy loaded modal and lazy routing tests
1 parent 66d496e commit 99f359c

20 files changed

+178
-202
lines changed

Diff for: app/app.css

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
button {
1+
@import '~/nativescript-theme-core/css/core.light.css';
2+
3+
button {
24
font-size: 13;
35
}
46

@@ -10,3 +12,4 @@ label {
1012
font-family: 'Courier'; /* Enabling this results in the error and shows a blank TabView */
1113
}
1214

15+

Diff for: app/app.module.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
1+
import { NgModule, NgModuleFactoryLoader, NO_ERRORS_SCHEMA } from "@angular/core";
22
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
3+
import { NSModuleFactoryLoader } from "nativescript-angular/router";
34

45
import { NavigationMainPageRouter } from "./main/main-page-router-outlet";
56
import { routableComponents, routes } from "./app.routes";
@@ -22,6 +23,12 @@ import { CustomTemplate } from "./list-view/list-view-item-template";
2223
NativeScriptRouterModule,
2324
NativeScriptRouterModule.forRoot(routes),
2425
],
26+
providers: [
27+
{
28+
provide: NgModuleFactoryLoader,
29+
useClass: NSModuleFactoryLoader
30+
}
31+
],
2532
schemas: [NO_ERRORS_SCHEMA],
2633
})
2734
export class AppModule { }

Diff for: app/app.routes.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { SecondComponentActionBar } from "./action-bar/action-bar-second.compone
55

66
import { AppComponent } from "./template/app.component";
77

8-
import { FirstComponent } from "./components/first.component";
9-
import { SecondComponent } from "./components/second.component";
10-
import { NavigationTestRouter, NavigationSubRoutes } from "./router/router-outlet";
8+
import { FirstComponent } from "./router/router-outlet/first.component";
9+
import { SecondComponent } from "./router/router-outlet/second.component";
10+
import { NavigationComponent, NavigationSubRoutes } from "./router/router-outlet/navigation.component";
11+
import { LazyNavigationComponent } from "./router/lazy-module-navigation/lazy-navigation.component";
1112

1213
import { BindingComponent } from "./binding/binding-page";
1314

@@ -22,6 +23,7 @@ import { ListPickerComponent } from "./list-picker/list-picker";
2223

2324
import { ModalTest, ModalTestWithPushStrategy, ModalContent } from "./modal/modal-dialogs/modal-dialog.component";
2425
import { ModalViewMainPageComponent } from "./modal/modal-view-main-page";
26+
import { LazyLoadModalComponent } from "./modal/lazy/lazy-load-modal.component";
2527

2628
import { TabViewComponent } from "./tab-view/tab-view.component";
2729

@@ -34,7 +36,8 @@ export const routableComponents = [
3436
ModalContent,
3537
AppComponent,
3638

37-
NavigationTestRouter,
39+
NavigationComponent,
40+
LazyNavigationComponent,
3841

3942
FirstComponent,
4043
SecondComponent,
@@ -56,6 +59,7 @@ export const routableComponents = [
5659
ModalViewMainPageComponent,
5760
ModalTest,
5861
ModalTestWithPushStrategy,
62+
LazyLoadModalComponent,
5963

6064
TabViewComponent,
6165

@@ -69,7 +73,8 @@ export const routes = [
6973
{ path: '', component: ModalContent, data: { title: "" } },
7074
{ path: 'template', component: AppComponent, data: { title: "Template", isNavigatable: true} },
7175

72-
{ path: 'router', component: NavigationTestRouter, children: NavigationSubRoutes, data: { title: "Router", isNavigatable: true} },
76+
{ path: 'router', component: NavigationComponent, children: NavigationSubRoutes, data: { title: "Router", isNavigatable: true} },
77+
{ path: 'lazy-router', component: LazyNavigationComponent, data: { title: "Lazy Router", isNavigatable: true} },
7378

7479
{ path: 'first', component: FirstComponent, data: { title: "First", isNavigatable: true} },
7580
{ path: 'second', component: SecondComponent, data: { title: "Second", isNavigatable: true} },
@@ -91,10 +96,17 @@ export const routes = [
9196
{ path: 'modal', component: ModalViewMainPageComponent, data: { title: "Modals", isNavigatable: true} },
9297
{ path: 'modal/modal-dialogs', component: ModalTest, data: { title: "modal" } },
9398
{ path: 'modal/modal-dialogs-push', component: ModalTestWithPushStrategy, data: { title: "modal(onPush)" } },
99+
{ path: 'modal/lazy', component: LazyLoadModalComponent, data: { title: "modal(lazy)" } },
94100

95101
{ path: 'tab-view', component: TabViewComponent, data: { title: "tab-view", isNavigatable: true } },
96102

97103
{ path: 'nav-options', component: NavigationOptionsComponent, data: { title: "nav-options", isNavigatable: true} },
98104
{ path: 'nav-info', component: NavigationInfoComponent, data: { title: "nav-info" } },
105+
106+
// Needed for AoT compilation
107+
{
108+
path: "lazy",
109+
loadChildren: "./lazy/lazy.module#LazyModule"
110+
},
99111
];
100112

Diff for: app/lazy/lazy.component.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<StackLayout>
2+
<GridLayout rows="auto" columns="auto" [visibility]="isModal ? 'visible' : 'collapsed'">
3+
<Button
4+
automationText="closeModal"
5+
text="Close Lazy Modal"
6+
(tap)="close()"
7+
class="btn action-item"
8+
row="0" col="0"></Button>
9+
</GridLayout>
10+
<StackLayout class="p-20">
11+
<Label
12+
automationText="lazyLoaded"
13+
[text]="'Lazily loaded via ' + (isModal ? 'modal' : 'route') + '!'"
14+
textWrap="true"
15+
class="h1"
16+
></Label>
17+
</StackLayout>
18+
</StackLayout>

Diff for: app/lazy/lazy.component.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Component } from "@angular/core";
2+
3+
import { RouterExtensions } from "nativescript-angular/router";
4+
import { ModalDialogParams } from "nativescript-angular/directives/dialogs";
5+
6+
@Component({
7+
selector: "ns-lazy",
8+
moduleId: module.id,
9+
templateUrl: "./lazy.component.html",
10+
})
11+
export class LazyComponent {
12+
public isModal: boolean;
13+
14+
constructor(
15+
private router: RouterExtensions,
16+
private params: ModalDialogParams
17+
) {
18+
if (params.context.isModal) {
19+
this.isModal = true;
20+
}
21+
}
22+
23+
public close() {
24+
if (this.isModal) {
25+
this.params.closeCallback();
26+
} else {
27+
this.router.back();
28+
}
29+
}
30+
}

Diff for: app/lazy/lazy.module.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
2+
import { NativeScriptRouterModule } from "nativescript-angular/router";
3+
import { ModalDialogParams } from "nativescript-angular/directives/dialogs";
4+
5+
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
6+
import { Routes } from "@angular/router";
7+
8+
import { LazyComponent } from "./lazy.component";
9+
10+
export function modalParamsFactory() {
11+
return new ModalDialogParams({}, null);
12+
}
13+
14+
const routes: Routes = [
15+
{
16+
path: "",
17+
component: LazyComponent
18+
}
19+
];
20+
21+
@NgModule({
22+
imports: [
23+
NativeScriptModule,
24+
NativeScriptRouterModule.forChild(routes),
25+
],
26+
declarations: [
27+
LazyComponent
28+
],
29+
entryComponents: [
30+
LazyComponent
31+
],
32+
providers: [
33+
// allows same component to be routed to
34+
// or lazily loaded via modal
35+
{ provide: ModalDialogParams, useFactory: modalParamsFactory }
36+
],
37+
schemas: [NO_ERRORS_SCHEMA]
38+
})
39+
export class LazyModule { }

Diff for: app/main/main-page-router-outlet.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class MainComponent {
1616

1717
constructor() {
1818
let navigatableRoutes = this._routes.filter((item) => {
19-
return item.data.isNavigatable == true && item.path != '';
19+
return item.data && item.data.isNavigatable && item.path;
2020
});
2121

2222
this._pages = navigatableRoutes;

Diff for: app/modal/lazy/lazy-load-modal.component.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
Component,
3+
ComponentFactory,
4+
NgModuleFactory,
5+
NgModuleFactoryLoader,
6+
ViewContainerRef,
7+
} from "@angular/core";
8+
9+
import { NSModuleFactoryLoader } from "nativescript-angular/router";
10+
import { ModalDialogService } from "nativescript-angular/directives/dialogs";
11+
12+
import { LazyComponent } from "../../lazy/lazy.component";
13+
14+
@Component({
15+
template: `
16+
<Button text="Load modal!" (tap)="openModal()"></Button>
17+
`
18+
})
19+
export class LazyLoadModalComponent {
20+
constructor(
21+
private moduleLoader: NgModuleFactoryLoader,
22+
private vcRef: ViewContainerRef,
23+
private modalService: ModalDialogService
24+
) { }
25+
26+
public openModal() {
27+
this.moduleLoader.load('./lazy/lazy.module#LazyModule')
28+
.then((module: NgModuleFactory<any>) => {
29+
const moduleRef = module.create(this.vcRef.parentInjector);
30+
31+
this.modalService.showModal(LazyComponent, {
32+
moduleRef,
33+
viewContainerRef: this.vcRef,
34+
context: { isModal: true }
35+
});
36+
});
37+
}
38+
}

Diff for: app/modal/modal-view-main-page.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Component } from "@angular/core";
66
<StackLayout>
77
<Button text="modal" [nsRouterLink]="['/modal','modal-dialogs']"></Button>
88
<Button text="modal(onPush)" [nsRouterLink]="['/modal','modal-dialogs-push']"></Button>
9+
<Button text="modal(lazy)" [nsRouterLink]="['/modal','lazy']"></Button>
910
</StackLayout>
1011
`,
1112
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from "@angular/core";
2+
3+
@Component({
4+
template: `<Button text="Lazy module" [nsRouterLink]="['/lazy']"></Button>`,
5+
})
6+
export class LazyNavigationComponent {
7+
}
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {Component} from "@angular/core";
2-
import {FirstComponent} from "../components/first.component";
3-
import {SecondComponent} from "../components/second.component";
1+
import { Component } from "@angular/core";
2+
import { FirstComponent } from "./first.component";
3+
import { SecondComponent } from "./second.component";
44

55
@Component({
66
moduleId: module.id,
7-
selector: 'navigation-test',
8-
styleUrls: ['./router-outlet.css'],
7+
selector: "navigation-test",
8+
styleUrls: ["./navigation.component.css"],
99
template: `
1010
<StackLayout>
1111
<StackLayout class="nav">
@@ -19,10 +19,10 @@ import {SecondComponent} from "../components/second.component";
1919
</StackLayout>
2020
`
2121
})
22-
export class NavigationTestRouter { }
22+
export class NavigationComponent { }
2323

2424
export var NavigationSubRoutes = [
25-
{ path: '', redirectTo: 'first', pathMatch: "full" },
26-
{ path: 'first', component: FirstComponent },
27-
{ path: 'second', component: SecondComponent },
25+
{ path: "", redirectTo: "first", pathMatch: "full" },
26+
{ path: "first", component: FirstComponent },
27+
{ path: "second", component: SecondComponent },
2828
];
File renamed without changes.

Diff for: app/vendor-platform.android.ts

-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
// Resolve JavaScript classes that extend a Java class, and need to resolve
2-
// their JavaScript module from a bundled script. For example:
3-
// NativeScriptApplication, NativeScriptActivity, etc.
4-
//
5-
// This module gets bundled together with the rest of the app code and the
6-
// `require` calls get resolved to the correct bundling import call.
7-
//
8-
// At runtime the module gets loaded *before* the rest of the app code, so code
9-
// placed here needs to be careful about its dependencies.
10-
111
require("application");
122
require("ui/frame");
133
require("ui/frame/activity");
14-
15-
if (global.TNS_WEBPACK) {
16-
global.__requireOverride = function (name, dir) {
17-
if (name === "./tns_modules/application/application.js") {
18-
return require("application");
19-
} else if (name === "./tns_modules/ui/frame/frame.js") {
20-
return require("ui/frame");
21-
} else if (name === "./tns_modules/ui/frame/activity.js") {
22-
return require("ui/frame/activity");
23-
}
24-
};
25-
}

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@angular/router": "~4.1.0",
2323
"nativescript-angular": "next",
2424
"nativescript-intl": "~3.0.0",
25+
"nativescript-theme-core": "^1.0.4",
2526
"reflect-metadata": "~0.1.8",
2627
"rxjs": "^5.0.1",
2728
"tns-core-modules": "next",
@@ -53,4 +54,4 @@
5354
"build-android-bundle": "npm run ns-bundle --android --build-app",
5455
"build-ios-bundle": "npm run ns-bundle --ios --build-app"
5556
}
56-
}
57+
}

Diff for: webpack.android.js

-2
This file was deleted.

0 commit comments

Comments
 (0)