2011-05-29 02:46:38 -04:00
|
|
|
(function() {
|
|
|
|
var MailCatcher;
|
|
|
|
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
|
|
|
MailCatcher = (function() {
|
|
|
|
function MailCatcher() {
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#messages tr').live('click', __bind(function(e) {
|
2011-05-29 02:46:38 -04:00
|
|
|
return this.loadMessage($(e.currentTarget).attr('data-message-id'));
|
|
|
|
}, this));
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#message .views .tab').live('click', __bind(function(e) {
|
|
|
|
return this.loadMessageBody($('#messages tr.selected').attr('data-message-id'), $(e.currentTarget).attr('data-message-format'));
|
2011-05-29 02:46:38 -04:00
|
|
|
}, this));
|
2011-05-31 13:15:05 -04:00
|
|
|
$('nav.app .clear a').live('click', __bind(function(e) {
|
|
|
|
if (confirm("You will lose all your received messages.\n\nAre you sure you want to clear all messages?")) {
|
|
|
|
return $.ajax({
|
|
|
|
url: '/messages',
|
|
|
|
type: 'DELETE',
|
|
|
|
success: function() {
|
|
|
|
$('#messages tbody, #message .metadata dd').empty();
|
|
|
|
$('#message .metadata .attachments').hide();
|
|
|
|
return $('#message iframe').attr('src', 'about:blank');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
return alert('Error while quitting.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this));
|
2011-05-31 12:49:36 -04:00
|
|
|
$('nav.app .quit a').live('click', __bind(function(e) {
|
2011-05-31 13:14:56 -04:00
|
|
|
if (confirm("You will lose all your received messages.\n\nAre you sure you want to quit?")) {
|
|
|
|
return $.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
success: function() {
|
|
|
|
return location.replace($('body > header h1 a').attr('href'));
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
return alert('Error while quitting.');
|
|
|
|
}
|
|
|
|
});
|
2011-05-31 12:49:36 -04:00
|
|
|
}
|
|
|
|
}, this));
|
2011-05-29 02:46:38 -04:00
|
|
|
this.refresh();
|
|
|
|
this.subscribe();
|
2011-05-27 09:46:12 -04:00
|
|
|
}
|
2011-05-29 03:42:07 -04:00
|
|
|
MailCatcher.prototype.parseDateRegexp = /^(\d{4})[-\/\\](\d{2})[-\/\\](\d{2})(?:\s+|T)(\d{2})[:-](\d{2})[:-](\d{2})(?:([ +-]\d{2}:\d{2}|\s*\S+|Z?))?$/;
|
|
|
|
MailCatcher.prototype.parseDate = function(date) {
|
|
|
|
var match;
|
|
|
|
if (match = this.parseDateRegexp.exec(date)) {
|
|
|
|
return new Date(match[1], match[2], match[3], match[4], match[5], match[6], 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.formatDate = function(date) {
|
|
|
|
if (typeof date === "string") {
|
|
|
|
date && (date = this.parseDate(date));
|
|
|
|
}
|
|
|
|
return date && (date = date.toString("dddd, d MMM yyyy h:mm:ss tt"));
|
|
|
|
};
|
2011-05-29 02:46:38 -04:00
|
|
|
MailCatcher.prototype.haveMessage = function(message) {
|
|
|
|
if (message.id != null) {
|
|
|
|
message = message.id;
|
|
|
|
}
|
2011-05-31 12:40:13 -04:00
|
|
|
return $("#messages tbody tr[data-message-id=\"" + message + "\"]").length > 0;
|
2011-05-29 02:46:38 -04:00
|
|
|
};
|
|
|
|
MailCatcher.prototype.addMessage = function(message) {
|
2011-05-31 12:40:13 -04:00
|
|
|
return $('#messages tbody').append($('<tr />').attr('data-message-id', message.id.toString()).append($('<td/>').text(message.sender)).append($('<td/>').text((message.recipients || []).join(', '))).append($('<td/>').text(message.subject)).append($('<td/>').text(this.formatDate(message.created_at))));
|
2011-05-29 02:46:38 -04:00
|
|
|
};
|
|
|
|
MailCatcher.prototype.loadMessage = function(id) {
|
|
|
|
if ((id != null ? id.id : void 0) != null) {
|
|
|
|
id = id.id;
|
|
|
|
}
|
2011-05-31 12:40:13 -04:00
|
|
|
id || (id = $('#messages tr.selected').attr('data-message-id'));
|
2011-05-29 02:46:38 -04:00
|
|
|
if (id != null) {
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#messages tbody tr:not([data-message-id="' + id + '"])').removeClass('selected');
|
|
|
|
$('#messages tbody tr[data-message-id="' + id + '"]').addClass('selected');
|
2011-05-29 02:46:38 -04:00
|
|
|
return $.getJSON('/messages/' + id + '.json', __bind(function(message) {
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#message .metadata dd.created_at').text(this.formatDate(message.created_at));
|
|
|
|
$('#message .metadata dd.from').text(message.sender);
|
|
|
|
$('#message .metadata dd.to').text((message.recipients || []).join(', '));
|
|
|
|
$('#message .metadata dd.subject').text(message.subject);
|
|
|
|
$('#message .views .tab.format').each(function(i, el) {
|
2011-05-29 02:46:38 -04:00
|
|
|
var $el, format;
|
|
|
|
$el = $(el);
|
2010-10-27 15:12:26 -04:00
|
|
|
format = $el.attr('data-message-format');
|
2011-05-29 02:46:38 -04:00
|
|
|
if ($.inArray(format, message.formats) >= 0) {
|
|
|
|
return $el.show();
|
|
|
|
} else {
|
|
|
|
return $el.hide();
|
|
|
|
}
|
|
|
|
});
|
2011-05-31 12:40:13 -04:00
|
|
|
if ($("#message .views .tab.selected:not(:visible)").length) {
|
|
|
|
$("#message .views .tab.selected").removeClass("selected");
|
|
|
|
$("#message .views .tab:visible:first").addClass("selected");
|
2011-05-29 02:46:38 -04:00
|
|
|
}
|
|
|
|
if (message.attachments.length) {
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#message .metadata dd.attachments ul').empty();
|
2011-05-29 02:46:38 -04:00
|
|
|
$.each(message.attachments, function(i, attachment) {
|
2011-05-31 12:40:13 -04:00
|
|
|
return $('#message .metadata dd.attachments ul').append($('<li>').append($('<a>').attr('href', attachment['href']).addClass(attachment['type'].split('/', 1)[0]).addClass(attachment['type'].replace('/', '-')).text(attachment['filename'])));
|
2011-05-29 02:46:38 -04:00
|
|
|
});
|
|
|
|
$('#message .metadata .attachments').show();
|
2010-10-27 15:12:26 -04:00
|
|
|
} else {
|
2011-05-29 02:46:38 -04:00
|
|
|
$('#message .metadata .attachments').hide();
|
2010-10-27 15:12:26 -04:00
|
|
|
}
|
2011-05-31 12:40:13 -04:00
|
|
|
$('#message .actions .download a').attr('href', "/messages/" + id + ".eml");
|
2011-05-29 02:46:38 -04:00
|
|
|
return this.loadMessageBody();
|
|
|
|
}, this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.loadMessageBody = function(id, format) {
|
2011-05-31 12:40:13 -04:00
|
|
|
id || (id = $('#messages tr.selected').attr('data-message-id'));
|
|
|
|
format || (format = $('#message .views .tab.format.selected').attr('data-message-format'));
|
2011-05-29 02:46:38 -04:00
|
|
|
format || (format = 'html');
|
2011-05-31 12:40:13 -04:00
|
|
|
$("#message .views .tab[data-message-format=\"" + format + "\"]:not(.selected)").addClass('selected');
|
|
|
|
$("#message .views .tab:not([data-message-format=\"" + format + "\"]).selected").removeClass('selected');
|
2011-05-29 02:46:38 -04:00
|
|
|
if (id != null) {
|
|
|
|
return $('#message iframe').attr("src", "/messages/" + id + "." + format);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.refresh = function() {
|
|
|
|
return $.getJSON('/messages', __bind(function(messages) {
|
|
|
|
return $.each(messages, __bind(function(i, message) {
|
|
|
|
if (!this.haveMessage(message)) {
|
|
|
|
return this.addMessage(message);
|
|
|
|
}
|
|
|
|
}, this));
|
|
|
|
}, this));
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.subscribe = function() {
|
|
|
|
if (typeof WebSocket !== "undefined" && WebSocket !== null) {
|
|
|
|
return this.subscribeWebSocket();
|
|
|
|
} else {
|
|
|
|
return this.subscribePoll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.subscribeWebSocket = function() {
|
|
|
|
var secure;
|
|
|
|
secure = window.location.scheme === 'https';
|
|
|
|
this.websocket = new WebSocket("" + (secure ? 'wss' : 'ws') + "://" + window.location.host + "/messages");
|
|
|
|
return this.websocket.onmessage = __bind(function(event) {
|
|
|
|
return this.addMessage($.parseJSON(event.data));
|
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
MailCatcher.prototype.subscribePoll = function() {
|
|
|
|
if (this.refreshInterval == null) {
|
|
|
|
return this.refreshInterval = setInterval((__bind(function() {
|
|
|
|
return this.refresh();
|
|
|
|
}, this)), 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return MailCatcher;
|
|
|
|
})();
|
|
|
|
$(function() {
|
|
|
|
return window.MailCatcher = new MailCatcher;
|
|
|
|
});
|
|
|
|
}).call(this);
|