235 lines
12 KiB
JavaScript
235 lines
12 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('searchModule')
|
|
.controller('SearchController', ['$rootScope', '$scope', 'searchModel', '$state', '$timeout', '$window', '$filter', 'i18nService', 'events', 'configurationModel', 'searchService',
|
|
function ($rootScope, $scope, searchModel, $state, $timeout, $window, $filter, i18nService, events, configurationModel, searchService) {
|
|
var searchMetadata = configurationModel.get('search.targetAreas');
|
|
var initialTypeFilters = [];
|
|
$scope.searchMetadata = { 'targetAreas': searchMetadata };
|
|
$scope.loadingMoreItems = false;
|
|
$scope.searchText = $state.params.searchText || '';
|
|
if ($state.params.searchCriteria) {
|
|
$scope.selectedTargetArea = _.findWhere(searchMetadata, { 'types': $state.params.searchCriteria });
|
|
}
|
|
else {
|
|
$scope.selectedTargetArea = _.findWhere(searchMetadata, { 'selected': true });
|
|
}
|
|
// public functions
|
|
$scope.changeSearchCriteria = function (targetAreaObj) {
|
|
var targetArea = (targetAreaObj) ? targetAreaObj.types : null;
|
|
var searchedText = $scope.searchText;
|
|
if (searchedText && targetArea) {
|
|
searchService.setSelectedTargetArea(targetAreaObj);
|
|
var targetAreaFilter = {
|
|
types: targetArea
|
|
};
|
|
$scope.clearAllFilters();
|
|
$scope.$emit(events.REFRESH_SEARCH_FILTERS);
|
|
$scope.getGlobalSearchResults(searchedText, targetAreaFilter);
|
|
}
|
|
};
|
|
$scope.getGlobalSearchResults = function (searchText, filters) {
|
|
$scope.isSearchDataLoading = true;
|
|
if (!searchText || i18nService.getByteLength(searchText) < 3) {
|
|
$scope.isSearchDataLoading = false;
|
|
}
|
|
else {
|
|
filters = filters || {};
|
|
if (!filters.types) {
|
|
filters.types = ($state.params.searchCriteria) ? $state.params.searchCriteria : initialTypeFilters = _.findWhere(searchMetadata, { 'selected': true }).types;
|
|
}
|
|
searchModel.getGlobalSearchResults(searchText, filters, true).finally(function () {
|
|
$scope.isSearchDataLoading = false;
|
|
$scope.searchResults = $scope.searchModel.globalSearchResults;
|
|
_.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
|
|
};
|
|
}
|
|
$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;
|
|
}
|
|
$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);
|
|
}
|
|
searchModel.loadMoreGlobalSearchResults($scope.searchText, appliedFilters, selectedCategoryItem.nextChunkIndex, true).finally(function () {
|
|
$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;
|
|
_.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) {
|
|
return $filter('localizeLabel')(status, 'status', 'knowledge');
|
|
};
|
|
// 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.$watchCollection('searchModel.selectedFilters', function () {
|
|
$scope.searchModel.filterCriteria = buildFilterCriteria($scope.searchModel.selectedFilters);
|
|
$scope.getGlobalSearchResults($scope.searchText, $scope.searchModel.filterCriteria);
|
|
});
|
|
}
|
|
function selectFirstItemInActiveSection() {
|
|
$scope.searchResultActiveCount = 0;
|
|
$scope.selectItem(_.chain($scope.searchResults.items).filter('active').find(function (item) {
|
|
$scope.searchResultActiveCount = item.results.length;
|
|
return item.results.length;
|
|
}).value().results[0]);
|
|
}
|
|
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;
|
|
}
|
|
init();
|
|
}]);
|
|
})();
|