"use strict"; (function () { 'use strict'; angular.module('assetModule') .controller('AddPeopleController', ['$scope', 'assetModel', 'configurationModel', 'userService', 'searchService', 'userModel', '$q', 'searchModel', 'events', 'systemAlertService', 'i18nService', '$filter', '$timeout', function ($scope, assetModel, configurationModel, userService, searchService, userModel, $q, searchModel, events, systemAlertService, i18nService, $filter, $timeout) { var debounceSearch; function init() { $scope.state = { dataIsLoading: false, isPeopleRelationsLoading: false, tooManyCompanies: false, dataSearching: false }; $scope.defaultExceeedChunkSize = { company: false, department: false, organization: false, supportgroup: false }; $scope.typeaheadSearch = { text: '', isExceedChunkSize: false }; $scope.peopleTypes = _.find(configurationModel.get('link')[EntityVO.TYPE_PERSON], { type: EntityVO.TYPE_PEOPLETYPE }).relations; $scope.peopleRelationshipTypes = _.find(configurationModel.get('link')[EntityVO.TYPE_PERSON], { type: EntityVO.TYPE_PERSON }).relations; $scope.selectedType = $scope.peopleTypes[0]; $scope.selectedRelationshipType = $scope.peopleRelationshipTypes[0]; $scope.searchPeople = { results: [], filter: '' }; $scope.company = {}; $scope.companyList = []; $scope.addPeopleData = { relationshipType: $scope.selectedRelationshipType, type: EntityVO.TYPE_PERSON, subType: $scope.selectedType, selectedPerson: undefined }; debounceSearch = _.debounce($scope.doSearch, 500); } $scope.onSearchTextChange = function () { if ($scope.typeaheadSearch.text.length > 2 || $scope.typeaheadSearch.text.length === 0) { $scope.state.dataSearching = true; debounceSearch(); } }; $scope.selectPeopleType = function (type) { clearTextbox(); $scope.selectedType = type; $scope.addPeopleData.subType = $scope.selectedType; if (type !== EntityVO.TYPE_PEOPLE) { $scope.doSearch(); } }; $scope.selectRelationshipType = function (type) { $scope.selectedRelationshipType = type; $scope.addPeopleData.relationshipType = $scope.selectedRelationshipType; }; $scope.selectPerson = function (person) { $scope.selectedPerson = person; $scope.addPeopleData.selectedPerson = $scope.selectedPerson; }; $scope.addPeopleToAsset = function () { $scope.state.isPeopleRelationsLoading = true; var personData = getPersonData(); assetModel.addPeople(personData).then(function () { $scope.state.isPeopleRelationsLoading = false; $scope.modalInstance.close(); }).catch(function (response) { if (response.data.results && response.data.results.error) { systemAlertService.error({ text: response.data.results.error, clear: false }); } $scope.state.isPeopleRelationsLoading = false; $scope.modalInstance.close({ error: true }); }); }; $scope.changeCompany = function (newCompany) { clearTextbox(); $scope.company = newCompany; $scope.doSearch(); }; $scope.searchUserByNameDebounce = _.debounce(function () { if (trimSearchedText($scope.searchPeople.text).length >= 3) { $scope.doSearch(); } }, 500); $scope.doSearch = function () { var companiesPromise, searchPromise = $q.when(1); if ($scope.selectedType === EntityVO.TYPE_PEOPLE) { companiesPromise = $q.when(1); } else { companiesPromise = searchModel.getOperatingCompanies(null, -1); } $scope.state.dataSearching = true; companiesPromise.then(function (results) { if ($scope.selectedType !== EntityVO.TYPE_PEOPLE) { $scope.companyList = results.companies; $scope.state.tooManyCompanies = results.exceedsChunkSize; if (!$scope.company.name) { $scope.company.name = assetModel.assetDetails.company.name ? assetModel.assetDetails.company.name : results[0].name; } } if ($scope.selectedType === EntityVO.TYPE_PEOPLE) { searchPromise = userService.searchUserByName(trimSearchedText($scope.searchPeople.text)); } else if ($scope.selectedType === EntityVO.TYPE_ORGANIZATION) { searchPromise = searchModel.getOrganizationsByCompany($scope.company.name, $scope.defaultExceeedChunkSize.organization ? $scope.typeaheadSearch.text : ''); } else if ($scope.selectedType === EntityVO.TYPE_SUPPORTGROUP) { searchPromise = searchModel.getSupportGroupsListByText($scope.company.name, $scope.defaultExceeedChunkSize.supportgroup ? $scope.typeaheadSearch.text : ''); } else if ($scope.selectedType === EntityVO.TYPE_DEPARTMENT) { searchPromise = searchModel.getDepartmentsByCompany($scope.company.name, $scope.defaultExceeedChunkSize.department ? $scope.typeaheadSearch.text : ''); } else if ($scope.selectedType === EntityVO.TYPE_COMPANY) { searchPromise = userModel.getOperatingCompaniesByText($scope.defaultExceeedChunkSize.company ? $scope.typeaheadSearch.text : ''); } return searchPromise.then(function (data) { if ($scope.typeaheadSearch.text.length === 0 && $scope.selectedType !== EntityVO.TYPE_PEOPLE) { $scope.defaultExceeedChunkSize[$scope.selectedType] = data.exceedsChunkSize; } $scope.typeaheadSearch.isExceedChunkSize = data.exceedsChunkSize; if ($scope.selectedType === EntityVO.TYPE_SUPPORTGROUP) { data = $scope.searchPeople.results = data.supportGroups; } else if ($scope.selectedType === EntityVO.TYPE_COMPANY) { data = $scope.searchPeople.results = data.companies; } else if ($scope.selectedType === EntityVO.TYPE_DEPARTMENT) { data = $scope.searchPeople.results = data.organizations; } else if ($scope.selectedType === EntityVO.TYPE_ORGANIZATION) { data = $scope.searchPeople.results = data.organizations; } else { $scope.searchPeople.results = data; } if (data.length) { $scope.selectedPerson = data[0]; $scope.addPeopleData.selectedPerson = $scope.selectedPerson; } }).catch(function (err) { if (err) { systemAlertService.error({ text: (err.data && err.data.error) || $filter('i18n')('error.unknown'), clear: false }); } }); }).finally(function () { $scope.state.isPeopleRelationsLoading = false; $scope.state.dataSearching = false; }); }; $scope.getCompaniesByName = function (name) { return searchModel.getCompaniesByText(name).then(function (response) { return response.companies; }); }; function trimSearchedText(text) { return text ? text.trim() : ''; } function getPersonData() { var personData = { relationshipType: $scope.selectedRelationshipType, type: EntityVO.TYPE_PERSON, subType: $scope.selectedType }; if ($scope.selectedType === EntityVO.TYPE_PEOPLE) { personData.id = $scope.selectedPerson.displayId; } else { personData.id = $scope.selectedPerson.id; } personData.relationshipType = $scope.selectedRelationshipType; return personData; } $scope.showTypeaheadSearchBox = function () { var showTypeAhead; switch ($scope.selectedType) { case EntityVO.TYPE_COMPANY: if ($scope.defaultExceeedChunkSize.company) { showTypeAhead = true; } break; case EntityVO.TYPE_ORGANIZATION: if ($scope.defaultExceeedChunkSize.organization) { showTypeAhead = true; } break; case EntityVO.TYPE_DEPARTMENT: if ($scope.defaultExceeedChunkSize.department) { showTypeAhead = true; } break; case EntityVO.TYPE_SUPPORTGROUP: if ($scope.defaultExceeedChunkSize.supportgroup) { showTypeAhead = true; } break; default: showTypeAhead = false; } return showTypeAhead; }; $scope.close = function () { if ($scope.searchPeople && $scope.searchPeople.results && $scope.searchPeople.results.length) { var modalInstance = systemAlertService.modal({ title: i18nService.getLocalizedString('common.notification.dirty.title'), text: i18nService.getLocalizedString('common.notification.dirty.message'), buttons: [ { text: i18nService.getLocalizedString('common.labels.yes'), data: true }, { text: i18nService.getLocalizedString('common.labels.no'), data: false } ] }); modalInstance.result.then(function (data) { if (data) { $scope.modalInstance.dismiss(); } }); } else { $scope.modalInstance.dismiss(); } }; function handleModalBackdropClick() { $scope.close(); } function clearTextbox() { $scope.searchPeople.text = ''; $scope.searchPeople.filter = ''; $scope.searchPeople.results = []; $scope.typeaheadSearch.text = ''; } $scope.$on(events.MODAL_BACKDROP_CLICK_PROPOGATE, handleModalBackdropClick); init(); }]); })();