Skip to content

Commit a18bd18

Browse files
committed
feat(modals): closed$ and ability to access ModalDialogParams outside of component context
1 parent 230461e commit a18bd18

File tree

1 file changed

+38
-1
lines changed
  • packages/angular/src/lib/legacy/directives

1 file changed

+38
-1
lines changed

packages/angular/src/lib/legacy/directives/dialogs.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, Injectable, Injector, NgModuleRef, NgZone, Type, ViewContainerRef, ɵmarkDirty } from '@angular/core';
22
import { Application, ContentView, Frame, ShowModalOptions, View, ViewBase } from '@nativescript/core';
3+
import { Subject } from 'rxjs';
34
import { AppHostAsyncView, AppHostView } from '../../app-host-view';
45
import { DetachedLoader } from '../../cdk/detached-loader';
56
import { ComponentPortal } from '../../cdk/portal/common';
@@ -15,9 +16,13 @@ export interface ModalDialogOptions extends BaseShowModalOptions {
1516
viewContainerRef?: ViewContainerRef;
1617
moduleRef?: NgModuleRef<any>;
1718
target?: View;
19+
/**
20+
* Use context data as component instance properties
21+
*/
22+
useContextAsComponentProps?: boolean;
1823
}
1924

20-
export interface ShowDialogOptions extends BaseShowModalOptions {
25+
export interface ShowDialogOptions extends ModalDialogOptions {
2126
containerRef?: ViewContainerRef;
2227
/**
2328
* which container to attach the change detection
@@ -39,8 +44,27 @@ export class ModalDialogParams {
3944

4045
@Injectable()
4146
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+
4255
constructor(private location: NSLocationStrategy, private zone: NgZone, private appRef: ApplicationRef, private defaultInjector: Injector) {}
4356

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+
4468
public showModal(type: Type<any>, options: ModalDialogOptions = {}): Promise<any> {
4569
// if (!options.viewContainerRef) {
4670
// throw new Error('No viewContainerRef: ' + 'Make sure you pass viewContainerRef in ModalDialogOptions.');
@@ -103,6 +127,10 @@ export class ModalDialogService {
103127
options.doneCallback.apply(undefined, args);
104128
if (componentViewRef) {
105129
componentViewRef.firstNativeLikeView.closeModal();
130+
const params = this.openedModalParams.pop();
131+
if (this._closed$) {
132+
this._closed$.next(params);
133+
}
106134
this.location._closeModalNavigation();
107135
if (detachedLoaderRef || portalOutlet) {
108136
this.zone.run(() => {
@@ -115,6 +143,10 @@ export class ModalDialogService {
115143
});
116144

117145
const modalParams = new ModalDialogParams(options.context, closeCallback);
146+
if (!this.openedModalParams) {
147+
this.openedModalParams = [];
148+
}
149+
this.openedModalParams.push(modalParams);
118150

119151
const childInjector = Injector.create({
120152
providers: [{ provide: ModalDialogParams, useValue: modalParams }],
@@ -139,6 +171,11 @@ export class ModalDialogService {
139171
const componentRef = portalOutlet.attach(portal);
140172
ɵmarkDirty(componentRef.instance);
141173
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+
}
142179
if (componentViewRef !== componentRef.location.nativeElement) {
143180
componentRef.location.nativeElement._ngDialogRoot = componentViewRef.firstNativeLikeView;
144181
}

0 commit comments

Comments
 (0)