import {Component} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {HtmlProjectReportService} from './htmlProjectReport.service';
import {ProjectService} from '../project/project.service';
import { Meta} from '../../sharedComponents/metaService';
import {PiwikService} from '../../utils/piwik/piwik.service';
import{EnvProperties} from '../../utils/properties/env-properties';
declare var UIkit: any;
@Component({
selector: 'htmlProjectReport',
template: `
{{warningMessage}}
{{header1}}
{{header2}}
Copy to clipboard
{{errorMessage}}
`
})
export class HtmlProjectReportComponent{
private projectId: string;
private totalResults: number = 10;
private resultsType: string = "publication";
public header1: string = "";
public header2: string = "";
public htmlResult: string = "";
public sub: any; piwiksub: any;
public subHTML: any;
public subHTMLInfo: any;
public warningMessage: string = "";
public errorMessage: string = "";
public showLoading: boolean = true;
properties:EnvProperties;
constructor ( private route: ActivatedRoute,
private htmlService: HtmlProjectReportService,
private _piwikService:PiwikService,
private _projectService: ProjectService,
private _meta: Meta, 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.projectId = params['projectId'];
if (params['size'] == parseInt(params['size'], 10)) {
this.totalResults = params['size'];
} else {
this.showLoading = false;
this.warningMessage="Requested size is not an integer";
}
if(params['type'] && (params['type'] == "publication" || params['type'] == "dataset" || params['type'] == "software")){
if(params['type'] == "publication") {
this.resultsType = 'publication';
} else if(params['type'] == "dataset") {
this.resultsType = 'research data';
} else if(params['type'] == "software") {
this.resultsType = 'software';
}
this.updateTitle("Project's "+this.resultsType+" report");
this.updateDescription("project, project "+ this.resultsType +" report, funding, open access, publications, research data, software");
} else {
this.showLoading = false;
this.warningMessage="Requested type should be publication or research data or software";
}
//showLoading is true if no warnings
if(this.showLoading) {
if(this.projectId) {
this.createHeaders();
} else {
this.showLoading = false;
this.warningMessage="No valid project id";
}
}
});
}
ngOnDestroy() {
this.sub.unsubscribe();
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
if(this.subHTML) {
this.subHTML.unsubscribe();
}
if(this.subHTMLInfo) {
this.subHTMLInfo.unsubscribe();
}
}
private createHeaders() {
this.subHTMLInfo = this._projectService.getHTMLInfo(this.projectId, this.properties).subscribe(
data => {
this.createHeader1(data);
if(data.acronym) {
this.updateTitle(data.acronym+" "+this.resultsType+" report");
} else if(data.title){
this.updateTitle(data.title+" "+this.resultsType+" report");
}
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
this.piwiksub = this._piwikService.trackView(this.properties, ((data.acronym)?data.acronym:data.title)+" "+this.resultsType+" report").subscribe();
}
},
err => {
console.log(err);
this.createClipboard();
}
);
if(this.resultsType == "publication") {
this.header2 += this.totalResults + " publications";
} else if(this.resultsType == "research data") {
this.header2 += this.totalResults + " research data";
} else if(this.resultsType == "software") {
this.header2 += this.totalResults + " software";
}
}
private createClipboard() {
let intro: string = '';
intro += '';
intro += '';
intro += ''+this.header1+' '
intro += '';
if (typeof window !== 'undefined') {
this.subHTML = this.htmlService.getHTML(this.projectId, this.totalResults, this.resultsType, this.properties.csvAPIURL).subscribe(
data => {
//let body: string = intro+''+this.header1+' '+this.header2+' '+data+'';
let body: string = intro+''+this.header1+' '+this.header2+' ';
body += " Title Authors Publication Year DOI Permanent Identifier Publication type Journal Project Name (GA Number) Access Mode "+data+"
";
body += '