openaire-library/sharedComponents/numbers/numbers.component.ts

319 lines
16 KiB
TypeScript

import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
import {EnvProperties} from '../../utils/properties/env-properties';
import {properties} from '../../../../environments/environment';
import {NumberSize, NumberUtils} from '../../utils/number-utils.class';
import {BehaviorSubject, Observable, Subscription, zip} from 'rxjs';
import {RouterHelper} from "../../utils/routerHelper.class";
import {HelperFunctions} from "../../utils/HelperFunctions.class";
import {OpenaireEntities} from "../../utils/properties/searchFields";
export interface Numbers {
publicationsSize?: NumberSize;
datasetsSize?: NumberSize;
datasetsLinkedSize?: NumberSize;
softwareLinkedSize?: NumberSize;
softwareSize?: NumberSize;
otherSize?: NumberSize;
fundersSize?: NumberSize;
projectsSize?: NumberSize;
datasourcesSize?: NumberSize;
organizationsSize?: NumberSize;
}
type Entity = 'publication' | 'dataset' | 'software' | 'other' | 'project' | 'datasource' | 'organization';
interface Link {
link: string,
params?: any
}
@Component({
selector: 'numbers',
template: `
<div *ngIf="!loading && numbers">
<div class="uk-container uk-section uk-padding-remove-bottom uk-text-center">
<div class="uk-grid uk-grid-large uk-child-width-1-3@m" uk-grid>
<div *ngIf="numbers.fundersSize">
<h3 *ngIf="links.get('project')">
<a *ngIf="externalLink" [href]="links.get('project').link" target="_blank" class="uk-text-bold number">
{{numbers.fundersSize.number|number}}<span class="number-size">{{numbers.fundersSize.size}}</span>
</a>
<a *ngIf="!externalLink" [routerLink]="links.get('project').link"
[queryParams]="links.get('project').params" class="uk-text-bold number">
{{numbers.fundersSize.number|number}}<span class="number-size">{{numbers.fundersSize.size}}</span>
</a>
</h3>
<span class="uk-text-uppercase uk-text-large">Funders</span>
</div>
<div *ngIf="numbers.datasourcesSize">
<h3>
<a *ngIf="externalLink" [href]="links.get('datasource').link" target="_blank" class="uk-text-bold number">
{{numbers.datasourcesSize.number|number}}<span
class="number-size">{{numbers.datasourcesSize.size}}</span>
</a>
<a *ngIf="!externalLink" [routerLink]="links.get('datasource').link"
[queryParams]="links.get('datasource').params" class="uk-text-bold number">
{{numbers.datasourcesSize.number|number}}<span
class="number-size">{{numbers.datasourcesSize.size}}</span>
</a>
</h3>
<span class="uk-text-uppercase uk-text-large">{{openaireEntities.DATASOURCES}}</span>
</div>
<div *ngIf="numbers.projectsSize">
<h3>
<a *ngIf="externalLink" [href]="links.get('project').link" target="_blank" class="uk-text-bold number">
{{numbers.projectsSize.number|number}}<span class="number-size">{{numbers.projectsSize.size}}</span>
</a>
<a *ngIf="!externalLink" [routerLink]="links.get('project').link"
[queryParams]="links.get('project').params" class="uk-text-bold number">
{{numbers.projectsSize.number|number}}<span class="number-size">{{numbers.projectsSize.size}}</span>
</a>
</h3>
<span class="uk-text-uppercase uk-text-large">{{openaireEntities.PROJECTS}}</span>
</div>
</div>
</div>
<div class="uk-section uk-margin-top" [ngClass]="backgroundClass">
<div class="uk-container">
<div class="uk-grid uk-flex-center uk-margin-auto-left@m uk-margin-auto-right@m uk-grid-large" uk-grid>
<div *ngIf="numbers.publicationsSize" class="uk-width-1-2@m uk-flex uk-flex-center">
<div class="number-width uk-flex uk-flex-column">
<a *ngIf="externalLink" [href]="links.get('publication').link" target="_blank"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.publicationsSize.count|number}}</a>
<a *ngIf="!externalLink" [routerLink]="links.get('publication').link"
[queryParams]="links.get('publication').params"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.publicationsSize.count|number}}</a>
<div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
<icon name="book" ratio="1.5" [flex]="true" class="uk-margin-right" [customClass]="colorClass"></icon>
<span>{{openaireEntities.PUBLICATIONS}}</span>
</div>
</div>
</div>
<div *ngIf="numbers.datasetsSize" class="uk-width-1-2@m uk-flex uk-flex-center">
<div class="number-width uk-flex uk-flex-column">
<a *ngIf="externalLink" [href]="links.get('dataset').link" target="_blank"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.datasetsSize.count|number}}</a>
<a *ngIf="!externalLink" [routerLink]="links.get('dataset').link"
[queryParams]="links.get('dataset').params"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.datasetsSize.count|number}}</a>
<div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
<icon name="database" ratio="1.5" [flex]="true" class="uk-margin-right"
[customClass]="colorClass"></icon>
<span>{{openaireEntities.DATASETS}}</span>
</div>
</div>
</div>
<div *ngIf="numbers.softwareSize" class="uk-width-1-2@m uk-flex uk-flex-center">
<div class="number-width uk-flex uk-flex-column">
<a *ngIf="externalLink" [href]="links.get('software').link" target="_blank"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.softwareSize.count|number}}</a>
<a *ngIf="!externalLink" [routerLink]="links.get('software').link"
[queryParams]="links.get('software').params"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.softwareSize.count|number}}</a>
<div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
<icon name="cog" ratio="1.5" [flex]="true" class="uk-margin-right" [customClass]="colorClass"></icon>
<span>{{openaireEntities.SOFTWARE}}</span>
</div>
</div>
</div>
<div *ngIf="numbers.otherSize" class="uk-width-1-2@m uk-flex uk-flex-center">
<div class="number-width uk-flex uk-flex-column">
<a *ngIf="externalLink" [href]="links.get('other').link" target="_blank"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.otherSize.count|number}}</a>
<a *ngIf="!externalLink" [routerLink]="links.get('other').link"
[queryParams]="links.get('other').params"
class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.otherSize.count|number}}</a>
<div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
<icon name="earth" ratio="1.5" [flex]="true" class="uk-margin-right"
[customClass]="colorClass"></icon>
<span>{{openaireEntities.OTHER}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div *ngIf="loading" class="uk-container uk-section uk-text-center" style="min-height: 400px">
<loading [color]="colorClass"></loading>
</div>
`,
})
export class NumbersComponent implements OnInit, OnDestroy {
@Input() colorClass = 'portal-color';
@Input() backgroundClass = null;
/** Add a value if you want to apply refine query*/
@Input() refineValue = null;
/** True: Default initialization
* False: Call init method to initialize numbers */
@Input() defaultInit = true;
/** Add an external link for numbers link */
@Input() externalLink;
/** When numbers have been initialized this emitter will emitted */
@Output() results: EventEmitter<Numbers> = new EventEmitter<Numbers>();
public properties: EnvProperties = properties;
public openaireEntities = OpenaireEntities;
public routerHelper: RouterHelper = new RouterHelper();
public numbers: Numbers = {};
public loading: boolean = true;
public links: Map<Entity, Link> = new Map<Entity, Link>();
private params: Map<Entity, {}> = new Map<Entity, {}>();
private emptySubject: BehaviorSubject<number> = new BehaviorSubject<number>(0);
private subs: any[] = [];
constructor(private searchResearchResultsService: SearchResearchResultsService,
private searchDataprovidersService: SearchDataprovidersService,
private refineFieldResultsService: RefineFieldResultsService) {
this.links = new Map<Entity, Link>();
this.params = new Map<Entity, {}>();
this.emptySubject = new BehaviorSubject<number>(0);
}
ngOnInit() {
if (this.defaultInit) {
this.init();
}
this.setLinks();
}
setParams() {
this.params.set('publication', {type: 'publications'});
this.params.set('dataset', {type: 'datasets'});
this.params.set('software', {type: 'software'});
this.params.set('other', {type: 'other'});
this.params.set('project', {});
this.params.set('datasource', {});
if (this.refineValue) {
this.params.forEach((value) => {
value['fq'] = this.refineValue;
});
}
}
setLinks() {
this.setParams();
if (this.externalLink) {
this.links.set('publication', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
});
this.links.set('dataset', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
});
this.links.set('software', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
});
this.links.set('other', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
});
this.links.set('project', {
link: this.externalLink + properties.searchLinkToProjects +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
});
this.links.set('datasource', {
link: this.externalLink + properties.searchLinkToDataProviders +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
});
} else {
this.links.set('publication', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
});
this.links.set('dataset', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
});
this.links.set('software', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
});
this.links.set('other', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
});
this.links.set('project', {
link: properties.searchLinkToProjects,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
});
this.links.set('datasource', {
link: properties.searchLinkToDataProviders,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
});
}
}
init(getDatasetsLinked = false, getSoftwareLinked = false, getPublications = true, getDatasets = true,
getSoftware = true, getOther = true, getProjects = true, getDataProviders = true, refineValue: string = null) {
this.loading = true;
if (refineValue) {
this.refineValue = refineValue;
}
let refineParams = (this.refineValue) ? ('&fq=' + this.refineValue) : null;
this.subs.push(zip(
(getPublications) ? this.searchResearchResultsService.numOfSearchResults('publication', '', this.properties, refineParams) : this.empty,
(getDatasets) ? this.searchResearchResultsService.numOfSearchResults('dataset', '', this.properties, refineParams) : this.empty,
(getDatasetsLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("dataset", this.properties) : this.empty,
(getSoftware) ? this.searchResearchResultsService.numOfSearchResults('software', '', this.properties, refineParams) : this.empty,
(getSoftwareLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("software", this.properties) : this.empty,
(getOther) ? this.searchResearchResultsService.numOfSearchResults('other', '', this.properties, refineParams) : this.empty,
(getProjects) ? this.refineFieldResultsService.getRefineFieldsResultsByEntityName(['funder'], 'project', this.properties, refineParams) : this.empty,
(getDataProviders) ? this.searchDataprovidersService.numOfSearchDataproviders('', this.properties, refineParams) : this.empty
).subscribe((data: any[]) => {
if (data[0] && data[0] > 0) {
this.numbers.publicationsSize = NumberUtils.roundNumber(data[0]);
}
if (data[1] && data[1] > 0) {
this.numbers.datasetsSize = NumberUtils.roundNumber(data[1]);
}
if (data[2] && data[2] > 0) {
this.numbers.datasetsLinkedSize = NumberUtils.roundNumber(data[2]);
}
if (data[3] && data[3] > 0) {
this.numbers.softwareSize = NumberUtils.roundNumber(data[3]);
}
if (data[4] && data[4] > 0) {
this.numbers.softwareLinkedSize = NumberUtils.roundNumber(data[4]);
}
if (data[5] && data[5] > 0) {
this.numbers.otherSize = NumberUtils.roundNumber(data[5]);
}
if (data[6][0] && data[6][0] > 0) {
this.numbers.projectsSize = NumberUtils.roundNumber(data[6][0]);
}
if (data[6][1] && data[6][1].length > 0 && data[6][1][0].filterId == 'funder' && data[6][1][0].values) {
this.numbers.fundersSize = NumberUtils.roundNumber(data[6][1][0].values.length);
}
if (data[7] && data[7] > 0) {
this.numbers.datasourcesSize = NumberUtils.roundNumber(data[7]);
}
this.results.emit(this.numbers);
this.loading = false;
}, err => {
this.handleError('Error getting numbers', err);
this.loading = false;
}));
}
ngOnDestroy() {
this.subs.forEach(sub => {
if (sub instanceof Subscription) {
sub.unsubscribe();
}
});
}
get empty(): Observable<number> {
return this.emptySubject.asObservable();
}
private handleError(message: string, error) {
console.error('Numbers: ' + message, error);
}
}