76 lines
3.9 KiB
JavaScript
76 lines
3.9 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Created by igor.samulenko on 7/29/2014.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
angular.module('personModule')
|
|
.directive('editPersonData', ['events', function (events) {
|
|
return {
|
|
restrict: 'E',
|
|
templateUrl: 'views/person/edit-person-data.html',
|
|
scope: {
|
|
personProfile: '='
|
|
},
|
|
controller: ['$scope', 'personModel', function ($scope, personModel) {
|
|
$scope.getOrganizationsAndUpdate = function (company) {
|
|
$scope.organizationsLoading = true;
|
|
personModel.getOrganizationList(company.name).then(function (data) {
|
|
$scope.organizations = data;
|
|
}).finally(function () {
|
|
$scope.organizationsLoading = false;
|
|
});
|
|
};
|
|
$scope.getDepartmentsAndUpdate = function (organization, company) {
|
|
$scope.departmentsLoading = true;
|
|
personModel.getDepartmentList(organization, company.name).then(function (data) {
|
|
$scope.departments = data;
|
|
}).finally(function () {
|
|
$scope.departmentsLoading = false;
|
|
});
|
|
};
|
|
$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 saveDetails() {
|
|
var personData = {
|
|
organization: $scope.person.organization,
|
|
department: $scope.person.department,
|
|
siteName: $scope.person.site.name,
|
|
firstName: $scope.person.firstName,
|
|
lastName: $scope.person.lastName,
|
|
jobTitle: $scope.person.jobTitle
|
|
};
|
|
$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);
|
|
});
|
|
}
|
|
var handleToggleEditMode = function () {
|
|
$scope.person = _.cloneDeep($scope.personProfile);
|
|
};
|
|
var handleSaveChanges = function () {
|
|
console.log('handleSaveChanges in editPersonData directive');
|
|
saveDetails();
|
|
};
|
|
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);
|
|
}]
|
|
};
|
|
}]);
|
|
}());
|