explore-services/explore/src/app/funders/funders.component.ts

261 lines
9.5 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Subscriber, Subscription, zip} from "rxjs";
import {Meta, Title} from "@angular/platform-browser";
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
import {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
import {properties} from "../../environments/environment";
import {RefineFieldResultsService} from '../openaireLibrary/services/refineFieldResults.service';
import {StakeholderService} from '../openaireLibrary/monitor/services/stakeholder.service';
import {Option} from '../openaireLibrary/sharedComponents/input/input.component';
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
import {HelperFunctions} from '../openaireLibrary/utils/HelperFunctions.class';
import {NumberUtils} from '../openaireLibrary/utils/number-utils.class';
@Component({
selector: 'funders',
templateUrl: './funders.component.html',
styleUrls: ['funders.component.less']
})
export class FundersComponent implements OnInit {
private subscriptions: Subscription[] = [];
url: string = null;
pageTitle: string = "OpenAIRE - Explore | Funders";
pageDescription: string = "Funders | Be an integral part of the open R&I ecosystem";
properties: EnvProperties = properties;
breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'funders'}];
showLoading: boolean = true;
index: number = 0;
funders: any[] = [];
groupedFunders: any[] = [];
options: Option[];
sortBy: string = 'all';
currentPage: number = 1;
pageSize: number = 6;
fundersNumber: number = 0;
researchProductsNumber: number = 0;
projectsNumber: number = 0;
fundersMap = new Map<string, {
"id": string,
"name": string,
"alias": string,
"researchProducts": number,
"openAccessResearchProducts": number,
"projects": number,
"monitorDashboard": string,
"monitorDashboardStatus": string,
"logoUrl": string
}>();
constructor(private router: Router,
private meta: Meta,
private title: Title,
private seoService: SEOService,
private piwikService: PiwikService,
private refineFieldResultsService: RefineFieldResultsService,
private stakeholderService: StakeholderService) {
}
ngOnInit() {
this.title.setTitle('OpenAIRE - Explore | Funders');
this.properties = properties;
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
this.subscriptions.push( this.piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe());
}
this.url = this.properties.domain + this.router.url;
this.seoService.createLinkForCanonicalURL(this.url);
this.updateUrl(this.url);
this.updateTitle(this.pageTitle);
this.updateDescription(this.pageDescription);
this.options = [
{value: 'all', label: 'All funders'},
{value: 'dashboard', label: 'Funders with dashboard'}
];
this.getFunders();
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
private updateDescription(description: string) {
this.meta.updateTag({content: description}, "name='description'");
this.meta.updateTag({content: description}, "property='og:description'");
}
private updateTitle(title: string) {
var title = ((title.length > 50) ? title.substring(0, 50) : title);
this.title.setTitle(title);
this.meta.updateTag({content: title}, "property='og:title'");
}
private updateUrl(url: string) {
this.meta.updateTag({content: url}, "property='og:url'");
}
private getFunders() {
let refineParams = '&fq=resultbestaccessright%20exact%20%22Open%20Access%22';
this.subscriptions.push(
zip(
this.refineFieldResultsService.getRefineFieldsResultsByEntityName(['relfunder'], 'result', this.properties),
this.refineFieldResultsService.getRefineFieldsResultsByEntityName(['relfunder'], 'result', this.properties, refineParams),
this.refineFieldResultsService.getRefineFieldsResultsByEntityName(['funder'], 'project', this.properties),
this.stakeholderService.getStakeholders(this.properties.monitorServiceAPIURL, 'funder')
).subscribe((data: any[]) => {
// storing all needed data to a map
// 1st call
let queriedFunders1 = data[0][1][0].values;
queriedFunders1.forEach(queriedFunder => {
this.fundersMap.set(queriedFunder.id, {
"id": queriedFunder.id,
"name": queriedFunder.name,
"alias": '',
"researchProducts": +queriedFunder.number,
"openAccessResearchProducts": 0,
"projects": 0,
"monitorDashboard": '',
"monitorDashboardStatus": '',
"logoUrl": ''
});
});
// 2nd call
let queriedFunders2 = data[1][1][0].values;
queriedFunders2.forEach(queriedFunder => {
if(this.fundersMap.has(queriedFunder.id)) {
this.fundersMap.get(queriedFunder.id).openAccessResearchProducts = +queriedFunder.number;
}
});
// 3rd call
let queriedFunders3 = data[2][1][0].values;
queriedFunders3.forEach(queriedFunder => {
if(this.fundersMap.has(queriedFunder.id)) {
this.fundersMap.get(queriedFunder.id).projects = +queriedFunder.number;
} else {
this.fundersMap.set(queriedFunder.id, {
"id": queriedFunder.id,
"name": queriedFunder.name,
"alias": '',
"researchProducts": 0,
"openAccessResearchProducts": 0,
"projects": +queriedFunder.number,
"monitorDashboard": '',
"monitorDashboardStatus": '',
"logoUrl": ''
});
}
});
// 4th call
let queriedFunders4 = data[3];
queriedFunders4.forEach(queriedFunder => {
let id = queriedFunder.index_id + '||' + queriedFunder.index_name + '||' + queriedFunder.index_shortName;
if(this.fundersMap.has(id)) {
this.fundersMap.get(id).alias = queriedFunder.alias;
this.fundersMap.get(id).monitorDashboard = queriedFunder.alias;
this.fundersMap.get(id).monitorDashboardStatus = queriedFunder.visibility;
this.fundersMap.get(id).logoUrl = queriedFunder.logoUrl;
} else {
this.fundersMap.set(id, {
"id": queriedFunder.id,
"name": queriedFunder.name,
"alias": queriedFunder.alias,
"researchProducts": 0,
"openAccessResearchProducts": 0,
"projects": 0,
"monitorDashboard": queriedFunder.alias,
"monitorDashboardStatus": queriedFunder.visibility,
"logoUrl": queriedFunder.logoUrl
});
}
});
// convert funders map into an array
this.funders = Array.from(this.fundersMap.values());
// group funders based on their initial letter
this.groupFunders(this.funders);
// calculate total numbers for intro content
this.calculateNumbers();
this.showLoading = false;
})
);
}
private groupFunders(funders) {
if(funders.length === 0) {
return [];
}
funders.sort((a, b) => a['name'].localeCompare(b['name']));
this.index = 0;
this.groupedFunders = Object.values(
funders.reduce((acc, funder) => {
let firstLetter = funder['name'][0].toLocaleUpperCase();
if(!acc[firstLetter]) {
acc[firstLetter] = {group: firstLetter, data: [funder]};
} else {
acc[firstLetter].data.push(funder);
}
return acc;
},{})
)
if(funders.length > 1) {
this.groupedFunders.unshift({group: 'All', data: funders});
}
}
private calculateSum(array, property) {
let sum = 0;
array.forEach(element => {
sum += element[property];
});
return sum;
}
private calculateNumbers() {
this.fundersNumber = this.funders.length;
this.researchProductsNumber = this.calculateSum(this.funders, 'researchProducts');
this.projectsNumber = this.calculateSum(this.funders, 'projects');
}
get showContentWithNumbers() {
return this.fundersNumber && this.researchProductsNumber && this.projectsNumber;
}
formatNumber(num: number | string) {
let formatted = NumberUtils.roundNumber(+num);
return formatted.number + formatted.size;
}
changeDisplayedFunders(i) {
this.currentPage = 1;
this.index = i;
}
urlEncodeAndQuote(str: string): string {
return StringUtils.quote(StringUtils.URIEncode(str));
}
sortByChanged() {
let displayedFunders = this.funders;
if(this.sortBy == 'dashboard') {
displayedFunders = this.funders.filter(funder => funder.monitorDashboard && funder.monitorDashboard?.length > 0 && funder.monitorDashboardStatus != 'PRIVATE');
}
this.currentPage = 1;
this.groupFunders(displayedFunders);
}
public updateCurrentPage($event) {
this.currentPage = $event.value;
HelperFunctions.scrollToId('target');
}
}