This commit is contained in:
Liwanyi 2022-09-23 18:23:09 +08:00
parent 6b85937b46
commit e6b3477c71

View File

@ -2,10 +2,10 @@ import Utils from "./Utils";
import addStyle from "../../func/utils/addStyle"; import addStyle from "../../func/utils/addStyle";
export default class Alert extends Utils { export default class Alert extends Utils {
static hasContainer: boolean = false; // static hasContainer: boolean = false;
static container: HTMLElement = null; static container: HTMLElement = null;
notify: HTMLElement = null; notify: MyHTMLElement = null;
callback: Function = null; intervalID = -1;
constructor(msg: string, options: WHNotifyOpt = {}) { constructor(msg: string, options: WHNotifyOpt = {}) {
super(); super();
@ -25,11 +25,12 @@ export default class Alert extends Utils {
// 通知的容器 // 通知的容器
if (!Alert.container) Alert.initContainer(); if (!Alert.container) Alert.initContainer();
this.notify = Alert.create(msg); this.notify = Alert.create(msg, timeout);
this.intervalID = this.notify.loopId;
this.callback = callback; this.callback = callback;
} }
static create(msg) { static create(msg, timeout) {
const date = new Date(); const date = new Date();
// 通知的唯一id // 通知的唯一id
const uid = '' + Utils.getRandomInt(1000, 9999); const uid = '' + Utils.getRandomInt(1000, 9999);
@ -53,11 +54,11 @@ export default class Alert extends Utils {
// 通知进度条 // 通知进度条
let progressCount = 101; let progressCount = 101;
// 删除通知 // 删除通知
new_node.close = () => { // new_node.close = () => {
clearInterval(intervalID); // clearInterval(intervalID);
new_node.remove(); // new_node.remove();
callback(); // callback();
}; // };
// 计时器 // 计时器
let intervalID = window.setInterval(() => { let intervalID = window.setInterval(() => {
if (mouse_enter) { if (mouse_enter) {
@ -70,9 +71,12 @@ export default class Alert extends Utils {
if (progressCount === 0) new_node.remove(); if (progressCount === 0) new_node.remove();
}, timeout * 1000 / 100); }, timeout * 1000 / 100);
new_node.querySelector('.wh-notify-close').addEventListener('click', new_node.close); new_node.querySelector('.wh-notify-close').addEventListener('click', new_node.close);
new_node.loopId = intervalID;
return new_node; return new_node;
} }
callback: Function = () => null;
static initContainer() { static initContainer() {
this.container = document.createElement('div'); this.container = document.createElement('div');
this.container.id = 'wh-notify'; this.container.id = 'wh-notify';
@ -123,6 +127,8 @@ cursor: pointer;
close() { close() {
this.notify.remove(); this.notify.remove();
this.notify = null; this.notify = null;
this.callback();
clearInterval(this.intervalID);
} }
} }