-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoasts.js
96 lines (81 loc) · 2.51 KB
/
toasts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {Meteor} from 'meteor/meteor';
import {Template} from "meteor/templating";
import {Blaze} from "meteor/blaze";
import {i18n} from "meteor/universe:i18n";
import './i18n/en.i18n.json';
import './i18n/nl.i18n.json';
import './template';
export class Toast {
static containerSelector = '.toasts';
handle = null;
element = null;
promises = [];
constructor(options) {
const self = this;
this.handle = Blaze.renderWithData(Template.toast, {
...options,
visitor(element) {
self.element = element;
self.element.on('hidden.bs.toast', function () {
Blaze.remove(self.handle);
});
for (let resolve of self.promises)
resolve(element);
}
}, document.body.querySelector(Toast.containerSelector));
}
async getElement() {
return new Promise(function (resolve) {
if (this.element) {
resolve(this.element);
} else {
this.promises.push(resolve);
}
}.bind(this));
}
show() {
return new Promise(function (resolve) {
this.getElement()
.then(function (element) {
resolve(element.toast('show'));
});
}.bind(this));
}
hide() {
return new Promise(function (resolve) {
this.getElement().then(function (element) {
resolve(element.toast('hide'));
});
}.bind(this));
}
static success(message, options = undefined) {
const opts = options || {};
return new Toast({
title: i18n.__('xerdi:toast.successTitle'),
body: message,
'class': 'bg-success',
...opts
}).show();
}
static error(messageOrObject, options = undefined) {
let msg;
switch(typeof(messageOrObject)) {
case 'string':
msg = messageOrObject;
break;
case 'object':
msg = messageOrObject.reason || messageOrObject.message
}
if (!msg)
throw new Meteor.Error('bad argument', 'Wrong argument given.');
const opts = options || {};
return new Toast({
title: i18n.__('xerdi:toast.errorTitle'),
body: msg,
'class': 'bg-danger',
...opts
}).show();
}
}
if (globalThis && !globalThis.Toast)
globalThis.Toast = Toast;