2021-01-05 18:16:23 +00:00
|
|
|
declare var window: Window;
|
|
|
|
|
|
|
|
export class Modal implements Modal {
|
|
|
|
modal: HTMLElement;
|
|
|
|
closeButton: HTMLSpanElement;
|
2021-05-07 12:22:07 +00:00
|
|
|
openEvent: CustomEvent;
|
|
|
|
closeEvent: CustomEvent;
|
2021-01-05 18:16:23 +00:00
|
|
|
constructor(modal: HTMLElement, important: boolean = false) {
|
|
|
|
this.modal = modal;
|
2021-05-07 13:30:30 +00:00
|
|
|
this.openEvent = new CustomEvent("modal-open-" + modal.id);
|
|
|
|
this.closeEvent = new CustomEvent("modal-close-" + modal.id);
|
|
|
|
const closeButton = this.modal.querySelector('span.modal-close');
|
2021-01-05 18:16:23 +00:00
|
|
|
if (closeButton !== null) {
|
|
|
|
this.closeButton = closeButton as HTMLSpanElement;
|
|
|
|
this.closeButton.onclick = this.close;
|
|
|
|
}
|
|
|
|
if (!important) {
|
|
|
|
window.addEventListener('click', (event: Event) => {
|
|
|
|
if (event.target == this.modal) { this.close(); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close = (event?: Event) => {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
this.modal.classList.add('modal-hiding');
|
|
|
|
const modal = this.modal;
|
2021-05-07 13:30:30 +00:00
|
|
|
const listenerFunc = () => {
|
2021-01-05 18:16:23 +00:00
|
|
|
modal.classList.remove('modal-shown');
|
|
|
|
modal.classList.remove('modal-hiding');
|
|
|
|
modal.removeEventListener(window.animationEvent, listenerFunc);
|
2021-05-07 12:22:07 +00:00
|
|
|
document.dispatchEvent(this.closeEvent);
|
2021-01-05 18:16:23 +00:00
|
|
|
};
|
|
|
|
this.modal.addEventListener(window.animationEvent, listenerFunc, false);
|
|
|
|
}
|
2021-05-07 12:22:07 +00:00
|
|
|
|
|
|
|
set onopen(f: () => void) {
|
|
|
|
document.addEventListener("modal-open-"+this.modal.id, f);
|
|
|
|
}
|
|
|
|
set onclose(f: () => void) {
|
|
|
|
document.addEventListener("modal-close-"+this.modal.id, f);
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
show = () => {
|
|
|
|
this.modal.classList.add('modal-shown');
|
2021-05-07 12:22:07 +00:00
|
|
|
document.dispatchEvent(this.openEvent);
|
2021-01-05 18:16:23 +00:00
|
|
|
}
|
|
|
|
toggle = () => {
|
|
|
|
if (this.modal.classList.contains('modal-shown')) {
|
|
|
|
this.close();
|
|
|
|
} else {
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|