"use strict"; (function () { 'use strict'; angular.module('searchModule') .controller('SearchController', ['$rootScope', '$scope', 'searchModel', '$state', '$filter', 'i18nService', 'events', 'configurationModel', 'searchService', 'metadataModel', 'authModel', 'browser', function ($rootScope, $scope, searchModel, $state, $filter, i18nService, events, configurationModel, searchService, metadataModel, authModel, browser) { var searchMetadata = configurationModel.get('search.targetAreas'); var initialTypeFilters = [], cachedTargetArea = searchService.getSelectedTargetArea(), searchCount = 0, SEARCH_TYPE = { HKM: "HKM", GLOBAL: "GLOBAL" }; $scope.showFilters = true; $scope.comaroundEnabled = false; $scope.isMobile = browser.isMobile; metadataModel.getMetadataByType('global').then(function (metadata) { $scope.searchMetadata = { 'targetAreas': searchMetadata }; $scope.loadingMoreItems = false; $scope.loadingGlobalItems = false; $scope.loadingHKMItems = false; $scope.searchText = $state.params.searchText || ''; if ($state.params.searchCriteria) { $scope.selectedTargetArea = _.find(searchMetadata, { 'types': $state.params.searchCriteria }); } else if (cachedTargetArea) { $scope.selectedTargetArea = _.find(searchMetadata, { 'types': cachedTargetArea.types }); } else { $scope.selectedTargetArea = _.find(searchMetadata, { 'selected': true }); } init(); }); // public functions $scope.changeSearchCriteria = function (targetAreaObj) { var targetArea = (targetAreaObj) ? targetAreaObj.types : null; var searchedText = $scope.searchText; if (searchedText && targetArea) { searchService.setSelectedTargetArea(targetAreaObj); if (configurationModel.comaroundEnabled && targetArea.length === 1 && targetArea[0] === EntityVO.TYPE_KNOWLEDGE) { $scope.showFilters = false; $scope.selectedItem = {}; } else { $scope.showFilters = true; } var targetAreaFilter = { types: targetArea }; $scope.clearAllFilters(); $scope.$emit(events.REFRESH_SEARCH_FILTERS); clearSearches(); $scope.getGlobalSearchResults(searchedText, targetAreaFilter); } }; $scope.getGlobalSearchResults = function (searchText, filters) { $scope.isSearchDataLoading = true; searchCount++; if (!searchText || i18nService.getByteLength(searchText) < 3) { $scope.isSearchDataLoading = false; } else { filters = filters || {}; if (!filters.types) { filters.types = ($state.params.searchCriteria) ? $state.params.searchCriteria : initialTypeFilters = _.find(searchMetadata, { 'selected': true }).types; } filters = _.cloneDeep(filters); //To avoid overriding values in filter if (configurationModel.comaroundEnabled) { if (_.includes(filters.types, EntityVO.TYPE_KNOWLEDGE) || _.includes(filters.types, "all")) { $scope.loadingHKMItems = true; var hkmSearchCallback = searchCallback(searchCount, SEARCH_TYPE.HKM); searchModel.getHKMSearchResults(searchText, true).finally(hkmSearchCallback); } else { $scope.loadingHKMItems = false; } if (_.includes(filters.types, EntityVO.TYPE_KNOWLEDGE)) { _.remove(filters.types, function (value) { return value === EntityVO.TYPE_KNOWLEDGE; }); } if (_.includes(filters.types, "all")) { filters.types = [EntityVO.FILTER_TYPE_ASSET, EntityVO.FILTER_TYPE_PERSON, EntityVO.FILTER_TYPE_TICKETS]; } } if (filters.types && filters.types.length) { $scope.loadingGlobalItems = true; var globalSearchCallback = searchCallback(searchCount, SEARCH_TYPE.GLOBAL); searchModel.getGlobalSearchResults(searchText, filters, true).finally(globalSearchCallback); } else { $scope.loadingGlobalItems = false; } } }; function searchCallback(index, searchType) { return function () { if (index !== searchCount) { //Return if it is a stale callback from previous searches return; } if (searchType === SEARCH_TYPE.HKM) { $scope.loadingHKMItems = false; loadSearchResults($scope.searchModel.hkmSearchResults); } else { $scope.loadingGlobalItems = false; loadSearchResults($scope.searchModel.globalSearchResults); } }; } function loadSearchResults(results) { var _a; $scope.isSearchDataLoading = false; if ($scope.searchResults && $scope.searchResults.items && $scope.searchResults.items.length) { (_a = $scope.searchResults.items).push.apply(_a, results.items); } else { $scope.searchResults = results; } if (!configurationModel.isServerApplicationEnabled(EntityVO.TYPE_SBEREQUEST) && $scope.searchResults && $scope.searchResults.items && $scope.searchResults.items.length > 0) { //should be handled from backend _.remove($scope.searchResults.items, { searchCategory: 'sberequest' }); console.error('There is a problem with enableSbeIntegration'); } _.forEach($scope.searchResults.items, function (item) { item.displayLimit = $scope.defaultDisplayLimit; if ($scope.selectedCategory) { if (item.searchCategory === $scope.selectedCategory.name) { item.displayLimit = item.totalCount; $scope.selectedCategory.totalCount = item.totalCount; } else { item.active = false; } } }); selectFirstItemInActiveSection(); } $scope.triggerListLimit = function (arr, section) { if (section.displayLimit === $scope.defaultDisplayLimit) { section.displayLimit = arr.length; } else { section.displayLimit = $scope.defaultDisplayLimit; } }; $scope.selectItem = function (item) { $scope.selectedItem = item; if ($scope.selectedItem.type === EntityVO.TYPE_ASSET) { $scope.assetIdsObject = { assetId: item.additionalInformation.reconciliationId, assetClassId: item.additionalInformation.classId }; } if (configurationModel.get('pwaEnabledTickets.tickets').indexOf(item.type) >= 0) { metadataModel.getMetadataByType('global').then(function (metadata) { var progressiveViewsEnabled = (metadata.configurationParameters['Enable-Progressive-Views'] === 'T' || metadata.configurationParameters['Enable-Progressive-Views'] === 'true'); progressiveViewsEnabled = localStorage.getItem('overridePV') === 'T' ? false : progressiveViewsEnabled; if (progressiveViewsEnabled && localStorage.getItem('midtierUrl')) { if (item.type === 'asset') { if ($scope.isMobile) { $state.go(item.type + 'PV', { assetId: item.additionalInformation.reconciliationId, assetClassId: item.additionalInformation.classId }); } else { var assetGuid = item.additionalInformation.reconciliationId + '|' + item.additionalInformation.classId; $state.go('search.' + item.type + 'PV', { guid: assetGuid, displayId: assetGuid }); } } else { if ($scope.isMobile) { $state.go(item.type + 'PV', { id: item.id }); } else { $state.go('search.' + item.type + 'PV', { guid: item.id, displayId: item.displayId }); } } } else { $state.go('search.' + item.type, {}, { location: false }); } }); } else { if ($scope.isMobile) { $state.go(item.type, { id: item.id }); } else { $state.go('search.' + item.type, {}, { location: false }); } } }; $scope.$on(events.SET_PREVIEW_ITEM, function (event, previewItem) { if (previewItem && previewItem.id && previewItem.type) { $scope.selectedItem = previewItem; } }); $scope.selectCategory = function (section) { if (section === 'sberequest') { $scope.selectedCategory = $scope.searchModel.sbeConfig; } else { $scope.selectedCategory = $scope.searchModel.categoriesConfig[section]; } _.forEach($scope.searchResults.items, function (item) { if (item.searchCategory !== section) { item.active = false; } else { item.displayLimit = item.totalCount; $scope.selectedCategory.totalCount = item.totalCount; } }); selectFirstItemInActiveSection(); $scope.isAllResultsDisplaying = false; if (initialTypeFilters === $scope.searchModel.filterCriteria.types) { // We should change types filter to the ones that selected category has, // only if there were no filters specified manually. This will prevent // situation, when we make request with incorrect types when loadMoreCategoryItems called $scope.searchModel.filterCriteria.types = $scope.selectedCategory.types; } if (configurationModel.comaroundEnabled && section === EntityVO.FILTER_TYPE_KNOWLEDGE) { $scope.showFilters = false; } $scope.$broadcast(events.ELLIPSIS_EVENT); }; /** * Handle scroll event */ $scope.loadMoreCategoryItems = function () { if (!$scope.loadingMoreItems && !$scope.isAllResultsDisplaying) { var selectedCategoryItem = _.find($scope.searchResults.items, function (searchItem) { return $scope.selectedCategory.name === searchItem.searchCategory && searchItem.totalCount > (searchItem.chunkIndex + 1) * searchModel.chunkSize; }); if (selectedCategoryItem) { $scope.loadingMoreItems = true; var appliedFilters = _.clone($scope.searchModel.filterCriteria); if (_.isEmpty(appliedFilters) || !appliedFilters.types) { appliedFilters.types = $scope.selectedCategory.types; } else { appliedFilters.types = _.intersection($scope.selectedCategory.types, appliedFilters.types); } if (configurationModel.comaroundEnabled && _.includes(appliedFilters.types, EntityVO.TYPE_KNOWLEDGE)) { searchModel.loadMoreHKMSearchResults($scope.searchText, selectedCategoryItem.nextChunkIndex, true).finally(function () { var knowledgeItem = _.find($scope.searchResults.items, function (item) { return item.searchCategory === EntityVO.FILTER_TYPE_KNOWLEDGE; }); var hkmSearchResult = $scope.searchModel.hkmSearchResults.items[0]; knowledgeItem.results = hkmSearchResult.results; knowledgeItem.chunkIndex = hkmSearchResult.chunkIndex; knowledgeItem.nextChunkIndex = hkmSearchResult.nextChunkIndex; knowledgeItem.totalCount = hkmSearchResult.totalCount; $scope.loadingMoreItems = false; }); } else { searchModel.loadMoreGlobalSearchResults($scope.searchText, appliedFilters, selectedCategoryItem.nextChunkIndex, true).finally(function () { if (configurationModel.comaroundEnabled) { _.forEach($scope.searchModel.globalSearchResults.items, function (item) { var searchItemResults = _.find($scope.searchResults.items, function (searchItem) { return searchItem.searchCategory === item.searchCategory; }); searchItemResults.results = item.results; searchItemResults.chunkIndex = item.chunkIndex; searchItemResults.nextChunkIndex = item.nextChunkIndex; searchItemResults.totalCount = item.totalCount; }); } // _.forEach() $scope.loadingMoreItems = false; }); } } } }; $scope.closeCategory = function () { if ($scope.selectedCategory) { if ($scope.searchModel.filterCriteria.types && ($scope.searchModel.filterCriteria.searchCompanies || $scope.searchModel.filterCriteria.createDateRange || $scope.searchModel.filterCriteria.modifiedDateRange || $scope.searchModel.filterCriteria.statuses)) { delete $scope.searchModel.filterCriteria.types; _.forEach($scope.searchModel.selectedFilters, function (item) { if (item.category) { $scope.removeFilter(item); } }); $scope.getGlobalSearchResults($state.params.searchText, $scope.searchModel.filterCriteria); } delete $scope.searchModel.filterCriteria.types; $scope.selectedCategory = null; $scope.isAllResultsDisplaying = true; if ($scope.comaroundEnabled && $scope.selectedTargetArea.types && ($scope.selectedTargetArea.types.length > 1 || $scope.selectedTargetArea.types[0] !== EntityVO.FILTER_TYPE_KNOWLEDGE)) { $scope.showFilters = true; } _.forEach($scope.searchResults.items, function (item) { item.active = true; item.displayLimit = $scope.defaultDisplayLimit; }); } }; $scope.openPreviewItemFullDetails = function () { if ($scope.selectedItem.type === EntityVO.TYPE_ASSET) { $state.go($scope.selectedItem.type, { assetId: $scope.assetIdsObject.assetId, assetClassId: $scope.assetIdsObject.assetClassId }); } else { $state.go($scope.selectedItem.type, { id: $scope.selectedItem.id }); } }; $scope.clearAllFilters = function () { _.forEach($scope.searchModel.selectedFilters, function (filterOption) { filterOption.active = false; }); $scope.searchModel.selectedFilters = []; $scope.selectedCategory = null; $scope.isAllResultsDisplaying = true; $scope.searchModel.filterCriteria = {}; }; //Knowledge status is not getting populated when using filter in html in case of browser refresh because metadataModel.cache doesn't fetch value at that time. $scope.localizedStatus = function (status) { if ($scope.comaroundEnabled) { return $filter('i18n')('hkm.status.' + status); } else { return $filter('localizeLabel')(status, 'status', 'knowledge'); } }; //To populate localized assetType value $scope.localizedAssetType = function (assetType) { return $filter('localizeLabel')(assetType, 'assetType', 'asset'); }; // private functions function init() { //$scope.sortByList = [ //removed this code for SW00509550 // 'Relevancy', // 'Age', // 'Number of Followers' //]; $scope.searchResults = {}; $scope.searchResults.items = []; $scope.defaultDisplayLimit = 4; $scope.searchResultActiveCount = 0; $scope.isAllResultsDisplaying = true; $scope.searchModel = searchModel; $scope.searchText = $state.params.searchText; $scope.comaroundEnabled = configurationModel.comaroundEnabled; if ($state.params && $state.params.isNewSearch) { $scope.clearAllFilters(); } $scope.$watchCollection('searchModel.selectedFilters', function () { $scope.searchModel.filterCriteria = buildFilterCriteria($scope.searchModel.selectedFilters); var filters = _.cloneDeep($scope.searchModel.filterCriteria), targetAreaObj = searchService.getSelectedTargetArea(); if (!(filters && filters.types && filters.types.length) && targetAreaObj) { filters.types = targetAreaObj.types; } clearSearches(); if (filters && filters.types && $scope.comaroundEnabled && _.includes($scope.selectedTargetArea.types, EntityVO.FILTER_TYPE_KNOWLEDGE)) { if (!_.includes(filters.types, EntityVO.FILTER_TYPE_KNOWLEDGE)) { filters.types.push(EntityVO.FILTER_TYPE_KNOWLEDGE); } if (filters.types.length === 1) { $scope.showFilters = false; } } $scope.getGlobalSearchResults($scope.searchText, filters); }); } function selectFirstItemInActiveSection() { $scope.searchResultActiveCount = 0; var itemToSelect = _.chain($scope.searchResults.items).filter('active').find(function (item) { $scope.searchResultActiveCount = item.results.length; return item.results.length; }).value().results[0]; if (!$scope.isMobile) { $scope.selectItem(itemToSelect); } } function buildFilterCriteria(filters) { var filterCriteria = {}; _.forEach(filters, function (filter) { var criteria = filter.criteria, isMultiSelect = _.isArray(criteria.value), name = criteria.name; if (filter.type === 'date') { _.forEach(criteria.defaultValues, function (value, key) { criteria.value[key] = $rootScope.$eval(value, { moment: moment }); }); } if (filterCriteria[name]) { if (isMultiSelect) { filterCriteria[name] = _.union(filterCriteria[name], criteria.value); } else { filters.splice(_.findIndex(filters, { name: filter.name }), 1); filterCriteria[name] = criteria.value; } } else { filterCriteria[name] = criteria.value; } }); return filterCriteria; } function clearSearches() { if ($scope.searchResults && $scope.searchResults.items) { $scope.searchResults.items = []; //Clear previous results } } }]); })();