SmartIT_Extensions/BMC/smart-it-full/scripts/app/person/person-edit-contact-directi...

70 lines
3.7 KiB
JavaScript

"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', function ($scope, personModel) {
$scope.getSitesAndUpdate = function (company, site) {
$scope.sitesLoading = true;
personModel.getSiteList(company.name).then(function (data) {
$scope.sites = data;
}).finally(function () {
$scope.sitesLoading = false;
});
$scope.person.site.address.address = site.address.address;
};
function saveContact() {
$scope.isPersonDataSaving = true;
var personData = {
siteName: $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);
};
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);
}]
};
}]);
}());