Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·180 lines (125 loc) · 3.08 KB
/
Copy pathindex.js
File metadata and controls
executable file
·180 lines (125 loc) · 3.08 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* For new notification types extend Notification
*/
var delegate = require('client/delegate');
/**
* Calculates translateY positions when notifications are added/removed
*/
class NotificationManager {
constructor(options = {}) {
this.notifications = [];
this.verticalSpace = options.verticalSpace || 8;
}
register(notification) {
this.notifications.unshift(notification);
setTimeout(() => this.recalculate(), 20);
}
unregister(notification) {
var idx = this.notifications.indexOf(notification);
this.notifications.splice(idx, 1);
this.recalculate();
}
recalculate() {
var top = this.verticalSpace;
this.notifications.forEach(notification => {
notification.top = top;
top += notification.height + this.verticalSpace;
});
}
}
var manager;
exports.init = function(options) {
manager = new NotificationManager(options);
};
class Notification {
constructor(html, type, timeout) {
var elemHtml = `<div class="notification notification_popup notification_${type}">
<div class="notification__content">${html}</div>
<button title="Закрыть" class="notification__close"></button></div>`;
document.body.insertAdjacentHTML("beforeEnd", elemHtml);
this.elem = document.body.lastElementChild;
switch(timeout) {
case undefined:
this.timeout = this.TIMEOUT_DEFAULT;
break;
case 'slow':
this.timeout = this.TIMEOUT_SLOW;
break;
case 'fast':
this.timeout = this.TIMEOUT_FAST;
break;
default:
this.timeout = timeout;
}
manager.register(this);
this.setupCloseHandler();
this.setupCloseTimeout();
}
get TIMEOUT_DEFAULT() {
return 2500;
}
get TIMEOUT_SLOW() {
return 5000;
}
get TIMEOUT_FAST() {
return 1500;
}
close() {
if (!this.elem.parentNode) return; // already closed (by user click?)
this.elem.remove();
manager.unregister(this);
}
setupCloseHandler() {
this.delegate('.notification__close', 'click', () => this.close());
}
setupCloseTimeout() {
if (this.timeout) {
setTimeout(() => this.close(), this.timeout);
}
}
get height() {
return this.elem.offsetHeight;
}
set top(value) {
this.elem.style.transform = 'translateY(' + value + 'px)';
}
}
delegate.delegateMixin(Notification.prototype);
class Info extends Notification {
constructor(html, timeout) {
super(html, 'info', timeout);
}
}
exports.Info = Info;
class Warning extends Notification {
constructor(html, timeout) {
super(html, 'warning', timeout);
}
}
exports.Warning = Warning;
class Success extends Notification {
constructor(html, timeout) {
super(html, 'success', timeout);
}
}
exports.Success = Success;
export class Error extends Notification {
constructor(html, timeout) {
super(html, 'error', timeout);
}
get TIMEOUT_DEFAULT() {
return 5000;
}
}
exports.Error = Error;
/*
export class Test extends Notification {
constructor(html) {
super(html, 'error');
}
get TIMEOUT_DEFAULT() {
return null;
}
}
exports.Test = Test;
*/