forked from timdows/MMM-JsonTable
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMMM-GmailFeed.js
199 lines (171 loc) · 5.41 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"use strict";
Module.register("MMM-GmailFeed", {
mailCount: 0,
jsonData: null,
errorData: null,
// Default module config.
defaults: {
updateInterval: 5 * 60 * 1000,
maxEmails: 5,
maxSubjectLength: 40,
maxFromLength: 15,
playSound: true,
autoHide: false,
displayMode: "table",
color: true,
showEmailAdressInHeader: true
},
start () {
this.getJson();
this.scheduleUpdate();
},
scheduleUpdate () {
const self = this;
setInterval(() => {
self.getJson();
}, this.config.updateInterval);
},
// Define required scripts.
getStyles () {
return ["MMM-GmailFeed.css"];
},
// Define required scripts.
getScripts () {
return ["moment.js"];
},
// Request node_helper to get json from url
getJson () {
this.sendSocketNotification("MMM-GmailFeed_GET_JSON", this.config);
},
socketNotificationReceived (notification, payload) {
// 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) {
if (notification === "MMM-GmailFeed_JSON_RESULT") {
this.jsonData = payload.data;
this.errorData = null;
this.updateDom(500);
}
if (notification === "MMM-GmailFeed_JSON_ERROR") {
this.jsonData = null;
this.errorData = `Error: [${payload.error}]`;
this.updateDom(500);
}
}
},
// Override getHeader method.
getHeader () {
let result;
if (this.jsonData) {
if (this.config.playSound && this.jsonData.fullcount > this.mailCount) {
new Audio(this.file("eventually.mp3")).play();
}
this.mailCount = this.jsonData.fullcount;
if (this.config.displayMode === "table") {
if (this.jsonData.fullcount === "0" && this.config.autoHide) {
this.jsonData.title = "";
} else if (this.config.showEmailAdressInHeader) {
result = `${this.jsonData.title} - ${this.jsonData.fullcount}`;
} else {
result = `GMAIL INBOX - ${this.jsonData.fullcount}`;
}
} else if (this.config.displayMode === "notification") {
this.jsonData.title = "";
}
} else {
result = "GmailFeed";
}
return result;
},
// Override dom generator.
getDom () {
const 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.fullcount === "0" && this.config.autoHide) {
table.classList.add("hidden");
}
if (!this.jsonData.entry) {
const row = document.createElement("tr");
table.append(row);
if (this.config.displayMode === "table") {
const cell = document.createElement("td");
row.append(cell);
cell.append(document.createTextNode("No New Mail"));
cell.setAttribute("colspan", "4");
return table;
}
}
let items = this.jsonData.entry;
// If the items is null, no new messages
if (this.config.displayMode === "table" &&
!items) {
return table;
}
// If the items is not an array, it's a single entry
if (!Array.isArray(items)) {
items = [items];
}
if (this.config.displayMode === "table") {
for (const element of items) {
const row = this.getTableRow(element);
table.append(row);
}
} else if (this.config.displayMode === "notification") {
const z = document.createElement("a");
z.setAttribute("height", "50px");
z.setAttribute("width", "100px");
z.setAttribute("href", "#");
z.classList.add("notification");
const logo = document.createElement("img");
if (this.config.color === true) {
logo.setAttribute("src", "/modules/MMM-GmailFeed/Gmail-logo.png");
} else if (this.config.color === false) {
logo.setAttribute(
"src",
"/modules/MMM-GmailFeed/Gmail-logo-grayscale.png"
);
}
logo.setAttribute("height", "50px");
logo.setAttribute("width", "50px");
const x = document.createElement("span");
x.classList.add("badge");
x.innerHTML = this.jsonData.fullcount;
z.append(x);
z.append(logo);
table.append(z);
}
return table;
},
getTableRow (jsonObject) {
const row = document.createElement("tr");
row.classList.add("normal");
const fromNode = document.createElement("td");
const subjNode = document.createElement("td");
const dtNode = document.createElement("td");
const tmNode = document.createElement("td");
const issueDt = moment(jsonObject.issued);
fromNode.append(document.createTextNode(jsonObject.author.name.slice(0, Math.max(0, this.config.maxFromLength))));
subjNode.append(document.createTextNode(jsonObject.title.slice(0, Math.max(0, this.config.maxSubjectLength))));
if (!issueDt.isSame(new Date(Date.now()), "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;
}
});