2014-11-26 04:31:52 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* JavaScript for SOGo.MailerUI module */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('SOGo.Common', []);
|
2014-12-12 19:39:02 +01:00
|
|
|
angular.module('SOGo.ContactsUI', []);
|
2014-11-26 04:31:52 +01:00
|
|
|
|
2015-03-23 15:53:17 +01:00
|
|
|
angular.module('SOGo.MailerUI', ['ngSanitize', 'ui.router', 'vs-repeat', 'ck', 'ngTagsInput', 'angularFileUpload', 'SOGo.Common', 'SOGo.UI', 'SOGo.UIDesktop', 'SOGo.ContactsUI', 'ngAnimate'])
|
2014-11-26 04:31:52 +01:00
|
|
|
|
|
|
|
.constant('sgSettings', {
|
|
|
|
baseURL: ApplicationBaseURL,
|
|
|
|
activeUser: {
|
|
|
|
login: UserLogin,
|
2015-01-17 05:29:33 +01:00
|
|
|
identification: UserIdentification,
|
2014-11-26 04:31:52 +01:00
|
|
|
language: UserLanguage,
|
|
|
|
folderURL: UserFolderURL,
|
|
|
|
isSuperUser: IsSuperUser
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-12-12 03:11:20 +01:00
|
|
|
.config(['$stateProvider', '$urlRouterProvider', 'tagsInputConfigProvider', function($stateProvider, $urlRouterProvider, tagsInputConfigProvider) {
|
2014-11-26 04:31:52 +01:00
|
|
|
$stateProvider
|
|
|
|
.state('mail', {
|
|
|
|
url: '/Mail',
|
|
|
|
views: {
|
|
|
|
mailboxes: {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailMainFrame', // UI/Templates/MailerUI/UIxMailMainFrame.wox
|
2014-11-26 04:31:52 +01:00
|
|
|
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',
|
2015-01-09 21:58:11 +01:00
|
|
|
views: {
|
|
|
|
'mailbox@mail': {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailFolderTemplate', // UI/Templates/MailerUI/UIxMailFolderTemplate.wox
|
2015-01-09 21:58:11 +01:00
|
|
|
controller: 'MailboxCtrl'
|
|
|
|
}
|
|
|
|
},
|
2014-11-26 04:31:52 +01:00
|
|
|
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;
|
|
|
|
};
|
2014-11-30 14:50:26 +01:00
|
|
|
return _find(stateAccount.$mailboxes);
|
2014-11-26 04:31:52 +01:00
|
|
|
}],
|
|
|
|
stateMessages: ['stateMailbox', function(stateMailbox) {
|
2015-03-18 19:59:06 +01:00
|
|
|
return stateMailbox.$filter();
|
2014-11-26 04:31:52 +01:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('mail.account.mailbox.message', {
|
2014-12-11 17:24:22 +01:00
|
|
|
url: '/:messageId',
|
2014-11-26 04:31:52 +01:00
|
|
|
views: {
|
|
|
|
message: {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailViewTemplate', // UI/Templates/MailerUI/UIxMailViewTemplate.wox
|
2014-11-26 04:31:52 +01:00
|
|
|
controller: 'MessageCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
2015-01-06 04:34:12 +01:00
|
|
|
stateMessage: ['encodeUriFilter', '$stateParams', '$state', 'stateMailbox', 'stateMessages', function(encodeUriFilter, $stateParams, $state, stateMailbox, stateMessages) {
|
2014-11-26 04:31:52 +01:00
|
|
|
var message = _.find(stateMessages, function(messageObject) {
|
|
|
|
return messageObject.uid == $stateParams.messageId;
|
|
|
|
});
|
2014-12-11 17:24:22 +01:00
|
|
|
|
2014-12-17 20:08:43 +01:00
|
|
|
if (message)
|
|
|
|
return message.$reload();
|
|
|
|
else
|
2015-01-06 04:34:12 +01:00
|
|
|
// Message not found
|
2014-12-17 20:08:43 +01:00
|
|
|
$state.go('mail.account.mailbox', { accountId: stateMailbox.$account.id, mailboxId: encodeUriFilter(stateMailbox.path) });
|
2014-12-11 17:24:22 +01:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2015-01-06 04:34:12 +01:00
|
|
|
.state('mail.account.mailbox.message.edit', {
|
2014-12-11 17:24:22 +01:00
|
|
|
url: '/edit',
|
|
|
|
views: {
|
|
|
|
'mailbox@mail': {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailEditor', // UI/Templates/MailerUI/UIxMailEditor.wox
|
2014-12-11 17:24:22 +01:00
|
|
|
controller: 'MessageEditorCtrl'
|
|
|
|
}
|
2014-12-12 03:11:20 +01:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateContent: ['stateMessage', function(stateMessage) {
|
|
|
|
return stateMessage.$editableContent();
|
|
|
|
}]
|
2014-12-11 17:24:22 +01:00
|
|
|
}
|
|
|
|
})
|
2015-01-06 18:09:14 +01:00
|
|
|
.state('mail.account.mailbox.message.action', {
|
|
|
|
url: '/{actionName:(?:reply|replyall|forward)}',
|
2015-01-06 04:34:12 +01:00
|
|
|
views: {
|
|
|
|
'mailbox@mail': {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailEditor', // UI/Templates/MailerUI/UIxMailEditor.wox
|
2015-01-06 04:34:12 +01:00
|
|
|
controller: 'MessageEditorCtrl'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2014-12-11 17:24:22 +01:00
|
|
|
.state('mail.newMessage', {
|
|
|
|
url: '/new',
|
|
|
|
views: {
|
|
|
|
mailbox: {
|
2015-01-21 22:40:26 +01:00
|
|
|
templateUrl: 'UIxMailEditor', // UI/Templates/MailerUI/UIxMailEditor.wox
|
2014-12-11 17:24:22 +01:00
|
|
|
controller: 'MessageEditorCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateMessage: ['stateAccounts', function(stateAccounts) {
|
|
|
|
if (stateAccounts.length > 0) {
|
|
|
|
return stateAccounts[0].$newMessage();
|
|
|
|
}
|
2014-11-26 04:31:52 +01:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// if none of the above states are matched, use this as the fallback
|
|
|
|
$urlRouterProvider.otherwise('/Mail');
|
2014-12-12 03:11:20 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
});
|
2014-11-26 04:31:52 +01:00
|
|
|
}])
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
.run(function($rootScope) {
|
|
|
|
$rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
|
2015-01-08 17:52:10 +01:00
|
|
|
console.error(event, current, previous, rejection)
|
2014-12-11 17:24:22 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-03-09 21:21:31 +01:00
|
|
|
.controller('MailboxesCtrl', ['$scope', '$rootScope', '$stateParams', '$state', '$timeout', 'sgFocus', 'encodeUriFilter', 'sgDialog', 'sgSettings', 'sgAccount', 'sgMailbox', 'stateAccounts', function($scope, $rootScope, $stateParams, $state, $timeout, focus, encodeUriFilter, Dialog, Settings, Account, Mailbox, stateAccounts) {
|
2015-01-17 05:29:33 +01:00
|
|
|
$scope.activeUser = Settings.activeUser;
|
2014-11-26 04:31:52 +01:00
|
|
|
$scope.accounts = stateAccounts;
|
|
|
|
|
2014-12-18 21:22:29 +01:00
|
|
|
$scope.newFolder = function(parentFolder) {
|
|
|
|
Dialog.prompt(l('New folder'),
|
|
|
|
l('Enter the new name of your folder :'))
|
|
|
|
.then(function(name) {
|
|
|
|
if (name && name.length > 0) {
|
|
|
|
parentFolder.$newMailbox(parentFolder.id, name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2015-01-08 17:52:10 +01:00
|
|
|
$scope.editFolder = function(folder) {
|
|
|
|
$rootScope.$broadcast('sgEditFolder', folder.id);
|
|
|
|
};
|
2014-11-26 04:31:52 +01:00
|
|
|
$scope.setCurrentFolder = function(account, folder) {
|
|
|
|
$rootScope.currentFolder = folder;
|
|
|
|
$state.go('mail.account.mailbox', { accountId: account.id, mailboxId: encodeUriFilter(folder.path) });
|
|
|
|
};
|
2014-11-30 14:50:26 +01:00
|
|
|
$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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-11-26 04:31:52 +01:00
|
|
|
|
2015-01-08 17:52:10 +01:00
|
|
|
// Register listeners
|
|
|
|
$scope.$on('sgRevertFolder', function(event, folderId) {
|
|
|
|
if (folderId == $scope.currentFolder.id) {
|
|
|
|
$scope.currentFolder.$reset();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$scope.$on('sgSaveFolder', function(event, folderId) {
|
|
|
|
if (folderId == $scope.currentFolder.id) {
|
|
|
|
$scope.currentFolder.$rename();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
if ($state.current.name == 'mail' && $scope.accounts.length > 0 && $scope.accounts[0].$mailboxes.length > 0) {
|
2015-01-08 17:52:10 +01:00
|
|
|
// Redirect to first mailbox of first account if no mailbox is selected
|
2014-11-26 04:31:52 +01:00
|
|
|
var account = $scope.accounts[0];
|
2014-11-30 14:50:26 +01:00
|
|
|
var mailbox = account.$mailboxes[0];
|
2014-11-26 04:31:52 +01:00
|
|
|
$state.go('mail.account.mailbox', { accountId: account.id, mailboxId: encodeUriFilter(mailbox.path) });
|
|
|
|
}
|
|
|
|
}])
|
|
|
|
|
2015-03-09 21:21:31 +01:00
|
|
|
.controller('MailboxCtrl', ['$scope', '$rootScope', '$stateParams', 'stateAccount', 'stateMailbox', '$timeout', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', function($scope, $rootScope, $stateParams, stateAccount, stateMailbox, $timeout, focus, Dialog, Account, Mailbox) {
|
2014-11-26 04:31:52 +01:00
|
|
|
$scope.account = stateAccount;
|
2015-03-18 19:59:06 +01:00
|
|
|
$rootScope.mailbox = stateMailbox;
|
2014-11-26 04:31:52 +01:00
|
|
|
$rootScope.currentFolder = stateMailbox;
|
|
|
|
$timeout(function() {
|
|
|
|
$rootScope.$broadcast('sgSelectFolder', stateMailbox.id);
|
|
|
|
});
|
|
|
|
}])
|
|
|
|
|
2015-03-09 21:21:31 +01:00
|
|
|
.controller('MessageCtrl', ['$scope', '$rootScope', '$stateParams', '$state', 'stateAccount', 'stateMailbox', 'stateMessage', '$timeout', 'encodeUriFilter', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', function($scope, $rootScope, $stateParams, $state, stateAccount, stateMailbox, stateMessage, $timeout, encodeUriFilter, focus, Dialog, Account, Mailbox) {
|
2014-11-26 04:31:52 +01:00
|
|
|
$rootScope.message = stateMessage;
|
2014-12-11 17:24:22 +01:00
|
|
|
$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) });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}])
|
|
|
|
|
2015-03-20 18:05:53 +01:00
|
|
|
.controller('MessageEditorCtrl', ['$scope', '$rootScope', '$stateParams', '$state', '$q', 'FileUploader', 'stateAccounts', 'stateMessage', '$timeout', 'encodeUriFilter', 'sgFocus', 'sgDialog', 'sgAccount', 'sgMailbox', 'sgAddressBook', function($scope, $rootScope, $stateParams, $state, $q, FileUploader, stateAccounts, stateMessage, $timeout, encodeUriFilter, focus, Dialog, Account, Mailbox, AddressBook) {
|
2015-01-06 18:09:14 +01:00
|
|
|
if ($stateParams.actionName == 'reply') {
|
2015-01-06 04:34:12 +01:00
|
|
|
stateMessage.$reply().then(function(msgObject) {
|
|
|
|
$scope.message = msgObject;
|
|
|
|
});
|
|
|
|
}
|
2015-01-06 18:09:14 +01:00
|
|
|
else if ($stateParams.actionName == 'replyall') {
|
|
|
|
stateMessage.$replyAll().then(function(msgObject) {
|
|
|
|
$scope.message = msgObject;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($stateParams.actionName == 'forward') {
|
|
|
|
stateMessage.$forward().then(function(msgObject) {
|
|
|
|
$scope.message = msgObject;
|
|
|
|
});
|
|
|
|
}
|
2015-01-06 04:34:12 +01:00
|
|
|
else if (angular.isDefined(stateMessage)) {
|
2014-12-11 17:24:22 +01:00
|
|
|
$scope.message = stateMessage;
|
|
|
|
}
|
2014-12-12 03:11:20 +01:00
|
|
|
$scope.identities = _.pluck(_.flatten(_.pluck(stateAccounts, 'identities')), 'full');
|
2015-03-20 16:16:24 +01:00
|
|
|
$scope.cancel = function() {
|
|
|
|
if ($scope.mailbox)
|
2015-03-20 18:05:53 +01:00
|
|
|
$state.go('mail.account.mailbox', { accountId: $scope.mailbox.$account.id, mailboxId: encodeUriFilter($scope.mailbox.path) });
|
2015-03-20 16:16:24 +01:00
|
|
|
else
|
|
|
|
$state.go('mail');
|
|
|
|
};
|
2014-12-11 17:24:22 +01:00
|
|
|
$scope.send = function(message) {
|
|
|
|
message.$send().then(function(data) {
|
2014-12-12 03:11:20 +01:00
|
|
|
$rootScope.message = null;
|
2014-12-12 03:52:46 +01:00
|
|
|
$state.go('mail');
|
2014-12-11 17:24:22 +01:00
|
|
|
}, function(data) {
|
|
|
|
console.debug('failure ' + JSON.stringify(data, undefined, 2));
|
|
|
|
});
|
|
|
|
};
|
2014-12-12 19:39:02 +01:00
|
|
|
$scope.userFilter = function($query) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
AddressBook.$filterAll($query).then(function(results) {
|
|
|
|
deferred.resolve(_.invoke(results, '$shortFormat'));
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2014-12-17 20:08:43 +01:00
|
|
|
$scope.uploader = new FileUploader({
|
|
|
|
url: stateMessage.$absolutePath({asDraft: true}) + '/save',
|
|
|
|
autoUpload: true,
|
|
|
|
alias: 'attachments',
|
|
|
|
onProgressItem: function(item, progress) {
|
|
|
|
console.debug(item); console.debug(progress);
|
|
|
|
},
|
|
|
|
onSuccessItem: function(item, response, status, headers) {
|
|
|
|
stateMessage.$setUID(response.uid);
|
|
|
|
stateMessage.$reload();
|
|
|
|
console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2));
|
|
|
|
},
|
|
|
|
onErrorItem: function(item, response, status, headers) {
|
|
|
|
console.debug(item); console.debug('error = ' + JSON.stringify(response, undefined, 2));
|
|
|
|
}
|
|
|
|
});
|
2014-11-26 04:31:52 +01:00
|
|
|
}]);
|
|
|
|
|
|
|
|
})();
|