1
1
import {
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 ,
4
10
} from "@angular/core" ;
11
+
5
12
import { Page } from "tns-core-modules/ui/page" ;
6
13
import { View } from "tns-core-modules/ui/core/view" ;
14
+
7
15
import { DetachedLoader } from "../common/detached-loader" ;
8
16
import { PageFactory , PAGE_FACTORY } from "../platform-providers" ;
9
17
10
18
export interface ModalDialogOptions {
11
19
context ?: any ;
12
20
fullscreen ?: boolean ;
13
21
viewContainerRef ?: ViewContainerRef ;
22
+ moduleRef ?: NgModuleRef < any > ;
14
23
}
15
24
16
25
export class ModalDialogParams {
@@ -20,42 +29,61 @@ export class ModalDialogParams {
20
29
}
21
30
}
22
31
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
+
23
43
@Injectable ( )
24
44
export 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 ) {
27
49
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
+ ) ;
29
53
}
30
54
31
- const viewContainerRef = options . viewContainerRef ;
32
55
const parentPage : Page = viewContainerRef . injector . get ( Page ) ;
33
- const resolver : ComponentFactoryResolver = viewContainerRef . injector . get (
34
- ComponentFactoryResolver ) ;
35
56
const pageFactory : PageFactory = viewContainerRef . injector . get ( PAGE_FACTORY ) ;
36
57
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,
44
70
parentPage,
45
- pageFactory
46
- ) , 10 ) ;
71
+ resolver,
72
+ type,
73
+ } ) , 10 ) ;
47
74
} ) ;
48
75
}
49
76
50
- private static showDialog (
51
- type : Type < any > ,
52
- options : ModalDialogOptions ,
77
+ private static showDialog ( {
78
+ containerRef ,
79
+ context ,
53
80
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 {
59
87
const page = pageFactory ( { isModal : true , componentType : type } ) ;
60
88
61
89
let detachedLoaderRef : ComponentRef < DetachedLoader > ;
@@ -66,7 +94,7 @@ export class ModalDialogService {
66
94
detachedLoaderRef . destroy ( ) ;
67
95
} ;
68
96
69
- const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
97
+ const modalParams = new ModalDialogParams ( context , closeCallback ) ;
70
98
71
99
const providers = ReflectiveInjector . resolve ( [
72
100
{ provide : Page , useValue : page } ,
@@ -85,7 +113,7 @@ export class ModalDialogService {
85
113
}
86
114
87
115
page . content = componentView ;
88
- parentPage . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
116
+ parentPage . showModal ( page , context , closeCallback , fullscreen ) ;
89
117
} ) ;
90
118
}
91
119
}
@@ -96,7 +124,9 @@ export class ModalDialogService {
96
124
} )
97
125
export class ModalDialogHost { // tslint:disable-line:directive-class-suffix
98
126
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
+ ) ;
101
131
}
102
132
}
0 commit comments