import {
AfterViewInit, ChangeDetectorRef,
Component,
ElementRef,
EventEmitter, HostListener,
Input,
OnDestroy,
OnInit,
Output,
ViewChild
} from "@angular/core";
import {fromEvent, Subscription} from 'rxjs';
import {delay} from "rxjs/operators";
import {HelperFunctions} from "../../HelperFunctions.class";
declare var UIkit;
declare var ResizeObserver;
@Component({
selector: 'fs-modal',
template: `
`
})
export class FullScreenModalComponent implements AfterViewInit, OnDestroy {
private static FS_MODAL_COUNTER: number = 0;
id: string = "fs-modal";
@Input() classTitle: string = "uk-background-primary uk-light";
@Input() classBody: string = 'uk-container-large';
back: boolean = false;
title: string;
okButton: boolean = false;
okButtonText = 'OK';
@Input()
okButtonDisabled = false;
@Output()
okEmitter: EventEmitter = new EventEmitter();
@Output()
cancelEmitter: EventEmitter = new EventEmitter();
@ViewChild('modal') modal: ElementRef;
@ViewChild('header') header: ElementRef;
@ViewChild('body') body: ElementRef;
observer: any;
headerHeight: number;
bodyHeight: number;
constructor(private cdr: ChangeDetectorRef) {
}
@HostListener('window:resize', ['$event'])
onResize() {
this.changeHeight();
}
ngOnInit() {
FullScreenModalComponent.FS_MODAL_COUNTER++;
this.id = 'fs-modal-' + FullScreenModalComponent.FS_MODAL_COUNTER;
}
ngAfterViewInit() {
if(typeof window !== "undefined") {
this.observer = new ResizeObserver(entries => {
for (let entry of entries) {
this.changeHeight();
}
});
this.observer.observe(this.header.nativeElement);
}
}
/* Height = Viewport - header - (Body padding) */
changeHeight() {
if(typeof window !== "undefined" && this.header) {
this.bodyHeight = window.innerHeight - this.header.nativeElement.clientHeight - 80;
this.cdr.detectChanges();
}
}
ngOnDestroy() {
if(this.observer && this.observer instanceof ResizeObserver) {
this.observer.disconnect();
}
if(typeof document !== "undefined") {
const element = document.getElementById("modal-container");
for (let i = element.childNodes.length - 1; i >= 0; --i) {
let child: ChildNode = element.childNodes[i];
if (child['id'] == this.id) {
child.remove();
}
}
}
}
get isOpen() {
return this.modal && UIkit.modal(this.modal.nativeElement).isToggled();
}
open() {
UIkit.modal(this.modal.nativeElement).show();
HelperFunctions.scroll();
}
cancel() {
UIkit.modal(this.modal.nativeElement).hide();
HelperFunctions.scroll();
this.cancelEmitter.emit();
}
ok() {
UIkit.modal(this.modal.nativeElement).hide();
HelperFunctions.scroll();
this.okEmitter.emit(true);
}
}