7
7
ReflectiveInjector ,
8
8
Type ,
9
9
ViewContainerRef ,
10
+ ElementRef ,
10
11
} from "@angular/core" ;
11
12
12
13
import { NSLocationStrategy } from "../router/ns-location-strategy" ;
@@ -17,15 +18,15 @@ import { AppHostView } from "../app-host-view";
17
18
import { DetachedLoader } from "../common/detached-loader" ;
18
19
import { PageFactory , PAGE_FACTORY } from "../platform-providers" ;
19
20
import { once } from "../common/utils" ;
20
- import { topmost , Frame } from "tns-core-modules/ui/frame" ;
21
+ import { topmost , Frame , ShowModalOptions } from "tns-core-modules/ui/frame" ;
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
+ sourceView ?: ElementRef ;
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,19 @@ 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 . sourceView ) {
66
+ parentView = options . sourceView . nativeElement ;
67
+ }
67
68
if ( parentView instanceof AppHostView && parentView . ngAppRoot ) {
68
69
parentView = parentView . ngAppRoot ;
69
70
}
@@ -75,11 +76,11 @@ export class ModalDialogService {
75
76
parentView = parentView . _ngDialogRoot ;
76
77
}
77
78
78
- const pageFactory : PageFactory = viewContainerRef . injector . get ( PAGE_FACTORY ) ;
79
+ const pageFactory : PageFactory = options . viewContainerRef . injector . get ( PAGE_FACTORY ) ;
79
80
80
81
// resolve from particular module (moduleRef)
81
82
// or from same module as parentView (viewContainerRef)
82
- const componentContainer = moduleRef || viewContainerRef ;
83
+ const componentContainer = options . moduleRef || options . viewContainerRef ;
83
84
const resolver = componentContainer . injector . get ( ComponentFactoryResolver ) ;
84
85
85
86
let frame = parentView ;
@@ -93,16 +94,14 @@ export class ModalDialogService {
93
94
setTimeout ( ( ) => {
94
95
try {
95
96
this . _showDialog ( {
96
- containerRef : viewContainerRef ,
97
- context,
97
+ ...options ,
98
+ containerRef : options . viewContainerRef ,
99
+ context : options . context ,
98
100
doneCallback : resolve ,
99
- fullscreen,
100
- animated,
101
- stretched,
102
101
pageFactory,
103
102
parentView,
104
103
resolver,
105
- type,
104
+ type
106
105
} ) ;
107
106
} catch ( err ) {
108
107
reject ( err ) ;
@@ -111,23 +110,12 @@ export class ModalDialogService {
111
110
} ) ;
112
111
}
113
112
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 {
113
+ private _showDialog ( options : ShowDialogOptions ) : void {
126
114
let componentView : View ;
127
115
let detachedLoaderRef : ComponentRef < DetachedLoader > ;
128
116
129
117
const closeCallback = once ( ( ...args ) => {
130
- doneCallback . apply ( undefined , args ) ;
118
+ options . doneCallback . apply ( undefined , args ) ;
131
119
if ( componentView ) {
132
120
componentView . closeModal ( ) ;
133
121
this . location . _closeModalNavigation ( ) ;
@@ -136,15 +124,15 @@ export class ModalDialogService {
136
124
}
137
125
} ) ;
138
126
139
- const modalParams = new ModalDialogParams ( context , closeCallback ) ;
127
+ const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
140
128
const providers = ReflectiveInjector . resolve ( [
141
129
{ provide : ModalDialogParams , useValue : modalParams } ,
142
130
] ) ;
143
131
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 ) => {
132
+ const childInjector = ReflectiveInjector . fromResolvedProviders ( providers , options . containerRef . parentInjector ) ;
133
+ const detachedFactory = options . resolver . resolveComponentFactory ( DetachedLoader ) ;
134
+ detachedLoaderRef = options . containerRef . createComponent ( detachedFactory , - 1 , childInjector , null ) ;
135
+ detachedLoaderRef . instance . loadComponent ( options . type ) . then ( ( compRef ) => {
148
136
const detachedProxy = < ProxyViewContainer > compRef . location . nativeElement ;
149
137
150
138
if ( detachedProxy . getChildrenCount ( ) > 1 ) {
@@ -157,9 +145,10 @@ export class ModalDialogService {
157
145
( < any > componentView . parent ) . removeChild ( componentView ) ;
158
146
}
159
147
148
+
160
149
// TODO: remove <any> cast after https://github.com/NativeScript/NativeScript/pull/5734
161
150
// is in a published version of tns-core-modules.
162
- ( < any > parentView ) . showModal ( componentView , context , closeCallback , fullscreen , animated , stretched ) ;
151
+ ( < any > options . parentView ) . showModal ( componentView , { ... options , closeCallback } ) ;
163
152
} ) ;
164
153
}
165
154
}
0 commit comments