"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('editPersonContact', ['events', function (events) { return { restrict: 'E', templateUrl: 'views/person/person-contact-edit.html', replace: true, scope: { personProfile: '=' }, controller: ['$scope', 'personModel', 'searchModel', function ($scope, personModel, searchModel) { $scope.state = { tooManySites: false }; $scope.sitesLoading = false; $scope.getSitesByCompany = function (company) { $scope.sitesLoading = true; return searchModel.getSitesByCompany(company).then(function (response) { $scope.sites = response.objects; $scope.state.tooManySites = response.exceedsChunkSize; }).finally(function () { $scope.sitesLoading = false; }); }; $scope.getSitesByTextAndCompany = function (searchText) { searchText = searchText ? searchText : '%'; return searchModel.getSitesByTextAndCompany(searchText, $scope.person.company.name).then(function (response) { return { list: response.list, exceedsChunkSize: response.exceedsChunkSize }; }); }; $scope.selectSite = function (item) { $scope.person.site = item; }; function saveContact() { $scope.isPersonDataSaving = true; var personData = { siteName: $scope.person.site ? $scope.person.site.name : '', phone: $scope.person.phone, fax: $scope.person.fax, cell: $scope.person.cell, email: $scope.person.email }; var agentData = {}; if ($scope.person.isSupportStaff) { agentData = { introduction: $scope.person.introduction, enabled: $scope.person.enabled, availableForAssignment: $scope.person.availableForAssignment, linkedIn: $scope.person.linkedIn, twitter: $scope.person.twitter }; personData = _.merge(personData, agentData); } $scope.$emit(events.SAVE_CHANGES_REQUEST, null, true); personModel.updatePersonInfo($scope.person.id, personData).then(function () { $scope.$emit('personDataUpdated'); }).finally(function () { $scope.$emit(events.SAVE_CHANGES_COMPLETE); $scope.isPersonDataSaving = false; }); } var handleToggleEditMode = function () { $scope.person = _.cloneDeep($scope.personProfile); $scope.getSitesByCompany($scope.person.company.name); //company is required field for person }; var handleSaveChanges = function () { console.log('handleSaveChanges in editPersonContact directive'); saveContact(); }; var handleDiscardChanges = function () { $scope.person = _.cloneDeep($scope.personProfile); }; $scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode); $scope.$on(events.SAVE_CHANGES, handleSaveChanges); $scope.$on(events.DISCARD_CHANGES, handleDiscardChanges); // initialize person data to ensure correct form validation when profile is opened initially $scope.person = _.cloneDeep($scope.personProfile); }] }; }]); }());