forked from Dmitry1987/vault-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Notify.js
86 lines (77 loc) · 2.54 KB
/
Notify.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
/* eslint-disable no-unused-vars */
class Notify {
constructor(node) {
this.node = node;
this.messages = [];
}
/**
* @param {String} message will be parsed to HTML
* @param {Object} options
* @param {Boolean} [options.removeOption] wether or not to show the ✖
* @param {Number} [options.time] when declared, notification will disappear after Xms
* @returns this
*/
error(message, options) {
// eslint-disable-next-line
return this.message({ level: 'error', message, ...options });
}
/**
* @param {String} message will be parsed to HTML
* @param {Object} options
* @param {Boolean} [options.removeOption] wether or not to show the ✖
* @param {Number} [options.time] when declared, notification will disappear after Xms
* @returns this
*/
success(message, options) {
// eslint-disable-next-line
return this.message({ level: 'success', message, ...options });
}
/**
* @param {String} message will be parsed to HTML
* @param {Object} options
* @param {Boolean} [options.removeOption] wether or not to show the ✖
* @param {Number} [options.time] when declared, notification will disappear after Xms
* @returns this
*/
info(message, options) {
// eslint-disable-next-line
return this.message({ level: 'info', message, ...options });
}
/**
* @param {Object} options
* @param {String} options.message will be parsed to HTML
* @param {String} [options.level] info|error|success
* @param {Boolean} [options.removeOption] wether or not to show the ✖
* @param {Number} [options.time] when declared, notification will disappear after Xms
* @returns this
*/
message({ level = 'info', message, time, removeOption = true }) {
const messageNode = document.createElement('div');
messageNode.classList.add('notify', `notify--${level}`);
messageNode.innerHTML = message;
this._append(messageNode);
if (removeOption) this._addRemoveOption(messageNode);
if (time) setTimeout(() => messageNode.remove(), time);
return this;
}
/**
* clears the node from all messages
* @returns this
*/
clear() {
this.node.innerHTML = '';
this.messages = [];
return this;
}
_addRemoveOption(node) {
const removeNode = document.createElement('button');
removeNode.innerHTML = '✖';
removeNode.classList.add('nobutton', 'link', 'notify__button');
removeNode.addEventListener('click', () => node.remove());
node.append(removeNode);
}
_append(node) {
this.messages.push(node);
this.node.append(node);
}
}