modal: add onopen/onclose

pull/97/head
Harvey Tindall 4 years ago
parent 99875b9176
commit 2816c6277d
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -3,8 +3,12 @@ declare var window: Window;
export class Modal implements Modal { export class Modal implements Modal {
modal: HTMLElement; modal: HTMLElement;
closeButton: HTMLSpanElement; closeButton: HTMLSpanElement;
openEvent: CustomEvent;
closeEvent: CustomEvent;
constructor(modal: HTMLElement, important: boolean = false) { constructor(modal: HTMLElement, important: boolean = false) {
this.modal = modal; this.modal = modal;
this.openEvent = new CustomEvent("modal-open-" + modal.id)
this.closeEvent = new CustomEvent("modal-close-" + modal.id)
const closeButton = this.modal.querySelector('span.modal-close') const closeButton = this.modal.querySelector('span.modal-close')
if (closeButton !== null) { if (closeButton !== null) {
this.closeButton = closeButton as HTMLSpanElement; this.closeButton = closeButton as HTMLSpanElement;
@ -26,11 +30,21 @@ export class Modal implements Modal {
modal.classList.remove('modal-shown'); modal.classList.remove('modal-shown');
modal.classList.remove('modal-hiding'); modal.classList.remove('modal-hiding');
modal.removeEventListener(window.animationEvent, listenerFunc); modal.removeEventListener(window.animationEvent, listenerFunc);
document.dispatchEvent(this.closeEvent);
}; };
this.modal.addEventListener(window.animationEvent, listenerFunc, false); this.modal.addEventListener(window.animationEvent, listenerFunc, false);
} }
set onopen(f: () => void) {
document.addEventListener("modal-open-"+this.modal.id, f);
}
set onclose(f: () => void) {
document.addEventListener("modal-close-"+this.modal.id, f);
}
show = () => { show = () => {
this.modal.classList.add('modal-shown'); this.modal.classList.add('modal-shown');
document.dispatchEvent(this.openEvent);
} }
toggle = () => { toggle = () => {
if (this.modal.classList.contains('modal-shown')) { if (this.modal.classList.contains('modal-shown')) {

Loading…
Cancel
Save