import {Component, Input, ViewChild} from '@angular/core'; import { ActivatedRoute} from '@angular/router'; import { Filter, Value} from '../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class'; import {SearchResult} from '../../openaireLibrary/utils/entities/searchResult'; import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes'; import {SearchFields, FieldDetails} from '../../openaireLibrary/utils/properties/searchFields'; import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component'; import {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 {ConnectHelper} from '../../openaireLibrary/connect/connectHelper'; @Component({ selector: 'openaire-search-projects', template: ` ` }) export class OpenaireSearchProjectsComponent { private errorCodes: ErrorCodes; public columnNames = ['Project', 'GrantId', 'Funder']; public results =[]; public filters =[]; public searchUtils:SearchUtilsClass = new SearchUtilsClass(); public sub: any; public subResults: any; public _location:Location; public searchFields:SearchFields = new SearchFields(); public refineFields: string[] = [];// = this.searchFields.JOURNAL_FIELDS; properties:EnvProperties; public disableForms: boolean = false; private communityId: string = ''; @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ; piwikSiteId = null; constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) { this.errorCodes = new ErrorCodes(); this.searchUtils.status = this.errorCodes.LOADING; var communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname); this.piwikSiteId = PiwikHelper.siteIDs[communityId]; } public ngOnInit() { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; //this.searchPage.refineFields = this.refineFields; this.sub = this.route.queryParams.subscribe(params => { this.communityId = params['communityId']; if(!this.communityId){ this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname); } this.searchUtils.keyword = (params['keyword']?params['keyword']:''); //this.filters = this.createFilters(); //this.searchPage.getParametersFromUrl(params); this._getResults(params); }); }); } public ngOnDestroy() { if(this.sub){ this.sub.unsubscribe(); } if(this.subResults){ this.subResults.unsubscribe(); } } private _getResults(params){ this.searchUtils.status = this.errorCodes.LOADING; this.disableForms = true; this.results = []; this.searchUtils.totalResults = 0; let size: number = 0; this.subResults = this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe( data => { this.filters = this.createFilters(data, params); console.info(data); this.searchUtils.totalResults = data.length; console.info("search Projects [total results:"+this.searchUtils.totalResults+"]"); this.results = data; this.searchPage.checkSelectedFilters(this.filters); //var errorCodes:ErrorCodes = new ErrorCodes(); this.searchUtils.status = this.errorCodes.DONE; if(this.searchUtils.totalResults == 0 ){ this.searchUtils.status = this.errorCodes.NONE; } this.disableForms = false; this.searchPage.triggerInitialLoad(); this.searchPage.transform(this.results); }, err => { console.log(err); //TODO check erros (service not available, bad request) if(err.status == '404') { this.searchUtils.status = this.errorCodes.NOT_FOUND; } else if(err.status == '500') { this.searchUtils.status = this.errorCodes.ERROR; } else { this.searchUtils.status = this.errorCodes.NOT_AVAILABLE; } this.disableForms = false; } ); } private setFilters(){ //TODO set filters from } private createFilters(data, params):Filter[] { let length = Array.isArray(data) ? data.length : 1; var filter_names=["Funder"]; var filter_ids=["relfunder"]; var searchFields = new SearchFields(); var filter_original_ids = ["relfunder"];//searchFields.JOURNAL_FIELDS; this.refineFields = ["relfunder"]; this.searchPage.refineFields = this.refineFields; this.searchPage.getParametersFromUrl(params); var value_names=[/*["Journal", "Journal Aggregator\/Publisher"]*/]; var value_original_ids=[/*["pubsrepository::journal", "aggregator::pubsrepository::journals"]*/]; var funders = new Set(); var value_name = []; var value_original_id = []; let i; for(i=0; i