import { Component , Input , OnInit , ContentChild } from '@angular/core' ; import { Location } from '@angular/common' ; import { ModalService } from './modal.service' ; @ Component ( { selector : 'tb-modal' , templateUrl : './modal.component.html' , host : { '(document:keyup.enter)' : 'keyup($event)' , '(document:keyup.escape)' : 'keyup($event)' } } ) export class ModalComponent implements OnInit { @ Input ( 'modal-id' ) modalId = '' ; @ Input ( 'modal-title' ) modalTitle = '' ; @ Input ( ) blocking = false ; @ Input ( ) wide = false ; @ ContentChild ( 'focusMe' , { static : false } ) focusElement : any ; @ ContentChild ( 'defaultAction' , { static : false } ) defaultActionElement : any ; isOpen = false ; animate = true ; constructor ( public modalService , private location ) { } ngOnInit ( ) { this . modalService . registerModal ( this ) ; } close ( checkBlocking = false ) { this . modalService . close ( this . modalId , checkBlocking ) ; const path = this . location . path ( ) . split ( '/' ) ; path . length -= 1 ; this . location . go ( path . join ( '/' ) ) ; } filterClick ( event ) { event = event || window . event ; if ( event . stopPropagation ) { event . stopPropagation ( ) ; } } keyup ( event ) { if ( event . key === 'Escape' && this . isOpen ) { this . close ( true ) ; } if ( event . key === 'Enter' ) { this . clickDefaultAction ( ) ; } } private clickDefaultAction ( ) { if ( this . defaultActionElement ) { this . defaultActionElement . nativeElement . click ( ) ; } } }