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-10-26 21:48:40 +02:00
|
|
|
CardController.$inject = ['$scope', '$timeout', '$window', '$mdDialog', 'sgSettings', 'AddressBook', 'Card', 'Dialog', 'sgHotkeys', 'sgFocus', '$state', '$stateParams', 'stateCard'];
|
|
|
|
function CardController($scope, $timeout, $window, $mdDialog, sgSettings, AddressBook, Card, Dialog, sgHotkeys, focus, $state, $stateParams, stateCard) {
|
|
|
|
var vm = this, hotkeys = [];
|
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 = [];
|
2015-12-04 21:00:58 +01:00
|
|
|
vm.showRawSource = false;
|
2015-05-28 19:48:45 +02:00
|
|
|
|
2016-09-27 21:19:24 +02:00
|
|
|
|
|
|
|
_registerHotkeys(hotkeys);
|
2018-01-23 19:30:56 +01:00
|
|
|
_loadCertificate();
|
2016-09-27 21:19:24 +02:00
|
|
|
|
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
// Deregister hotkeys
|
|
|
|
_.forEach(hotkeys, function(key) {
|
|
|
|
sgHotkeys.deregisterHotkey(key);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function _registerHotkeys(keys) {
|
2017-06-14 19:37:13 +02:00
|
|
|
_.forEach(['backspace', 'delete'], function(hotkey) {
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: hotkey,
|
|
|
|
description: l('Delete'),
|
|
|
|
callback: function($event) {
|
2018-08-31 21:56:26 +02:00
|
|
|
if (vm.currentFolder.acls.objectEraser && vm.currentFolder.$selectedCount() === 0)
|
2021-10-04 16:02:33 +02:00
|
|
|
vm.confirmDelete();
|
2017-06-14 19:37:13 +02:00
|
|
|
$event.preventDefault();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
2016-09-27 21:19:24 +02:00
|
|
|
|
|
|
|
// Register the hotkeys
|
|
|
|
_.forEach(keys, function(key) {
|
|
|
|
sgHotkeys.registerHotkey(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-23 19:30:56 +01:00
|
|
|
function _loadCertificate() {
|
|
|
|
if (vm.card.hasCertificate)
|
|
|
|
vm.card.$certificate().then(function(crt) {
|
|
|
|
vm.certificate = crt;
|
|
|
|
}, function() {
|
|
|
|
delete vm.card.hasCertificate;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:02:33 +02:00
|
|
|
this.transformCategory = function (input) {
|
2016-01-18 20:46:23 +01:00
|
|
|
if (angular.isString(input))
|
|
|
|
return { value: input };
|
|
|
|
else
|
|
|
|
return input;
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.removeAttribute = function (form, attribute, index) {
|
|
|
|
this.card.$delete(attribute, index);
|
2017-03-23 17:47:16 +01:00
|
|
|
form.$setDirty();
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.addOrg = function () {
|
|
|
|
var i = this.card.$addOrg({ value: '' });
|
2017-03-03 16:35:23 +01:00
|
|
|
focus('org_' + i);
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.addBirthday = function () {
|
|
|
|
this.card.birthday = new Date();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addScreenName = function () {
|
|
|
|
this.card.$addScreenName('');
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addEmail = function () {
|
|
|
|
var i = this.card.$addEmail('');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('email_' + i);
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.addPhone = function () {
|
|
|
|
var i = this.card.$addPhone('');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('phone_' + i);
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.addUrl = function () {
|
|
|
|
var i = this.card.$addUrl('', 'https://www.fsf.org/');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('url_' + i);
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.canAddCustomField = function () {
|
|
|
|
return _.keys(this.customFields).length < 4;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addCustomField = function () {
|
|
|
|
if (!angular.isDefined(this.card.customFields))
|
|
|
|
this.card.customFields = {};
|
2017-05-30 15:12:13 +02:00
|
|
|
|
|
|
|
// Find the first 'available' custom field
|
2021-10-04 16:02:33 +02:00
|
|
|
var availableKeys = _.pullAll(['1', '2', '3', '4'], _.keys(this.customFields));
|
|
|
|
this.card.customFields[availableKeys[0]] = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
this.deleteCustomField = function (key) {
|
|
|
|
delete this.card.customFields[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addAddress = function () {
|
|
|
|
var i = this.card.$addAddress('', '', '', '', '', '', '', '');
|
2015-05-06 04:06:13 +02:00
|
|
|
focus('address_' + i);
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.userFilter = function ($query, excludedCards) {
|
2016-10-26 21:48:40 +02:00
|
|
|
if ($query.length < sgSettings.minimumSearchLength())
|
2016-10-14 22:35:57 +02:00
|
|
|
return [];
|
|
|
|
|
2016-10-26 21:48:40 +02:00
|
|
|
return AddressBook.selectedFolder.$filter($query, {dry: true, excludeLists: true}, excludedCards).then(function(cards) {
|
|
|
|
return cards;
|
|
|
|
});
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.save = function (form, options) {
|
2015-05-06 04:06:13 +02:00
|
|
|
if (form.$valid) {
|
2021-10-04 16:02:33 +02:00
|
|
|
this.card.$save(options)
|
2015-05-06 04:06:13 +02:00
|
|
|
.then(function(data) {
|
2016-03-03 19:38:54 +01:00
|
|
|
var i = _.indexOf(_.map(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
|
2016-02-26 22:24:18 +01: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 });
|
2021-10-04 16:02:33 +02:00
|
|
|
}, function(response) {
|
|
|
|
vm.duplicatedCard = new Card(response.data);
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.close = function () {
|
2016-07-19 22:31:48 +02:00
|
|
|
$state.go('app.addressbook').then(function() {
|
2015-08-27 19:37:47 +02:00
|
|
|
vm.card = null;
|
|
|
|
delete AddressBook.selectedFolder.selectedCard;
|
|
|
|
});
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.edit = function (form) {
|
|
|
|
this.duplicatedCard = false;
|
|
|
|
form.$setPristine();
|
|
|
|
form.$setDirty();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.reset = function (form) {
|
2015-05-28 19:48:45 +02:00
|
|
|
vm.card.$reset();
|
2016-11-21 16:43:24 +01:00
|
|
|
form.$setPristine();
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.cancel = function () {
|
2015-05-28 19:48:45 +02:00
|
|
|
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
|
|
|
}
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.confirmDelete = function () {
|
2016-09-27 21:19:24 +02:00
|
|
|
var card = stateCard;
|
|
|
|
|
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
|
2016-07-19 22:31:48 +02:00
|
|
|
AddressBook.selectedFolder.$deleteCards([card])
|
2015-05-06 04:06:13 +02:00
|
|
|
.then(function() {
|
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()));
|
|
|
|
});
|
|
|
|
});
|
2021-10-04 16:02:33 +02:00
|
|
|
};
|
2015-08-05 02:32:43 +02:00
|
|
|
|
2021-10-04 16:02:33 +02:00
|
|
|
this.toggleRawSource = function ($event) {
|
|
|
|
if (!this.showRawSource && !this.rawSource) {
|
|
|
|
Card.$$resource.post(this.currentFolder.id + '/' + this.card.id, "raw").then(function(data) {
|
2015-12-04 21:00:58 +01:00
|
|
|
vm.rawSource = data;
|
|
|
|
vm.showRawSource = true;
|
2015-08-05 02:32:43 +02:00
|
|
|
});
|
2015-12-04 21:00:58 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-10-04 16:02:33 +02:00
|
|
|
this.showRawSource = !this.showRawSource;
|
2015-12-04 21:00:58 +01:00
|
|
|
}
|
2021-10-04 16:02:33 +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
|
|
|
})();
|