mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
modal: add onopen/onclose
This commit is contained in:
parent
99875b9176
commit
2816c6277d
@ -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…
Reference in New Issue
Block a user