import {DOCUMENT} from '@angular/common'; import { Component, Output, EventEmitter, Input, Inject, ElementRef, Renderer2, OnInit, AfterViewInit, OnDestroy } from '@angular/core'; import {ModalDismissReasons} from './modal-dismiss-reasons'; @Component({ selector: 'ngb-modal-window', host: { '[class]': '"modal fade show d-block" + (windowClass ? " " + windowClass : "")', 'role': 'dialog', 'tabindex': '-1', '(keyup.esc)': 'escKey($event)', '(click)': 'backdropClick($event)' }, template: `
` }) export class NgbModalWindow implements OnInit, AfterViewInit, OnDestroy { private _document: any; private _elWithFocus: Element; // element that is focused prior to modal opening @Input() backdrop: boolean | string = true; @Input() centered: string; @Input() keyboard = true; @Input() size: string; @Input() windowClass: string; @Output('dismiss') dismissEvent = new EventEmitter(); constructor(@Inject(DOCUMENT) document, private _elRef: ElementRef, private _renderer: Renderer2) { this._document = document; } backdropClick($event): void { if (this.backdrop === true && this._elRef.nativeElement === $event.target) { this.dismiss(ModalDismissReasons.BACKDROP_CLICK); } } escKey($event): void { if (this.keyboard && !$event.defaultPrevented) { this.dismiss(ModalDismissReasons.ESC); } } dismiss(reason): void { this.dismissEvent.emit(reason); } ngOnInit() { this._elWithFocus = this._document.activeElement; this._renderer.addClass(this._document.body, 'modal-open'); } ngAfterViewInit() { if (!this._elRef.nativeElement.contains(document.activeElement)) { this._elRef.nativeElement['focus'].apply(this._elRef.nativeElement, []); } } ngOnDestroy() { const body = this._document.body; const elWithFocus = this._elWithFocus; let elementToFocus; if (elWithFocus && elWithFocus['focus'] && body.contains(elWithFocus)) { elementToFocus = elWithFocus; } else { elementToFocus = body; } elementToFocus['focus'].apply(elementToFocus, []); this._elWithFocus = null; this._renderer.removeClass(body, 'modal-open'); } }