(function() { var MailCatcher; MailCatcher = (function() { function MailCatcher() { var _this = this; $('#messages tr').live('click', function(e) { e.preventDefault(); return _this.loadMessage($(e.currentTarget).attr('data-message-id')); }); $('input[name=search]').live('keyup', function(e) { e.preventDefault(); console.log(e); if (e.currentTarget.value === "") { return _this.clearSearch(); } else { return _this.searchMessages(e.currentTarget.value); } }); $('#message .views .format.tab a').live('click', function(e) { e.preventDefault(); return _this.loadMessageBody(_this.selectedMessage(), $($(e.currentTarget).parent('li')).data('message-format')); }); $('#message .views .analysis.tab a').live('click', function(e) { e.preventDefault(); return _this.loadMessageAnalysis(_this.selectedMessage()); }); $('#resizer').live({ mousedown: function(e) { var events; e.preventDefault(); return $(window).bind(events = { mouseup: function(e) { e.preventDefault(); return $(window).unbind(events); }, mousemove: function(e) { e.preventDefault(); return $('#messages').css({ height: e.clientY - $('#messages').offset().top }); } }); } }); $('nav.app .clear a').live('click', function(e) { e.preventDefault(); 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.'); } }); } }); $('nav.app .quit a').live('click', function(e) { e.preventDefault(); 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.'); } }); } }); this.refresh(); this.subscribe(); } 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] - 1, match[3], match[4], match[5], match[6], 0); } }; MailCatcher.prototype.offsetTimeZone = function(date) { var offset; offset = Date.now().getTimezoneOffset() * 60000; date.setTime(date.getTime() - offset); return date; }; MailCatcher.prototype.formatDate = function(date) { if (typeof date === "string") date && (date = this.parseDate(date)); date && (date = this.offsetTimeZone(date)); return date && (date = date.toString("dddd, d MMM yyyy h:mm:ss tt")); }; MailCatcher.prototype.haveMessage = function(message) { if (message.id != null) message = message.id; return $("#messages tbody tr[data-message-id=\"" + message + "\"]").length > 0; }; MailCatcher.prototype.selectedMessage = function() { return $('#messages tr.selected').data('message-id'); }; MailCatcher.prototype.searchMessages = function(term) { $('#messages tbody tr:not(:contains("' + term + '"))').hide(); return $('#messages tbody tr(:contains("' + term + '"))').show(); }; MailCatcher.prototype.clearSearch = function() { return $('#messages tbody tr').show(); }; MailCatcher.prototype.addMessage = function(message) { return $('#messages tbody').append($('').attr('data-message-id', message.id.toString()).append($('').text(message.sender || "No sender").toggleClass("blank", !message.sender)).append($('').text((message.recipients || []).join(', ') || "No receipients").toggleClass("blank", !message.recipients.length)).append($('').text(message.subject || "No subject").toggleClass("blank", !message.subject)).append($('').text(this.formatDate(message.created_at)))); }; MailCatcher.prototype.loadMessage = function(id) { var _this = this; if ((id != null ? id.id : void 0) != null) id = id.id; id || (id = $('#messages tr.selected').attr('data-message-id')); if (id != null) { $('#messages tbody tr:not([data-message-id="' + id + '"])').removeClass('selected'); $('#messages tbody tr[data-message-id="' + id + '"]').addClass('selected'); return $.getJSON('/messages/' + id + '.json', function(message) { var $ul; $('#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) { var $el, format; $el = $(el); format = $el.attr('data-message-format'); if ($.inArray(format, message.formats) >= 0) { $el.find('a').attr('href', '/messages/' + id + '.' + format); return $el.show(); } else { return $el.hide(); } }); if ($("#message .views .tab.selected:not(:visible)").length) { $("#message .views .tab.selected").removeClass("selected"); $("#message .views .tab:visible:first").addClass("selected"); } if (message.attachments.length) { $ul = $('