monitor/src/app/home/home.component.ts

386 lines
12 KiB
TypeScript
Raw Normal View History

import {Component, ElementRef, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Meta, Title} from '@angular/platform-browser';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
import {RefineFieldResultsService} from "../openaireLibrary/services/refineFieldResults.service";
import {NumberUtils} from "../openaireLibrary/utils/number-utils.class";
import {SearchResearchResultsService} from "../openaireLibrary/services/searchResearchResults.service";
import {animate, state, style, transition, trigger} from "@angular/animations";
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
import {LocalStorageService} from "../openaireLibrary/services/localStorage.service";
import {Stakeholder} from "../openaireLibrary/monitor/entities/stakeholder";
@Component({
selector: 'home',
templateUrl: 'home.component.html',
styleUrls: ['home.component.css'],
animations: [
trigger('captain-left', [
state('before', style({
transform: 'translateX(-300px)'
})),
state('after', style({
transform: 'translateX(0)'
})),
transition('before => after', [
animate('0.7s')
])
]),
trigger('captain-right', [
state('before', style({
transform: 'translateX(300px)'
})),
state('after', style({
transform: 'translateX(0)'
})),
transition('before => after', [
animate('0.7s')
])
]),
trigger('1', [
state('1', style({
opacity: 1
})),
state('2', style({
opacity: 0.4
})),
state('3', style({
opacity: 0.4
})),
transition('* => *', [
animate('0.3s')
])
]),
trigger('2', [
state('1', style({
opacity: 0.4
})),
state('2', style({
opacity: 1
})),
state('3', style({
opacity: 0.4
})),
transition('* => *', [
animate('0.3s')
])
]),
trigger('3', [
state('1', style({
opacity: 0.4
})),
state('2', style({
opacity: 0.4
})),
state('3', style({
opacity: 1
})),
transition('* => *', [
animate('0.3s')
])
])
]
})
export class HomeComponent {
public piwiksub: any;
public pageTitle = "OpenAIRE | Monitor";
public stakeholders: any[] = [];
public selected: Stakeholder = null;
public pageContents = null;
public divContents = null;
// Message variables
public status: number;
public loading: boolean = true;
public subscriberErrorMessage: string = "";
public errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
properties: EnvProperties;
public keyword: string = "";
public type: string = null;
public publicationsSize: any = null;
public datasetsSize: any = null;
public softwareSize: any = null;
public otherSize: any = null;
public fundersSize: any = null;
numberSubs = [];
public state = 1;
private timeouts: any[] = [];
public observer: IntersectionObserver;
public captains: boolean = false;
@ViewChild('ref') ref: ElementRef;
@ViewChild('AlertModal') modal;
private directLink: boolean = true;
constructor(
private route: ActivatedRoute,
private _router: Router,
private _meta: Meta,
private _title: Title,
private _piwikService: PiwikService,
private _stakeholderService: StakeholderService,
private localStorageService: LocalStorageService,
private helper: HelperService,
private seoService: SEOService,
private _refineFieldResultsService: RefineFieldResultsService, private _searchResearchResultsService: SearchResearchResultsService) {
var description = "OpenAIRE - Monitor, Statistics, Search, Funder, EC, European Commision";
var title = "OpenAIRE - Monitor";
this._meta.updateTag({content: description}, "name='description'");
this._meta.updateTag({content: description}, "property='og:description'");
this._meta.updateTag({content: title}, "property='og:title'");
this._title.setTitle(title);
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.status = this.errorCodes.LOADING;
}
public ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
var url = data.envSpecific.baseLink + this._router.url;
this.seoService.createLinkForCanonicalURL(url, false);
this._meta.updateTag({content: url}, "property='og:url'");
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Monitor", this.properties.piwikSiteId).subscribe();
}
this.getNumbers();
this.getFunders();
// this.createGifs();
//this.getDivContents();
this.getPageContents();
this.localStorageService.get().subscribe(value => {
this.directLink = value;
})
});
if(typeof document != "undefined") {
this.initAnimations();
this.startAnimation();
}
}
public initAnimations() {
this.observer = new IntersectionObserver(entries => {
if (entries[0].isIntersecting == true && this.captains == false) {
this.captains = true;
}
}, {threshold: [0]});
this.observer.observe(this.ref.nativeElement);
}
public startAnimation(state = 1) {
this.clearTimeouts();
this.state = state;
this.timeouts.push(setTimeout(() => {
if (this.state < 3) {
this.startAnimation(this.state + 1);
} else {
this.startAnimation();
}
}, 6000));
}
private clearTimeouts() {
this.timeouts.forEach(timeout => {
clearTimeout(timeout);
});
}
private getPageContents() {
this.helper.getPageHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
this.pageContents = contents;
})
}
private getDivContents() {
this.helper.getDivHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
this.divContents = contents;
})
}
public get stakeholdersNumber(): number {
if(this.type === null) {
return this.stakeholders.length;
} else {
return this.stakeholders.filter(stakeholder => stakeholder.type === this.type).length;
}
}
getNumbers() {
this.numberSubs.push(this._refineFieldResultsService.getRefineFieldsResultsByEntityName(["funder"], "project", this.properties).subscribe(
data => {
if (data[1].length > 0 && data[1][0].filterId == "funder" && data[1][0].values) {
this.fundersSize = NumberUtils.roundNumber(data[1][0].values.length);
}
},
err => {
//console.log(err);
this.handleError("Error getting 'funder' field results of projects", err);
}));
this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("publication", "", this.properties).subscribe(
data => {
if (data && data > 0) {
this.publicationsSize = NumberUtils.roundNumber(data);
}
},
err => {
//console.log(err);
this.handleError("Error getting number of publications", err);
}
));
this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("dataset", "", this.properties).subscribe(
data => {
if (data && data > 0) {
this.datasetsSize = NumberUtils.roundNumber(data);
}
},
err => {
//console.log(err);
this.handleError("Error getting number of research data", err);
}
));
this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("software", "", this.properties).subscribe(
data => {
if (data && data > 0) {
this.softwareSize = NumberUtils.roundNumber(data);
}
},
err => {
this.handleError("Error getting number of software data", err);
}
));
this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("other", "", this.properties).subscribe(
data => {
if (data && data > 0) {
this.otherSize = NumberUtils.roundNumber(data);
}
},
err => {
this.handleError("Error getting number of software data", err);
}
));
}
public getFunders() {
this.loading = true;
this.status = this.errorCodes.LOADING;
this.subscriberErrorMessage = "";
this._stakeholderService.getStakeholders(this.properties.monitorServiceAPIURL).subscribe(
stakeholders => {
if (!stakeholders || stakeholders.length == 0) {
this.status = this.errorCodes.NONE;
} else {
this.stakeholders = [];
stakeholders.forEach(stakeholder => {
let index = (this.stakeholders.push(HelperFunctions.copy(stakeholder)) - 1);
this.setSizeOfStakeholder(index);
});
stakeholders.forEach(stakeholder => {
let index = (this.stakeholders.push(HelperFunctions.copy(stakeholder)) - 1);
this.setSizeOfStakeholder(index);
});
stakeholders.forEach(stakeholder => {
let index = (this.stakeholders.push(HelperFunctions.copy(stakeholder)) - 1);
this.setSizeOfStakeholder(index);
});
stakeholders.forEach(stakeholder => {
let index = (this.stakeholders.push(HelperFunctions.copy(stakeholder)) - 1);
this.setSizeOfStakeholder(index);
});
}
this.loading = false;
},
error => {
this.status = this.handleError("Error getting funders", error);
this.loading = false;
}
);
}
private setSizeOfStakeholder(index) {
let mod = index % 9;
if (mod === 0 || mod === 4 || mod === 8) {
this.stakeholders[index].size = 'large';
} else if (mod === 1 || mod === 5 || mod === 6) {
this.stakeholders[index].size = 'small';
} else {
this.stakeholders[index].size = 'medium';
}
}
public confirmModalOpen(result: Stakeholder) {
this.selected = result;
this.modal.cancelButton = true;
this.modal.okButton = true;
this.modal.alertTitle = 'You are going to visit ' + result.name + ' Monitor Dashboard';
this.modal.alertMessage = false;
this.modal.okButtonLeft = false;
this.modal.okButtonText = 'Yes';
this.modal.cancelButtonText = 'No';
this.modal.choice = true;
this.modal.open();
}
public getStakeholderPageUrl(stakeholder: Stakeholder) {
return this.properties.baseLink + '/dashboard/' + stakeholder.alias;
}
public goToPage(data: any) {
if (data.value == true) {
let url = this.getStakeholderPageUrl(this.selected);
this.localStorageService.setCommunityDirectLink(data.choice);
window.open(url, '_blank');
}
}
public quote(param: string): string {
return StringUtils.quote(param);
}
public ngOnDestroy() {
if (this.piwiksub) {
this.piwiksub.unsubscribe();
}
this.clearTimeouts();
if(this.observer) {
this.observer.disconnect();
}
}
private handleError(message: string, error): number {
var code = "";
if (!error.status) {
var error = error.json();
code = error.code;
} else {
code = error.status;
}
console.error("Communities (component): " + message, error);
return this.errorMessages.getErrorCode(code);
}
}