connect/src/app/searchPages/simple/searchDataproviders.compone...

180 lines
6.6 KiB
TypeScript
Raw Normal View History

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 {SearchCommunityDataprovidersService} from '../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
import {PiwikHelper} from '../../utils/piwikHelper';
@Component({
selector: 'openaire-search-dataproviders',
template: `
<search-page-table pageTitle="OpenAIRE Content Providers Table"
type="content providers" entityType="dataprovider"
[(results)] = "results" [(searchUtils)] = "searchUtils"
[columnNames]="columnNames"
[showResultCount]=false
[disableForms]="disableForms"
searchFormClass="datasourcesTableSearchForm"
formPlaceholderText="Search for Content Providers">
</search-page-table>
`
})
export class OpenaireSearchDataprovidersComponent {
private errorCodes: ErrorCodes;
public columnNames = ['Name', 'Official Name'];
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 _searchDataprovidersService: SearchCommunityDataprovidersService) {
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._searchDataprovidersService.searchDataproviders(this.properties, this.communityId).subscribe(
data => {
//this.filters = this.createFilters(data, params);
console.info(data);
this.searchUtils.totalResults = data.length;
console.info("search Content Providers [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=[];
var filter_ids=[];
var searchFields = new SearchFields();
var filter_original_ids = [];//searchFields.JOURNAL_FIELDS;
this.refineFields = ["relfunder"];
this.searchPage.refineFields = this.refineFields;
this.searchPage.getParametersFromUrl(params);
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++){
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' };
filters.push(filter);
}
console.info(filters);
return filters;
}
*/
}