2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2016-01-18 20:33:31 +01:00
|
|
|
MailboxController.$inject = ['$window', '$timeout', '$state', '$mdDialog', 'stateAccounts', 'stateAccount', 'stateMailbox', 'encodeUriFilter', 'Dialog', 'Account', 'Mailbox'];
|
|
|
|
function MailboxController($window, $timeout, $state, $mdDialog, stateAccounts, stateAccount, stateMailbox, encodeUriFilter, Dialog, Account, Mailbox) {
|
2015-08-27 03:22:39 +02:00
|
|
|
var vm = this, messageDialog = null;
|
2015-07-13 21:22:58 +02:00
|
|
|
|
2016-01-07 21:43:26 +01:00
|
|
|
// Expose controller
|
|
|
|
$window.$mailboxController = vm;
|
|
|
|
|
2015-09-14 19:48:06 +02:00
|
|
|
Mailbox.selectedFolder = stateMailbox;
|
2015-07-13 21:22:58 +02:00
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
vm.service = Mailbox;
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.accounts = stateAccounts;
|
|
|
|
vm.account = stateAccount;
|
|
|
|
vm.selectedFolder = stateMailbox;
|
|
|
|
vm.selectMessage = selectMessage;
|
2015-09-09 21:18:00 +02:00
|
|
|
vm.toggleMessageSelection = toggleMessageSelection;
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.unselectMessages = unselectMessages;
|
|
|
|
vm.confirmDeleteSelectedMessages = confirmDeleteSelectedMessages;
|
2016-02-08 21:36:01 +01:00
|
|
|
vm.markOrUnMarkMessagesAsJunk = markOrUnMarkMessagesAsJunk;
|
2015-07-13 21:22:58 +02:00
|
|
|
vm.copySelectedMessages = copySelectedMessages;
|
2016-02-03 22:48:04 +01:00
|
|
|
vm.moveSelectedMessages = moveSelectedMessages;
|
2015-08-10 19:10:45 +02:00
|
|
|
vm.markSelectedMessagesAsFlagged = markSelectedMessagesAsFlagged;
|
|
|
|
vm.markSelectedMessagesAsUnread = markSelectedMessagesAsUnread;
|
2015-07-23 16:54:46 +02:00
|
|
|
vm.selectAll = selectAll;
|
2015-07-20 22:44:26 +02:00
|
|
|
vm.sort = sort;
|
|
|
|
vm.sortedBy = sortedBy;
|
|
|
|
vm.cancelSearch = cancelSearch;
|
2015-08-02 19:25:38 +02:00
|
|
|
vm.newMessage = newMessage;
|
2016-03-08 19:20:34 +01:00
|
|
|
vm.mode = { search: false, multiple: 0 };
|
2015-07-15 19:44:17 +02:00
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
function selectMessage(message) {
|
2015-09-10 22:38:30 +02:00
|
|
|
if (Mailbox.$virtualMode)
|
|
|
|
$state.go('mail.account.virtualMailbox.message', {accountId: stateAccount.id, mailboxId: encodeUriFilter(message.$mailbox.path), messageId: message.uid});
|
|
|
|
else
|
2016-03-03 21:03:42 +01:00
|
|
|
$state.go('mail.account.mailbox.message', {messageId: message.uid});
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:18:00 +02:00
|
|
|
function toggleMessageSelection($event, message) {
|
|
|
|
message.selected = !message.selected;
|
2016-03-08 19:20:34 +01:00
|
|
|
vm.mode.multiple += message.selected? 1 : -1;
|
2015-09-09 21:18:00 +02:00
|
|
|
$event.preventDefault();
|
|
|
|
$event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
function unselectMessages() {
|
2016-03-08 19:20:34 +01:00
|
|
|
_.forEach(vm.selectedFolder.$messages, function(message) {
|
|
|
|
message.selected = false;
|
|
|
|
});
|
|
|
|
vm.mode.multiple = 0;
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function confirmDeleteSelectedMessages() {
|
|
|
|
Dialog.confirm(l('Warning'),
|
2016-02-10 16:52:46 +01:00
|
|
|
l('Are you sure you want to delete the selected messages?'),
|
|
|
|
{ ok: l('Delete') })
|
2015-07-13 21:22:58 +02:00
|
|
|
.then(function() {
|
2016-02-03 22:48:04 +01:00
|
|
|
var deleteSelectedMessage = false;
|
2015-12-10 17:21:29 +01:00
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) {
|
2016-01-08 22:31:06 +01:00
|
|
|
if (message.selected &&
|
|
|
|
message.uid == vm.selectedFolder.selectedMessage)
|
2016-02-03 22:48:04 +01:00
|
|
|
deleteSelectedMessage = true;
|
2015-12-10 17:21:29 +01:00
|
|
|
return message.selected;
|
|
|
|
});
|
2016-01-08 22:31:06 +01:00
|
|
|
vm.selectedFolder.$deleteMessages(selectedMessages).then(function(index) {
|
2016-02-03 22:48:04 +01:00
|
|
|
unselectMessage(deleteSelectedMessage, index);
|
2015-07-13 21:22:58 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:36:01 +01:00
|
|
|
function markOrUnMarkMessagesAsJunk() {
|
|
|
|
var moveSelectedMessage = false;
|
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) {
|
|
|
|
if (message.selected &&
|
|
|
|
message.uid == vm.selectedFolder.selectedMessage)
|
|
|
|
moveSelectedMessage = true;
|
|
|
|
return message.selected;
|
|
|
|
});
|
|
|
|
|
|
|
|
vm.selectedFolder.$markOrUnMarkMessagesAsJunk(selectedMessages).then(function() {
|
|
|
|
var folder = '/' + vm.account.id + '/folderINBOX';
|
|
|
|
|
|
|
|
if (vm.selectedFolder.type != 'junk') {
|
|
|
|
folder = '/' + vm.account.$getMailboxByType('junk').id;
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.selectedFolder.$moveMessages(selectedMessages, folder).then(function(index) {
|
|
|
|
unselectMessage(moveSelectedMessage, index);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:48:04 +01:00
|
|
|
function unselectMessage(message, index) {
|
|
|
|
// Unselect current message and cleverly load the next message
|
|
|
|
var nextMessage, previousMessage, nextIndex = index;
|
2016-03-08 19:20:34 +01:00
|
|
|
vm.mode.multiple = vm.selectedFolder.$selectedCount();
|
2016-02-03 22:48:04 +01:00
|
|
|
if (message) {
|
|
|
|
if (Mailbox.$virtualMode) {
|
|
|
|
$state.go('mail.account.virtualMailbox');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Select either the next or previous message
|
|
|
|
if (index > 0) {
|
|
|
|
nextIndex -= 1;
|
|
|
|
nextMessage = vm.selectedFolder.$messages[nextIndex];
|
|
|
|
}
|
|
|
|
if (index < vm.selectedFolder.$messages.length)
|
|
|
|
previousMessage = vm.selectedFolder.$messages[index];
|
|
|
|
if (nextMessage) {
|
|
|
|
if (nextMessage.isread && previousMessage && !previousMessage.isread) {
|
|
|
|
nextIndex = index;
|
|
|
|
nextMessage = previousMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (previousMessage) {
|
|
|
|
nextIndex = index;
|
|
|
|
nextMessage = previousMessage;
|
|
|
|
}
|
|
|
|
if (nextMessage) {
|
|
|
|
$state.go('mail.account.mailbox.message', { messageId: nextMessage.uid });
|
|
|
|
vm.selectedFolder.$topIndex = nextIndex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$state.go('mail.account.mailbox');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:22:58 +02:00
|
|
|
function copySelectedMessages(folder) {
|
2015-07-24 22:14:53 +02:00
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) { return message.selected; });
|
2016-03-03 19:38:54 +01:00
|
|
|
var selectedUIDs = _.map(selectedMessages, 'uid');
|
2015-12-10 17:21:29 +01:00
|
|
|
vm.selectedFolder.$copyMessages(selectedUIDs, '/' + folder);
|
2015-07-13 21:22:58 +02:00
|
|
|
}
|
2015-05-09 20:37:40 +02:00
|
|
|
|
2016-02-03 22:48:04 +01:00
|
|
|
function moveSelectedMessages(folder) {
|
|
|
|
var moveSelectedMessage = false;
|
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) {
|
|
|
|
if (message.selected &&
|
|
|
|
message.uid == vm.selectedFolder.selectedMessage)
|
|
|
|
moveSelectedMessage = true;
|
|
|
|
return message.selected;
|
|
|
|
});
|
|
|
|
vm.selectedFolder.$moveMessages(selectedMessages, '/' + folder).then(function(index) {
|
|
|
|
unselectMessage(moveSelectedMessage, index);
|
|
|
|
});
|
|
|
|
}
|
2015-07-20 22:44:26 +02:00
|
|
|
|
2015-07-23 16:54:46 +02:00
|
|
|
function selectAll() {
|
2015-11-24 21:05:03 +01:00
|
|
|
var i = 0, length = vm.selectedFolder.$messages.length;
|
|
|
|
for (; i < length; i++)
|
|
|
|
vm.selectedFolder.$messages[i].selected = true;
|
2016-03-08 19:20:34 +01:00
|
|
|
vm.mode.multiple = length;
|
2015-07-23 16:54:46 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 19:10:45 +02:00
|
|
|
function markSelectedMessagesAsFlagged() {
|
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) { return message.selected; });
|
2016-03-03 19:38:54 +01:00
|
|
|
var selectedUIDs = _.map(selectedMessages, 'uid');
|
2015-08-10 19:10:45 +02:00
|
|
|
|
|
|
|
vm.selectedFolder.$flagMessages(selectedUIDs, '\\Flagged', 'add').then(function(d) {
|
|
|
|
// Success
|
|
|
|
_.forEach(selectedMessages, function(message) {
|
|
|
|
message.isflagged = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function markSelectedMessagesAsUnread() {
|
|
|
|
var selectedMessages = _.filter(vm.selectedFolder.$messages, function(message) { return message.selected; });
|
2016-03-03 19:38:54 +01:00
|
|
|
var selectedUIDs = _.map(selectedMessages, 'uid');
|
2015-08-10 19:10:45 +02:00
|
|
|
|
|
|
|
vm.selectedFolder.$flagMessages(selectedUIDs, 'seen', 'remove').then(function(d) {
|
|
|
|
// Success
|
|
|
|
_.forEach(selectedMessages, function(message) {
|
|
|
|
message.isread = false;
|
|
|
|
vm.selectedFolder.unseenCount++;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:44:26 +02:00
|
|
|
function sort(field) {
|
|
|
|
vm.selectedFolder.$filter({ sort: field });
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortedBy(field) {
|
2015-07-22 17:28:08 +02:00
|
|
|
return Mailbox.$query.sort == field;
|
2015-07-20 22:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function cancelSearch() {
|
|
|
|
vm.mode.search = false;
|
2016-01-18 20:33:31 +01:00
|
|
|
vm.selectedFolder.$filter().then(function() {
|
|
|
|
if (vm.selectedFolder.selectedMessage) {
|
|
|
|
$timeout(function() {
|
|
|
|
vm.selectedFolder.$topIndex = vm.selectedFolder.uidsMap[vm.selectedFolder.selectedMessage];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-20 22:44:26 +02:00
|
|
|
}
|
2015-07-30 19:06:28 +02:00
|
|
|
|
2015-08-02 19:25:38 +02:00
|
|
|
function newMessage($event) {
|
2015-08-27 03:22:39 +02:00
|
|
|
var message;
|
|
|
|
|
|
|
|
if (messageDialog === null) {
|
|
|
|
message = vm.account.$newMessage();
|
|
|
|
messageDialog = $mdDialog
|
|
|
|
.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
targetEvent: $event,
|
|
|
|
clickOutsideToClose: false,
|
|
|
|
escapeToClose: false,
|
|
|
|
templateUrl: 'UIxMailEditor',
|
|
|
|
controller: 'MessageEditorController',
|
|
|
|
controllerAs: 'editor',
|
|
|
|
locals: {
|
|
|
|
stateAccounts: vm.accounts,
|
|
|
|
stateMessage: message,
|
|
|
|
stateRecipients: []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(function() {
|
|
|
|
messageDialog = null;
|
|
|
|
});
|
|
|
|
}
|
2015-08-02 19:25:38 +02:00
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:31:06 +01:00
|
|
|
angular
|
|
|
|
.module('material.components.virtualRepeat')
|
|
|
|
.decorator('mdVirtualRepeatContainerDirective', mdVirtualRepeatContainerDirectiveDecorator);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
|
|
|
mdVirtualRepeatContainerDirectiveDecorator.$inject = ['$delegate'];
|
|
|
|
function mdVirtualRepeatContainerDirectiveDecorator($delegate) {
|
|
|
|
$delegate[0].controller.prototype.resetScroll = function() {
|
|
|
|
// Don't scroll to top if current virtual repeater is the messages list
|
|
|
|
// but do update the container size
|
|
|
|
if (this.$element.parent().attr('id') == 'messagesList')
|
|
|
|
this.updateSize();
|
|
|
|
else
|
|
|
|
this.scrollTo(0);
|
|
|
|
};
|
|
|
|
return $delegate;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
angular
|
2015-09-09 21:18:00 +02:00
|
|
|
.module('SOGo.MailerUI')
|
|
|
|
.controller('MailboxController', MailboxController);
|
2015-05-06 04:06:13 +02:00
|
|
|
})();
|
|
|
|
|