Skip to content

Commit 9cfa127

Browse files
Marcus WilliamsVladimirAmiorkov
Marcus Williams
authored and
VladimirAmiorkov
committed
feat(modal): add ‘ios presentationStyle’ option to ModalDialogParams
feat(modal): expose current/future core options via ModalDialogParams
1 parent 06bbcae commit 9cfa127

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

Diff for: nativescript-angular/directives/dialogs.ts

+28-39
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ReflectiveInjector,
88
Type,
99
ViewContainerRef,
10+
ElementRef,
1011
} from "@angular/core";
1112

1213
import { NSLocationStrategy } from "../router/ns-location-strategy";
@@ -17,15 +18,15 @@ import { AppHostView } from "../app-host-view";
1718
import { DetachedLoader } from "../common/detached-loader";
1819
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
1920
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";
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+
sourceView?: ElementRef;
2930
}
3031

3132
export 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,19 @@ 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.sourceView) {
66+
parentView = options.sourceView.nativeElement;
67+
}
6768
if (parentView instanceof AppHostView && parentView.ngAppRoot) {
6869
parentView = parentView.ngAppRoot;
6970
}
@@ -75,11 +76,11 @@ export class ModalDialogService {
7576
parentView = parentView._ngDialogRoot;
7677
}
7778

78-
const pageFactory: PageFactory = viewContainerRef.injector.get(PAGE_FACTORY);
79+
const pageFactory: PageFactory = options.viewContainerRef.injector.get(PAGE_FACTORY);
7980

8081
// resolve from particular module (moduleRef)
8182
// or from same module as parentView (viewContainerRef)
82-
const componentContainer = moduleRef || viewContainerRef;
83+
const componentContainer = options.moduleRef || options.viewContainerRef;
8384
const resolver = componentContainer.injector.get(ComponentFactoryResolver);
8485

8586
let frame = parentView;
@@ -93,16 +94,14 @@ export class ModalDialogService {
9394
setTimeout(() => {
9495
try {
9596
this._showDialog({
96-
containerRef: viewContainerRef,
97-
context,
97+
...options,
98+
containerRef: options.viewContainerRef,
99+
context: options.context,
98100
doneCallback: resolve,
99-
fullscreen,
100-
animated,
101-
stretched,
102101
pageFactory,
103102
parentView,
104103
resolver,
105-
type,
104+
type
106105
});
107106
} catch (err) {
108107
reject(err);
@@ -111,23 +110,12 @@ export class ModalDialogService {
111110
});
112111
}
113112

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 {
126114
let componentView: View;
127115
let detachedLoaderRef: ComponentRef<DetachedLoader>;
128116

129117
const closeCallback = once((...args) => {
130-
doneCallback.apply(undefined, args);
118+
options.doneCallback.apply(undefined, args);
131119
if (componentView) {
132120
componentView.closeModal();
133121
this.location._closeModalNavigation();
@@ -136,15 +124,15 @@ export class ModalDialogService {
136124
}
137125
});
138126

139-
const modalParams = new ModalDialogParams(context, closeCallback);
127+
const modalParams = new ModalDialogParams(options.context, closeCallback);
140128
const providers = ReflectiveInjector.resolve([
141129
{ provide: ModalDialogParams, useValue: modalParams },
142130
]);
143131

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) => {
148136
const detachedProxy = <ProxyViewContainer>compRef.location.nativeElement;
149137

150138
if (detachedProxy.getChildrenCount() > 1) {
@@ -157,9 +145,10 @@ export class ModalDialogService {
157145
(<any>componentView.parent).removeChild(componentView);
158146
}
159147

148+
160149
// TODO: remove <any> cast after https://github.com/NativeScript/NativeScript/pull/5734
161150
// 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 });
163152
});
164153
}
165154
}

0 commit comments

Comments
 (0)