11import {
22 ReflectiveInjector , ComponentFactoryResolver , ViewContainerRef ,
3- Injector , Type , Injectable , ComponentRef , Directive
3+ Type , Injectable , ComponentRef , Directive
44} from '@angular/core' ;
5- import { Page } from 'ui/page' ;
6- import { View } from 'ui/core/view' ;
7- import { DetachedLoader } from '../common/detached-loader' ;
5+ import { Page } from 'ui/page' ;
6+ import { View } from 'ui/core/view' ;
7+ import { DetachedLoader } from '../common/detached-loader' ;
88
99export interface ModalDialogOptions {
1010 context ?: any ;
1111 fullscreen ?: boolean ;
12+ viewContainerRef ?: ViewContainerRef ;
1213}
1314
1415export class ModalDialogParams {
@@ -22,52 +23,61 @@ export class ModalDialogParams {
2223export class ModalDialogService {
2324 private containerRef : ViewContainerRef ;
2425
25- constructor (
26- private page : Page ,
27- private resolver : ComponentFactoryResolver ) {
28- }
29-
3026 public registerViewContainerRef ( ref : ViewContainerRef ) {
3127 this . containerRef = ref ;
3228 }
3329
3430 public showModal ( type : Type < any > , options : ModalDialogOptions ) : Promise < any > {
35- if ( ! this . containerRef ) {
36- throw new Error ( "No viewContainerRef: Make sure you have the modal-dialog-host directive inside your component." ) ;
31+ let viewContainerRef = options . viewContainerRef || this . containerRef ;
32+
33+ if ( ! viewContainerRef ) {
34+ throw new Error ( "No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions." ) ;
3735 }
36+
37+ const parentPage : Page = viewContainerRef . injector . get ( Page ) ;
38+ const resolver : ComponentFactoryResolver = viewContainerRef . injector . get ( ComponentFactoryResolver ) ;
39+
3840 return new Promise ( ( resolve , reject ) => {
39- setTimeout ( ( ) => this . showDialog ( type , options , resolve ) , 10 ) ;
41+ setTimeout ( ( ) => ModalDialogService . showDialog ( type , options , resolve , viewContainerRef , resolver , parentPage ) , 10 ) ;
4042 } ) ;
4143 }
4244
43- private showDialog ( type : Type < any > , options : ModalDialogOptions , doneCallback ) : void {
45+ private static showDialog (
46+ type : Type < any > ,
47+ options : ModalDialogOptions ,
48+ doneCallback ,
49+ containerRef : ViewContainerRef ,
50+ resolver : ComponentFactoryResolver ,
51+ parentPage : Page ) : void {
52+
4453 const page = new Page ( ) ;
4554
46- var detachedLoaderRef : ComponentRef < DetachedLoader > ;
55+ let detachedLoaderRef : ComponentRef < DetachedLoader > ;
4756 const closeCallback = ( ...args ) => {
4857 doneCallback . apply ( undefined , args ) ;
4958 page . closeModal ( ) ;
5059 detachedLoaderRef . destroy ( ) ;
51- }
60+ } ;
61+
5262 const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
5363
5464 const providers = ReflectiveInjector . resolve ( [
55- { provide : Page , useValue : page } ,
56- { provide : ModalDialogParams , useValue : modalParams } ,
65+ { provide : Page , useValue : page } ,
66+ { provide : ModalDialogParams , useValue : modalParams } ,
5767 ] ) ;
5868
59- const childInjector = ReflectiveInjector . fromResolvedProviders ( providers , this . containerRef . parentInjector ) ;
60- const detachedFactory = this . resolver . resolveComponentFactory ( DetachedLoader ) ;
61- detachedLoaderRef = this . containerRef . createComponent ( detachedFactory , - 1 , childInjector , null )
69+ const childInjector = ReflectiveInjector . fromResolvedProviders ( providers , containerRef . parentInjector ) ;
70+ const detachedFactory = resolver . resolveComponentFactory ( DetachedLoader ) ;
71+ detachedLoaderRef = containerRef . createComponent ( detachedFactory , - 1 , childInjector , null ) ;
6272 detachedLoaderRef . instance . loadComponent ( type ) . then ( ( compRef ) => {
6373 const componentView = < View > compRef . location . nativeElement ;
64-
74+
6575 if ( componentView . parent ) {
6676 ( < any > componentView . parent ) . removeChild ( componentView ) ;
6777 }
68-
78+
6979 page . content = componentView ;
70- this . page . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
80+ parentPage . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
7181 } ) ;
7282 }
7383}
@@ -78,6 +88,8 @@ export class ModalDialogService {
7888} )
7989export class ModalDialogHost {
8090 constructor ( containerRef : ViewContainerRef , modalService : ModalDialogService ) {
91+ console . log ( "ModalDialogHost is deprecated. Call ModalDialogService.showModal() by passing ViewContainerRef in the options instead." )
92+
8193 modalService . registerViewContainerRef ( containerRef ) ;
8294 }
8395}
0 commit comments