Skip to content

Commit 4505d29

Browse files
committed
refactor(dialogs): pass show modal args as objects
1 parent 92fe69d commit 4505d29

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed
Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import {
2-
ReflectiveInjector, ComponentFactoryResolver, ViewContainerRef, NgModuleRef,
3-
Type, Injectable, ComponentRef, Directive
2+
ComponentFactoryResolver,
3+
ComponentRef,
4+
Directive,
5+
Injectable,
6+
NgModuleRef,
7+
ReflectiveInjector,
8+
Type,
9+
ViewContainerRef,
410
} from "@angular/core";
11+
512
import { Page } from "tns-core-modules/ui/page";
613
import { View } from "tns-core-modules/ui/core/view";
14+
715
import { DetachedLoader } from "../common/detached-loader";
816
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
917

@@ -21,44 +29,61 @@ export class ModalDialogParams {
2129
}
2230
}
2331

32+
interface ShowDialogOptions {
33+
containerRef: ViewContainerRef;
34+
context: any;
35+
doneCallback;
36+
fullscreen: boolean;
37+
pageFactory: PageFactory;
38+
parentPage: Page;
39+
resolver: ComponentFactoryResolver;
40+
type: Type<any>;
41+
}
42+
2443
@Injectable()
2544
export class ModalDialogService {
26-
public showModal(type: Type<any>, options: ModalDialogOptions): Promise<any> {
27-
if (!options.viewContainerRef) {
45+
public showModal(type: Type<any>,
46+
{viewContainerRef, moduleRef, context, fullscreen}: ModalDialogOptions
47+
): Promise<any> {
48+
if (!viewContainerRef) {
2849
throw new Error(
29-
"No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions.");
50+
"No viewContainerRef: " +
51+
"Make sure you pass viewContainerRef in ModalDialogOptions."
52+
);
3053
}
3154

32-
const viewContainerRef = options.viewContainerRef;
3355
const parentPage: Page = viewContainerRef.injector.get(Page);
56+
const pageFactory: PageFactory = viewContainerRef.injector.get(PAGE_FACTORY);
57+
3458
// resolve from particular module (moduleRef)
3559
// or from same module as parentPage (viewContainerRef)
36-
const resolver: ComponentFactoryResolver = (options.moduleRef || viewContainerRef).injector.get(
37-
ComponentFactoryResolver);
38-
const pageFactory: PageFactory = viewContainerRef.injector.get(PAGE_FACTORY);
60+
const componentContainer = moduleRef || viewContainerRef;
61+
const resolver = componentContainer.injector.get(ComponentFactoryResolver);
3962

40-
return new Promise((resolve) => {
41-
setTimeout(() => ModalDialogService.showDialog(
42-
type,
43-
options,
44-
resolve,
45-
viewContainerRef,
46-
resolver,
63+
return new Promise(resolve => {
64+
setTimeout(() => ModalDialogService.showDialog({
65+
containerRef: viewContainerRef,
66+
context,
67+
doneCallback: resolve,
68+
fullscreen,
69+
pageFactory,
4770
parentPage,
48-
pageFactory
49-
), 10);
71+
resolver,
72+
type,
73+
}), 10);
5074
});
5175
}
5276

53-
private static showDialog(
54-
type: Type<any>,
55-
options: ModalDialogOptions,
77+
private static showDialog({
78+
containerRef,
79+
context,
5680
doneCallback,
57-
containerRef: ViewContainerRef,
58-
resolver: ComponentFactoryResolver,
59-
parentPage: Page,
60-
pageFactory: PageFactory): void {
61-
81+
fullscreen,
82+
pageFactory,
83+
parentPage,
84+
resolver,
85+
type,
86+
}: ShowDialogOptions): void {
6287
const page = pageFactory({ isModal: true, componentType: type });
6388

6489
let detachedLoaderRef: ComponentRef<DetachedLoader>;
@@ -69,7 +94,7 @@ export class ModalDialogService {
6994
detachedLoaderRef.destroy();
7095
};
7196

72-
const modalParams = new ModalDialogParams(options.context, closeCallback);
97+
const modalParams = new ModalDialogParams(context, closeCallback);
7398

7499
const providers = ReflectiveInjector.resolve([
75100
{ provide: Page, useValue: page },
@@ -88,7 +113,7 @@ export class ModalDialogService {
88113
}
89114

90115
page.content = componentView;
91-
parentPage.showModal(page, options.context, closeCallback, options.fullscreen);
116+
parentPage.showModal(page, context, closeCallback, fullscreen);
92117
});
93118
}
94119
}
@@ -99,7 +124,9 @@ export class ModalDialogService {
99124
})
100125
export class ModalDialogHost { // tslint:disable-line:directive-class-suffix
101126
constructor() {
102-
throw new Error("ModalDialogHost is deprecated. Call ModalDialogService.showModal() " +
103-
"by passing ViewContainerRef in the options instead.");
127+
throw new Error("ModalDialogHost is deprecated. " +
128+
"Call ModalDialogService.showModal() " +
129+
"by passing ViewContainerRef in the options instead."
130+
);
104131
}
105132
}

0 commit comments

Comments
 (0)