MailHog/assets/js/controllers.js

120 lines
3 KiB
JavaScript
Raw Normal View History

2014-04-20 14:35:59 +00:00
var mailhogApp = angular.module('mailhogApp', []);
2014-04-21 21:32:34 +00:00
mailhogApp.controller('MailCtrl', function ($scope, $http, $sce) {
$scope.cache = {};
2014-04-26 12:12:26 +00:00
$scope.previewAllHeaders = false;
2014-04-20 14:35:59 +00:00
$scope.refresh = function() {
$http.get('/api/v1/messages').success(function(data) {
$scope.messages = data;
});
}
$scope.refresh();
$scope.selectMessage = function(message) {
if($scope.cache[message.Id]) {
$scope.preview = $scope.cache[message.Id];
2014-04-26 12:12:26 +00:00
reflow();
} else {
$scope.preview = message;
$http.get('/api/v1/messages/' + message.Id).success(function(data) {
$scope.cache[message.Id] = data;
data.previewHTML = $sce.trustAsHtml($scope.getMessageHTML(data));
$scope.preview = data;
preview = $scope.cache[message.Id];
2014-04-26 12:12:26 +00:00
reflow();
});
}
}
2014-04-26 12:12:26 +00:00
$scope.toggleHeaders = function(val) {
$scope.previewAllHeaders = val;
var t = window.setInterval(function() {
if(val) {
if($('#hide-headers').length) {
window.clearInterval(t);
reflow();
}
} else {
if($('#show-headers').length) {
window.clearInterval(t);
reflow();
}
}
}, 10);
}
2014-04-21 21:32:34 +00:00
$scope.getMessagePlain = function(message) {
var part;
if(message.MIME) {
for(var p in message.MIME.Parts) {
2014-04-26 11:16:57 +00:00
if("Content-Type" in message.MIME.Parts[p].Headers) {
if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/plain;?.*/)) {
part = message.MIME.Parts[p];
break;
}
}
}
2014-04-21 21:32:34 +00:00
}
}
if(!part) part = message.Content;
return part.Body;
}
$scope.getMessageHTML = function(message) {
var part;
if(message.MIME) {
for(var p in message.MIME.Parts) {
2014-04-26 11:16:57 +00:00
if("Content-Type" in message.MIME.Parts[p].Headers) {
if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/html;?.*/)) {
part = message.MIME.Parts[p];
break;
}
}
}
2014-04-21 21:32:34 +00:00
}
}
if(!part) part = message.Content;
return part.Body;
}
2014-04-20 14:35:59 +00:00
$scope.date = function(timestamp) {
return (new Date(timestamp)).toString();
};
$scope.deleteAll = function() {
$('#confirm-delete-all').modal('show');
}
2014-04-20 19:13:39 +00:00
$scope.getSource = function(message) {
var source = "";
$.each(message.Content.Headers, function(k, v) {
source += k + ": " + v + "\n";
});
source += "\n";
source += message.Content.Body;
return source;
}
2014-04-20 14:35:59 +00:00
$scope.deleteAllConfirm = function() {
$('#confirm-delete-all').modal('hide');
$http.post('/api/v1/messages/delete').success(function() {
$scope.refresh();
$scope.preview = null;
});
}
$scope.deleteOne = function(message) {
2014-04-20 16:09:06 +00:00
$http.post('/api/v1/messages/' + message.Id + '/delete').success(function() {
2014-04-20 14:35:59 +00:00
if($scope.preview._id == message._id) $scope.preview = null;
$scope.refresh();
});
}
2014-04-27 15:06:58 +00:00
});