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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
document.addEventListener('DOMContentLoaded', function () {
const emailRows = document.querySelectorAll('.email-row');
const preview = document.querySelector('.preview');
const prefsData = document.getElementById('mail-preferences');
let currentEmailId = null;
let markAsReadTimer = null;
// Parse preferences from data attributes
const prefs = {
MarkMessagesAsRead: prefsData ? prefsData.dataset.markAsRead : 'Immediately',
ShowEmailAddressWithDisplayName: prefsData ? prefsData.dataset.showAddress === 'true' : true,
DisplayHTML: prefsData ? prefsData.dataset.displayHtml === 'true' : true,
LoadRemoteContent: prefsData ? prefsData.dataset.loadRemote : 'Never'
};
emailRows.forEach(row => {
row.addEventListener('click', async function (e) {
if (e.target.closest('.email-flag')) {
return;
}
const emailId = this.dataset.emailId;
if (emailId === currentEmailId) {
return;
}
emailRows.forEach(r => r.classList.remove('active'));
this.classList.add('active');
currentEmailId = emailId;
// Clear previous timer
if (markAsReadTimer) {
clearTimeout(markAsReadTimer);
}
try {
const response = await fetch(`/api/mail/email/${emailId}`);
if (!response.ok) throw new Error('Failed to fetch email');
const email = await response.json();
renderEmail(email);
// Handle mark as read based on preference
if (!email.IsRead) {
handleMarkAsRead(emailId, this);
}
} catch (error) {
console.error('Error fetching email:', error);
showError('Error loading email');
}
});
});
// Handle flag clicks
document.querySelectorAll('.email-flag').forEach(flag => {
flag.addEventListener('click', async function (e) {
e.stopPropagation();
const row = this.closest('.email-row');
const emailId = row.dataset.emailId;
try {
const response = await fetch(`/api/mail/email/${emailId}/flag`, {
method: 'POST'
});
if (!response.ok) throw new Error('Failed to toggle flag');
const data = await response.json();
if (data.flagged) {
this.classList.add('flagged');
this.title = 'Unflag';
} else {
this.classList.remove('flagged');
this.title = 'Flag';
}
} catch (error) {
console.error('Error toggling flag:', error);
}
});
});
function handleMarkAsRead(emailId, row) {
const markOption = prefs.MarkMessagesAsRead || 'Immediately';
const delays = {
'Never': null,
'Immediately': 0,
'After 5 Seconds': 5000,
'After 10 Seconds': 10000,
'After 30 Seconds': 30000,
'After 1 Minute': 60000
};
const delay = delays[markOption];
if (delay === null) {
return; // Never mark as read
}
markAsReadTimer = setTimeout(async () => {
try {
const response = await fetch(`/api/mail/email/${emailId}/read`, {
method: 'POST'
});
if (response.ok) {
row.classList.remove('unread');
}
} catch (error) {
console.error('Error marking as read:', error);
}
}, delay);
}
function renderEmail(email) {
preview.innerHTML = '';
// Header
const header = createHeader(email);
preview.appendChild(header);
// Sender info
const sender = createSenderInfo(email);
preview.appendChild(sender);
// Recipients
const recipients = createRecipients(email);
preview.appendChild(recipients);
// Attachments
if (email.Attachments && email.Attachments.length > 0) {
const attachments = createAttachments(email.Attachments);
preview.appendChild(attachments);
}
// Body
const body = createBody(email);
preview.appendChild(body);
}
function createHeader(email) {
const header = document.createElement('div');
header.className = 'email-header';
const subject = document.createElement('h2');
subject.className = 'email-subject';
if (email.Subject) {
subject.textContent = email.Subject;
} else {
const noSubject = document.createElement('span');
noSubject.className = 'no-subject';
noSubject.textContent = '[No Subject]';
subject.appendChild(noSubject);
}
const actions = document.createElement('div');
actions.className = 'email-actions';
const actionButtons = [
{ title: 'Reply', symbol: '↶' },
{ title: 'Reply All', symbol: '⇄' },
{ title: 'Forward', symbol: '→' },
{ title: 'Archive', symbol: '▼' },
{ title: 'Delete', symbol: '×' }
];
actionButtons.forEach(btn => {
const button = document.createElement('button');
button.className = 'btn-icon';
button.title = btn.title;
button.textContent = btn.symbol;
actions.appendChild(button);
});
header.appendChild(subject);
header.appendChild(actions);
return header;
}
function createSenderInfo(email) {
const sender = document.createElement('div');
sender.className = 'email-sender';
const senderInfo = document.createElement('div');
senderInfo.className = 'sender-info';
// Respect ShowEmailAddressWithDisplayName preference
const showAddress = prefs.ShowEmailAddressWithDisplayName;
const strong = document.createElement('strong');
strong.textContent = email.FromName || email.From;
senderInfo.appendChild(strong);
if (showAddress && email.FromName) {
const address = document.createTextNode(` <${email.From}>`);
senderInfo.appendChild(address);
}
const dateDiv = document.createElement('div');
dateDiv.className = 'email-date';
dateDiv.textContent = formatDate(email.Date);
sender.appendChild(senderInfo);
sender.appendChild(dateDiv);
return sender;
}
function createRecipients(email) {
const recipients = document.createElement('div');
recipients.className = 'email-recipients';
const toDiv = document.createElement('div');
const toStrong = document.createElement('strong');
toStrong.textContent = 'To: ';
toDiv.appendChild(toStrong);
toDiv.appendChild(document.createTextNode(email.To || ''));
recipients.appendChild(toDiv);
if (email.CC) {
const ccDiv = document.createElement('div');
const ccStrong = document.createElement('strong');
ccStrong.textContent = 'Cc: ';
ccDiv.appendChild(ccStrong);
ccDiv.appendChild(document.createTextNode(email.CC));
recipients.appendChild(ccDiv);
}
return recipients;
}
function createAttachments(attachments) {
const container = document.createElement('div');
container.className = 'email-attachments';
const label = document.createElement('strong');
label.textContent = 'Attachments: ';
container.appendChild(label);
attachments.forEach(att => {
const link = document.createElement('a');
link.href = `/api/mail/attachment/${att.ID}`;
link.className = 'attachment';
link.download = att.Filename;
link.textContent = `${att.Filename} (${att.Size})`;
container.appendChild(link);
});
return container;
}
function createBody(email) {
const body = document.createElement('div');
body.className = 'email-body';
// Respect DisplayHTML preference
const displayHTML = prefs.DisplayHTML;
if (displayHTML && email.Body) {
// Use ShadowRenderer library to encapsulate styles
const shadow = ShadowRenderer.render(body, email.Body);
// Handle remote content based on LoadRemoteContent preference
handleRemoteContent(shadow);
} else {
const pre = document.createElement('pre');
pre.textContent = email.Body || '[Empty Content]';
body.appendChild(pre);
}
return body;
}
function handleRemoteContent(container) {
const loadOption = prefs.LoadRemoteContent || 'Never';
if (loadOption === 'Never') {
// Block all external images
const images = container.querySelectorAll('img');
images.forEach(img => {
const src = img.getAttribute('src');
if (src && src.startsWith('http')) {
img.removeAttribute('src');
img.dataset.src = src; // Store original src
img.alt = '[Remote image blocked]';
img.style.border = '1px dashed #ccc';
img.style.padding = '5px';
img.style.display = 'inline-block';
}
});
}
// TODO: Implement "From my contacts" check
// For "Always", images load normally
}
function showError(message) {
preview.innerHTML = '';
const error = document.createElement('div');
error.className = 'no-email-selected';
const p = document.createElement('p');
p.textContent = message;
error.appendChild(p);
preview.appendChild(error);
}
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleString('en-US', {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
});
|