/
MessageEditorController.js
322 lines (292 loc) · 11.2 KB
/
MessageEditorController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
/**
* @ngInject
*/
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, hotkeys = [];
vm.addRecipient = addRecipient;
vm.autocomplete = {to: {}, cc: {}, bcc: {}};
vm.autosave = null;
vm.autosaveDrafts = autosaveDrafts;
vm.cancel = cancel;
vm.contactFilter = contactFilter;
vm.isFullscreen = false;
vm.hideBcc = (stateMessage.editable.bcc.length === 0);
vm.hideCc = (stateMessage.editable.cc.length === 0);
vm.identities = _.map(stateAccount.identities, 'full');
vm.message = stateMessage;
vm.recipientSeparatorKeys = [
$mdConstant.KEY_CODE.ENTER,
$mdConstant.KEY_CODE.TAB,
$mdConstant.KEY_CODE.COMMA,
$mdConstant.KEY_CODE.SEMICOLON
];
vm.removeAttachment = removeAttachment;
vm.save = save;
vm.send = send;
vm.sendState = false;
vm.toggleFullscreen = toggleFullscreen;
vm.uploader = new FileUploader({
url: stateMessage.$absolutePath({asDraft: true, withResourcePath: true}) + '/save',
autoUpload: true,
alias: 'attachments',
removeAfterUpload: false,
// onProgressItem: function(item, progress) {
// console.debug(item); console.debug(progress);
// },
onSuccessItem: function(item, response, status, headers) {
stateMessage.$setUID(response.uid);
stateMessage.$reload({asDraft: false});
item.inlineUrl = response.lastAttachmentAttrs[0].url;
//console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2));
},
onCancelItem: function(item, response, status, headers) {
//console.debug(item); console.debug('cancel = ' + JSON.stringify(response, undefined, 2));
// We remove the attachment
stateMessage.$deleteAttachment(item.file.name);
this.removeFromQueue(item);
},
onErrorItem: function(item, response, status, headers) {
$mdToast.show(
$mdToast.simple()
.content(l('Error while uploading the file \"%{0}\":', item.file.name) +
' ' + (response.message? l(response.message) : ''))
.position('top right')
.action(l('OK'))
.hideDelay(false));
this.removeFromQueue(item);
//console.debug(item); console.debug('error = ' + JSON.stringify(response, undefined, 2));
}
});
// Destroy file uploader when the controller is being deactivated
$scope.$on('$destroy', function() { vm.uploader.destroy(); });
if ($stateParams.actionName == 'reply') {
stateMessage.$reply().then(function(msgObject) {
vm.message = msgObject;
vm.hideCc = (!msgObject.editable.cc || msgObject.editable.cc.length === 0);
vm.hideBcc = (!msgObject.editable.bcc || msgObject.editable.bcc.length === 0);
});
}
else if ($stateParams.actionName == 'replyall') {
stateMessage.$replyAll().then(function(msgObject) {
vm.message = msgObject;
vm.hideCc = (!msgObject.editable.cc || msgObject.editable.cc.length === 0);
vm.hideBcc = (!msgObject.editable.bcc || msgObject.editable.bcc.length === 0);
});
}
else if ($stateParams.actionName == 'forward') {
stateMessage.$forward().then(function(msgObject) {
vm.message = msgObject;
addAttachments();
});
}
else if (angular.isDefined(stateMessage)) {
vm.message = stateMessage;
addAttachments();
}
/**
* If this is a popup window, retrieve the mailbox controller of the parent window.
*/
function $parentControllers() {
var originMessage, ctrls = {};
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;
}
}
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;
}
}
}
}
}
catch (e) {}
return ctrls;
}
function addAttachments() {
// Add existing attached files to uploader
var i, data, fileItem, attrs = vm.message.editable.attachmentAttrs;
if (attrs)
for (i = 0; i < attrs.length; i++) {
data = {
name: attrs[i].filename,
type: attrs[i].mimetype,
size: parseInt(attrs[i].size)
};
fileItem = new FileUploader.FileItem(vm.uploader, data);
fileItem.progress = 100;
fileItem.isUploaded = true;
fileItem.isSuccess = true;
fileItem.inlineUrl = attrs[i].url;
vm.uploader.queue.push(fileItem);
}
}
function removeAttachment(item, id) {
if (item.isUploading)
vm.uploader.cancelItem(item);
else {
vm.message.$deleteAttachment(item.file.name);
item.remove();
}
// 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);
}
function cancel() {
if (vm.autosave)
$timeout.cancel(vm.autosave);
if (vm.message.isNew && vm.message.attachmentAttrs)
vm.message.$mailbox.$deleteMessages([vm.message]);
$mdDialog.cancel();
}
function save() {
var ctrls = $parentControllers();
vm.message.$save().then(function(data) {
vm.message.$rawSource = null;
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 });
}
});
}
$mdToast.show(
$mdToast.simple()
.content(l('Your email has been saved'))
.position('top right')
.hideDelay(3000));
});
}
function send() {
var ctrls = $parentControllers();
vm.sendState = 'sending';
if (vm.autosave)
$timeout.cancel(vm.autosave);
vm.message.$send().then(function(data) {
vm.sendState = 'sent';
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();
}
$mdToast.show(
$mdToast.simple()
.content(l('Your email has been sent'))
.position('top right')
.hideDelay(3000));
// 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;
});
}
function toggleFullscreen() {
vm.isFullscreen = !vm.isFullscreen;
}
function contactFilter($query) {
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;
});
});
}
function addRecipient(contact, field) {
var recipients, recipient, list;
recipients = vm.message.editable[field];
if (angular.isString(contact)) {
_.forEach(contact.split(/[,;]/), function(address) {
recipients.push(address);
});
return null;
}
if (contact.$isList({expandable: true})) {
// If the list's members were already fetch, use them
if (angular.isDefined(contact.refs) && contact.refs.length) {
_.forEach(contact.refs, function(ref) {
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;
}
// Drafts autosaving
function autosaveDrafts() {
vm.message.$save();
if (Preferences.defaults.SOGoMailAutoSave)
vm.autosave = $timeout(vm.autosaveDrafts, Preferences.defaults.SOGoMailAutoSave*1000*60);
}
// Read user's defaults
Preferences.ready().then(function() {
if (Preferences.defaults.SOGoMailAutoSave)
// Enable auto-save of draft
vm.autosave = $timeout(vm.autosaveDrafts, Preferences.defaults.SOGoMailAutoSave*1000*60);
// Set the locale of CKEditor
vm.localeCode = Preferences.defaults.LocaleCode;
});
}
SendMessageToastController.$inject = ['$scope', '$mdToast'];
function SendMessageToastController($scope, $mdToast) {
$scope.closeToast = function() {
$mdToast.hide();
};
}
angular
.module('SOGo.MailerUI')
.controller('SendMessageToastController', SendMessageToastController)
.controller('MessageEditorController', MessageEditorController);
})();