"use strict"; /** * Created by igor.samulenko on 7/29/2014. */ (function () { 'use strict'; angular.module('personModule') .directive('editPersonData', ['events', '$sce', function (events, $sce) { return { restrict: 'E', templateUrl: 'views/person/edit-person-data.html', scope: { personProfile: '=' }, controller: ['$scope', 'personModel', 'searchModel', 'utilityFunctions', function ($scope, personModel, searchModel, utilityFunctions) { $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.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 saveDetails() { var personData = { organization: $scope.person.organization, department: $scope.person.department, siteName: $scope.person.site ? $scope.person.site.name : '', firstName: utilityFunctions.escapeHTML($scope.person.firstName), lastName: utilityFunctions.escapeHTML($scope.person.lastName), jobTitle: utilityFunctions.escapeHTML($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); $scope.getSitesByCompany($scope.person.company.name); //company is required field for person }; 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); }] }; }]); }());