-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathemail.js
144 lines (120 loc) · 5.37 KB
/
email.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
/**
* Created by debayan on 7/24/16.
*/
Module.register("email",{
// defaults : [
// {
// user: 'a@b.com',
// password: 'xxx',
// host: 'jjj.kkk.com',
// port: 993,
// tls: true,
// authTimeout: 10000,
// numberOfEmails: 5,
// fade: true,
// maxCharacters: 30
// }
// ]
payload: [],
start : function(){
console.log("Email module started!");
console.log('MEMEME', this.config);
this.sendSocketNotification('LISTEN_EMAIL',{config: this.config, payload: this.payload, loaded: this.loaded});
this.loaded = false;
},
socketNotificationReceived: function(notification, payload){
if (notification === 'EMAIL_RESPONSE'){
if(payload){
this.loaded = true;
var that = this;
console.log("NEW PAYLOAD: ", payload);
var payloadIds = that.payload.map(function(m) {return m.id});
payload.forEach(function(m){
if(payloadIds.indexOf(m.id) == -1)
that.payload.push(m);
});
this.payload.sort(function(a,b) {return b.id - a.id; });
this.sendSocketNotification('LISTEN_EMAIL',{config: this.config, payload: this.payload, loaded: this.loaded});
this.updateDom(2000);
}
}
},
// 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.payload.length > 0)
{
if (typeof that.config.accounts !== "undefined") {
var indexToRemove = [];
for (var i = 0; i < this.config.accounts.length; i++) {
var maxNumEmails = this.config.accounts[i].numberOfEmails;
var count = 0;
for (var j = 0; j < this.payload.length; j++) {
if (this.payload[j].host === this.config.accounts[i].user) {
count++;
}
if (count > maxNumEmails) {
indexToRemove.push(j);
}
}
}
for (var j = 0; j < this.payload.length; j++) {
if (indexToRemove.indexOf(j) > -1) {
delete this.payload[j];
}
}
this.payload.forEach(function (mailObj) {
var host = mailObj.host.slice(0,1) + '@' + mailObj.host.substr(mailObj.host.indexOf('@') + 1)[0];
var name = mailObj.sender[0].name.replace(/['"]+/g, "");
name = name.substring(0, that.config.maxCharacters);
var subject = mailObj.subject.replace(/[\['"\]]+/g, "");
subject = subject.substring(0, that.config.maxCharacters);
var emailWrapper = document.createElement("tr");
emailWrapper.className = "normal";
var senderWrapper = document.createElement("tr");
senderWrapper.className = "normal";
var nameWrapper = document.createElement("td");
nameWrapper.className = "bright";
nameWrapper.setAttribute("data-letters", host);
nameWrapper.innerHTML = name;
senderWrapper.appendChild(nameWrapper);
var addressWrapper = document.createElement("td");
addressWrapper.className = "address xsmall thin dimmed";
addressWrapper.innerHTML = mailObj.sender[0].address;
senderWrapper.appendChild(addressWrapper);
emailWrapper.appendChild(senderWrapper);
var subjectWrapper = document.createElement("tr");
subjectWrapper.className = "light";
subjectWrapper.innerHTML = subject;
emailWrapper.appendChild(subjectWrapper);
wrapper.appendChild(emailWrapper);
// Calculate total possible emails
var totalEmails = 0;
for (var i = 0; i < that.config.accounts.length; i++) {
totalEmails += that.config.accounts[i].numberOfEmails;
}
// Create fade effect.
if (that.config.fade) {
var startingPoint = that.payload.slice(0, totalEmails).length * 0.25;
var steps = that.payload.slice(0, that.config.numberOfEmails).length - startingPoint;
if (count >= startingPoint) {
var currentStep = count - startingPoint;
emailWrapper.style.opacity = 1 - (1 / steps * currentStep);
}
}
});
}
}
else{
wrapper.innerHTML = (this.loaded) ? "No new mails" : this.translate("LOADING");
wrapper.className = "small dimmed";
return wrapper;
}
return wrapper;
}
});