decoration of the message body in iframe's load event handler

This commit is contained in:
Jakub Pavlík jn 2013-07-28 22:57:36 +02:00
parent ffb4ec4e4c
commit 2f5c2ff01c
2 changed files with 29 additions and 28 deletions

View File

@ -22,6 +22,9 @@ class MailCatcher
$('#message .views .analysis.tab a').live 'click', (e) => $('#message .views .analysis.tab a').live 'click', (e) =>
e.preventDefault() e.preventDefault()
@loadMessageAnalysis @selectedMessage() @loadMessageAnalysis @selectedMessage()
$('#message iframe').load =>
@decorateMessageBody()
$('#resizer').live $('#resizer').live
mousedown: (e) -> mousedown: (e) ->
@ -253,21 +256,20 @@ class MailCatcher
$('#message iframe').attr "src", "/messages/#{id}.#{format}" $('#message iframe').attr "src", "/messages/#{id}.#{format}"
app = this app = this
$('#message iframe').load ( -> app.decorateMessageBody format )
decorateMessageBody: (format) -> decorateMessageBody: ->
if format == 'html' format = $('#message .views .tab.format.selected').attr 'data-message-format'
setTimeout ( ->
switch format
when 'html'
body = $('#message iframe').contents().find('body') body = $('#message iframe').contents().find('body')
$("a", body).attr("target", "_blank") $("a", body).attr("target", "_blank")
), 10 when 'plain'
message_iframe = $('#message iframe').contents()
else if format == 'plain' text = message_iframe.text()
message_iframe = $('#message iframe').contents() text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>')
text = message_iframe.text() text = text.replace(/\n/g, '<br/>')
text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>') message_iframe.find('html').html('<html><body>' + text + '</html></body>')
text = text.replace(/\n/g, '<br/>')
message_iframe.find('html').html('<html><body>' + text + '</html></body>')
loadMessageAnalysis: (id) -> loadMessageAnalysis: (id) ->
id ||= @selectedMessage() id ||= @selectedMessage()

View File

@ -36,6 +36,9 @@
e.preventDefault(); e.preventDefault();
return _this.loadMessageAnalysis(_this.selectedMessage()); return _this.loadMessageAnalysis(_this.selectedMessage());
}); });
$('#message iframe').load(function() {
return _this.decorateMessageBody();
});
$('#resizer').live({ $('#resizer').live({
mousedown: function(e) { mousedown: function(e) {
var events; var events;
@ -331,27 +334,23 @@
$("#message .views .tab:not([data-message-format=\"" + format + "\"]).selected").removeClass('selected'); $("#message .views .tab:not([data-message-format=\"" + format + "\"]).selected").removeClass('selected');
if (id != null) { if (id != null) {
$('#message iframe').attr("src", "/messages/" + id + "." + format); $('#message iframe').attr("src", "/messages/" + id + "." + format);
app = this; return app = this;
return $('#message iframe').load((function() {
return app.decorateMessageBody(format);
}));
} }
}; };
MailCatcher.prototype.decorateMessageBody = function(format) { MailCatcher.prototype.decorateMessageBody = function() {
var message_iframe, text; var body, format, message_iframe, text;
if (format === 'html') { format = $('#message .views .tab.format.selected').attr('data-message-format');
return setTimeout((function() { switch (format) {
var body; case 'html':
body = $('#message iframe').contents().find('body'); body = $('#message iframe').contents().find('body');
return $("a", body).attr("target", "_blank"); return $("a", body).attr("target", "_blank");
}), 10); case 'plain':
} else if (format === 'plain') { message_iframe = $('#message iframe').contents();
message_iframe = $('#message iframe').contents(); text = message_iframe.text();
text = message_iframe.text(); text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>');
text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>'); text = text.replace(/\n/g, '<br/>');
text = text.replace(/\n/g, '<br/>'); return message_iframe.find('html').html('<html><body>' + text + '</html></body>');
return message_iframe.find('html').html('<html><body>' + text + '</html></body>');
} }
}; };