1
1
import { ApplicationRef , ComponentFactoryResolver , ComponentRef , Injectable , Injector , NgModuleRef , NgZone , Type , ViewContainerRef , ɵmarkDirty } from '@angular/core' ;
2
2
import { Application , ContentView , Frame , ShowModalOptions , View , ViewBase } from '@nativescript/core' ;
3
+ import { Subject } from 'rxjs' ;
3
4
import { AppHostAsyncView , AppHostView } from '../../app-host-view' ;
4
5
import { DetachedLoader } from '../../cdk/detached-loader' ;
5
6
import { ComponentPortal } from '../../cdk/portal/common' ;
@@ -15,9 +16,13 @@ export interface ModalDialogOptions extends BaseShowModalOptions {
15
16
viewContainerRef ?: ViewContainerRef ;
16
17
moduleRef ?: NgModuleRef < any > ;
17
18
target ?: View ;
19
+ /**
20
+ * Use context data as component instance properties
21
+ */
22
+ useContextAsComponentProps ?: boolean ;
18
23
}
19
24
20
- export interface ShowDialogOptions extends BaseShowModalOptions {
25
+ export interface ShowDialogOptions extends ModalDialogOptions {
21
26
containerRef ?: ViewContainerRef ;
22
27
/**
23
28
* which container to attach the change detection
@@ -39,8 +44,27 @@ export class ModalDialogParams {
39
44
40
45
@Injectable ( )
41
46
export class ModalDialogService {
47
+ /**
48
+ * Any opened ModalDialogParams in order of when they were opened (Most recent on top).
49
+ * This can be used when you need access to ModalDialogParams outside of the component which had them injected.
50
+ * Each is popped off as modals are closed.
51
+ */
52
+ openedModalParams : Array < ModalDialogParams > ;
53
+ _closed$ : Subject < ModalDialogParams > ;
54
+
42
55
constructor ( private location : NSLocationStrategy , private zone : NgZone , private appRef : ApplicationRef , private defaultInjector : Injector ) { }
43
56
57
+ /**
58
+ * Emits anytime a modal is closed with the ModalDialogParams which were injected into the component which is now closing.
59
+ * For example, can be used to wire up Rx flows outside the scope of just the component being handled.
60
+ */
61
+ get closed$ ( ) {
62
+ if ( ! this . _closed$ ) {
63
+ this . _closed$ = new Subject ( ) ;
64
+ }
65
+ return this . _closed$ ;
66
+ }
67
+
44
68
public showModal ( type : Type < any > , options : ModalDialogOptions = { } ) : Promise < any > {
45
69
// if (!options.viewContainerRef) {
46
70
// throw new Error('No viewContainerRef: ' + 'Make sure you pass viewContainerRef in ModalDialogOptions.');
@@ -103,6 +127,10 @@ export class ModalDialogService {
103
127
options . doneCallback . apply ( undefined , args ) ;
104
128
if ( componentViewRef ) {
105
129
componentViewRef . firstNativeLikeView . closeModal ( ) ;
130
+ const params = this . openedModalParams . pop ( ) ;
131
+ if ( this . _closed$ ) {
132
+ this . _closed$ . next ( params ) ;
133
+ }
106
134
this . location . _closeModalNavigation ( ) ;
107
135
if ( detachedLoaderRef || portalOutlet ) {
108
136
this . zone . run ( ( ) => {
@@ -115,6 +143,10 @@ export class ModalDialogService {
115
143
} ) ;
116
144
117
145
const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
146
+ if ( ! this . openedModalParams ) {
147
+ this . openedModalParams = [ ] ;
148
+ }
149
+ this . openedModalParams . push ( modalParams ) ;
118
150
119
151
const childInjector = Injector . create ( {
120
152
providers : [ { provide : ModalDialogParams , useValue : modalParams } ] ,
@@ -139,6 +171,11 @@ export class ModalDialogService {
139
171
const componentRef = portalOutlet . attach ( portal ) ;
140
172
ɵmarkDirty ( componentRef . instance ) ;
141
173
componentViewRef = new NgViewRef ( componentRef ) ;
174
+ if ( options . useContextAsComponentProps && options . context ) {
175
+ for ( const key in options . context ) {
176
+ ( < ComponentRef < any > > componentViewRef . ref ) . instance [ key ] = options . context [ key ] ;
177
+ }
178
+ }
142
179
if ( componentViewRef !== componentRef . location . nativeElement ) {
143
180
componentRef . location . nativeElement . _ngDialogRoot = componentViewRef . firstNativeLikeView ;
144
181
}
0 commit comments