From 8f8c5a3c10f41ad208024c8e731e5816fdda96c7 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Thu, 21 Mar 2019 11:15:23 -0300 Subject: [PATCH 1/2] feat(modal): expose current/future core options --- nativescript-angular/directives/dialogs.ts | 63 ++++++++-------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/nativescript-angular/directives/dialogs.ts b/nativescript-angular/directives/dialogs.ts index 7c8b45739..8dd468ce7 100644 --- a/nativescript-angular/directives/dialogs.ts +++ b/nativescript-angular/directives/dialogs.ts @@ -17,13 +17,11 @@ 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 { - context?: any; - fullscreen?: boolean; - animated?: boolean; - stretched?: boolean; +export type BaseShowModalOptions = Pick>; + +export interface ModalDialogOptions extends BaseShowModalOptions { viewContainerRef?: ViewContainerRef; moduleRef?: NgModuleRef; } @@ -35,13 +33,9 @@ export class ModalDialogParams { } } -interface ShowDialogOptions { +interface ShowDialogOptions extends BaseShowModalOptions { containerRef: ViewContainerRef; - context: any; doneCallback; - fullscreen: boolean; - animated: boolean; - stretched: boolean; pageFactory: PageFactory; parentView: ViewBase; resolver: ComponentFactoryResolver; @@ -54,16 +48,16 @@ export class ModalDialogService { } public showModal(type: Type, - { viewContainerRef, moduleRef, context, fullscreen, animated, stretched }: ModalDialogOptions + options: ModalDialogOptions ): Promise { - 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; } @@ -75,11 +69,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; @@ -93,12 +87,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, @@ -111,23 +103,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; const closeCallback = once((...args) => { - doneCallback.apply(undefined, args); + options.doneCallback.apply(undefined, args); if (componentView) { componentView.closeModal(); this.location._closeModalNavigation(); @@ -136,15 +117,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 = compRef.location.nativeElement; if (detachedProxy.getChildrenCount() > 1) { @@ -157,9 +138,7 @@ export class ModalDialogService { (componentView.parent).removeChild(componentView); } - // TODO: remove cast after https://github.com/NativeScript/NativeScript/pull/5734 - // is in a published version of tns-core-modules. - (parentView).showModal(componentView, context, closeCallback, fullscreen, animated, stretched); + options.parentView.showModal(componentView, { ...options, closeCallback }); }); } } From 7e09efd9771ecce6d3c7dbb8b0813344227bdb99 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Thu, 21 Mar 2019 11:33:01 -0300 Subject: [PATCH 2/2] context should not be required (backwards compat) --- nativescript-angular/directives/dialogs.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nativescript-angular/directives/dialogs.ts b/nativescript-angular/directives/dialogs.ts index 8dd468ce7..6f158e145 100644 --- a/nativescript-angular/directives/dialogs.ts +++ b/nativescript-angular/directives/dialogs.ts @@ -19,9 +19,10 @@ import { PageFactory, PAGE_FACTORY } from "../platform-providers"; import { once } from "../common/utils"; import { topmost, Frame, ShowModalOptions } from "tns-core-modules/ui/frame"; -export type BaseShowModalOptions = Pick>; +export type BaseShowModalOptions = Pick>; export interface ModalDialogOptions extends BaseShowModalOptions { + context?: any; viewContainerRef?: ViewContainerRef; moduleRef?: NgModuleRef; } @@ -34,6 +35,7 @@ export class ModalDialogParams { } interface ShowDialogOptions extends BaseShowModalOptions { + context: any; containerRef: ViewContainerRef; doneCallback; pageFactory: PageFactory;