openaire-library/landingPages/project/project.component.ts

427 lines
20 KiB
TypeScript

import {Component, ViewChild} from '@angular/core';
import {ElementRef, Input} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Params} from '@angular/router';
import {Title, Meta} from '@angular/platform-browser';
import {Observable} from 'rxjs/Observable';
import {ProjectService} from './project.service';
import {ProjectInfo} from '../../utils/entities/projectInfo';
import {RouterHelper} from '../../utils/routerHelper.class';
import {FetchPublications} from '../../utils/fetchEntitiesClasses/fetchPublications.class';
import {SearchPublicationsService} from '../../services/searchPublications.service';
import {FetchDatasets} from '../../utils/fetchEntitiesClasses/fetchDatasets.class';
import {SearchDatasetsService} from '../../services/searchDatasets.service';
import {FetchSoftware} from '../../utils/fetchEntitiesClasses/fetchSoftware.class';
import {SearchSoftwareService} from '../../services/searchSoftware.service';
import {ModalLoading} from '../../utils/modal/loading.component';
import {ReportsService} from '../../services/reports.service';
import {ErrorCodes} from '../../utils/properties/errorCodes'
import {PiwikService} from '../../utils/piwik/piwik.service';
import {EnvProperties} from '../../utils/properties/env-properties';
@Component({
selector: 'project',
templateUrl: 'project.component.html',
})
export class ProjectComponent{
@Input() piwikSiteId = null;
public projectInfo: ProjectInfo;
public projectId : string ;
public projectName: string;
// Metrics tab variables
public metricsClicked: boolean;
public viewsFrameUrl: string;
public downloadsFrameUrl: string;
private totalViews: number;
private totalDownloads: number;
private pageViews: number;
// Statistics tab variables
public statsClicked: boolean;
public chartScientificResultsUrl: string;
public chartAccessModeUrl: string;
public chartDatasourcesUrl: string;
// HTML variables in APP BOX
public publications_dynamic: string;
public datasets_dynamic: string;
public software_dynamic: string;
public project ;
// CSV variables
public downloadURLAPI: string;
public csvParams: string;
public csvParamsDatasets: string;
public csvParamsSoftware: string;
// Message variables
public warningMessage = "";
public errorMessage = "";
public showLoading: boolean = true;
// Active tab variable for responsiveness
public activeTab: string = "Publications";
// Request results for research data and software only the one time (first time tab is clicked)
private reloadDatasets: boolean = true;
private reloadSoftware: boolean = true;
// Variables for publications, research data, software tabs
public fetchPublications : FetchPublications;
public linkToSearchPublications = "";
public fetchDatasets : FetchDatasets;
public linkToSearchDatasets = "";
public fetchSoftware: FetchSoftware;
public linkToSearchSoftware = "";
public routerHelper:RouterHelper = new RouterHelper();
public errorCodes:ErrorCodes = new ErrorCodes();
@ViewChild (ModalLoading) loading : ModalLoading ;
// Alert box when something is wrong with CSV requests
@ViewChild('AlertModalCsvError') alertCsvError;
sub: any; piwiksub: any; infoSub: any; downloadFilePiwikSub: any;
properties:EnvProperties;
constructor ( private element: ElementRef,
private _projectService: ProjectService,
private _piwikService:PiwikService,
private route: ActivatedRoute,
private _searchPublicationsService: SearchPublicationsService,
private _searchDatasetsService: SearchDatasetsService,
private _searchSoftwareService: SearchSoftwareService,
private _reportsService: ReportsService,
private _meta: Meta,
private _title: Title,
private _router: Router) {
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.updateUrl(data.envSpecific.baseLink+this._router.url);
});
this.sub = this.route.queryParams.subscribe(params => {
this.metricsClicked = false;
this.statsClicked = false;
this.fetchPublications = new FetchPublications( this._searchPublicationsService);
this.fetchDatasets = new FetchDatasets(this._searchDatasetsService);
this.fetchSoftware = new FetchSoftware(this._searchSoftwareService);
var title = "Project";
var description = "project, funding, open access, publications, research data, software";
this.updateTitle(title);
this.updateDescription(description);
this.projectId = params['projectId'];
var grantId = params['grantId'];
var funder = params['funder'];
if(this.projectId){
this.getProjectInfo(this.projectId);
this.actionsAfterLoadId();
}else if(grantId && funder){
this.getProjectInfoByGrantId(grantId,funder);
}else{
this.showLoading = false;
this.warningMessage="No valid project id";
}
this.downloadURLAPI = this.properties.csvAPIURL;
this.createClipboard();
this.csvParams = "?format=csv-special&type=publications&fq=(((oaftype exact result) and (resulttypeid exact publication)) and (relprojectid exact "+this.projectId+"))";
this.csvParamsDatasets = "?format=csv-special&type=datasets&fq=(((oaftype exact result) and (resulttypeid exact dataset)) and (relprojectid exact "+this.projectId+"))";
this.csvParamsSoftware = "?format=csv-special&type=software&fq=(((oaftype exact result) and (resulttypeid exact software)) and (relprojectid exact "+this.projectId+"))";
if (typeof document !== 'undefined') {
this.element.nativeElement.scrollIntoView();
}
});
}
actionsAfterLoadId(){
this.publications_dynamic =
"<script type=\"text/javascript\">"
+ "\n<!--"
+ "\ndocument.write('<div id=\"oa_widget\"></div>');"
+ "\ndocument.write('<script type=\"text/javascript\""
+ "\nsrc=\"https://beta.openaire.eu/index.php?option=com_openaire&view=widget&format=raw&projectId="
+ this.projectId + "&type=publication\"></script>');"
+ "\n-->"
+ "\n</script>";
this.datasets_dynamic =
"<script type=\"text/javascript\">"
+ "\n<!--"
+ "\ndocument.write('<div id=\"oa_widget\"></div>');"
+ "\ndocument.write('<script type=\"text/javascript\""
+ "\nsrc=\"https://beta.openaire.eu/index.php?option=com_openaire&view=widget&format=raw&projectId="
+ this.projectId + "&type=dataset\"></script>');"
+ "\n-->"
+ "\n</script>";
this.software_dynamic =
"<script type=\"text/javascript\">"
+ "\n<!--"
+ "\ndocument.write('<div id=\"oa_widget\"></div>');"
+ "\ndocument.write('<script type=\"text/javascript\""
+ "\nsrc=\"https://beta.openaire.eu/index.php?option=com_openaire&view=widget&format=raw&projectId="
+ this.projectId + "&type=software\"></script>');"
+ "\n-->"
+ "\n</script>";
this.getProjectInfo(this.projectId);
this.searchPublications();
this.fetchDatasets.getNumForEntity("project", this.projectId, this.properties);
this.fetchSoftware.getNumForEntity("project", this.projectId, this.properties);
}
ngOnDestroy() {
this.sub.unsubscribe();
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
if(this.infoSub) {
this.infoSub.unsubscribe();
}
if(this.downloadFilePiwikSub) {
this.downloadFilePiwikSub.unsubscribe();
}
}
private createClipboard() {
if(typeof window !== 'undefined') {
let publ_clipboard, datasets_clipboard, software_clipboard;
let Clipboard;
Clipboard = require('clipboard');
publ_clipboard = new Clipboard('.publ_clipboard_btn');
datasets_clipboard = new Clipboard('.datasets_clipboard_btn');
software_clipboard = new Clipboard('.software_clipboard_btn');
}
}
private searchPublications() {
this.fetchPublications.getResultsForEntity("project", this.projectId, 1, 10, this.properties);
this.linkToSearchPublications = this.properties.searchLinkToAdvancedPublications;// + "?project=" + this.projectId+"&pr=and";
if(this.fetchPublications.searchUtils.totalResults > 0) {
//this.activeTab = "Publications";
} else {
//this.searchDatasetsInit();
}
}
private searchDatasets() {
this.fetchDatasets.getResultsForEntity("project", this.projectId, 1, 10, this.properties);
this.linkToSearchDatasets = this.properties.searchLinkToAdvancedDatasets;// + "?project=" + this.projectId+"&pr=and";
this.reloadDatasets = false;
//this.activeTab = "Research Data";
}
private searchSoftware() {
this.fetchSoftware.getResultsForEntity("project", this.projectId, 1, 10, this.properties);
this.linkToSearchSoftware = this.properties.searchLinkToAdvancedSoftware;
this.reloadSoftware = false;
}
public searchDatasetsInit() {
console.info("searchDatasetsInit");
if(this.reloadDatasets && this.fetchDatasets.searchUtils.totalResults > 0) {
this.searchDatasets();
} else if(this.fetchDatasets.searchUtils.totalResults == 0) {
//this.statsClicked=true;
//this.activeTab = "Statistics";
}
}
public searchSoftwareInit() {
console.info("searchSoftwareInit");
if(this.reloadSoftware && this.fetchSoftware.searchUtils.totalResults > 0) {
this.searchSoftware();
}
}
private getProjectInfo (id:string) {
this.warningMessage = '';
this.errorMessage=""
this.showLoading = true;
var funder = "EC";
var grantId ="654142";
this.infoSub = this._projectService.getProjectInfo(id, this.properties).subscribe(
data => {
this.projectInfo = data;
this.actionsAfterGettingProjectInfo();
},
err => {
console.log(err);
this.errorMessage = 'No project found';
this.showLoading = false;
}
);
}
private getProjectInfoByGrantId (grantId:string, funder:string) {
this.warningMessage = '';
this.errorMessage=""
this.showLoading = true;
this._projectService.getProjectInfoByGrantId(grantId,funder,this.properties).subscribe(
data =>{
this.projectInfo = data;
this.actionsAfterGettingProjectInfo();
this.projectId = this.projectInfo.id;
this.actionsAfterLoadId();
},
err => {
console.log(err);
this.errorMessage = 'No project found';
this.showLoading = false;
}
);
}
actionsAfterGettingProjectInfo(){
this.projectName = this.projectInfo.acronym;
if(this.projectName == undefined || this.projectName == '') {
this.projectName = this.projectInfo.title;
}
this.updateTitle(this.projectName);
this.updateDescription("project, funding, open access, publications, research data, "+this.projectName+ ","+this.projectInfo.funder);
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
this.piwiksub = this._piwikService.trackView(this.properties, this.projectName, this.piwikSiteId).subscribe();
}
this.project= { funderId: "", funderName: this.projectInfo.funder, projectId: this.projectId, projectName: this.projectInfo.title, projectAcronym: this.projectInfo.acronym, startDate: this.projectInfo.startDate, endDate: this.projectInfo.endDate };
this.viewsFrameUrl = this.properties.framesAPIURL+'merge.php?com=query&data=[{"query":"projRepoViews","projTitle":"'+this.projectId+'","table":"","fields":[{"fld":"sum","agg":"sum","type":"column","yaxis":1,"c":false}],"xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,"sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"","xaxistitle":"","yaxisheaders":["Monthly views"],"generalxaxis":"","theme":0,"in":[]}]&info_types=["column"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]&style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)&colors[]=rgba(124, 181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125, 1)&colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233, 1)&colors[]=rgba(241, 92, 128, 1)&colors[]=rgba(228, 211, 84, 1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91, 1)&colors[]=rgba(145, 232, 225, 1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false';
this.downloadsFrameUrl = this.properties.framesAPIURL+'merge.php?com=query&data=[{"query":"projRepoDownloads","projTitle":"'+this.projectId+'","table":"","fields":[{"fld":"sum","agg":"sum","type":"column","yaxis":1,"c":false}],"xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,"sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"","xaxistitle":"","yaxisheaders":["Monthly downloads"],"generalxaxis":"","theme":0,"in":[]}]&info_types=["column"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]&style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)&colors[]=rgba(124, 181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125, 1)&colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233, 1)&colors[]=rgba(241, 92, 128, 1)&colors[]=rgba(228, 211, 84, 1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91, 1)&colors[]=rgba(145, 232, 225, 1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false';
//stats tab charts
this.chartScientificResultsUrl='https://beta.openaire.eu/stats/chart.php?com=query&persistent=false&data={"query":"projScient","projTitle":"'+this.projectId+'", "table": "result", "fields": [{"fld": "number", "agg": "count", "type": "spline", "yaxis":1, "c":false}], "xaxis":{"name": "result_classifications-type", "agg": "avg"}, "group": "", "color": "", "type": "chart", "size":30, "sort": "xaxis", "xStyle":{"r": "-", "s": "-", "l": "-", "ft": "-", "wt": "-"}, "yaxisheaders": [""], "fieldsheaders": ["Research Results"], "in": [], "filters": [{"name": "result_datasources-datasource-name", "values": [" "], "to": "-1"}], "having": [], "incfilters": [], "inchaving": [], "title": "", "subtitle": "", "xaxistitle": ""}&w=600&h=250';
this.chartAccessModeUrl='https://beta.openaire.eu/stats/chart.php?com=query&persistent=false&data={"query":"projOA","projTitle":"'+this.projectId+'", "table": "result", "fields": [{"fld": "number", "agg": "count", "type": "pie", "yaxis":1, "c":false}], "xaxis":{"name": "result_classifications-type", "agg": "avg"}, "group": "", "color": "", "type": "chart", "size":30, "sort": "xaxis", "xStyle":{"r": "-", "s": "-", "l": "-", "ft": "-", "wt": "-"}, "yaxisheaders": [""], "fieldsheaders": ["Research Results"], "in": [], "filters": [{"name": "result_datasources-datasource-name", "values": [" "], "to": "-1"}], "having": [], "incfilters": [], "inchaving": [], "title": "", "subtitle": "", "xaxistitle": ""}&w=600&h=250';
this.chartDatasourcesUrl= 'https://beta.openaire.eu/stats/chart.php?com=query&persistent=false&data={"query":"projPubsRepos","projTitle":"'+this.projectId+'", "table": "result", "fields": [{"fld": "number", "agg": "count", "type": "bar", "yaxis":1, "c":false}], "xaxis":{"name": "result_classifications-type", "agg": "avg"}, "group": "", "color": "", "type": "chart", "size":30, "sort": "xaxis", "xStyle":{"r": "-", "s": "-", "l": "-", "ft": "-", "wt": "-"}, "yaxisheaders": [""], "fieldsheaders": ["Research Results"], "in": [], "filters": [{"name": "result_datasources-datasource-name", "values": [" "], "to": "-1"}], "having": [], "incfilters": [], "inchaving": [], "title": "", "subtitle": "", "xaxistitle": ""}&w=600&h=250';
this.showLoading = false;
}
public downloadfile(url:string){
this.openLoading();
this.setMessageLoading("Downloading CSV file");
this._reportsService.downloadCSVFile(url).subscribe(
data => {
this.closeLoading();
window.open(window.URL.createObjectURL(data));
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
}
},
error => {
console.log("Error downloading the file.");
this.closeLoading();
this.confirmOpenCsvError();
},
() => console.log('Completed file download.')
);
}
/*
showHTML(){
let info:string = "<h1>Publications of Project ";
if(this.projectInfo.title != undefined && this.projectInfo.title != "") {
info += this.projectInfo.title;
}
if((this.projectInfo.title != undefined && this.projectInfo.title != "") &&
((this.projectInfo.acronym != undefined && this.projectInfo.acronym != "") ||
(this.projectInfo.callIdentifier != undefined && this.projectInfo.callIdentifier != ""))) {
info += "(";
}
if(this.projectInfo.acronym != undefined && this.projectInfo.acronym != "") {
info += this.projectInfo.acronym + " - ";
}
if(this.projectInfo.callIdentifier != undefined && this.projectInfo.callIdentifier != "") {
info += this.projectInfo.callIdentifier;
}
if((this.projectInfo.title != undefined && this.projectInfo.title != "") &&
((this.projectInfo.acronym != undefined && this.projectInfo.acronym != "") ||
(this.projectInfo.callIdentifier != undefined && this.projectInfo.callIdentifier != ""))) {
info += ")";
}
info +="</h1>";
info += "<h3>"+this.fetchPublications.searchUtils.totalResults+" publications</h3>";
let htmlParams = 'resources?format=html&page=0&size='+this.fetchPublications.searchUtils.totalResults+'&type=publications&query=(((oaftype exact result) and (resulttypeid exact publication)) and (relprojectid exact "'+this.projectId+'"))';
this._reportsService.downloadHTMLFile(this.downloadURLAPI+htmlParams, info)
.subscribe(data => this.funct(data),
error => console.log("Error downloading the file."),
() => console.log('Completed file download.'));
}
funct(data) {
var win = window.open(window.URL.createObjectURL(data));
}
*/
public metricsResults($event) {
this.totalViews = $event.totalViews;
this.totalDownloads = $event.totalDownloads;
this.pageViews = $event.pageViews;
}
private updateDescription(description:string) {
this._meta.updateTag({content:description},"name='description'");
this._meta.updateTag({content:description},"property='og:description'");
}
private updateTitle(title:string) {
var _prefix ="OpenAIRE | ";
var _title = _prefix + ((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 openLoading(){
if(this.loading){
this.loading.open();
}
}
private closeLoading(){
if(this.loading){
this.loading.close();
}
}
private setMessageLoading(message: string){
if(this.loading){
this.loading.message = message;
}
}
public confirmOpenCsvError(){
this.alertCsvError.cancelButton = false;
this.alertCsvError.okButton = true;
this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
this.alertCsvError.okButtonText = "OK";
this.alertCsvError.open();
}
}