Project

General

Profile

1
import {Component, Input}               from '@angular/core';
2
import {ActivatedRoute, Params, Router} from '@angular/router';
3
import {Title, Meta}                  from '@angular/platform-browser';
4

    
5
import {Observable}                     from 'rxjs/Observable';
6

    
7
import {EnvProperties}                  from '../../utils/properties/env-properties';
8

    
9
import {HtmlProjectReportService}       from './htmlProjectReport.service';
10
import {ProjectService}                 from '../project/project.service';
11
import {PiwikService}                   from '../../utils/piwik/piwik.service';
12
import { SEOService } from '../../sharedComponents/SEO/SEO.service';
13

    
14
declare var UIkit: any;
15

    
16
@Component({
17
    selector: 'htmlProjectReport',
18
    template: `
19
    <div id="tm-main" class=" uk-section  uk-margin-small-top tm-middle"   >
20
      <div uk-grid uk-grid>
21
       <div class="tm-main uk-width-1-1@s uk-width-1-1@m  uk-width-1-1@l uk-row-first ">
22

    
23
      <div class="uk-container uk-margin-top">
24
          <div *ngIf="warningMessage" class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
25
          <div [style.display]="showLoading ? 'inline' : 'none'" class="uk-animation-fade uk-margin-large-top  uk-width-1-1" role="alert"><span class="loading-gif  uk-align-center" ></span></div>
26

    
27
          <div *ngIf="!showLoading && !warningMessage">
28
            <h1 *ngIf="header1">{{header1}}</h1>
29
            <h2 *ngIf="header1 || htmlResult">{{header2}}</h2>
30

    
31
            <div class="uk-clearfix uk-margin-bottom">
32
              <button *ngIf="htmlResult" class="uk-icon-clipboard uk-button uk-button-primary clipBtn uk-float-right" (click)="copied()">
33
                  Copy to clipboard
34
              </button>
35
            </div>
36
            <!--div class="uk-panel-scrollable custom-html-table-height" *ngIf="htmlResult" id="clipboard" [innerHTML]="htmlResult"></div-->
37
            <div class="uk-overflow-auto custom-html-table-height" *ngIf="htmlResult" id="clipboard" [innerHTML]="htmlResult"></div>
38

    
39
            <div *ngIf="errorMessage" class="uk-alert uk-alert-danger" role="alert">{{errorMessage}}</div>
40
          </div>
41
      </div>
42
      </div>
43
    </div>
44
  </div>
45

    
46
    `
47
 })
48
export class HtmlProjectReportComponent{
49
  @Input() piwikSiteId = null;
50
    private projectId: string;
51
    private totalResults: number = 10;
52
    private resultsType: string = "publication";
53

    
54
    public header1: string = "";
55
    public header2: string = "";
56
    public htmlResult: string = "";
57

    
58
    public sub: any; piwiksub: any;
59
    public subHTML: any;
60
    public subHTMLInfo: any;
61

    
62
    public warningMessage: string = "";
63
    public errorMessage: string = "";
64
    public showLoading: boolean = true;
65
    properties:EnvProperties;
66

    
67
    constructor ( private  route: ActivatedRoute,
68
                  private htmlService: HtmlProjectReportService,
69
                  private _piwikService:PiwikService,
70
                  private _projectService: ProjectService,
71
                  private _meta: Meta,
72
                  private _title: Title,
73
                  private _router: Router,
74
                private seoService: SEOService) {
75
    }
76

    
77
    ngOnInit() {
78
      this.route.data
79
        .subscribe((data: { envSpecific: EnvProperties }) => {
80
           this.properties = data.envSpecific;
81
           this.updateUrl(data.envSpecific.baseLink+this._router.url);
82
           this.seoService.createLinkForCanonicalURL(this.properties.baseLink+this._router.url);
83

    
84
        });
85
        this.sub =  this.route.queryParams.subscribe(params => {
86
            this.projectId = params['projectId'];
87

    
88
            if (params['size'] == parseInt(params['size'], 10)) {
89
              this.totalResults = params['size'];
90
            } else {
91
              this.showLoading = false;
92
    		      this.warningMessage="Requested size is not an integer";
93
            }
94

    
95
            if(params['type'] && (params['type'] == "publication" || params['type'] == "dataset" || params['type'] == "software" || params['type'] == "other")){
96
              if(params['type'] == "publication") {
97
                this.resultsType = 'publication';
98
              } else if(params['type'] == "dataset") {
99
                this.resultsType = 'research data';
100
              } else if(params['type'] == "software") {
101
                this.resultsType = 'software';
102
              } else if(params['type'] == "other") {
103
                this.resultsType = "other research product";
104
              }
105

    
106
              var title = "Project's "+this.resultsType+" report";
107
              var description = "project, project "+ this.resultsType +" report, funding, open access, publications, research data, software, other research products";
108
              this.updateTitle(title);
109
              this.updateDescription(description);
110

    
111

    
112
            } else {
113
              this.showLoading = false;
114
    		      this.warningMessage="Requested type should be publication or research data or software or other research product";
115
            }
116

    
117
            //showLoading is true if no warnings
118
            if(this.showLoading) {
119
              if(this.projectId) {
120
                this.createHeaders();
121
              } else {
122
                this.showLoading = false;
123
      		      this.warningMessage="No valid project id";
124
              }
125
            }
126
        });
127
    }
128

    
129
    ngOnDestroy() {
130
      this.sub.unsubscribe();
131
      if(this.piwiksub){
132
        this.piwiksub.unsubscribe();
133
      }
134
      if(this.subHTML) {
135
        this.subHTML.unsubscribe();
136
      }
137
      if(this.subHTMLInfo) {
138
        this.subHTMLInfo.unsubscribe();
139
      }
140
    }
141

    
142
    private createHeaders() {
143
        this.subHTMLInfo = this._projectService.getHTMLInfo(this.projectId, this.properties).subscribe(
144
            data => {
145
                this.createHeader1(data);
146
                if(data.acronym) {
147
                  this.updateTitle(data.acronym+" "+this.resultsType+" report");
148
                } else if(data.title){
149
                  this.updateTitle(data.title+" "+this.resultsType+" report");
150
                }
151
                if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
152
                  this.piwiksub = this._piwikService.trackView(this.properties, ((data.acronym)?data.acronym:data.title)+" "+this.resultsType+" report", this.piwikSiteId).subscribe();
153
                }
154
            },
155
            err => {
156
                console.log(err);
157
                this.createClipboard();
158
            }
159
        );
160

    
161
        if(this.resultsType == "publication") {
162
          this.header2 += this.totalResults.toLocaleString('en-US') + " publications";
163
        } else if(this.resultsType == "research data") {
164
          this.header2 += this.totalResults.toLocaleString('en-US') + " research data";
165
        } else if(this.resultsType == "software") {
166
          this.header2 += this.totalResults.toLocaleString('en-US') + " software";
167
        } else if(this.resultsType == "other research product") {
168
          this.header2 += this.totalResults.toLocaleString('en-US') + " other";
169
        }
170
    }
171

    
172
    private createClipboard() {
173
        let intro: string = '<!doctype html>';
174
        intro += '<html lang="en-gb" dir="ltr" vocab="http://schema.org/">';
175
        intro += '<head>';
176
        intro += '<title>'+this.header1+'</title>'
177
        intro += '</head>';
178

    
179
        if (typeof window !== 'undefined') {
180
            this.subHTML = this.htmlService.getHTML(this.projectId, this.totalResults, this.resultsType, this.properties.csvAPIURL).subscribe(
181
                data => {
182
                    //let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>'+data+'</body></html>';
183
                    let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>';
184
                    body += "<table><thead><tr> <th>Title</th><th>Authors</th><th>Publication Year</th><th>DOI</th><th>Permanent Identifier</th><th>Publication type</th><th>Journal</th><th>Project Name (GA Number)</th><th>Access Mode</th></tr></thead><tbody>"+data+"</tbody></table>";
185
                    body += '</body></html>';
186

    
187
                    //this.htmlResult = data;
188
                    this.htmlResult = "<table><thead><tr> <th>Title</th><th>Authors</th><th>Publication Year</th><th>DOI</th><th>Permanent Identifier</th><th>Publication type</th><th>Journal</th><th>Project Name (GA Number)</th><th>Access Mode</th></tr></thead><tbody>"+data+"</tbody></table>";
189

    
190
                    let clipboard;
191
                    let Clipboard;
192
                    Clipboard = require('clipboard');
193
                    clipboard = new Clipboard('.clipBtn', {
194
                        /*target: function(trigger) {
195
                            return document.getElementById("clipboard");
196
                        }*/
197
                        text: function(trigger) {
198
                            return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo";
199
                        }
200
                    });
201

    
202
                    this.showLoading = false;
203
                },
204
                err => {
205
                    console.log(err);
206
                    this.errorMessage = 'Service not available';
207
                    this.showLoading = false;
208
                }
209
            );
210
        }
211
    }
212

    
213
    createHeader1(data: {"title": string, "acronym": string, "callIdentifier": string}) {
214
        if(this.resultsType == "publication") {
215
          this.header1 += "Publications";
216
        } else if(this.resultsType == "research data") {
217
          this.header1 += "Research Data";
218
        } else if(this.resultsType == "software") {
219
          this.header1 += "Software";
220
        } else if(this.resultsType == "other research product") {
221
          this.header1 += "Other Research Products";
222
        }
223

    
224
        if(data != undefined) {
225
            if(data.title != undefined && data.title != "") {
226
                this.header1 += data.title;
227
            }
228
            if((data.title != undefined && data.title != "") &&
229
             ((data.acronym != undefined && data.acronym != "") ||
230
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
231
                    this.header1 += "(";
232
            }
233
            if(data.acronym != undefined && data.acronym != "") {
234
                this.header1 += data.acronym + " - ";
235
            }
236
            if(data.callIdentifier != undefined && data.callIdentifier != "") {
237
                this.header1 += data.callIdentifier;
238
            }
239
            if((data.title != undefined && data.title != "") &&
240
             ((data.acronym != undefined && data.acronym != "") ||
241
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
242
                    this.header1 += ")";
243
            }
244
        }
245

    
246
        this.createClipboard();
247
    }
248

    
249
    public copied() {
250
      UIkit.notification({
251
          message : '<strong>Raw html is copied. Please paste it on an html file.<strong>',
252
          status  : 'success',
253
          timeout : 3000,
254
          pos     : 'top-center'
255
      });
256
    }
257

    
258
    private updateDescription(description:string){
259
      this._meta.updateTag({content:description},"name='description'");
260
      this._meta.updateTag({content:description},"property='og:description'");
261
    }
262
    private updateTitle(title:string){
263
      var _prefix ="OpenAIRE | ";
264
      var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
265
      this._title.setTitle(_title);
266
      this._meta.updateTag({content:_title},"property='og:title'");
267
    }
268
    private updateUrl(url:string){
269
      this._meta.updateTag({content:url},"property='og:url'");
270
    }
271
}
(2-2/4)