import {
Component,
Output,
EventEmitter,
Input,
ElementRef,
Renderer2,
OnInit,
AfterViewInit,
OnDestroy
} from '@angular/core';
import {ModalDismissReasons} from './modal-dismiss-reasons';
@Component({
selector: 'ng2v-modal-window',
host: {
'[class]': '"modal fade show" + (windowClass ? " " + windowClass : "")',
'role': 'dialog',
'tabindex': '-1',
'style': 'display: block;',
'(keyup.esc)': 'escKey($event)',
'(click)': 'backdropClick($event)'
},
template: `
`
})
export class Ng2vModalWindow implements OnInit,
AfterViewInit, OnDestroy {
private _elWithFocus: Element; // element that is focused prior to modal opening
@Input() backdrop: boolean | string = true;
@Input() keyboard = true;
@Input() size: string;
@Input() windowClass: string;
@Output('dismiss') dismissEvent = new EventEmitter();
constructor(private _elRef: ElementRef, private _renderer: Renderer2) {}
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 = document.activeElement;
this._renderer.addClass(document.body, 'modal-open');
}
ngAfterViewInit() {
if (!this._elRef.nativeElement.contains(document.activeElement)) {
this._elRef.nativeElement['focus'].apply(this._elRef.nativeElement, []);
}
}
ngOnDestroy() {
if (this._elWithFocus && document.body.contains(this._elWithFocus)) {
this._elWithFocus['focus'].apply(this._elWithFocus, []);
} else {
document.body['focus'].apply(document.body, []);
}
this._elWithFocus = null;
this._renderer.removeClass(document.body, 'modal-open');
}
}