-
Notifications
You must be signed in to change notification settings - Fork 8
/
MMM-Mail.js
126 lines (111 loc) · 3.58 KB
/
MMM-Mail.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
Module.register("MMM-Mail",{
defaults:{
host: '',
port: '',
user: '',
pass: '',
subjectlength: 50,
},
messages: [], //The storage for the Mails
start: function(){
console.log("Email module started!");
this.sendSocketNotification('LISTEN_EMAIL',this.config);
this.loaded = false;
},
socketNotificationReceived: function(notification, payload){
if(payload.user==this.config.user)
{
if (notification === 'EMAIL_FETCH'){
if(payload.messages){
this.messages.length = 0; //clear Message storage
console.log("Email-Fetch Event");
this.messages = payload.messages;
if(this.messages.length>0)
{
console.log(this.messages[0].id);
this.messages.sort(function(a,b) {return b.id - a.id; });
}
this.updateDom(2000);
}
}
if(notification === 'EMAIL_NEWMAIL')
{
var sender;
if(payload.sender.name.length>0)
{
sender = payload.sender.name;
}
else
{
sender = payload.sender.address;
}
this.sendNotification("SHOW_ALERT",{
type: "notification",
title: "New Email on "+payload.user,
message: "from "+sender
});
}
if(notification === 'EMAIL_ERROR')
{
console.log("Email module restarted!");
this.sendSocketNotification('LISTEN_EMAIL',this.config);
}
}
},
// Define required scripts.
getStyles: function() {
return ["email.css", "font-awesome.css"];
},
getDom: function(){
var wrapper = document.createElement("table");
wrapper.className = "small";
var that =this;
if(this.messages.length > 0)
{
var count = 0;
this.messages.slice(0,this.config.numberOfEmails).forEach(function (mailObj) {
var name = mailObj.sender[0].name.replace(/['"]+/g,"");
var subject = mailObj.subject.replace(/[\['"\]]+/g,"");
var emailWrapper = document.createElement("tr");
emailWrapper.className = "normal";
var nameWrapper = document.createElement("tr");
nameWrapper.className = "bright";
if(name.length)
{
nameWrapper.innerHTML = name;
}
else
{
nameWrapper.innerHTML = mailObj.sender[0].address;
}
emailWrapper.appendChild(nameWrapper);
var subjectWrapper = document.createElement("tr");
subjectWrapper.className = "light";
//cut the subject
if(subject.length > that.config.subjectlength)
{
subject = subject.substring(0,that.config.subjectlength);
}
subjectWrapper.innerHTML = subject;
emailWrapper.appendChild(subjectWrapper);
wrapper.appendChild(emailWrapper);
// Create fade effect.
if (that.config.fade) {
var startingPoint = that.messages.slice(0,that.config.numberOfEmails).length * 0.25;
var steps = that.messages.slice(0,that.config.numberOfEmails).length - startingPoint;
if (count >= startingPoint) {
var currentStep = count - startingPoint;
emailWrapper.style.opacity = 1 - (1 / steps * currentStep);
}
}
count++;
});
}
else{
wrapper.innerHTML = "No Unread mails";
wrapper.className = "small dimmed";
return wrapper;
}
return wrapper;
}
});