2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2016-01-15 16:26:38 +01:00
|
|
|
AddressBookController.$inject = ['$scope', '$q', '$window', '$state', '$timeout', '$mdDialog', 'Account', 'Card', 'AddressBook', 'Dialog', 'sgSettings', 'stateAddressbooks', 'stateAddressbook'];
|
|
|
|
function AddressBookController($scope, $q, $window, $state, $timeout, $mdDialog, Account, Card, AddressBook, Dialog, Settings, stateAddressbooks, stateAddressbook) {
|
2015-05-28 19:48:45 +02:00
|
|
|
var vm = this;
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
AddressBook.selectedFolder = stateAddressbook;
|
|
|
|
|
2015-09-15 03:35:34 +02:00
|
|
|
vm.service = AddressBook;
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.selectedFolder = stateAddressbook;
|
|
|
|
vm.selectCard = selectCard;
|
2015-09-10 15:19:28 +02:00
|
|
|
vm.toggleCardSelection = toggleCardSelection;
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.newComponent = newComponent;
|
|
|
|
vm.notSelectedComponent = notSelectedComponent;
|
|
|
|
vm.unselectCards = unselectCards;
|
|
|
|
vm.confirmDeleteSelectedCards = confirmDeleteSelectedCards;
|
2015-08-05 22:02:57 +02:00
|
|
|
vm.saveSelectedCards = saveSelectedCards;
|
2015-08-10 15:48:47 +02:00
|
|
|
vm.copySelectedCards = copySelectedCards;
|
2015-08-05 21:04:47 +02:00
|
|
|
vm.selectAll = selectAll;
|
2015-07-20 20:24:36 +02:00
|
|
|
vm.sort = sort;
|
|
|
|
vm.sortedBy = sortedBy;
|
2015-07-20 21:03:29 +02:00
|
|
|
vm.cancelSearch = cancelSearch;
|
2015-08-27 17:01:23 +02:00
|
|
|
vm.newMessage = newMessage;
|
|
|
|
vm.newMessageWithSelectedCards = newMessageWithSelectedCards;
|
|
|
|
vm.newMessageWithRecipient = newMessageWithRecipient;
|
2015-07-20 21:03:29 +02:00
|
|
|
vm.mode = { search: false };
|
2015-05-09 20:37:40 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
function selectCard(card) {
|
2015-05-08 18:06:52 +02:00
|
|
|
$state.go('app.addressbook.card.view', {addressbookId: stateAddressbook.id, cardId: card.id});
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-05-09 20:37:40 +02:00
|
|
|
|
2015-09-10 15:19:28 +02:00
|
|
|
function toggleCardSelection($event, card) {
|
|
|
|
card.selected = !card.selected;
|
|
|
|
$event.preventDefault();
|
|
|
|
$event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
function newComponent(ev) {
|
2015-05-09 20:37:40 +02:00
|
|
|
$mdDialog.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
targetEvent: ev,
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
|
|
|
template: [
|
|
|
|
'<md-dialog aria-label="' + l('Create component') + '">',
|
2015-08-04 22:39:02 +02:00
|
|
|
' <md-dialog-content>',
|
2015-05-09 20:37:40 +02:00
|
|
|
' <div layout="column">',
|
|
|
|
' <md-button ng-click="create(\'card\')">',
|
|
|
|
' ' + l('Contact'),
|
|
|
|
' </md-button>',
|
|
|
|
' <md-button ng-click="create(\'list\')">',
|
|
|
|
' ' + l('List'),
|
|
|
|
' </md-button>',
|
|
|
|
' </div>',
|
2015-08-04 22:39:02 +02:00
|
|
|
' </md-dialog-content>',
|
2015-05-09 20:37:40 +02:00
|
|
|
'</md-dialog>'
|
|
|
|
].join(''),
|
|
|
|
locals: {
|
2015-05-28 19:48:45 +02:00
|
|
|
addressbookId: vm.selectedFolder.id
|
2015-05-09 20:37:40 +02:00
|
|
|
},
|
|
|
|
controller: ComponentDialogController
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2015-07-15 21:15:17 +02:00
|
|
|
ComponentDialogController.$inject = ['scope', '$mdDialog', '$state', 'addressbookId'];
|
|
|
|
function ComponentDialogController(scope, $mdDialog, $state, addressbookId) {
|
2015-05-09 20:37:40 +02:00
|
|
|
scope.create = function(type) {
|
|
|
|
$mdDialog.hide();
|
2015-07-15 21:15:17 +02:00
|
|
|
$state.go('app.addressbook.new', { addressbookId: addressbookId, contactType: type });
|
2015-07-24 22:14:53 +02:00
|
|
|
};
|
2015-05-09 20:37:40 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-05-08 21:47:09 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
function notSelectedComponent(currentCard, type) {
|
2015-07-27 18:47:40 +02:00
|
|
|
return (currentCard && currentCard.c_component == type && !currentCard.selected);
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-05-08 21:47:09 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
function unselectCards() {
|
2016-02-26 22:24:18 +01:00
|
|
|
_.each(vm.selectedFolder.$cards, function(card) { card.selected = false; });
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-05-08 21:47:09 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
function confirmDeleteSelectedCards() {
|
2015-05-08 21:47:09 +02:00
|
|
|
Dialog.confirm(l('Warning'),
|
2016-02-10 16:52:46 +01:00
|
|
|
l('Are you sure you want to delete the selected contacts?'),
|
|
|
|
{ ok: l('Delete') })
|
2015-05-08 21:47:09 +02:00
|
|
|
.then(function() {
|
|
|
|
// User confirmed the deletion
|
2016-02-26 22:24:18 +01:00
|
|
|
var selectedCards = _.filter(vm.selectedFolder.$cards, function(card) { return card.selected; });
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.selectedFolder.$deleteCards(selectedCards);
|
2015-07-24 22:14:53 +02:00
|
|
|
delete vm.selectedFolder.selectedCard;
|
2015-05-08 21:47:09 +02:00
|
|
|
}, function(data, status) {
|
|
|
|
// Delete failed
|
|
|
|
});
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-07-20 20:24:36 +02:00
|
|
|
|
2015-08-05 22:02:57 +02:00
|
|
|
function saveSelectedCards() {
|
2016-02-26 22:24:18 +01:00
|
|
|
var selectedCards = _.filter(vm.selectedFolder.$cards, function(card) { return card.selected; });
|
2015-08-05 22:02:57 +02:00
|
|
|
var selectedUIDs = _.pluck(selectedCards, 'id');
|
2016-01-15 16:26:38 +01:00
|
|
|
$window.location.href = ApplicationBaseURL + '/' + vm.selectedFolder.id + '/export?uid=' + selectedUIDs.join('&uid=');
|
2015-08-05 22:02:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 15:48:47 +02:00
|
|
|
function copySelectedCards(folder) {
|
2016-02-26 22:24:18 +01:00
|
|
|
var selectedCards = _.filter(vm.selectedFolder.$cards, function(card) { return card.selected; });
|
2015-08-10 15:48:47 +02:00
|
|
|
vm.selectedFolder.$copyCards(selectedCards, folder).then(function() {
|
|
|
|
// TODO: refresh target addressbook?
|
|
|
|
}, function(error) {
|
|
|
|
Dialog.alert(l('Error'), error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-05 21:04:47 +02:00
|
|
|
function selectAll() {
|
2016-02-26 22:24:18 +01:00
|
|
|
_.each(vm.selectedFolder.$cards, function(card) {
|
2015-08-05 21:04:47 +02:00
|
|
|
card.selected = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-20 20:24:36 +02:00
|
|
|
function sort(field) {
|
|
|
|
vm.selectedFolder.$filter('', { sort: field });
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortedBy(field) {
|
2015-08-14 21:22:59 +02:00
|
|
|
return AddressBook.$query.sort == field;
|
2015-07-20 20:24:36 +02:00
|
|
|
}
|
2015-07-20 21:03:29 +02:00
|
|
|
|
|
|
|
function cancelSearch() {
|
|
|
|
vm.mode.search = false;
|
|
|
|
vm.selectedFolder.$filter('');
|
|
|
|
}
|
2015-08-27 17:01:23 +02:00
|
|
|
|
|
|
|
function newMessage($event, recipients) {
|
|
|
|
Account.$findAll().then(function(accounts) {
|
|
|
|
var account = _.filter(accounts, function(o) {
|
|
|
|
if (o.id === 0)
|
|
|
|
return o;
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
// We must initialize the Account with its mailbox
|
|
|
|
// list before proceeding with message's creation
|
|
|
|
account.$getMailboxes().then(function(mailboxes) {
|
|
|
|
account.$newMessage().then(function(message) {
|
|
|
|
$mdDialog.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
targetEvent: $event,
|
|
|
|
clickOutsideToClose: false,
|
|
|
|
escapeToClose: false,
|
|
|
|
templateUrl: '../Mail/UIxMailEditor',
|
|
|
|
controller: 'MessageEditorController',
|
|
|
|
controllerAs: 'editor',
|
|
|
|
locals: {
|
|
|
|
stateAccounts: accounts,
|
|
|
|
stateMessage: message,
|
|
|
|
stateRecipients: recipients
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function newMessageWithRecipient($event, recipient, fn) {
|
|
|
|
var recipients = [{full: fn + ' <' + recipient + '>'}];
|
|
|
|
vm.newMessage($event, recipients);
|
2015-12-08 16:35:26 +01:00
|
|
|
$event.stopPropagation();
|
|
|
|
$event.preventDefault();
|
2015-08-27 17:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function newMessageWithSelectedCards($event) {
|
2016-02-26 22:24:18 +01:00
|
|
|
var selectedCards = _.filter(vm.selectedFolder.$cards, function(card) { return card.selected; });
|
2015-08-27 17:01:23 +02:00
|
|
|
var promises = [], recipients = [];
|
|
|
|
|
|
|
|
_.each(selectedCards, function(card) {
|
|
|
|
if (card.c_component == 'vcard' && card.c_mail.length) {
|
|
|
|
recipients.push({full: card.c_cn + ' <' + card.c_mail + '>'});
|
|
|
|
}
|
2016-01-15 16:26:38 +01:00
|
|
|
else if (card.$isList()) {
|
2015-08-27 17:01:23 +02:00
|
|
|
// If the list's members were already fetch, use them
|
|
|
|
if (angular.isDefined(card.refs) && card.refs.length) {
|
|
|
|
_.each(card.refs, function(ref) {
|
|
|
|
if (ref.email.length)
|
|
|
|
recipients.push({full: ref.c_cn + ' <' + ref.email + '>'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
promises.push(vm.selectedFolder.$getCard(card.id).then(function(card) {
|
|
|
|
return card.$futureCardData.then(function(data) {
|
|
|
|
_.each(data.refs, function(ref) {
|
|
|
|
if (ref.email.length)
|
|
|
|
recipients.push({full: ref.c_cn + ' <' + ref.email + '>'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$q.all(promises).then(function() {
|
|
|
|
if (recipients.length)
|
|
|
|
vm.newMessage($event, recipients);
|
|
|
|
});
|
|
|
|
}
|
2015-05-08 21:47:09 +02:00
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
|
|
|
|
angular
|
|
|
|
.module('SOGo.ContactsUI')
|
|
|
|
.controller('AddressBookController', AddressBookController);
|
|
|
|
})();
|