wuhu-torn-helper/src/class/utils/DialogMsgBox.ts
2022-11-03 16:34:58 +08:00

55 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Log from "../Log";
import DIALOG_MSG_BOX_HTML from "../../static/html/dialog_msg_box.html";
export default class DialogMsgBox {
private static existed = false;
private readonly container: HTMLElement;
constructor(msg: string, opt: DialogMsgBoxOptions) {
Log.info('创建DialogMsgBox', { msg, opt });
let { title = '提示', callback, cancel } = opt;
if (!callback) {
Log.error('无callback');
throw new Error('无callback');
}
if (DialogMsgBox.existed) {
Log.error('无法创建DialogMsgBox已存在');
throw new Error('无法创建DialogMsgBox已存在');
}
this.container = document.createElement('div');
this.container.id = 'wh-dialog';
this.container.innerHTML = DIALOG_MSG_BOX_HTML.replace('{{}}', title).replace('{{}}', msg);
let [confirm, cancelBtn] = Array.from(this.container.querySelectorAll('button'));
confirm.addEventListener('click', () => {
callback(this.container);
this.destroy();
});
cancelBtn.addEventListener('click', () => {
cancel ? cancel() : null;
this.destroy();
});
document.body.append(this.container);
// this.hideChat();
DialogMsgBox.existed = true;
}
// private hideChat() {
// document.querySelector('#chatRoot').classList.add('wh-hide');
// }
//
// private showChat() {
// document.querySelector('#chatRoot').classList.remove('wh-hide');
// }
private destroy() {
this.container.remove();
// this.showChat();
DialogMsgBox.existed = false;
}
}
interface DialogMsgBoxOptions {
title?: string;
callback: Function;
cancel?: Function;
}