6
6
NgModuleRef ,
7
7
ReflectiveInjector ,
8
8
Type ,
9
- ViewContainerRef ,
9
+ ViewContainerRef
10
10
} from "@angular/core" ;
11
11
12
12
import { NSLocationStrategy } from "../router/ns-location-strategy" ;
@@ -18,14 +18,15 @@ import { DetachedLoader } from "../common/detached-loader";
18
18
import { PageFactory , PAGE_FACTORY } from "../platform-providers" ;
19
19
import { once } from "../common/utils" ;
20
20
import { topmost , Frame } from "tns-core-modules/ui/frame" ;
21
+ import { ShowModalOptions } from "tns-core-modules/ui/core/view" ;
21
22
22
- export interface ModalDialogOptions {
23
+ export type BaseShowModalOptions = Pick < ShowModalOptions , Exclude < keyof ShowModalOptions , "closeCallback" | "context" > > ;
24
+
25
+ export interface ModalDialogOptions extends BaseShowModalOptions {
23
26
context ?: any ;
24
- fullscreen ?: boolean ;
25
- animated ?: boolean ;
26
- stretched ?: boolean ;
27
27
viewContainerRef ?: ViewContainerRef ;
28
28
moduleRef ?: NgModuleRef < any > ;
29
+ target ?: View ;
29
30
}
30
31
31
32
export class ModalDialogParams {
@@ -35,13 +36,10 @@ export class ModalDialogParams {
35
36
}
36
37
}
37
38
38
- interface ShowDialogOptions {
39
+ interface ShowDialogOptions extends BaseShowModalOptions {
39
40
containerRef : ViewContainerRef ;
40
41
context : any ;
41
42
doneCallback ;
42
- fullscreen : boolean ;
43
- animated : boolean ;
44
- stretched : boolean ;
45
43
pageFactory : PageFactory ;
46
44
parentView : ViewBase ;
47
45
resolver : ComponentFactoryResolver ;
@@ -54,16 +52,20 @@ export class ModalDialogService {
54
52
}
55
53
56
54
public showModal ( type : Type < any > ,
57
- { viewContainerRef , moduleRef , context , fullscreen , animated , stretched } : ModalDialogOptions
55
+ options : ModalDialogOptions
58
56
) : Promise < any > {
59
- if ( ! viewContainerRef ) {
57
+ if ( ! options . viewContainerRef ) {
60
58
throw new Error (
61
59
"No viewContainerRef: " +
62
60
"Make sure you pass viewContainerRef in ModalDialogOptions."
63
61
) ;
64
62
}
65
63
66
- let parentView = viewContainerRef . element . nativeElement ;
64
+ let parentView = options . viewContainerRef . element . nativeElement ;
65
+ if ( options . target ) {
66
+ parentView = options . target ;
67
+ }
68
+
67
69
if ( parentView instanceof AppHostView && parentView . ngAppRoot ) {
68
70
parentView = parentView . ngAppRoot ;
69
71
}
@@ -75,11 +77,11 @@ export class ModalDialogService {
75
77
parentView = parentView . _ngDialogRoot ;
76
78
}
77
79
78
- const pageFactory : PageFactory = viewContainerRef . injector . get ( PAGE_FACTORY ) ;
80
+ const pageFactory : PageFactory = options . viewContainerRef . injector . get ( PAGE_FACTORY ) ;
79
81
80
82
// resolve from particular module (moduleRef)
81
83
// or from same module as parentView (viewContainerRef)
82
- const componentContainer = moduleRef || viewContainerRef ;
84
+ const componentContainer = options . moduleRef || options . viewContainerRef ;
83
85
const resolver = componentContainer . injector . get ( ComponentFactoryResolver ) ;
84
86
85
87
let frame = parentView ;
@@ -93,16 +95,14 @@ export class ModalDialogService {
93
95
setTimeout ( ( ) => {
94
96
try {
95
97
this . _showDialog ( {
96
- containerRef : viewContainerRef ,
97
- context,
98
+ ...options ,
99
+ containerRef : options . viewContainerRef ,
100
+ context : options . context ,
98
101
doneCallback : resolve ,
99
- fullscreen,
100
- animated,
101
- stretched,
102
102
pageFactory,
103
103
parentView,
104
104
resolver,
105
- type,
105
+ type
106
106
} ) ;
107
107
} catch ( err ) {
108
108
reject ( err ) ;
@@ -111,23 +111,12 @@ export class ModalDialogService {
111
111
} ) ;
112
112
}
113
113
114
- private _showDialog ( {
115
- containerRef,
116
- context,
117
- doneCallback,
118
- fullscreen,
119
- animated,
120
- stretched,
121
- pageFactory,
122
- parentView,
123
- resolver,
124
- type,
125
- } : ShowDialogOptions ) : void {
114
+ private _showDialog ( options : ShowDialogOptions ) : void {
126
115
let componentView : View ;
127
116
let detachedLoaderRef : ComponentRef < DetachedLoader > ;
128
117
129
118
const closeCallback = once ( ( ...args ) => {
130
- doneCallback . apply ( undefined , args ) ;
119
+ options . doneCallback . apply ( undefined , args ) ;
131
120
if ( componentView ) {
132
121
componentView . closeModal ( ) ;
133
122
this . location . _closeModalNavigation ( ) ;
@@ -136,15 +125,15 @@ export class ModalDialogService {
136
125
}
137
126
} ) ;
138
127
139
- const modalParams = new ModalDialogParams ( context , closeCallback ) ;
128
+ const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
140
129
const providers = ReflectiveInjector . resolve ( [
141
130
{ provide : ModalDialogParams , useValue : modalParams } ,
142
131
] ) ;
143
132
144
- const childInjector = ReflectiveInjector . fromResolvedProviders ( providers , containerRef . parentInjector ) ;
145
- const detachedFactory = resolver . resolveComponentFactory ( DetachedLoader ) ;
146
- detachedLoaderRef = containerRef . createComponent ( detachedFactory , - 1 , childInjector , null ) ;
147
- detachedLoaderRef . instance . loadComponent ( type ) . then ( ( compRef ) => {
133
+ const childInjector = ReflectiveInjector . fromResolvedProviders ( providers , options . containerRef . parentInjector ) ;
134
+ const detachedFactory = options . resolver . resolveComponentFactory ( DetachedLoader ) ;
135
+ detachedLoaderRef = options . containerRef . createComponent ( detachedFactory , - 1 , childInjector , null ) ;
136
+ detachedLoaderRef . instance . loadComponent ( options . type ) . then ( ( compRef ) => {
148
137
const detachedProxy = < ProxyViewContainer > compRef . location . nativeElement ;
149
138
150
139
if ( detachedProxy . getChildrenCount ( ) > 1 ) {
@@ -157,9 +146,7 @@ export class ModalDialogService {
157
146
( < any > componentView . parent ) . removeChild ( componentView ) ;
158
147
}
159
148
160
- // TODO: remove <any> cast after https://github.com/NativeScript/NativeScript/pull/5734
161
- // is in a published version of tns-core-modules.
162
- ( < any > parentView ) . showModal ( componentView , context , closeCallback , fullscreen , animated , stretched ) ;
149
+ options . parentView . showModal ( componentView , { ...options , closeCallback } ) ;
163
150
} ) ;
164
151
}
165
152
}
0 commit comments