2020-08-06 23:44:37 +02:00
|
|
|
class NotificationHandler {
|
|
|
|
static instance = new NotificationHandler();
|
|
|
|
|
2020-08-05 11:24:59 +02:00
|
|
|
constructor() {
|
2020-08-06 23:44:37 +02:00
|
|
|
this.outer = $('.notification');
|
|
|
|
this.notifications = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
await template.loadTemplate('notification');
|
|
|
|
}
|
|
|
|
|
|
|
|
static createNotification(message, type, time) {
|
|
|
|
time = parseInt(time || "3000");
|
|
|
|
let handler = NotificationHandler.instance,
|
|
|
|
not = new Notification(message, type, time);
|
|
|
|
handler.notifications.push(not);
|
|
|
|
not.show();
|
|
|
|
return not;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Notification {
|
|
|
|
constructor(message, type, time) {
|
|
|
|
this.outer = NotificationHandler.instance.outer;
|
|
|
|
this.message = message;
|
|
|
|
this.type = type;
|
|
|
|
this.time = time;
|
|
|
|
this.isRemoved = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async show() {
|
|
|
|
let self = this,
|
|
|
|
endless = self.time === -1;
|
|
|
|
|
|
|
|
self.item = create('div');
|
|
|
|
self.item.addClass('notification-item, ' + self.type);
|
|
|
|
if (endless) {
|
|
|
|
self.type += ' endless';
|
|
|
|
}
|
|
|
|
self.updateContent(self.message);
|
2020-08-07 19:31:30 +02:00
|
|
|
this.outer.prepend(self.item);
|
2020-08-06 23:44:37 +02:00
|
|
|
if (!endless) {
|
|
|
|
setTimeout(this.remove.bind(this), self.time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async remove() {
|
|
|
|
if (this.isRemoved) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isRemoved = true;
|
|
|
|
this.outer.removeChild(this.item);
|
|
|
|
let not = NotificationHandler.instance.notifications,
|
|
|
|
index = not.indexOf(this);
|
|
|
|
not.splice(index, 1);
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateContent(message) {
|
|
|
|
let self = this,
|
|
|
|
isEndless = self.time === -1,
|
|
|
|
data = {
|
|
|
|
message: message,
|
|
|
|
time: isEndless ? 1000 : self.time + 1,
|
|
|
|
type: self.type,
|
|
|
|
}
|
|
|
|
this.item.innerHTML = template.parseTemplate('notification', data);
|
|
|
|
}
|
2020-08-05 11:24:59 +02:00
|
|
|
|
2020-08-06 23:44:37 +02:00
|
|
|
updateMessageOnly(message) {
|
|
|
|
let item = $('.message', this.item);
|
|
|
|
if (item) {
|
|
|
|
item.innerHTML = message;
|
|
|
|
}
|
2020-08-05 11:24:59 +02:00
|
|
|
}
|
|
|
|
}
|