2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller to view and edit a card
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2016-01-15 16:26:38 +01:00
|
|
|
CardController.$inject = ['$scope', '$timeout', '$window', '$mdDialog', 'AddressBook', 'Card', 'Dialog', 'sgFocus', '$state', '$stateParams', 'stateCard'];
|
|
|
|
function CardController($scope, $timeout, $window, $mdDialog, AddressBook, Card, Dialog, focus, $state, $stateParams, stateCard) {
|
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
|
|
|
vm.card = stateCard;
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.currentFolder = AddressBook.selectedFolder;
|
|
|
|
vm.allEmailTypes = Card.$EMAIL_TYPES;
|
|
|
|
vm.allTelTypes = Card.$TEL_TYPES;
|
|
|
|
vm.allUrlTypes = Card.$URL_TYPES;
|
|
|
|
vm.allAddressTypes = Card.$ADDRESS_TYPES;
|
|
|
|
vm.categories = {};
|
|
|
|
vm.userFilterResults = [];
|
2016-01-18 20:46:23 +01:00
|
|
|
vm.transformCategory = transformCategory;
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.addOrgUnit = addOrgUnit;
|
2015-08-20 16:29:47 +02:00
|
|
|
vm.addBirthday = addBirthday;
|
2015-11-27 16:58:56 +01:00
|
|
|
vm.addScreenName = addScreenName;
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.addEmail = addEmail;
|
|
|
|
vm.addPhone = addPhone;
|
|
|
|
vm.addUrl = addUrl;
|
|
|
|
vm.addAddress = addAddress;
|
|
|
|
vm.userFilter = userFilter;
|
|
|
|
vm.save = save;
|
2015-08-26 23:28:03 +02:00
|
|
|
vm.close = close;
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.reset = reset;
|
|
|
|
vm.cancel = cancel;
|
|
|
|
vm.confirmDelete = confirmDelete;
|
2015-08-25 02:04:56 +02:00
|
|
|
vm.exportCard = exportCard;
|
2015-12-04 21:00:58 +01:00
|
|
|
vm.toggleRawSource = toggleRawSource;
|
|
|
|
vm.showRawSource = false;
|
2015-05-28 19:48:45 +02:00
|
|
|
|
2016-01-18 20:46:23 +01:00
|
|
|
function transformCategory(input) {
|
|
|
|
if (angular.isString(input))
|
|
|
|
return { value: input };
|
|
|
|
else
|
|
|
|
return input;
|
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function addOrgUnit() {
|
|
|
|
var i = vm.card.$addOrgUnit('');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('orgUnit_' + i);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-08-20 16:29:47 +02:00
|
|
|
function addBirthday() {
|
|
|
|
vm.card.birthday = new Date();
|
|
|
|
}
|
2015-11-27 16:58:56 +01:00
|
|
|
function addScreenName() {
|
|
|
|
vm.card.$addScreenName('');
|
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function addEmail() {
|
|
|
|
var i = vm.card.$addEmail('');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('email_' + i);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function addPhone() {
|
|
|
|
var i = vm.card.$addPhone('');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('phone_' + i);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function addUrl() {
|
|
|
|
var i = vm.card.$addUrl('', '');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('url_' + i);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function addAddress() {
|
|
|
|
var i = vm.card.$addAddress('', '', '', '', '', '', '', '');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('address_' + i);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function userFilter($query, excludedCards) {
|
2015-07-16 22:29:54 +02:00
|
|
|
AddressBook.selectedFolder.$filter($query, {dry: true, excludeLists: true}, excludedCards);
|
2016-02-20 05:19:07 +01:00
|
|
|
return AddressBook.selectedFolder.$$cards;
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function save(form) {
|
2015-05-06 04:06:13 +02:00
|
|
|
if (form.$valid) {
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.card.$save()
|
2015-05-06 04:06:13 +02:00
|
|
|
.then(function(data) {
|
2015-05-28 19:48:45 +02:00
|
|
|
var i = _.indexOf(_.pluck(AddressBook.selectedFolder.cards, 'id'), vm.card.id);
|
2015-05-06 04:06:13 +02:00
|
|
|
if (i < 0) {
|
|
|
|
// New card; reload contacts list and show addressbook in which the card has been created
|
2015-05-28 19:48:45 +02:00
|
|
|
AddressBook.selectedFolder.$reload();
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Update contacts list with new version of the Card object
|
2015-05-28 19:48:45 +02:00
|
|
|
AddressBook.selectedFolder.cards[i] = angular.copy(vm.card);
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
$state.go('app.addressbook.card.view', { cardId: vm.card.id });
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-08-26 23:28:03 +02:00
|
|
|
function close() {
|
2015-08-27 19:37:47 +02:00
|
|
|
$state.go('app.addressbook', { addressbookId: AddressBook.selectedFolder.id }).then(function() {
|
|
|
|
vm.card = null;
|
|
|
|
delete AddressBook.selectedFolder.selectedCard;
|
|
|
|
});
|
2015-08-26 23:28:03 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function reset() {
|
|
|
|
vm.card.$reset();
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function cancel() {
|
|
|
|
vm.card.$reset();
|
|
|
|
if (vm.card.isNew) {
|
2015-05-06 04:06:13 +02:00
|
|
|
// Cancelling the creation of a card
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.card = null;
|
2015-07-23 17:59:15 +02:00
|
|
|
delete AddressBook.selectedFolder.selectedCard;
|
2015-05-28 19:48:45 +02:00
|
|
|
$state.go('app.addressbook', { addressbookId: AddressBook.selectedFolder.id });
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Cancelling the edition of an existing card
|
2015-05-28 19:48:45 +02:00
|
|
|
$state.go('app.addressbook.card.view', { cardId: vm.card.id });
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-28 19:48:45 +02:00
|
|
|
function confirmDelete(card) {
|
2015-05-06 04:06:13 +02:00
|
|
|
Dialog.confirm(l('Warning'),
|
2016-02-20 05:19:07 +01:00
|
|
|
l('Are you sure you want to delete the card of %{0}?', '<b>' + card.$fullname() + '</b>'),
|
2016-02-10 16:52:46 +01:00
|
|
|
{ ok: l('Delete') })
|
2015-05-06 04:06:13 +02:00
|
|
|
.then(function() {
|
|
|
|
// User confirmed the deletion
|
|
|
|
card.$delete()
|
|
|
|
.then(function() {
|
2015-11-25 17:22:20 +01:00
|
|
|
// Remove card from addressbook
|
2015-05-28 19:48:45 +02:00
|
|
|
AddressBook.selectedFolder.cards = _.reject(AddressBook.selectedFolder.cards, function(o) {
|
2015-05-06 04:06:13 +02:00
|
|
|
return o.id == card.id;
|
|
|
|
});
|
2015-11-25 17:22:20 +01:00
|
|
|
close();
|
2015-05-06 04:06:13 +02:00
|
|
|
}, function(data, status) {
|
|
|
|
Dialog.alert(l('Warning'), l('An error occured while deleting the card "%{0}".',
|
|
|
|
card.$fullname()));
|
|
|
|
});
|
|
|
|
});
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-08-05 02:32:43 +02:00
|
|
|
|
2015-08-25 02:04:56 +02:00
|
|
|
function exportCard() {
|
2016-01-15 16:26:38 +01:00
|
|
|
$window.location.href = ApplicationBaseURL + '/' + vm.currentFolder.id + '/export?uid=' + vm.card.id;
|
2015-08-25 02:04:56 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:00:58 +01:00
|
|
|
function toggleRawSource($event) {
|
|
|
|
if (!vm.showRawSource && !vm.rawSource) {
|
|
|
|
Card.$$resource.post(vm.currentFolder.id + '/' + vm.card.id, "raw").then(function(data) {
|
|
|
|
vm.rawSource = data;
|
|
|
|
vm.showRawSource = true;
|
2015-08-05 02:32:43 +02:00
|
|
|
});
|
2015-12-04 21:00:58 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm.showRawSource = !vm.showRawSource;
|
|
|
|
}
|
2015-08-05 02:32:43 +02:00
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
angular
|
2015-05-06 23:46:58 +02:00
|
|
|
.module('SOGo.ContactsUI')
|
|
|
|
.controller('CardController', CardController);
|
2015-05-06 04:06:13 +02:00
|
|
|
})();
|