import {Component, ElementRef, EventEmitter, Input, OnInit, Output} from "@angular/core";
import {fromEvent, Subscription} from 'rxjs';
import {delay} from "rxjs/operators";
@Component({
selector: 'fs-modal',
template: `
`
})
export class FullScreenModalComponent implements OnInit {
okButtonText = 'OK';
cancelButtonText = 'Cancel';
cancelButton: boolean = false;
okButton: boolean = false;
title: string = null;
@Input()
okButtonDisabled = false;
@Input()
cancelButtonDisabled = false;
@Output()
okEmitter: EventEmitter = new EventEmitter();
@Output()
cancelEmitter: EventEmitter = new EventEmitter();
opened: boolean = false;
private readonly element: any;
private subscriptions: any[] = [];
constructor(private el: ElementRef) {
this.element = el.nativeElement;
}
ngOnInit() {
if (typeof document !== "undefined") {
document.body.appendChild(this.element);
this.subscriptions.push(fromEvent(document, 'keydown').pipe(delay(1)).subscribe((event: KeyboardEvent) => {
if(event.keyCode === 27) {
this.close();
}
}));
}
}
ngOnDestroy(): void {
this.element.remove();
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
})
}
open() {
if (typeof document !== "undefined") {
this.opened = true;
document.getElementsByTagName('html')[0].classList.add('fs-modal-open');
}
}
close() {
if (typeof document !== "undefined") {
this.opened = false;
document.getElementsByTagName('html')[0].classList.remove('fs-modal-open');
this.cancelEmitter.emit();
}
}
ok() {
this.close();
this.okEmitter.emit(true);
}
}