Skip to content

Refactor modal options to expose current/future core options #1769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 22 additions & 41 deletions nativescript-angular/directives/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import { AppHostView } from "../app-host-view";
import { DetachedLoader } from "../common/detached-loader";
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
import { once } from "../common/utils";
import { topmost, Frame } from "tns-core-modules/ui/frame";
import { topmost, Frame, ShowModalOptions } from "tns-core-modules/ui/frame";

export interface ModalDialogOptions {
export type BaseShowModalOptions = Pick<ShowModalOptions, Exclude<keyof ShowModalOptions, "closeCallback" | "context">>;

export interface ModalDialogOptions extends BaseShowModalOptions {
context?: any;
fullscreen?: boolean;
animated?: boolean;
stretched?: boolean;
viewContainerRef?: ViewContainerRef;
moduleRef?: NgModuleRef<any>;
}
Expand All @@ -35,13 +34,10 @@ export class ModalDialogParams {
}
}

interface ShowDialogOptions {
containerRef: ViewContainerRef;
interface ShowDialogOptions extends BaseShowModalOptions {
context: any;
containerRef: ViewContainerRef;
doneCallback;
fullscreen: boolean;
animated: boolean;
stretched: boolean;
pageFactory: PageFactory;
parentView: ViewBase;
resolver: ComponentFactoryResolver;
Expand All @@ -54,16 +50,16 @@ export class ModalDialogService {
}

public showModal(type: Type<any>,
{ viewContainerRef, moduleRef, context, fullscreen, animated, stretched }: ModalDialogOptions
options: ModalDialogOptions
): Promise<any> {
if (!viewContainerRef) {
if (!options.viewContainerRef) {
throw new Error(
"No viewContainerRef: " +
"Make sure you pass viewContainerRef in ModalDialogOptions."
);
}

let parentView = viewContainerRef.element.nativeElement;
let parentView = options.viewContainerRef.element.nativeElement;
if (parentView instanceof AppHostView && parentView.ngAppRoot) {
parentView = parentView.ngAppRoot;
}
Expand All @@ -75,11 +71,11 @@ export class ModalDialogService {
parentView = parentView._ngDialogRoot;
}

const pageFactory: PageFactory = viewContainerRef.injector.get(PAGE_FACTORY);
const pageFactory: PageFactory = options.viewContainerRef.injector.get(PAGE_FACTORY);

// resolve from particular module (moduleRef)
// or from same module as parentView (viewContainerRef)
const componentContainer = moduleRef || viewContainerRef;
const componentContainer = options.moduleRef || options.viewContainerRef;
const resolver = componentContainer.injector.get(ComponentFactoryResolver);

let frame = parentView;
Expand All @@ -93,12 +89,10 @@ export class ModalDialogService {
setTimeout(() => {
try {
this._showDialog({
containerRef: viewContainerRef,
context,
...options,
containerRef: options.viewContainerRef,
context: options.context,
doneCallback: resolve,
fullscreen,
animated,
stretched,
pageFactory,
parentView,
resolver,
Expand All @@ -111,23 +105,12 @@ export class ModalDialogService {
});
}

private _showDialog({
containerRef,
context,
doneCallback,
fullscreen,
animated,
stretched,
pageFactory,
parentView,
resolver,
type,
}: ShowDialogOptions): void {
private _showDialog(options: ShowDialogOptions): void {
let componentView: View;
let detachedLoaderRef: ComponentRef<DetachedLoader>;

const closeCallback = once((...args) => {
doneCallback.apply(undefined, args);
options.doneCallback.apply(undefined, args);
if (componentView) {
componentView.closeModal();
this.location._closeModalNavigation();
Expand All @@ -136,15 +119,15 @@ export class ModalDialogService {
}
});

const modalParams = new ModalDialogParams(context, closeCallback);
const modalParams = new ModalDialogParams(options.context, closeCallback);
const providers = ReflectiveInjector.resolve([
{ provide: ModalDialogParams, useValue: modalParams },
]);

const childInjector = ReflectiveInjector.fromResolvedProviders(providers, containerRef.parentInjector);
const detachedFactory = resolver.resolveComponentFactory(DetachedLoader);
detachedLoaderRef = containerRef.createComponent(detachedFactory, -1, childInjector, null);
detachedLoaderRef.instance.loadComponent(type).then((compRef) => {
const childInjector = ReflectiveInjector.fromResolvedProviders(providers, options.containerRef.parentInjector);
const detachedFactory = options.resolver.resolveComponentFactory(DetachedLoader);
detachedLoaderRef = options.containerRef.createComponent(detachedFactory, -1, childInjector, null);
detachedLoaderRef.instance.loadComponent(options.type).then((compRef) => {
const detachedProxy = <ProxyViewContainer>compRef.location.nativeElement;

if (detachedProxy.getChildrenCount() > 1) {
Expand All @@ -157,9 +140,7 @@ export class ModalDialogService {
(<any>componentView.parent).removeChild(componentView);
}

// TODO: remove <any> cast after https://github.com/NativeScript/NativeScript/pull/5734
// is in a published version of tns-core-modules.
(<any>parentView).showModal(componentView, context, closeCallback, fullscreen, animated, stretched);
options.parentView.showModal(componentView, { ...options, closeCallback });
});
}
}
Expand Down