11import {
2- Component , Input , OnInit ,
3- ComponentFactoryResolver , ViewContainerRef , ViewChild , ElementRef , Renderer2 , AfterContentInit
2+ Component , Input , OnInit , ComponentFactoryResolver , ViewContainerRef , ViewChild ,
3+ ElementRef , Renderer2 , AfterContentInit , EventEmitter , Output , HostListener , OnChanges , AfterViewInit
44} from '@angular/core' ;
55import { ComponentLoaderService } from '../component-loader.service' ;
66
@@ -11,11 +11,13 @@ import { ComponentLoaderService } from '../component-loader.service';
1111 styleUrls : [ './modal-dialog.component.css' ]
1212} )
1313
14- export class ModalDialogComponent implements OnInit , AfterContentInit {
14+ export class ModalDialogComponent implements OnInit , AfterContentInit , AfterViewInit {
1515 @Input ( ) title : string ;
1616 @Input ( ) componentData : string ;
1717 @Input ( ) componentName : any ;
18- public name : any ;
18+ @Output ( ) close = new EventEmitter < any > ( ) ;
19+ public name : any ;
20+ public overlayDiv : any ;
1921
2022 @ViewChild ( 'datacontainer' , { read : ViewContainerRef } ) entry : ViewContainerRef ;
2123
@@ -24,34 +26,59 @@ export class ModalDialogComponent implements OnInit, AfterContentInit {
2426 private ren : Renderer2 ,
2527 private viewContainer : ViewContainerRef ,
2628 private resolver : ComponentFactoryResolver ,
27- private loaderService : ComponentLoaderService
29+ private loaderService : ComponentLoaderService
2830 ) { }
2931
30- public div = this . ren . createElement ( 'div' ) ;
31-
32- ngOnInit ( ) {
33-
32+ public div = this . ren . createElement ( 'div' ) ;
33+
34+ ngOnInit ( ) {
35+ console . log ( 'Modal Component is called.' ) ;
3436 }
35-
36- ngAfterContentInit ( ) {
37+
38+ ngAfterContentInit ( ) {
3739 this . ren . addClass ( this . el . nativeElement . ownerDocument . body , 'modal-open' ) ;
38- this . ren . appendChild ( this . el . nativeElement , this . div ) ;
40+ this . ren . insertBefore ( this . el . nativeElement . children [ 0 ] , this . div , this . el . nativeElement . children [ 0 ] . children [ 0 ] ) ;
3941 this . ren . setAttribute ( this . div , 'class' , 'modal-backdrop fade in' ) ;
42+ this . ren . setAttribute ( this . div , 'tabindex' , '-1' ) ;
4043 this . createModalPopup ( ) ;
44+ console . log ( this . el . nativeElement . children [ 0 ] ) ;
45+ }
46+
47+ ngAfterViewInit ( ) {
48+ this . ren . listen ( this . overlayDiv , 'click' , ( event ) => {
49+ this . closeModal ( ) ;
50+ } ) ;
51+ this . ren . listen ( this . el . nativeElement , 'keydown' , ( event ) => {
52+ console . log ( 'event keydown => ' , event ) ;
53+ if ( event . keyCode === 27 || event . key === 'Escape' || event . which === 27 ) { // ESCAPE key from keyboard
54+ this . closeModal ( ) ;
55+ event . preventDefault ( ) ;
56+ }
57+ } ) ;
4158 }
4259
43- createModalPopup ( ) {
60+ createModalPopup ( ) {
61+ console . log ( 'createModalPopup is called.' ) ;
4462 const name = this . loaderService . getComponent ( this . componentName ) ;
45- console . log ( " Component Name => " , name ) ;
63+ console . log ( ' Component Name => ' , name ) ;
4664 const myFactory = this . resolver . resolveComponentFactory ( < any > name ) ;
4765 const myRef = this . entry . createComponent ( myFactory ) ;
4866 myRef . instance [ 'data' ] = this . componentData ;
67+ this . overlayDiv = this . el . nativeElement . children [ 0 ] ;
68+ console . log ( 'overlayDiv => ' , this . el . nativeElement . children [ 0 ] ) ;
69+ this . setFocus ( ) ;
70+ }
71+
72+ setFocus ( ) {
73+ const focusDiv = this . el . nativeElement . children [ 0 ] . children [ 1 ] . children [ 0 ] ;
74+ console . log ( 'focusDiv => ' , focusDiv ) ;
75+ focusDiv . focus ( ) ;
4976 }
5077
5178 closeModal ( ) {
5279 this . ren . removeClass ( this . el . nativeElement . ownerDocument . body , 'modal-open' ) ;
53- this . ren . removeChild ( this . el . nativeElement , this . div ) ;
5480 this . el . nativeElement . remove ( ) ;
81+ this . close . emit ( 'close' ) ;
5582 }
5683
5784
0 commit comments