99} from "@angular/core" ;
1010import { Subscription } from "rxjs" ;
1111import { DialogConfig } from "./dialog-config.interface" ;
12- import { DialogPlaceholderService } from "./dialog- placeholder.service " ;
12+ import { PlaceholderService } from "./../ placeholder/placeholder.module " ;
1313
1414/**
1515 * `Dialog` object to be injected into other components.
@@ -67,7 +67,7 @@ export class DialogService {
6767 constructor (
6868 protected componentFactoryResolver : ComponentFactoryResolver ,
6969 protected injector : Injector ,
70- protected dialogPlaceholderService : DialogPlaceholderService
70+ protected placeholderService : PlaceholderService
7171 ) { }
7272
7373 /**
@@ -103,21 +103,13 @@ export class DialogService {
103103 */
104104 open ( viewContainer : ViewContainerRef , dialogConfig : DialogConfig ) {
105105 if ( ! this . dialogRef ) {
106- // holder for either the provided view, or the view from DialogPlaceholderService
107- let view = viewContainer ;
108- if ( dialogConfig . appendToBody && this . dialogPlaceholderService . viewContainerRef ) {
109- view = this . dialogPlaceholderService . viewContainerRef ;
110- // add our component to the view
111- this . dialogRef = view . createComponent ( this . componentFactory , 0 , this . injector ) ;
112- } else if ( dialogConfig . appendToBody && ! this . dialogPlaceholderService . viewContainerRef ) {
113- // fallback to the old insertion method if the viewref doesn't exist
114- this . dialogRef = view . createComponent ( this . componentFactory , 0 , this . injector ) ;
115- setTimeout ( ( ) => {
116- window . document . querySelector ( "body" ) . appendChild ( this . dialogRef . location . nativeElement ) ;
117- } ) ;
106+ // holder for either the provided view, or the view from Placeholder
107+ if ( dialogConfig . appendToBody ) {
108+ // add our component to the placeholder
109+ this . dialogRef = this . placeholderService . createComponent ( this . componentFactory , this . injector ) ;
118110 } else {
119111 // add our component to the view
120- this . dialogRef = view . createComponent ( this . componentFactory , 0 , this . injector ) ;
112+ this . dialogRef = viewContainer . createComponent ( this . componentFactory , 0 , this . injector ) ;
121113 }
122114
123115 // initialize some extra options
@@ -128,7 +120,7 @@ export class DialogService {
128120 this . isOpen = true ;
129121
130122 this . dialogSubscription = this . onClose . subscribe ( ( ) => {
131- this . close ( view ) ;
123+ this . close ( viewContainer ) ;
132124 } ) ;
133125
134126 this . dialogRef . instance . elementRef . nativeElement . focus ( ) ;
@@ -148,9 +140,8 @@ export class DialogService {
148140
149141 if ( this . dialogRef ) {
150142 let elementToFocus = this . dialogRef . instance . dialogConfig [ "previouslyFocusedElement" ] ;
151- if ( this . dialogRef . instance . dialogConfig . appendToBody && this . dialogPlaceholderService . viewContainerRef ) {
152- const vcRef = this . dialogPlaceholderService . viewContainerRef ;
153- vcRef . remove ( vcRef . indexOf ( this . dialogRef . hostView ) ) ;
143+ if ( this . dialogRef . instance . dialogConfig . appendToBody ) {
144+ this . placeholderService . destroyComponent ( this . dialogRef ) ;
154145 } else {
155146 viewContainer . remove ( viewContainer . indexOf ( this . dialogRef . hostView ) ) ;
156147 }
0 commit comments