-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-GitHub-Notifications.js
95 lines (71 loc) · 2.57 KB
/
MMM-GitHub-Notifications.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
Module.register('MMM-GitHub-Notifications', {
notifications: [],
defaults: {
updateInterval: 10 * 60 * 1000,
maxContentLength: 30,
maxNotifications: 5
},
start: function () {
this.getNotifications();
this.scheduleUpdate();
},
scheduleUpdate: function () {
var self = this;
setInterval(function () { self.getNotifications() }, this.config.updateInterval);
},
getNotifications: function () {
this.sendSocketNotification('MMM-GitHub-Notifications_GET', { config: this.config });
},
getStyles: function () {
return ['./MMM-GitHub-Notifications.css'];
},
getHeader: function () {
return 'GitHub Notifications';
},
getDom: function () {
var table = document.createElement('table');
table.classList.add('notificationsTable');
if (this.notifications.length == 0) {
var row = document.createElement('tr');
table.append(row);
var cell = document.createElement('td');
row.append(cell);
cell.append(document.createTextNode('No new notifications'));
cell.setAttribute('colspan', '4');
return table;
}
this.notifications.forEach(element => {
var row = this.getTableRow(element);
table.appendChild(row);
});
return table;
},
getTableRow: function (notification) {
var self = this;
var row = document.createElement('tr');
var title = notification.title;
if (title.length > self.config.maxContentLength)
title = notification.title.substring(0, self.config.maxContentLength).trim() + '...';
var titleNode = document.createElement('td');
titleNode.append(document.createTextNode(title));
titleNode.classList.add('colTitle');
var repoNode = document.createElement('td');
repoNode.append(document.createTextNode(notification.repository))
row.append(titleNode);
row.append(repoNode);
row.addEventListener('click', function () {
self.sendSocketNotification('MMM-GitHub-Notifications_READ', {
config: self.config,
notificationId: notification.id
});
});
return row;
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'MMM-GitHub-Notifications_RESULT') {
this.notifications.pop();
this.notifications = payload.data;
this.updateDom();
}
}
});