66 NgModuleRef ,
77 ReflectiveInjector ,
88 Type ,
9- ViewContainerRef ,
9+ ViewContainerRef
1010} from "@angular/core" ;
1111
1212import { NSLocationStrategy } from "../router/ns-location-strategy" ;
@@ -18,14 +18,15 @@ import { DetachedLoader } from "../common/detached-loader";
1818import { PageFactory , PAGE_FACTORY } from "../platform-providers" ;
1919import { once } from "../common/utils" ;
2020import { topmost , Frame } from "tns-core-modules/ui/frame" ;
21+ import { ShowModalOptions } from "tns-core-modules/ui/core/view" ;
2122
22- export interface ModalDialogOptions {
23+ export type BaseShowModalOptions = Pick < ShowModalOptions , Exclude < keyof ShowModalOptions , "closeCallback" | "context" > > ;
24+
25+ export interface ModalDialogOptions extends BaseShowModalOptions {
2326 context ?: any ;
24- fullscreen ?: boolean ;
25- animated ?: boolean ;
26- stretched ?: boolean ;
2727 viewContainerRef ?: ViewContainerRef ;
2828 moduleRef ?: NgModuleRef < any > ;
29+ target ?: View ;
2930}
3031
3132export class ModalDialogParams {
@@ -35,13 +36,10 @@ export class ModalDialogParams {
3536 }
3637}
3738
38- interface ShowDialogOptions {
39+ interface ShowDialogOptions extends BaseShowModalOptions {
3940 containerRef : ViewContainerRef ;
4041 context : any ;
4142 doneCallback ;
42- fullscreen : boolean ;
43- animated : boolean ;
44- stretched : boolean ;
4543 pageFactory : PageFactory ;
4644 parentView : ViewBase ;
4745 resolver : ComponentFactoryResolver ;
@@ -54,16 +52,20 @@ export class ModalDialogService {
5452 }
5553
5654 public showModal ( type : Type < any > ,
57- { viewContainerRef , moduleRef , context , fullscreen , animated , stretched } : ModalDialogOptions
55+ options : ModalDialogOptions
5856 ) : Promise < any > {
59- if ( ! viewContainerRef ) {
57+ if ( ! options . viewContainerRef ) {
6058 throw new Error (
6159 "No viewContainerRef: " +
6260 "Make sure you pass viewContainerRef in ModalDialogOptions."
6361 ) ;
6462 }
6563
66- let parentView = viewContainerRef . element . nativeElement ;
64+ let parentView = options . viewContainerRef . element . nativeElement ;
65+ if ( options . target ) {
66+ parentView = options . target ;
67+ }
68+
6769 if ( parentView instanceof AppHostView && parentView . ngAppRoot ) {
6870 parentView = parentView . ngAppRoot ;
6971 }
@@ -75,11 +77,11 @@ export class ModalDialogService {
7577 parentView = parentView . _ngDialogRoot ;
7678 }
7779
78- const pageFactory : PageFactory = viewContainerRef . injector . get ( PAGE_FACTORY ) ;
80+ const pageFactory : PageFactory = options . viewContainerRef . injector . get ( PAGE_FACTORY ) ;
7981
8082 // resolve from particular module (moduleRef)
8183 // or from same module as parentView (viewContainerRef)
82- const componentContainer = moduleRef || viewContainerRef ;
84+ const componentContainer = options . moduleRef || options . viewContainerRef ;
8385 const resolver = componentContainer . injector . get ( ComponentFactoryResolver ) ;
8486
8587 let frame = parentView ;
@@ -93,16 +95,14 @@ export class ModalDialogService {
9395 setTimeout ( ( ) => {
9496 try {
9597 this . _showDialog ( {
96- containerRef : viewContainerRef ,
97- context,
98+ ...options ,
99+ containerRef : options . viewContainerRef ,
100+ context : options . context ,
98101 doneCallback : resolve ,
99- fullscreen,
100- animated,
101- stretched,
102102 pageFactory,
103103 parentView,
104104 resolver,
105- type,
105+ type
106106 } ) ;
107107 } catch ( err ) {
108108 reject ( err ) ;
@@ -111,23 +111,12 @@ export class ModalDialogService {
111111 } ) ;
112112 }
113113
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 {
126115 let componentView : View ;
127116 let detachedLoaderRef : ComponentRef < DetachedLoader > ;
128117
129118 const closeCallback = once ( ( ...args ) => {
130- doneCallback . apply ( undefined , args ) ;
119+ options . doneCallback . apply ( undefined , args ) ;
131120 if ( componentView ) {
132121 componentView . closeModal ( ) ;
133122 this . location . _closeModalNavigation ( ) ;
@@ -136,15 +125,15 @@ export class ModalDialogService {
136125 }
137126 } ) ;
138127
139- const modalParams = new ModalDialogParams ( context , closeCallback ) ;
128+ const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
140129 const providers = ReflectiveInjector . resolve ( [
141130 { provide : ModalDialogParams , useValue : modalParams } ,
142131 ] ) ;
143132
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 ) => {
148137 const detachedProxy = < ProxyViewContainer > compRef . location . nativeElement ;
149138
150139 if ( detachedProxy . getChildrenCount ( ) > 1 ) {
@@ -157,9 +146,7 @@ export class ModalDialogService {
157146 ( < any > componentView . parent ) . removeChild ( componentView ) ;
158147 }
159148
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 } ) ;
163150 } ) ;
164151 }
165152}
0 commit comments