forked from shaneapowell/MMM-GmailFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-GmailFeed.js
157 lines (125 loc) · 3.59 KB
/
MMM-GmailFeed.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
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
'use strict';
Module.register("MMM-GmailFeed", {
mailCount: 0,
jsonData: null,
errorData: null,
// Default module config.
defaults: {
updateInterval: 60000,
maxEmails: 5,
maxSubjectLength: 40,
maxFromLength: 15,
playSound: true
},
start: function () {
this.getJson();
this.scheduleUpdate();
},
scheduleUpdate: function () {
var self = this;
setInterval(function () {
self.getJson();
}, this.config.updateInterval);
},
// Define required scripts.
getStyles: function () {
return ["MMM-GmailFeed.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Request node_helper to get json from url
getJson: function () {
this.sendSocketNotification("MMM-GmailFeed_GET_JSON", this.config);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-GmailFeed_JSON_RESULT") {
// Only continue if the notification came from the request we made
// This way we can load the module more than once
if (payload.username === this.config.username) {
this.jsonData = payload.data;
this.errorData = null;
this.updateDom(500);
}
}
if (notification === "MMM-GmailFeed_JSON_ERROR") {
if(payload.username === this.config.username) {
this.jsonData = null;
this.errorData = "Error: [" + payload.error + "]";
this.updateDom(500);
}
}
},
// Override getHeader method.
getHeader: function() {
if (!this.jsonData) {
return "GmailFeed";
}
if (this.config.playSound && this.jsonData.fullcount > this.mailCount) {
new Audio(this.file("eventually.mp3")).play();
}
this.mailCount = this.jsonData.fullcount;
return this.jsonData.title + " - " + this.jsonData.fullcount;
},
// Override dom generator.
getDom: function () {
var table = document.createElement("table");
table.classList.add("mailtable");
if (this.errorData) {
table.innerHTML = this.errorData;
return table;;
}
if (!this.jsonData) {
table.innerHTML = "Loading...";
return table;
}
if (!this.jsonData.entry) {
var row = document.createElement("tr");
table.append(row);
var cell = document.createElement("td");
row.append(cell);
cell.append(document.createTextNode("No New Mail"));
cell.setAttribute("colspan", "4");
return table;
}
var items = this.jsonData.entry;
// If the items is null, no new messages
if (!items) {
return table;
}
// If the items is not an array, it's a single entry
if (!Array.isArray(items)) {
items = [ items ]
}
items.forEach(element => {
var row = this.getTableRow(element);
table.appendChild(row);
});
return table;
},
getTableRow: function (jsonObject) {
var row = document.createElement("tr");
row.classList.add("normal");
var fromNode = document.createElement("td");
var subjNode = document.createElement("td");
var dtNode = document.createElement("td");
var tmNode = document.createElement("td");
var issueDt = moment(jsonObject.issued);
fromNode.append(document.createTextNode(jsonObject.author.name.substring(0, this.config.maxFromLength)));
subjNode.append(document.createTextNode(jsonObject.title.substring(0, this.config.maxSubjectLength)));
if (!issueDt.isSame(new Date(), "day")) {
dtNode.append(document.createTextNode(issueDt.format("MMM DD - ")));
}
tmNode.append(document.createTextNode(issueDt.format("h:mm a")));
fromNode.classList.add("colfrom");
subjNode.classList.add("colsubj");
dtNode.classList.add("coldt");
tmNode.classList.add("coltm");
row.append(fromNode);
row.append(subjNode);
row.append(dtNode);
row.append(tmNode);
return row;
}
});