connect/src/app/searchPages/simple/searchProjects.component.ts

247 lines
9.9 KiB
TypeScript

import {Component, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AdvancedField, Filter, Value} from '../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class';
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../../openaireLibrary/utils/errorMessages.component';
import {SearchFields} from '../../openaireLibrary/utils/properties/searchFields';
import {SearchCustomFilter, SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {SearchCommunityProjectsService} from '../../openaireLibrary/connect/projects/searchProjects.service';
import {PiwikHelper} from '../../utils/piwikHelper';
import {properties} from "../../../environments/environment";
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
import {Subscriber} from "rxjs";
import {NewSearchPageComponent} from "../../openaireLibrary/searchPages/searchUtils/newSearchPage.component";
import {SearchResult} from "../../openaireLibrary/utils/entities/searchResult";
@Component({
selector: 'openaire-search-projects',
template: `
<new-search-page
pageTitle="Search Projects"
entityType="project"
type="project"
[results]="results"
[searchUtils]="searchUtils"
[sortedByChanged]="searchUtils.sortBy" [resultsPerPageChanged]="searchUtils.size"
[fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
[simpleSearchLink]="properties.searchLinkToProjects"
[disableForms]="disableForms"
[disableRefineForms]="disableRefineForms"
[loadPaging]="loadPaging"
[oldTotalResults]="oldTotalResults"
[openaireLink]="'https://' + (properties.environment == 'production'?'':'beta.') + 'explore.openaire.eu/search/find/projects'"
[includeOnlyResultsAndFilter]="false"
[piwikSiteId]=piwikSiteId [hasPrefix]="false"
searchFormClass="datasourcesTableSearchForm" [entitiesSelection]="true" [showSwitchSearchLink]="false"
[filters]="filters"
[simpleView]="true" formPlaceholderText="Search by name..."
[showResultCount]="false" [showIndexInfo]="false" [showDownload]="false"
[sort]="false" [showBreadcrumb]="true">
[customFilter]=customFilter
</new-search-page>
`
})
export class OpenaireSearchProjectsComponent {
private errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
public columnNames = ['Project', 'GrantId', 'Funder'];
public results =[];
public originalFilters =[];
public filters =[];
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
subscriptions = [];
public searchFields:SearchFields = new SearchFields();
public refineFields: string[] = ["funder"];
properties:EnvProperties = properties;
public disableForms: boolean = false;
public enableSearchView: boolean = true;
private communityId: string = '';
piwikSiteId = null;
customFilter: SearchCustomFilter = null;
initialLoad = true;
public allResults =[];
@ViewChild(NewSearchPageComponent, { static: true }) searchPage: NewSearchPageComponent;
public fieldIds: string[] = this.searchFields.PROJECT_ADVANCED_FIELDS;
public fieldIdsMap= this.searchFields.PROJECT_FIELDS;
public selectedFields:AdvancedField[] = [];
public disableRefineForms: boolean = false;
public loadPaging: boolean = true;
public oldTotalResults: number = 0;
keyword;
constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService, private _communityService: CommunityService) {
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.searchUtils.status = this.errorCodes.LOADING;
}
public ngOnInit() {
this.searchUtils.baseUrl = this.properties.searchLinkToProjects;
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(community =>{
if(community != null){
this.communityId = community.communityId;
this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, community.shortTitle);
this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment);
this.subscriptions.push(this.route.queryParams.subscribe(params => {
let page = (params['page'] === undefined) ? 1 : +params['page'];
this.searchUtils.page = (page <= 0) ? 1 : page;
this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
this.searchUtils.size = 10;
}
this.keyword = decodeURIComponent(params['fv0']?params['fv0']:(params['keyword']?params['keyword']:''));
this.selectedFields = [];
this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap, this.customFilter, params, "project");
this.filters = [];
this.searchPage.prepareFiltersToShow(this.filters, 0);
if(this.initialLoad) {
this.initialLoad = false;
this._getResults();
}else{
this.filterResults();
}
}));
}
}));
}
public ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
private _getResults(){
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.enableSearchView = false;
this.results = [];
this.searchUtils.totalResults = 0;
this.subscriptions.push(this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe(
data => {
this.originalFilters = this.createFilters(data);
this.allResults = this.parseResults(data);
this.filterResults();
},
err => {
this.handleError("Error getting projects for community with id: "+this.communityId, err);
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
this.enableSearchView = true;
}
));
}
private createFilters(data):Filter[] {
let length = Array.isArray(data) ? data.length : 1;
var filter_names=["Funder"];
var filter_ids=["funder"];
var filter_original_ids = ["funder"];
var value_names=[];
var value_original_ids=[];
var funders = new Set<String>();
var value_name = [];
var value_original_id = [];
let i;
for(i=0; i<length; i++) {
let resData = Array.isArray(data) ? data[i] : data;
if(resData.funder && !funders.has(resData.funder)) {
funders.add(resData.funder);
value_name.push(resData.funder);
value_original_id.push(resData.funder.trim());
}
}
value_names.push(value_name);
value_original_ids.push(value_original_id);
var filters: Filter[] =[];
for(i =0 ; i < filter_names.length;i++){
if(value_names[i].length > 0) {
var values: Value[] = [];
for (var j = 0; j < value_names[i].length; j++) {
var value: Value = {name: value_names[i][j], id: value_original_ids[i][j], number: j, selected: false}
values.push(value);
}
var filter: Filter = {
title: filter_names[i],
filterId: filter_ids[i],
originalFilterId: filter_original_ids[i],
values: values,
countSelectedValues: 0,
"filterOperator": 'or',
valueIsExact: true,
filterType: "checkbox"
};
filters.push(filter);
}
}
return filters;
}
parseResults(data){
let results:SearchResult[]=[];
for(let result of data){
let sResult:SearchResult = new SearchResult();
sResult.id = result.openaireId;
sResult.title = {name:"", accessMode: null, sc39: null};
sResult.title.name = result.name?result.name:result.acronym;
sResult.acronym = result['acronym'];
sResult.code = result['grantId'];
sResult['funderShortname'] = result['funder'];
results.push(sResult);
}
return results;
}
filterResults(){
this.searchUtils.refineStatus = this.errorCodes.LOADING;
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.disableRefineForms = true;
this.enableSearchView = false;
let results = this.allResults.filter(value => { return value.title.name && value.title.name.toLowerCase().indexOf(this.keyword.toLowerCase()) !=-1 || value.acronym && value.acronym.toLowerCase().indexOf(this.keyword.toLowerCase()) !=-1});
let funderFilterValues = [];
for(let filter of this.filters){
if(filter.countSelectedValues > 0){
for(let value of filter.values){
if(value.selected) {
funderFilterValues.push(value.name);
}
}
}
}
if(funderFilterValues.length > 0) {
results = results.filter(value => {
return funderFilterValues.indexOf(value.funderShortname) != -1
});
}
this.oldTotalResults = results.length;
this.searchUtils.totalResults = results.length;
this.subscriptions.push(setTimeout(() => {
this.filters = this.searchPage.prepareFiltersToShow(this.originalFilters, this.allResults.length);
this.results = results.slice((this.searchUtils.page - 1) * this.searchUtils.size, this.searchUtils.page *this.searchUtils.size );
this.searchUtils.status = this.results.length == 0 ? this.errorCodes.NONE: this.errorCodes.DONE;
this.disableForms = false;
this.disableRefineForms = false;
this.enableSearchView = true;
this.searchUtils.refineStatus = this.errorCodes.DONE;
}, 300));
}
private handleError(message: string, error) {
console.error("Projects simple Search Page: "+message, error);
}
}