class Dialog { config = { body: this.$("body"), AgainFatherlassName: '', AgainCancelClassName: '', // 取消的类名 AgainConfirmClassName: '', // 确定的类名 title: '提示', content: '这是一段信息', cancelText: '取消', confirmText: '确定', beforeCloseType: null, showClose: false, // true 显示,false关闭 confirmEvent: () => {}, cancelEvent: () => {}, }; constructor(option) { this.config.title = option.title || this.config.title; this.config.content = option.content || this.config.content; this.config.cancelText = option.cancelText || this.config.cancelText; this.config.confirmText = option.confirmText || this.config.confirmText; this.config.beforeCloseType = option.beforeCloseType || this.config.beforeCloseType; this.config.showClose = option.showClose || this.config.showClose; this.config.confirmEvent = option.confirmEvent || this.config.confirmEvent; this.config.cancelEvent = option.cancelEvent || this.config.cancelEvent; this.createdDialog(); this.BindCancel(); this.BindConfirm(); } createdDialog() { this.config.body.insertAdjacentHTML( "beforeend", `
${this.config.title}
${ this.config.showClose ? `` : `` }
${this.config.content}
${this.config.cancelText}
${this.config.confirmText}
` ) } createdAgainConfirm() { if (this.$(".again") == null) { this.config.AgainFatherlassName = "again"; this.config.AgainCancelClassName = "againCancel"; this.config.AgainConfirmClassName = "againConfirm"; this.config.title = "" this.config.content = "确认关闭?" this.config.cancelText = "取消" this.config.confirmText = "确定" this.createdDialog(); this.click(".againCancel", ()=> { this.deleteDialog(".again"); }) this.click(".againConfirm", ()=> { this.config.confirmEvent(); this.deleteDialog(".again"); this.deleteDialog(".dialog"); }) } } deleteDialog(className) { this.config.body.removeChild(this.$(className)) } // 取消 BindCancel() { this.click(".dialog_cancel", ()=> { this.isShowConfirm("cancel"); if (this.config.beforeCloseType == null) { this.config.cancelEvent(); } }) } // 确定 BindConfirm() { this.click(".dialog_confirm", ()=> { this.isShowConfirm("confirm") if (this.config.beforeCloseType == null) { this.config.confirmEvent(); } }) } // 判断如何展示确定关闭弹窗 isShowConfirm(type) { if (this.config.beforeCloseType == type) { this.createdAgainConfirm() } else { this.deleteDialog(".dialog"); } } click(className, methods) { this.$(className).onclick = () => methods(); } $(className) { return document.querySelector(className) } } window.Dialog = Dialog