2010-10-27 15:12:26 -04:00
|
|
|
var MailCatcher = {
|
|
|
|
init: function() {
|
|
|
|
$('#mail tr').live('click', function() {
|
|
|
|
MailCatcher.load($(this).attr('data-message-id'));
|
|
|
|
});
|
|
|
|
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .actions ul li.tab').live('click', function() {
|
2010-10-27 15:12:26 -04:00
|
|
|
MailCatcher.loadBody($('#mail tr.selected').attr('data-message-id'), $(this).attr('data-message-format'));
|
|
|
|
});
|
|
|
|
|
|
|
|
MailCatcher.refresh();
|
2011-05-27 09:46:12 -04:00
|
|
|
|
2010-10-27 15:12:26 -04:00
|
|
|
MailCatcher.subscribe();
|
|
|
|
},
|
|
|
|
|
|
|
|
addMessage: function(message) {
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#mail tbody').append(
|
|
|
|
$('<tr />').attr('data-message-id', message.id.toString())
|
|
|
|
.append($('<td/>').text(message.sender))
|
2011-05-27 10:10:05 -04:00
|
|
|
.append($('<td/>').text((message.recipients || []).join(', ')))
|
2011-05-27 09:46:12 -04:00
|
|
|
.append($('<td/>').text(message.subject))
|
|
|
|
.append($('<td/>').text((new Date(message.created_at)).toString("dddd, d MMM yyyy h:mm:ss tt")))
|
|
|
|
);
|
2010-10-27 15:12:26 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function() {
|
|
|
|
$.getJSON('/messages', function(mail) {
|
|
|
|
$.each(mail, function(i, message) {
|
|
|
|
MailCatcher.addMessage(message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribe: function () {
|
2011-05-27 09:46:12 -04:00
|
|
|
if (WebSocket !== undefined) {
|
|
|
|
MailCatcher.websocket = new WebSocket("ws" + (window.location.scheme == 'https' ? 's' : '') + "://" + window.location.host + "/messages");
|
|
|
|
MailCatcher.websocket.onmessage = function (event) {
|
|
|
|
MailCatcher.addMessage($.parseJSON(event.data));
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
if (!MailCatcher.refreshInterval) {
|
|
|
|
MailCatcher.refreshInterval = setInterval(MailCatcher.refresh, 30000);
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 15:12:26 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
load: function(id) {
|
|
|
|
id = id || $('#mail tr.selected').attr('data-message-id');
|
|
|
|
|
|
|
|
if (id !== null) {
|
|
|
|
$('#mail tbody tr:not([data-message-id="'+id+'"])').removeClass('selected');
|
|
|
|
$('#mail tbody tr[data-message-id="'+id+'"]').addClass('selected');
|
2011-05-27 09:46:12 -04:00
|
|
|
|
|
|
|
$.getJSON('/messages/' + id + '.json', function(message) {
|
|
|
|
$('#message .received span').text((new Date(message.created_at)).toString("dddd, d MMM yyyy h:mm:ss tt"));
|
2010-10-27 15:12:26 -04:00
|
|
|
$('#message .from span').text(message.sender);
|
2011-05-27 10:10:05 -04:00
|
|
|
$('#message .to span').text((message.recipients || []).join(', '));
|
2010-10-27 15:12:26 -04:00
|
|
|
$('#message .subject span').text(message.subject);
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .actions ul li.format').each(function(i, el) {
|
2010-10-27 15:12:26 -04:00
|
|
|
var $el = $(el),
|
|
|
|
format = $el.attr('data-message-format');
|
|
|
|
if ($.inArray(format, message.formats) >= 0) {
|
|
|
|
$el.show();
|
|
|
|
} else {
|
|
|
|
$el.hide();
|
|
|
|
}
|
|
|
|
});
|
2011-05-27 09:46:12 -04:00
|
|
|
if ($("#message .actions ul li.tab.selected:not(:visible)")) {
|
|
|
|
$("#message .actions ul li.tab.selected").removeClass("selected");
|
|
|
|
$("#message .actions ul li.tab:visible:first").addClass("selected");
|
2010-10-27 15:12:26 -04:00
|
|
|
}
|
|
|
|
if (message.attachments.length > 0) {
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .metadata .attachments ul').empty();
|
2010-10-27 15:12:26 -04:00
|
|
|
$.each(message.attachments, function (i, attachment) {
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .metadata .attachments ul').append($('<li>').append($('<a>').attr('href', attachment['href']).addClass(attachment['type'].split('/', 1)[0]).addClass(attachment['type'].replace('/', '-')).text(attachment['filename'])));
|
2010-10-27 15:12:26 -04:00
|
|
|
});
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .metadata .attachments').show();
|
2010-10-27 15:12:26 -04:00
|
|
|
} else {
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .metadata .attachments').hide();
|
2010-10-27 15:12:26 -04:00
|
|
|
}
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .actions ul li.download a').attr('href', '/messages/' + id + '.eml');
|
2010-10-27 15:12:26 -04:00
|
|
|
MailCatcher.loadBody();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadBody: function(id, format) {
|
|
|
|
id = id || $('#mail tr.selected').attr('data-message-id');
|
2011-05-27 09:46:12 -04:00
|
|
|
format = format || $('#message .actions ul li.selected').first().attr('data-message-format') || 'html';
|
2010-10-27 15:12:26 -04:00
|
|
|
|
2011-05-27 09:46:12 -04:00
|
|
|
$('#message .actions ul li.tab[data-message-format="'+format+'"]').addClass('selected');
|
|
|
|
$('#message .actions ul li.tab:not([data-message-format="'+format+'"])').removeClass('selected');
|
2010-10-27 15:12:26 -04:00
|
|
|
|
|
|
|
if (id !== undefined && id !== null) {
|
|
|
|
$('#message iframe').attr('src', '/messages/' + id + '.' + format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|