import {Component, Input} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Meta, Title} from '@angular/platform-browser'; import {EnvProperties} from '../../utils/properties/env-properties'; import {HtmlProjectReportService} from './htmlProjectReport.service'; import {ProjectService} from '../project/project.service'; import {PiwikService} from '../../utils/piwik/piwik.service'; import {SEOService} from '../../sharedComponents/SEO/SEO.service'; import {HelperService} from "../../utils/helper/helper.service"; import {Subscriber} from "rxjs"; import {properties} from "../../../../environments/environment"; declare var UIkit: any; @Component({ selector: 'htmlProjectReport', template: `
{{header1}}
{{header2}}
` }) export class HtmlProjectReportComponent { @Input() communityId = null; private projectId: string; private totalResults: number = 10; private resultsType: string = "publication"; public header1: string = ""; public header2: string = ""; public htmlResult: string = ""; subscriptions = []; public warningMessage: string = ""; public errorMessage: string = ""; public showLoading: boolean = true; properties: EnvProperties; public pageContents = null; public divContents = null; constructor(private route: ActivatedRoute, private htmlService: HtmlProjectReportService, private _piwikService: PiwikService, private _projectService: ProjectService, private _meta: Meta, private _title: Title, private _router: Router, private helper: HelperService, private seoService: SEOService) { } ngOnInit() { this.properties = properties; //this.getDivContents(); this.getPageContents(); this.updateUrl(this.properties.domain + this.properties.baseLink + this._router.url); this.seoService.createLinkForCanonicalURL(this.properties.domain + this.properties.baseLink + this._router.url); this.subscriptions.push(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" || params['type'] == "other")) { if (params['type'] == "publication") { this.resultsType = 'publication'; } else if (params['type'] == "dataset") { this.resultsType = 'research data'; } else if (params['type'] == "software") { this.resultsType = 'software'; } else if (params['type'] == "other") { this.resultsType = "other research product"; } var title = "Project's " + this.resultsType + " report"; var description = "project, project " + this.resultsType + " report, funding, open access, publications, research data, software, other research products"; this.updateTitle(title); this.updateDescription(description); } else { this.showLoading = false; this.warningMessage = "Requested type should be publication or research data or software or other research product"; } //showLoading is true if no warnings if (this.showLoading) { if (this.projectId) { this.createHeaders(); } else { this.showLoading = false; this.warningMessage = "No valid project id"; } } })); } private getPageContents() { if(this.communityId) { this.subscriptions.push(this.helper.getPageHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => { this.pageContents = contents; })); } } private getDivContents() { if(this.communityId) { this.subscriptions.push(this.helper.getDivHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => { this.divContents = contents; })); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } private createHeaders() { this.subscriptions.push(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"); } this.subscriptions.push(this._piwikService.trackView(this.properties, ((data.acronym) ? data.acronym : data.title) + " " + this.resultsType + " report").subscribe()); }, err => { this.handleError("Error getting html information for project id: " + this.projectId, err); //console.log(err); this.createClipboard(); } )); if (this.resultsType == "publication") { this.header2 += this.totalResults.toLocaleString('en-US') + " publications"; } else if (this.resultsType == "research data") { this.header2 += this.totalResults.toLocaleString('en-US') + " research data"; } else if (this.resultsType == "software") { this.header2 += this.totalResults.toLocaleString('en-US') + " software"; } else if (this.resultsType == "other research product") { this.header2 += this.totalResults.toLocaleString('en-US') + " other"; } } private createClipboard() { let intro: string = ''; intro += ''; intro += ''; intro += '' + this.header1 + '' intro += ''; if (typeof window !== 'undefined') { this.subscriptions.push(this.htmlService.getHTML(this.projectId, 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 += "" + data + "
TitleAuthorsPublication YearDOIPermanent IdentifierPublication typeJournalProject Name (GA Number)Access Mode
"; body += ''; //this.htmlResult = data; this.htmlResult = "" + data + "
TitleAuthorsPublication YearDOIPermanent IdentifierPublication typeJournalProject Name (GA Number)Access Mode
"; let clipboard; let Clipboard; Clipboard = require('clipboard'); clipboard = new Clipboard('.clipBtn', { /*target: function(trigger) { return document.getElementById("clipboard"); }*/ text: function (trigger) { return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo"; } }); this.showLoading = false; }, err => { //console.log(err); this.handleError("Error getting html for id: " + this.projectId, err); this.errorMessage = 'Service not available'; this.showLoading = false; } )); } } createHeader1(data: { "title": string, "acronym": string, "callIdentifier": string }) { if (this.resultsType == "publication") { this.header1 += "Publications"; } else if (this.resultsType == "research data") { this.header1 += "Research Data"; } else if (this.resultsType == "software") { this.header1 += "Software"; } else if (this.resultsType == "other research product") { this.header1 += "Other Research Products"; } if (data != undefined) { if (data.title != undefined && data.title != "") { this.header1 += data.title; } if ((data.title != undefined && data.title != "") && ((data.acronym != undefined && data.acronym != "") || (data.callIdentifier != undefined && data.callIdentifier != ""))) { this.header1 += "("; } if (data.acronym != undefined && data.acronym != "") { this.header1 += data.acronym + " - "; } if (data.callIdentifier != undefined && data.callIdentifier != "") { this.header1 += data.callIdentifier; } if ((data.title != undefined && data.title != "") && ((data.acronym != undefined && data.acronym != "") || (data.callIdentifier != undefined && data.callIdentifier != ""))) { this.header1 += ")"; } } this.createClipboard(); } public copied() { UIkit.notification({ message: 'Raw html is copied. Please paste it on an html file.', status: 'success', timeout: 3000, pos: 'top-center' }); } 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 = ""; if(!this.communityId) { _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 handleError(message: string, error) { console.error("Html Project Report Page: " + message, error); } }