-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
93 lines (81 loc) · 2.69 KB
/
main.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
var Nanocomponent = require('nanocomponent')
var html = require('nanohtml')
class Notifications extends Nanocomponent {
constructor (opts) {
opts = opts || {}
super()
this.repo = opts.repo
this.icons = Object.assign({
error: 'octicon octicon-flame',
warning: 'octicon octicon-alert',
info: 'octicon octicon-info',
success: 'octicon octicon-check',
close: 'octicon octicon-x'
}, opts.icons)
this.getMessageBody = this.getMessageBody.bind(this)
this.notifications = []
this.renderedLength = -1
}
add (notification) {
this.notifications.push(notification)
this.rerender()
}
info (text) {
this.add({ type: 'info', message: text })
}
error (text) {
this.add({ type: 'error', message: text })
}
warning (text) {
this.add({ type: 'warning', message: text })
}
success (text) {
this.add({ type: 'success', message: text })
}
getMessageBody (notification) {
if (notification.element) {
notification.element.className = 'notification-message'
return notification.element
}
if (this.repo && notification.type === 'error') {
return html`
<div class="notification-message">
${notification.message}<br>
<a href="${this.repo}/issues/new?title=${encodeURI(notification.message)}" target="_blank" rel="noopener noreferrer" class="notification-btn">Create an issue for this error</a>
</div>`
} else {
return html`<div class="notification-message">${notification.message}</div>`
}
}
createElement (notifications) {
if (notifications) this.notifications = notifications
this.renderedLength = notifications.length
return html`<div class="notification-container">${
notifications
.map((notification) => {
var classNames = [
'notification',
notification.closed ? 'notification-hidden' : 'notification-show',
'notification-' + (notification.type || 'info')
]
return html`
<div class="${classNames.join(' ')}">
<div class="notification-icon">
<span>
<span class="${this.icons[notification.type || 'info']}"></span>
</span>
</div>
${this.getMessageBody(notification)}
<span onclick=${() => { notification.closed = true; this.rerender() }} class="notification-close" title="Dismiss this notification">
<span class="${this.icons.close}"></span>
</span>
</div>`
})
}
</div>`
}
update (notifications) {
return notifications !== this.notifications || notifications.length !== this.renderedLength
}
}
module.exports = Notifications