'use strict'; /* global angular:false */ /* global $:false */ angular.module('Application').controller('EmailsController', ['$scope', '$location', 'Client', function ($scope, $location, Client) { Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); }); $scope.ready = false; $scope.config = Client.getConfig(); $scope.user = Client.getUserInfo(); $scope.domains = []; $scope.activity = { busy: true, eventLogs: [], activeEventLog: null, currentPage: 1, perPage: 1000, fetchEventLogs: function () { $scope.activity.busy = true; Client.getMailEventLogs($scope.activity.currentPage, $scope.activity.perPage, function (error, result) { if (error) return console.error('Failed to fetch mail eventlogs.', error); $scope.activity.busy = false; // FIXME reverse should come from the server $scope.activity.eventLogs = result.reverse(); }); }, showNextPage: function () { $scope.activity.currentPage++; $scope.activity.fetchEventLogs(); }, showPrevPage: function () { if ($scope.activity.currentPage > 1) $scope.activity.currentPage--; else $scope.activity.currentPage = 1; $scope.activity.fetchEventLogs(); }, showEventLogDetails: function (eventLog) { if ($scope.activity.activeEventLog === eventLog) $scope.activity.activeEventLog = null; else $scope.activity.activeEventLog = eventLog; } }; $scope.testEmail = { busy: false, error: {}, mailTo: '', domain: null, clearForm: function () { $scope.testEmail.mailTo = ''; }, show: function (domain) { $scope.testEmail.error = {}; $scope.testEmail.busy = false; $scope.testEmail.domain = domain; $scope.testEmail.mailTo = $scope.user.email; $('#testEmailModal').modal('show'); }, submit: function () { $scope.testEmail.error = {}; $scope.testEmail.busy = true; Client.sendTestMail($scope.testEmail.domain.domain, $scope.testEmail.mailTo, function (error) { $scope.testEmail.busy = false; if (error) { $scope.testEmail.error.generic = error.message; console.error(error); $('#inputTestMailTo').focus(); return; } $('#testEmailModal').modal('hide'); }); } }; function refreshDomainStatuses() { $scope.domains.forEach(function (domain) { Client.getMailStatusForDomain(domain.domain, function (error, result) { if (error) return console.error('Failed to fetch mail status for domain', domain.domain, error); domain.status = result; domain.statusOk = result.rbl.status && result.relay.status && result.dns.dkim.status && result.dns.dmarc.status && result.dns.mx.status && result.dns.ptr.status && result.dns.spf.status; }); }); } Client.onReady(function () { Client.getDomains(function (error, domains) { if (error) return console.error('Unable to get domain listing.', error); $scope.domains = domains; $scope.ready = true; $scope.activity.fetchEventLogs(); refreshDomainStatuses(); }); }); // setup all the dialog focus handling ['testEmailModal'].forEach(function (id) { $('#' + id).on('shown.bs.modal', function () { $(this).find('[autofocus]:first').focus(); }); }); $('.modal-backdrop').remove(); }]);