2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2016-07-15 20:03:16 +02:00
|
|
|
MessageEditorController.$inject = ['$scope', '$window', '$stateParams', '$mdConstant', '$mdDialog', '$mdToast', 'FileUploader', 'stateAccount', 'stateMessage', 'encodeUriFilter', '$timeout', 'Dialog', 'AddressBook', 'Card', 'Preferences'];
|
|
|
|
function MessageEditorController($scope, $window, $stateParams, $mdConstant, $mdDialog, $mdToast, FileUploader, stateAccount, stateMessage, encodeUriFilter, $timeout, Dialog, AddressBook, Card, Preferences) {
|
|
|
|
var vm = this;
|
2015-07-13 21:22:58 +02:00
|
|
|
|
2015-08-26 04:17:57 +02:00
|
|
|
vm.addRecipient = addRecipient;
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.autocomplete = {to: {}, cc: {}, bcc: {}};
|
2015-07-21 17:19:47 +02:00
|
|
|
vm.autosave = null;
|
|
|
|
vm.autosaveDrafts = autosaveDrafts;
|
2016-03-15 14:54:15 +01:00
|
|
|
vm.hideCc = (stateMessage.editable.cc.length === 0);
|
|
|
|
vm.hideBcc = (stateMessage.editable.bcc.length === 0);
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.cancel = cancel;
|
2015-12-10 19:41:07 +01:00
|
|
|
vm.save = save;
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.send = send;
|
2016-07-15 20:03:16 +02:00
|
|
|
vm.sendState = false;
|
2015-12-01 22:09:29 +01:00
|
|
|
vm.removeAttachment = removeAttachment;
|
2015-07-28 02:43:43 +02:00
|
|
|
vm.contactFilter = contactFilter;
|
2016-05-10 20:41:24 +02:00
|
|
|
vm.identities = _.map(stateAccount.identities, 'full');
|
2016-07-15 20:03:16 +02:00
|
|
|
vm.recipientSeparatorKeys = [
|
|
|
|
$mdConstant.KEY_CODE.ENTER,
|
|
|
|
$mdConstant.KEY_CODE.TAB,
|
|
|
|
$mdConstant.KEY_CODE.COMMA,
|
|
|
|
$mdConstant.KEY_CODE.SEMICOLON
|
|
|
|
];
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.uploader = new FileUploader({
|
|
|
|
url: stateMessage.$absolutePath({asDraft: true}) + '/save',
|
|
|
|
autoUpload: true,
|
|
|
|
alias: 'attachments',
|
2015-12-01 22:09:29 +01:00
|
|
|
removeAfterUpload: false,
|
|
|
|
// onProgressItem: function(item, progress) {
|
|
|
|
// console.debug(item); console.debug(progress);
|
|
|
|
// },
|
2015-07-13 21:22:58 +02:00
|
|
|
onSuccessItem: function(item, response, status, headers) {
|
|
|
|
stateMessage.$setUID(response.uid);
|
2015-12-01 22:09:29 +01:00
|
|
|
stateMessage.$reload({asDraft: false});
|
2015-12-02 22:32:39 +01:00
|
|
|
item.inlineUrl = response.lastAttachmentAttrs[0].url;
|
2015-12-01 22:09:29 +01:00
|
|
|
//console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2));
|
2015-07-13 21:22:58 +02:00
|
|
|
},
|
|
|
|
onCancelItem: function(item, response, status, headers) {
|
2015-12-01 22:09:29 +01:00
|
|
|
//console.debug(item); console.debug('cancel = ' + JSON.stringify(response, undefined, 2));
|
2015-07-13 21:22:58 +02:00
|
|
|
// We remove the attachment
|
|
|
|
stateMessage.$deleteAttachment(item.file.name);
|
|
|
|
this.removeFromQueue(item);
|
|
|
|
},
|
|
|
|
onErrorItem: function(item, response, status, headers) {
|
2016-02-17 14:24:32 +01:00
|
|
|
$mdToast.show(
|
|
|
|
$mdToast.simple()
|
2016-02-17 14:40:57 +01:00
|
|
|
.content(l('Error while uploading the file \"%{0}\":', item.file.name))
|
2016-02-17 14:24:32 +01:00
|
|
|
.position('top right')
|
|
|
|
.hideDelay(3000));
|
|
|
|
this.removeFromQueue(item);
|
2015-12-01 22:09:29 +01:00
|
|
|
//console.debug(item); console.debug('error = ' + JSON.stringify(response, undefined, 2));
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-15 20:03:16 +02:00
|
|
|
// Destroy file uploader when the controller is being deactivated
|
|
|
|
$scope.$on('$destroy', function() { vm.uploader.destroy(); });
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
if ($stateParams.actionName == 'reply') {
|
|
|
|
stateMessage.$reply().then(function(msgObject) {
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.message = msgObject;
|
2015-07-24 22:14:53 +02:00
|
|
|
vm.hideCc = (!msgObject.editable.cc || msgObject.editable.cc.length === 0);
|
|
|
|
vm.hideBcc = (!msgObject.editable.bcc || msgObject.editable.bcc.length === 0);
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($stateParams.actionName == 'replyall') {
|
|
|
|
stateMessage.$replyAll().then(function(msgObject) {
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.message = msgObject;
|
2015-07-24 22:14:53 +02:00
|
|
|
vm.hideCc = (!msgObject.editable.cc || msgObject.editable.cc.length === 0);
|
|
|
|
vm.hideBcc = (!msgObject.editable.bcc || msgObject.editable.bcc.length === 0);
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($stateParams.actionName == 'forward') {
|
|
|
|
stateMessage.$forward().then(function(msgObject) {
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.message = msgObject;
|
2015-12-01 22:09:29 +01:00
|
|
|
addAttachments();
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (angular.isDefined(stateMessage)) {
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.message = stateMessage;
|
2015-12-01 22:09:29 +01:00
|
|
|
addAttachments();
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
2015-07-13 21:22:58 +02:00
|
|
|
|
2016-01-07 21:43:26 +01:00
|
|
|
/**
|
|
|
|
* If this is a popup window, retrieve the mailbox controller of the parent window.
|
|
|
|
*/
|
|
|
|
function $parentControllers() {
|
|
|
|
var originMessage, ctrls = {};
|
2016-11-28 21:33:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if ($window.opener) {
|
|
|
|
if ('$mailboxController' in $window.opener &&
|
|
|
|
'selectedFolder' in $window.opener.$mailboxController) {
|
|
|
|
if ($window.opener.$mailboxController.selectedFolder.type == 'draft') {
|
|
|
|
ctrls.draftMailboxCtrl = $window.opener.$mailboxController;
|
|
|
|
if ('$messageController' in $window.opener &&
|
|
|
|
$window.opener.$messageController.message.uid == stateMessage.uid) {
|
|
|
|
// The draft is opened in the parent window
|
|
|
|
ctrls.draftMessageCtrl = $window.opener.$messageController;
|
|
|
|
}
|
2016-01-07 21:43:26 +01:00
|
|
|
}
|
2016-11-28 21:33:02 +01:00
|
|
|
else if (stateMessage.origin) {
|
|
|
|
originMessage = stateMessage.origin.message;
|
|
|
|
if ($window.opener.$mailboxController.selectedFolder.$id() == originMessage.$mailbox.$id()) {
|
|
|
|
// The message mailbox is opened in the parent window
|
|
|
|
ctrls.originMailboxCtrl = $window.opener.$mailboxController;
|
|
|
|
}
|
2016-01-07 21:43:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 21:33:02 +01:00
|
|
|
catch (e) {}
|
|
|
|
|
2016-01-07 21:43:26 +01:00
|
|
|
return ctrls;
|
|
|
|
}
|
|
|
|
|
2015-12-01 22:09:29 +01:00
|
|
|
function addAttachments() {
|
|
|
|
// Add existing attached files to uploader
|
2016-03-03 21:41:29 +01:00
|
|
|
var i, data, fileItem, attrs = vm.message.editable.attachmentAttrs;
|
|
|
|
if (attrs)
|
|
|
|
for (i = 0; i < attrs.length; i++) {
|
2015-12-01 22:09:29 +01:00
|
|
|
data = {
|
2016-03-03 21:41:29 +01:00
|
|
|
name: attrs[i].filename,
|
|
|
|
type: attrs[i].mimetype,
|
|
|
|
size: parseInt(attrs[i].size)
|
2015-12-01 22:09:29 +01:00
|
|
|
};
|
|
|
|
fileItem = new FileUploader.FileItem(vm.uploader, data);
|
|
|
|
fileItem.progress = 100;
|
|
|
|
fileItem.isUploaded = true;
|
|
|
|
fileItem.isSuccess = true;
|
2016-03-03 21:41:29 +01:00
|
|
|
fileItem.inlineUrl = attrs[i].url;
|
2015-12-01 22:09:29 +01:00
|
|
|
vm.uploader.queue.push(fileItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 16:21:33 +01:00
|
|
|
function removeAttachment(item, id) {
|
2015-12-01 22:09:29 +01:00
|
|
|
if (item.isUploading)
|
|
|
|
vm.uploader.cancelItem(item);
|
|
|
|
else {
|
|
|
|
vm.message.$deleteAttachment(item.file.name);
|
|
|
|
item.remove();
|
|
|
|
}
|
2016-11-10 16:21:33 +01:00
|
|
|
// Hack to allow adding the same file again
|
|
|
|
// See https://github.com/nervgh/angular-file-upload/issues/671
|
|
|
|
var element = $window.document.getElementById(id);
|
|
|
|
if (element)
|
|
|
|
angular.element(element).prop('value', null);
|
2015-12-01 22:09:29 +01:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
function cancel() {
|
2015-07-21 17:19:47 +02:00
|
|
|
if (vm.autosave)
|
|
|
|
$timeout.cancel(vm.autosave);
|
|
|
|
|
2016-01-25 20:21:38 +01:00
|
|
|
if (vm.message.isNew && vm.message.attachmentAttrs)
|
|
|
|
vm.message.$mailbox.$deleteMessages([vm.message]);
|
|
|
|
|
2015-08-02 19:25:38 +02:00
|
|
|
$mdDialog.cancel();
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 19:41:07 +01:00
|
|
|
function save() {
|
2016-01-07 21:43:26 +01:00
|
|
|
var ctrls = $parentControllers();
|
2015-12-10 19:41:07 +01:00
|
|
|
vm.message.$save().then(function(data) {
|
2016-05-11 03:23:15 +02:00
|
|
|
vm.message.$rawSource = null;
|
2016-01-07 21:43:26 +01:00
|
|
|
if (ctrls.draftMailboxCtrl) {
|
|
|
|
// We're saving a draft from a popup window.
|
|
|
|
// Reload draft mailbox
|
|
|
|
ctrls.draftMailboxCtrl.selectedFolder.$filter().then(function() {
|
|
|
|
if (ctrls.draftMessageCtrl) {
|
|
|
|
// Reload selected message
|
|
|
|
ctrls.draftMessageCtrl.$state.go('mail.account.mailbox.message', { messageId: vm.message.uid });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-12-10 19:41:07 +01:00
|
|
|
$mdToast.show(
|
|
|
|
$mdToast.simple()
|
|
|
|
.content(l('Your email has been saved'))
|
|
|
|
.position('top right')
|
|
|
|
.hideDelay(3000));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
function send() {
|
2016-01-07 21:43:26 +01:00
|
|
|
var ctrls = $parentControllers();
|
2016-07-15 20:03:16 +02:00
|
|
|
|
|
|
|
vm.sendState = 'sending';
|
2015-07-21 17:19:47 +02:00
|
|
|
if (vm.autosave)
|
|
|
|
$timeout.cancel(vm.autosave);
|
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.message.$send().then(function(data) {
|
2016-07-15 20:03:16 +02:00
|
|
|
vm.sendState = 'sent';
|
2016-01-07 21:43:26 +01:00
|
|
|
if (ctrls.draftMailboxCtrl) {
|
|
|
|
// We're sending a draft from a popup window and the draft mailbox is opened.
|
|
|
|
// Reload draft mailbox
|
|
|
|
ctrls.draftMailboxCtrl.selectedFolder.$filter().then(function() {
|
|
|
|
if (ctrls.draftMessageCtrl) {
|
|
|
|
// Close draft
|
|
|
|
ctrls.draftMessageCtrl.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (ctrls.originMailboxCtrl) {
|
|
|
|
// We're sending a draft from a popup window and the original mailbox is opened.
|
|
|
|
// Reload mailbox
|
|
|
|
ctrls.originMailboxCtrl.selectedFolder.$filter();
|
|
|
|
}
|
2015-12-10 19:41:07 +01:00
|
|
|
$mdToast.show(
|
|
|
|
$mdToast.simple()
|
|
|
|
.content(l('Your email has been sent'))
|
|
|
|
.position('top right')
|
|
|
|
.hideDelay(3000));
|
2016-07-15 20:03:16 +02:00
|
|
|
|
|
|
|
// Let the user see the succesfull message before closing the dialog
|
|
|
|
$timeout($mdDialog.hide, 1000);
|
|
|
|
}, function(response) {
|
|
|
|
vm.sendState = 'error';
|
|
|
|
vm.errorMessage = response.data? response.data.message : response.statusText;
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:43:43 +02:00
|
|
|
function contactFilter($query) {
|
2016-05-25 16:25:53 +02:00
|
|
|
return AddressBook.$filterAll($query).then(function(cards) {
|
|
|
|
// Divide the matching cards by email addresses so the user can select
|
|
|
|
// the recipient address of her choice
|
|
|
|
var explodedCards = [];
|
|
|
|
_.forEach(_.invokeMap(cards, 'explode'), function(manyCards) {
|
|
|
|
_.forEach(manyCards, function(card) {
|
|
|
|
explodedCards.push(card);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// Remove duplicates
|
|
|
|
return _.uniqBy(explodedCards, function(card) {
|
|
|
|
return card.$$fullname + ' ' + card.$$email;
|
|
|
|
});
|
|
|
|
});
|
2015-08-26 04:17:57 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 22:58:40 +01:00
|
|
|
function addRecipient(contact, field) {
|
|
|
|
var recipients, recipient, list;
|
2015-08-26 04:17:57 +02:00
|
|
|
|
2015-12-22 22:58:40 +01:00
|
|
|
if (angular.isString(contact))
|
|
|
|
return contact;
|
2015-08-26 04:17:57 +02:00
|
|
|
|
2015-12-22 22:58:40 +01:00
|
|
|
recipients = vm.message.editable[field];
|
|
|
|
|
2016-05-19 22:34:42 +02:00
|
|
|
if (contact.$isList({expandable: true})) {
|
2015-12-22 22:58:40 +01:00
|
|
|
// If the list's members were already fetch, use them
|
|
|
|
if (angular.isDefined(contact.refs) && contact.refs.length) {
|
2016-03-03 19:38:54 +01:00
|
|
|
_.forEach(contact.refs, function(ref) {
|
2015-12-22 22:58:40 +01:00
|
|
|
if (ref.email.length)
|
|
|
|
recipients.push(ref.$shortFormat());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list = Card.$find(contact.container, contact.c_name);
|
|
|
|
list.$id().then(function(listId) {
|
|
|
|
_.forEach(list.refs, function(ref) {
|
|
|
|
if (ref.email.length)
|
|
|
|
recipients.push(ref.$shortFormat());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
recipient = contact.$shortFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient)
|
|
|
|
return recipient;
|
|
|
|
else
|
|
|
|
return null;
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
2015-07-21 17:19:47 +02:00
|
|
|
|
|
|
|
// Drafts autosaving
|
|
|
|
function autosaveDrafts() {
|
|
|
|
vm.message.$save();
|
|
|
|
if (Preferences.defaults.SOGoMailAutoSave)
|
|
|
|
vm.autosave = $timeout(vm.autosaveDrafts, Preferences.defaults.SOGoMailAutoSave*1000*60);
|
|
|
|
}
|
|
|
|
|
2015-12-01 22:09:29 +01:00
|
|
|
// Read user's defaults
|
2015-07-21 17:19:47 +02:00
|
|
|
Preferences.ready().then(function() {
|
|
|
|
if (Preferences.defaults.SOGoMailAutoSave)
|
2015-12-01 22:09:29 +01:00
|
|
|
// Enable auto-save of draft
|
2015-07-21 17:19:47 +02:00
|
|
|
vm.autosave = $timeout(vm.autosaveDrafts, Preferences.defaults.SOGoMailAutoSave*1000*60);
|
2015-12-01 22:09:29 +01:00
|
|
|
// Set the locale of CKEditor
|
2015-08-06 22:09:49 +02:00
|
|
|
vm.localeCode = Preferences.defaults.LocaleCode;
|
2015-07-21 17:19:47 +02:00
|
|
|
});
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 22:30:21 +01:00
|
|
|
SendMessageToastController.$inject = ['$scope', '$mdToast'];
|
|
|
|
function SendMessageToastController($scope, $mdToast) {
|
|
|
|
$scope.closeToast = function() {
|
|
|
|
$mdToast.hide();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
angular
|
2015-11-26 22:30:21 +01:00
|
|
|
.module('SOGo.MailerUI')
|
|
|
|
.controller('SendMessageToastController', SendMessageToastController)
|
2015-12-22 22:58:40 +01:00
|
|
|
.controller('MessageEditorController', MessageEditorController);
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
})();
|