/
mailbox.js
203 lines (186 loc) · 5.79 KB
/
mailbox.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
200
201
202
var baseURL = window.location.protocol + '//' + window.location.host;
var navBarOffset = 75;
var mediumDeviceWidth = 980;
var messageListMargin = 275;
var clipboard = null;
var messageListScroll = false;
var messageListData = null;
// clearMessageSearch resets the message list search
function clearMessageSearch() {
$('#message-search').val('');
updateMessageSearch();
}
// deleteMessage sends a delete request for a message
function deleteMessage(id) {
$('#message-content').empty();
$.ajax({
type: 'DELETE',
url: '/api/v1/mailbox/' + mailbox + '/' + id,
success: loadList
})
}
// Delete the mailbox
function deleteMailBox() {
cont = confirm("Are you sure you want delete the Mailbox?")
if (cont == false) {
return;
}
$.ajax({
type: 'DELETE',
url: '/api/v1/mailbox/' + mailbox,
success: loadList
})
}
// flashTooltip temporarily changes the text of a tooltip
function flashTooltip(el, text) {
var prevText = $(el).attr('data-original-title');
$(el).attr('data-original-title', text).tooltip('show');
$(el).attr('data-original-title', prevText);
}
// htmlView pops open another window for viewing message as HTML
function htmlView(id) {
window.open('/mailbox/' + mailbox + '/' + id + "/html", '_blank',
'width=800,height=600,' +
'menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
}
// loadList loads the message list for this mailbox via AJAX
function loadList() {
$('#message-list').hide().empty();
$.ajax({
dataType: "json",
url: '/api/v1/mailbox/' + mailbox,
success: function(data) {
messageListData = data.reverse();
// Render list
$('#message-list').loadTemplate($('#list-entry-template'), data);
$('.message-list-entry').click(onMessageListClick);
// Reveal and select current message
$("#message-list").slideDown();
if (selected != "") {
$("#" + selected).click();
selected = "";
}
onDocumentChange();
updateMessageSearch();
}
});
}
// makeDelay creates a call-back timer that prevents itself from being
// stacked
function makeDelay(ms) {
var timer = 0;
return (function(callback) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
});
}
// messageSource pops open another window for message source
function messageSource(id) {
window.open('/mailbox/' + mailbox + '/' + id + "/source", '_blank',
'width=800,height=600,' +
'menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}
// toggleMessageLink shows/hids the message link URL form
function toggleMessageLink(id) {
var url = baseURL + '/link/' + mailbox + '/' + id;
$('#link-input-control').val(url);
$('#link-row').slideToggle();
}
// onDocumentChange is called each time we load partials into the DOM
function onDocumentChange() {
// Bootstrap tooltips
$('[data-toggle="tooltip"]').tooltip()
// Clipboard functionality
if (clipboard != null) {
clipboard.destroy();
}
clipboard = new Clipboard('.btn-clipboard');
clipboard.on('success', function(el) {
flashTooltip(el.trigger, 'Copied!');
el.clearSelection();
});
clipboard.on('error', function(el) {
flashTooltip(el.trigger, 'Copy Failed!');
});
}
// onDocumentReady is called by mailbox/index.html to initialize
function onDocumentReady() {
// Prevent search and resize handlers being called too often
var searchDelay = makeDelay(200);
var resizeDelay = makeDelay(100);
$.addTemplateFormatter({
"date": function(value, template) {
return moment(value).calendar();
},
"subject": function(value, template) {
if (value == null || value.length == 0) {
return "(No Subject)";
}
return value;
}
});
$("#message-list").hide();
onWindowResize();
$(window).resize(function() {
resizeDelay(onWindowResize);
});
$('#message-search').on('change keyup', function(el) {
searchDelay(updateMessageSearch);
});
loadList();
}
// onMessageListClick is triggered by clicks on the message list
function onMessageListClick() {
$('.message-list-entry').removeClass("disabled");
$(this).addClass("disabled");
$('#message-content').load('/mailbox/' + mailbox + '/' + this.id, onMessageLoaded);
selected = this.id;
}
// onMessageLoaded is called each time a new message is shown
function onMessageLoaded(responseText, textStatus, XMLHttpRequest) {
if (textStatus == "error") {
alert("Failed to load message, server said:\n" + responseText);
return;
}
onDocumentChange();
$('#body-tabs a:first').tab('show')
var top = $('#message-container').offset().top - navBarOffset;
$(window).scrollTop(top);
}
// onWindowResize handles special cases when window is resized
function onWindowResize() {
if ($(window).width() > mediumDeviceWidth) {
var content_height = $(window).height() - messageListMargin;
var messageListWrapper = $('#message-list-wrapper');
messageListWrapper.height(content_height);
if (!messageListScroll) {
messageListScroll = true;
messageListWrapper.addClass("message-list-scroll");
}
} else {
if (messageListScroll) {
messageListScroll = false;
$('#message-list-wrapper').height('auto').removeClass("message-list-scroll");
}
}
}
// updateMessageSearch compares the message list subjects and senders against
// the search string and hides entries that don't match
function updateMessageSearch() {
var criteria = $('#message-search').val();
if (criteria.length < 2) {
$('.message-list-entry').show();
return;
}
criteria = criteria.toLowerCase();
for (i=0; i<messageListData.length; i++) {
entry = messageListData[i];
if ((entry.subject.toLowerCase().indexOf(criteria) > -1) ||
(entry.from.toLowerCase().indexOf(criteria) > -1)) {
// Match
$('#' + entry.id).show();
} else {
$('#' + entry.id).hide();
}
}
}