"use strict"; /** * Created by viktor.shevchenko on 2/13/2015. */ (function () { 'use strict'; angular.module('myitsmApp') .controller('FoundationSelectorController', ['$scope', 'foundationService', 'events', 'searchModel', 'i18nService', '$timeout', function ($scope, foundationService, events, searchModel, i18nService, $timeout) { var state = { loadingFieldData: false, showTooltip: false }; $scope.fdSelector = foundationService.buildControls($scope.type, $scope.options); $scope.search = { typeaheadText: '' }; /** * Load data for fields * @param fields * @param field */ $scope.loadDataForField = function (fields, field) { $scope.searchFilterText = ''; if (state.loadingFieldData) { return; } state.loadingFieldData = true; field.populatingAvailableValues = true; var params = { type: field.name, searchOptions: foundationService.prepareSearchOptions(fields, field) }; if ($scope.useChunking) { params.chunkInfo = { startIndex: 0, chunkSize: searchModel.locationSiteChunkSize }; } foundationService.getFieldData(params) .then(function (result) { field.availableValues = result.foundationItems; if ($scope.typeaheadMode && $scope.useChunking) { if (result.length >= searchModel.locationSiteChunkSize) { $scope.state.tooManyData = true; } else { $scope.state.tooManyData = false; } } else if ($scope.typeaheadMode && !$scope.useChunking) { $scope.state.tooManyData = result.exceedsChunkSize; } }).finally(function () { state.loadingFieldData = false; field.populatingAvailableValues = false; }); }; $scope.loadDataByText = function (fields, field, searchText) { var params = { type: field.name, searchOptions: foundationService.prepareSearchOptions(fields, field) }; if ($scope.useChunking) { params.chunkInfo = { startIndex: 0, chunkSize: searchModel.locationSiteChunkSize }; } if (searchText) { params.searchText = searchText; } return foundationService.getFieldData(params) .then(function (result) { state.showTooltip = result.exceedsChunkSize; return result.foundationItems; }).finally(function () { state.loadingFieldData = false; field.populatingAvailableValues = false; }); }; $scope.handleKeyup = function ($event) { $scope.searchFilterText = $event.target.value; }; $scope.clearSearchText = function () { $timeout(function () { $scope.search.typeaheadText = ''; }, 250); }; $scope.selectFieldValue = function (fields, field, newValue, $event) { if (field.value !== newValue.name) { foundationService.setDependantFieldValues(fields, field, newValue); field.value = newValue.name; field.id = newValue.id; foundationService.resetFurtherFieldValues(fields, field.index); if (!$scope.multiple) { $scope.selectedFoundations = foundationService.collectValues($scope.fdSelector); $scope.selectedFoundations.fieldChanged = field.name; } if ($event) { $scope.focusNextInputElement($event); } $scope.search.typeaheadText = ''; } }; $scope.resetFieldValue = function (fields, field, $event) { $event.stopPropagation(); $scope.focusInputElement($event); foundationService.resetFieldValue(fields, field); if (!$scope.multiple) { $scope.selectedFoundations = foundationService.collectValues($scope.fdSelector); $scope.selectedFoundations.fieldChanged = field.name; } $scope.loadDataForField(fields, field); }; $scope.add = function ($event) { var item = foundationService.collectValues($scope.fdSelector); $scope.selectedFoundations.push(item); foundationService.resetFurtherFieldValues($scope.fdSelector.fdFields, -1); $scope.$emit(events.FOUNDATION_ADD, item); $scope.focusFirstInputElement($event); }; $scope.isAddButtonDisabled = function () { var isDisabled = false, fdFieldValue = foundationService.collectValues($scope.fdSelector); if (!(_.filter($scope.fdSelector.fdFields, 'value')).length) { isDisabled = true; } if (!isDisabled) { _.forEach($scope.selectedFoundations, function (foundation) { if (angular.equals(foundation, fdFieldValue)) { isDisabled = true; } }); } return isDisabled; }; $scope.getLabel = function (item) { var itemLabel; if (item.name) { itemLabel = i18nService.getLocalizedString(item.name); } return itemLabel; }; $scope.filterDropDown = function (filterText) { if (filterText) { return function (item) { return _.includes($scope.getLabel(item).toLowerCase(), filterText.toLowerCase()); }; } }; $scope.state = state; }]); })();