mailcatcher/public/javascripts/application.coffee

128 lines
4.9 KiB
CoffeeScript
Raw Normal View History

2011-05-29 02:46:38 -04:00
class MailCatcher
constructor: ->
2011-05-31 12:40:13 -04:00
$('#messages tr').live 'click', (e) =>
2011-05-29 02:46:38 -04:00
@loadMessage $(e.currentTarget).attr 'data-message-id'
2011-05-31 12:40:13 -04:00
$('#message .views .tab').live 'click', (e) =>
@loadMessageBody $('#messages tr.selected').attr('data-message-id'), $(e.currentTarget).attr 'data-message-format'
2011-06-10 08:47:38 -04:00
2011-05-31 13:15:05 -04:00
$('nav.app .clear a').live 'click', (e) =>
if confirm "You will lose all your received messages.\n\nAre you sure you want to clear all messages?"
2011-06-10 08:47:38 -04:00
$.ajax
2011-05-31 13:15:05 -04:00
url: '/messages'
type: 'DELETE'
success: ->
$('#messages tbody, #message .metadata dd').empty()
$('#message .metadata .attachments').hide()
$('#message iframe').attr 'src', 'about:blank'
error: ->
alert 'Error while quitting.'
2011-05-29 02:46:38 -04:00
$('nav.app .quit a').live 'click', (e) =>
if confirm "You will lose all your received messages.\n\nAre you sure you want to quit?"
$.ajax
type: 'DELETE'
success: ->
location.replace $('body > header h1 a').attr('href')
error: ->
alert 'Error while quitting.'
2011-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
@refresh()
@subscribe()
2011-06-10 08:47:38 -04:00
# Only here because Safari's Date parsing *sucks*
# We throw away the timezone, but you could use it for something...
parseDateRegexp: /^(\d{4})[-\/\\](\d{2})[-\/\\](\d{2})(?:\s+|T)(\d{2})[:-](\d{2})[:-](\d{2})(?:([ +-]\d{2}:\d{2}|\s*\S+|Z?))?$/
parseDate: (date) ->
if match = @parseDateRegexp.exec(date)
new Date match[1], match[2], match[3], match[4], match[5], match[6], 0
2011-06-10 08:47:38 -04:00
formatDate: (date) ->
date &&= @parseDate(date) if typeof(date) == "string"
date &&= date.toString("dddd, d MMM yyyy h:mm:ss tt")
2011-05-29 02:46:38 -04:00
haveMessage: (message) ->
message = message.id if message.id?
2011-05-31 12:40:13 -04:00
$("#messages tbody tr[data-message-id=\"#{message}\"]").length > 0
2011-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
addMessage: (message) ->
2011-05-31 12:40:13 -04:00
$('#messages tbody').append \
2011-05-29 02:46:38 -04:00
$('<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 @formatDate message.created_at)
2011-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
loadMessage: (id) ->
id = id.id if id?.id?
2011-05-31 12:40:13 -04:00
id ||= $('#messages tr.selected').attr 'data-message-id'
2011-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
if id?
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-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
$.getJSON '/messages/' + id + '.json', (message) =>
2011-05-31 12:40:13 -04:00
$('#message .metadata dd.created_at').text @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 (i, el) ->
2011-05-29 02:46:38 -04:00
$el = $(el)
format = $el.attr 'data-message-format'
if $.inArray(format, message.formats) >= 0
$el.show()
else
$el.hide()
2011-06-10 08:47:38 -04:00
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-06-10 08:47:38 -04:00
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, (i, attachment) ->
2011-05-31 12:40:13 -04:00
$('#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()
else
$('#message .metadata .attachments').hide()
2011-06-10 08:47:38 -04:00
$('#message .views .download a').attr 'href', "/messages/#{id}.eml"
2011-05-29 02:46:38 -04:00
@loadMessageBody()
loadMessageBody: (id, format) ->
2011-05-31 12:40:13 -04:00
id ||= $('#messages tr.selected').attr 'data-message-id'
format ||= $('#message .views .tab.format.selected').attr 'data-message-format'
2011-05-29 02:46:38 -04:00
format ||= 'html'
2011-06-10 08:47:38 -04:00
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-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
if id?
$('#message iframe').attr "src", "/messages/#{id}.#{format}"
2011-06-10 08:47:38 -04:00
2011-05-29 02:46:38 -04:00
refresh: ->
$.getJSON '/messages', (messages) =>
$.each messages, (i, message) =>
unless @haveMessage message
@addMessage message
subscribe: ->
if WebSocket?
@subscribeWebSocket()
else
@subscribePoll()
subscribeWebSocket: ->
secure = window.location.scheme == 'https'
@websocket = new WebSocket("#{if secure then 'wss' else 'ws'}://#{window.location.host}/messages");
@websocket.onmessage = (event) =>
@addMessage $.parseJSON event.data
subscribePoll: ->
unless @refreshInterval?
@refreshInterval = setInterval (=> @refresh()), 1000
2011-06-10 08:47:38 -04:00
$ -> window.MailCatcher = new MailCatcher