1
1
import {
2
2
ReflectiveInjector , ComponentFactoryResolver , ViewContainerRef ,
3
- Injector , Type , Injectable , ComponentRef , Directive
3
+ Type , Injectable , ComponentRef , Directive
4
4
} 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' ;
8
8
9
9
export interface ModalDialogOptions {
10
10
context ?: any ;
11
11
fullscreen ?: boolean ;
12
+ viewContainerRef ?: ViewContainerRef ;
12
13
}
13
14
14
15
export class ModalDialogParams {
@@ -22,52 +23,61 @@ export class ModalDialogParams {
22
23
export class ModalDialogService {
23
24
private containerRef : ViewContainerRef ;
24
25
25
- constructor (
26
- private page : Page ,
27
- private resolver : ComponentFactoryResolver ) {
28
- }
29
-
30
26
public registerViewContainerRef ( ref : ViewContainerRef ) {
31
27
this . containerRef = ref ;
32
28
}
33
29
34
30
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." ) ;
37
35
}
36
+
37
+ const parentPage : Page = viewContainerRef . injector . get ( Page ) ;
38
+ const resolver : ComponentFactoryResolver = viewContainerRef . injector . get ( ComponentFactoryResolver ) ;
39
+
38
40
return new Promise ( ( resolve , reject ) => {
39
- setTimeout ( ( ) => this . showDialog ( type , options , resolve ) , 10 ) ;
41
+ setTimeout ( ( ) => ModalDialogService . showDialog ( type , options , resolve , viewContainerRef , resolver , parentPage ) , 10 ) ;
40
42
} ) ;
41
43
}
42
44
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
+
44
53
const page = new Page ( ) ;
45
54
46
- var detachedLoaderRef : ComponentRef < DetachedLoader > ;
55
+ let detachedLoaderRef : ComponentRef < DetachedLoader > ;
47
56
const closeCallback = ( ...args ) => {
48
57
doneCallback . apply ( undefined , args ) ;
49
58
page . closeModal ( ) ;
50
59
detachedLoaderRef . destroy ( ) ;
51
- }
60
+ } ;
61
+
52
62
const modalParams = new ModalDialogParams ( options . context , closeCallback ) ;
53
63
54
64
const providers = ReflectiveInjector . resolve ( [
55
- { provide : Page , useValue : page } ,
56
- { provide : ModalDialogParams , useValue : modalParams } ,
65
+ { provide : Page , useValue : page } ,
66
+ { provide : ModalDialogParams , useValue : modalParams } ,
57
67
] ) ;
58
68
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 ) ;
62
72
detachedLoaderRef . instance . loadComponent ( type ) . then ( ( compRef ) => {
63
73
const componentView = < View > compRef . location . nativeElement ;
64
-
74
+
65
75
if ( componentView . parent ) {
66
76
( < any > componentView . parent ) . removeChild ( componentView ) ;
67
77
}
68
-
78
+
69
79
page . content = componentView ;
70
- this . page . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
80
+ parentPage . showModal ( page , options . context , closeCallback , options . fullscreen ) ;
71
81
} ) ;
72
82
}
73
83
}
@@ -78,6 +88,8 @@ export class ModalDialogService {
78
88
} )
79
89
export class ModalDialogHost {
80
90
constructor ( containerRef : ViewContainerRef , modalService : ModalDialogService ) {
91
+ console . log ( "ModalDialogHost is deprecated. Call ModalDialogService.showModal() by passing ViewContainerRef in the options instead." )
92
+
81
93
modalService . registerViewContainerRef ( containerRef ) ;
82
94
}
83
95
}
0 commit comments