11import {
2- ReflectiveInjector , ComponentFactoryResolver , ViewContainerRef ,
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+
512import { Page } from "tns-core-modules/ui/page" ;
613import { View } from "tns-core-modules/ui/core/view" ;
14+
715import { DetachedLoader } from "../common/detached-loader" ;
816import { PageFactory , PAGE_FACTORY } from "../platform-providers" ;
917
1018export interface ModalDialogOptions {
1119 context ?: any ;
1220 fullscreen ?: boolean ;
1321 viewContainerRef ?: ViewContainerRef ;
22+ moduleRef ?: NgModuleRef < any > ;
1423}
1524
1625export class ModalDialogParams {
@@ -20,42 +29,61 @@ export class ModalDialogParams {
2029 }
2130}
2231
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+
2343@Injectable ( )
2444export class ModalDialogService {
25- public showModal ( type : Type < any > , options : ModalDialogOptions ) : Promise < any > {
26- if ( ! options . viewContainerRef ) {
45+ public showModal ( type : Type < any > ,
46+ { viewContainerRef, moduleRef, context, fullscreen} : ModalDialogOptions
47+ ) : Promise < any > {
48+ if ( ! viewContainerRef ) {
2749 throw new Error (
28- "No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions." ) ;
50+ "No viewContainerRef: " +
51+ "Make sure you pass viewContainerRef in ModalDialogOptions."
52+ ) ;
2953 }
3054
31- const viewContainerRef = options . viewContainerRef ;
3255 const parentPage : Page = viewContainerRef . injector . get ( Page ) ;
33- const resolver : ComponentFactoryResolver = viewContainerRef . injector . get (
34- ComponentFactoryResolver ) ;
3556 const pageFactory : PageFactory = viewContainerRef . injector . get ( PAGE_FACTORY ) ;
3657
37- return new Promise ( ( resolve ) => {
38- setTimeout ( ( ) => ModalDialogService . showDialog (
39- type ,
40- options ,
41- resolve ,
42- viewContainerRef ,
43- resolver ,
58+ // resolve from particular module (moduleRef)
59+ // or from same module as parentPage (viewContainerRef)
60+ const componentContainer = moduleRef || viewContainerRef ;
61+ const resolver = componentContainer . injector . get ( ComponentFactoryResolver ) ;
62+
63+ return new Promise ( resolve => {
64+ setTimeout ( ( ) => ModalDialogService . showDialog ( {
65+ containerRef : viewContainerRef ,
66+ context,
67+ doneCallback : resolve ,
68+ fullscreen,
69+ pageFactory,
4470 parentPage,
45- pageFactory
46- ) , 10 ) ;
71+ resolver,
72+ type,
73+ } ) , 10 ) ;
4774 } ) ;
4875 }
4976
50- private static showDialog (
51- type : Type < any > ,
52- options : ModalDialogOptions ,
77+ private static showDialog ( {
78+ containerRef ,
79+ context ,
5380 doneCallback,
54- containerRef : ViewContainerRef ,
55- resolver : ComponentFactoryResolver ,
56- parentPage : Page ,
57- pageFactory : PageFactory ) : void {
58-
81+ fullscreen,
82+ pageFactory,
83+ parentPage,
84+ resolver,
85+ type,
86+ } : ShowDialogOptions ) : void {
5987 const page = pageFactory ( { isModal : true , componentType : type } ) ;
6088
6189 let detachedLoaderRef : ComponentRef < DetachedLoader > ;
@@ -66,7 +94,7 @@ export class ModalDialogService {
6694 detachedLoaderRef . destroy ( ) ;
6795 } ;
6896
69- const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
97+ const modalParams = new ModalDialogParams ( context , closeCallback ) ;
7098
7199 const providers = ReflectiveInjector . resolve ( [
72100 { provide : Page , useValue : page } ,
@@ -85,7 +113,7 @@ export class ModalDialogService {
85113 }
86114
87115 page . content = componentView ;
88- parentPage . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
116+ parentPage . showModal ( page , context , closeCallback , fullscreen ) ;
89117 } ) ;
90118 }
91119}
@@ -96,7 +124,9 @@ export class ModalDialogService {
96124} )
97125export class ModalDialogHost { // tslint:disable-line:directive-class-suffix
98126 constructor ( ) {
99- throw new Error ( "ModalDialogHost is deprecated. Call ModalDialogService.showModal() " +
100- "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+ ) ;
101131 }
102132}
0 commit comments