angular.module('contactsApp') .controller('contactdetailsCtrl', function(ContactService, AddressBookService, vCardPropertiesService, $route, $routeParams, $scope) { var ctrl = this; ctrl.init = true; ctrl.loading = false; ctrl.show = false; ctrl.clearContact = function() { $route.updateParams({ gid: $routeParams.gid, uid: undefined }); ctrl.show = false; ctrl.contact = undefined; }; ctrl.uid = $routeParams.uid; ctrl.t = { noContacts : t('contacts', 'No contacts in here'), placeholderName : t('contacts', 'Name'), placeholderOrg : t('contacts', 'Organization'), placeholderTitle : t('contacts', 'Title'), selectField : t('contacts', 'Add field …'), download : t('contacts', 'Download'), delete : t('contacts', 'Delete'), save : t('contacts', 'Save changes'), addressBook : t('contacts', 'Address book'), loading : t('contacts', 'Loading contacts …') }; ctrl.fieldDefinitions = vCardPropertiesService.fieldDefinitions; ctrl.focus = undefined; ctrl.field = undefined; ctrl.addressBooks = []; AddressBookService.getAll().then(function(addressBooks) { ctrl.addressBooks = addressBooks; if (!angular.isUndefined(ctrl.contact)) { ctrl.addressBook = _.find(ctrl.addressBooks, function(book) { return book.displayName === ctrl.contact.addressBookId; }); } ctrl.init = false; // Start watching for ctrl.uid when we have addressBooks, as they are needed for fetching // full details. $scope.$watch('ctrl.uid', function(newValue) { ctrl.changeContact(newValue); }); }); ctrl.changeContact = function(uid) { if (typeof uid === 'undefined') { ctrl.show = false; $('#app-navigation-toggle').removeClass('showdetails'); return; } ctrl.loading = true; ContactService.getById(ctrl.addressBooks, uid).then(function(contact) { if (angular.isUndefined(contact)) { ctrl.clearContact(); return; } ctrl.contact = contact; ctrl.show = true; ctrl.loading = false; $('#app-navigation-toggle').addClass('showdetails'); ctrl.addressBook = _.find(ctrl.addressBooks, function(book) { return book.displayName === ctrl.contact.addressBookId; }); }); }; ctrl.deleteContact = function() { ContactService.delete(ctrl.addressBook, ctrl.contact); }; ctrl.addField = function(field) { var defaultValue = vCardPropertiesService.getMeta(field).defaultValue || {value: ''}; ctrl.contact.addProperty(field, defaultValue); ctrl.focus = field; ctrl.field = ''; }; ctrl.deleteField = function (field, prop) { ctrl.contact.removeProperty(field, prop); ctrl.focus = undefined; }; ctrl.changeAddressBook = function (addressBook, oldAddressBook) { ContactService.moveContact(ctrl.contact, addressBook, oldAddressBook); }; ctrl.updateContact = function() { ContactService.queueUpdate(ctrl.contact); }; ctrl.closeMenus = function() { ctrl.openedMenu = false; }; ctrl.openMenu = function(index) { ctrl.closeMenus(); ctrl.openedMenu = index; }; ctrl.toggleMenu = function(index) { if (ctrl.openedMenu === index) { ctrl.closeMenus(); } else { ctrl.openMenu(index); } }; });