248 lines
9.8 KiB
JavaScript
248 lines
9.8 KiB
JavaScript
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* JavaScript for SOGo.MailerUI module */
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
angular.module('SOGo.Common', []);
|
|
|
|
angular.module('SOGo.MailerUI', ['ngSanitize', 'ui.router', 'mm.foundation', 'vs-repeat', 'ck', 'ngTagsInput', 'SOGo.Common', 'SOGo.UICommon', 'SOGo.UIDesktop'])
|
|
|
|
.constant('sgSettings', {
|
|
baseURL: ApplicationBaseURL,
|
|
activeUser: {
|
|
login: UserLogin,
|
|
language: UserLanguage,
|
|
folderURL: UserFolderURL,
|
|
isSuperUser: IsSuperUser
|
|
}
|
|
})
|
|
|
|
.config(['$stateProvider', '$urlRouterProvider', 'tagsInputConfigProvider', function($stateProvider, $urlRouterProvider, tagsInputConfigProvider) {
|
|
$stateProvider
|
|
.state('mail', {
|
|
url: '/Mail',
|
|
views: {
|
|
mailboxes: {
|
|
templateUrl: 'mailboxes.html',
|
|
controller: 'MailboxesCtrl'
|
|
}
|
|
},
|
|
resolve: {
|
|
stateAccounts: ['$q', 'sgAccount', function($q, Account) {
|
|
var accounts = Account.$findAll(mailAccounts);
|
|
var promises = [];
|
|
// Fetch list of mailboxes for each account
|
|
angular.forEach(accounts, function(account, i) {
|
|
var mailboxes = account.$getMailboxes();
|
|
promises.push(mailboxes.then(function(objects) {
|
|
return account;
|
|
}));
|
|
});
|
|
return $q.all(promises);
|
|
}]
|
|
}
|
|
})
|
|
.state('mail.account', {
|
|
url: '/:accountId',
|
|
abstract: true,
|
|
views: {
|
|
mailbox: {
|
|
template: '<ui-view/>',
|
|
}
|
|
},
|
|
resolve: {
|
|
stateAccount: ['$stateParams', 'stateAccounts', function($stateParams, stateAccounts) {
|
|
return _.find(stateAccounts, function(account) {
|
|
return account.id == $stateParams.accountId;
|
|
});
|
|
}]
|
|
}
|
|
})
|
|
.state('mail.account.mailbox', {
|
|
url: '/:mailboxId',
|
|
templateUrl: 'mailbox.html',
|
|
controller: 'MailboxCtrl',
|
|
resolve: {
|
|
stateMailbox: ['$stateParams', 'stateAccount', 'decodeUriFilter', function($stateParams, stateAccount, decodeUriFilter) {
|
|
var mailboxId = decodeUriFilter($stateParams.mailboxId);
|
|
// Recursive find function
|
|
var _find = function(mailboxes) {
|
|
var mailbox = _.find(mailboxes, function(o) {
|
|
return o.path == mailboxId;
|
|
});
|
|
if (!mailbox) {
|
|
angular.forEach(mailboxes, function(o) {
|
|
if (!mailbox && o.children && o.children.length > 0) {
|
|
mailbox = _find(o.children);
|
|
}
|
|
});
|
|
}
|
|
return mailbox;
|
|
};
|
|
return _find(stateAccount.$mailboxes);
|
|
}],
|
|
stateMessages: ['stateMailbox', function(stateMailbox) {
|
|
return stateMailbox.$update();
|
|
}]
|
|
}
|
|
})
|
|
.state('mail.account.mailbox.message', {
|
|
url: '/:messageId',
|
|
views: {
|
|
message: {
|
|
templateUrl: 'message.html',
|
|
controller: 'MessageCtrl'
|
|
}
|
|
},
|
|
resolve: {
|
|
stateMessage: ['$stateParams', 'stateMailbox', 'stateMessages', function($stateParams, stateMailbox, stateMessages) {
|
|
var message = _.find(stateMessages, function(messageObject) {
|
|
return messageObject.uid == $stateParams.messageId;
|
|
});
|
|
|
|
return message.$update();
|
|
}]
|
|
}
|
|
})
|
|
.state('mail.account.mailbox.message.editMessage', {
|
|
url: '/edit',
|
|
views: {
|
|
'mailbox@mail': {
|
|
templateUrl: 'editorTemplate', // UI/Templates/MailerUI/UIxMailEditor.wox
|
|
controller: 'MessageEditorCtrl'
|
|
}
|
|
},
|
|
resolve: {
|
|
stateContent: ['stateMessage', function(stateMessage) {
|
|
return stateMessage.$editableContent();
|
|
}]
|
|
}
|
|
})
|
|
.state('mail.newMessage', {
|
|
url: '/new',
|
|
views: {
|
|
mailbox: {
|
|
templateUrl: 'editorTemplate', // UI/Templates/MailerUI/UIxMailEditor.wox
|
|
controller: 'MessageEditorCtrl'
|
|
}
|
|
},
|
|
resolve: {
|
|
stateMessage: ['stateAccounts', function(stateAccounts) {
|
|
if (stateAccounts.length > 0) {
|
|
return stateAccounts[0].$newMessage();
|
|
}
|
|
}]
|
|
}
|
|
});
|
|
|
|
// if none of the above states are matched, use this as the fallback
|
|
$urlRouterProvider.otherwise('/Mail');
|
|
|
|
// Set default configuration for tags input
|
|
tagsInputConfigProvider.setDefaults('tagsInput', {
|
|
addOnComma: false,
|
|
replaceSpacesWithDashes: false,
|
|
allowedTagsPattern: /([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)/i
|
|
});
|
|
}])
|
|
|
|
.run(function($rootScope) {
|
|
$rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
|
|
console.log(event, current, previous, rejection)
|
|
})
|
|
})
|
|
|
|
.directive('sgFocusOn', function() {
|
|
return function(scope, elem, attr) {
|
|
scope.$on('sgFocusOn', function(e, name) {
|
|
if (name === attr.sgFocusOn) {
|
|
elem[0].focus();
|
|
elem[0].select();
|
|
}
|
|
});
|
|
};
|
|
})
|
|
|
|
.factory('sgFocus', ['$rootScope', '$timeout', function($rootScope, $timeout) {
|
|
return function(name) {
|
|
$timeout(function() {
|
|
$rootScope.$broadcast('sgFocusOn', name);
|
|
});
|
|
}
|
|
}])
|
|
|
|
.controller('MailboxesCtrl', ['$scope', '$rootScope', '$stateParams', '$state', '$timeout', '$modal', 'sgFocus', 'encodeUriFilter', 'sgDialog', 'sgAccount', 'sgMailbox', 'stateAccounts', function($scope, $rootScope, $stateParams, $state, $timeout, $modal, focus, encodeUriFilter, Dialog, Account, Mailbox, stateAccounts) {
|
|
$scope.accounts = stateAccounts;
|
|
|
|
$scope.setCurrentFolder = function(account, folder) {
|
|
$rootScope.currentFolder = folder;
|
|
$state.go('mail.account.mailbox', { accountId: account.id, mailboxId: encodeUriFilter(folder.path) });
|
|
};
|
|
$scope.exportMails = function() {
|
|
window.location.href = ApplicationBaseURL + '/' + $rootScope.currentFolder.id + '/exportFolder';
|
|
};
|
|
$scope.confirmDelete = function() {
|
|
Dialog.confirm(l('Confirmation'), l('Do you really want to move this folder into the trash ?'))
|
|
.then(function(res) {
|
|
if (res) {
|
|
$rootScope.currentFolder.$delete()
|
|
.then(function() {
|
|
$rootScope.currentFolder = null;
|
|
}, function(data, status) {
|
|
Dialog.alert(l('An error occured while deleting the mailbox "%{0}".',
|
|
$rootScope.currentFolder.name),
|
|
l(data.error));
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
if ($state.current.name == 'mail' && $scope.accounts.length > 0 && $scope.accounts[0].$mailboxes.length > 0) {
|
|
var account = $scope.accounts[0];
|
|
var mailbox = account.$mailboxes[0];
|
|
$state.go('mail.account.mailbox', { accountId: account.id, mailboxId: encodeUriFilter(mailbox.path) });
|
|
}
|
|
}])
|
|
|
|
.controller('MailboxCtrl', ['$scope', '$rootScope', '$stateParams', 'stateAccount', 'stateMailbox', '$timeout', '$modal', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', function($scope, $rootScope, $stateParams, stateAccount, stateMailbox, $timeout, $modal, focus, Dialog, Account, Mailbox) {
|
|
$scope.account = stateAccount;
|
|
$scope.mailbox = stateMailbox;
|
|
$rootScope.currentFolder = stateMailbox;
|
|
$timeout(function() {
|
|
$rootScope.$broadcast('sgSelectFolder', stateMailbox.id);
|
|
});
|
|
}])
|
|
|
|
.controller('MessageCtrl', ['$scope', '$rootScope', '$stateParams', '$state', 'stateAccount', 'stateMailbox', 'stateMessage', '$timeout', '$modal', 'encodeUriFilter', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', function($scope, $rootScope, $stateParams, $state, stateAccount, stateMailbox, stateMessage, $timeout, $modal, encodeUriFilter, focus, Dialog, Account, Mailbox) {
|
|
$rootScope.message = stateMessage;
|
|
$scope.doDelete = function() {
|
|
stateMailbox.$deleteMessages([stateMessage.uid]).then(function() {
|
|
// Remove card from list of addressbook
|
|
stateMailbox.$messages = _.reject(stateMailbox.$messages, function(o) {
|
|
return o.uid == stateMessage.uid;
|
|
});
|
|
// Remove card object from scope
|
|
$rootScope.message = null;
|
|
$state.go('mail.account.mailbox', { accountId: stateAccount.id, mailboxId: encodeUriFilter(stateMailbox.path) });
|
|
});
|
|
};
|
|
}])
|
|
|
|
.controller('MessageEditorCtrl', ['$scope', '$rootScope', '$stateParams', '$state', 'stateAccounts', 'stateMessage', '$timeout', '$modal', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', function($scope, $rootScope, $stateParams, $state, stateAccounts, stateMessage, $timeout, $modal, focus, Dialog, Account, Mailbox) {
|
|
if (angular.isDefined(stateMessage)) {
|
|
$scope.message = stateMessage;
|
|
}
|
|
$scope.identities = _.pluck(_.flatten(_.pluck(stateAccounts, 'identities')), 'full');
|
|
$scope.send = function(message) {
|
|
message.$send().then(function(data) {
|
|
$rootScope.message = null;
|
|
$state.go('mail');
|
|
}, function(data) {
|
|
console.debug('failure ' + JSON.stringify(data, undefined, 2));
|
|
});
|
|
};
|
|
}]);
|
|
|
|
})();
|