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

    
13
declare var UIkit: any;
14

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

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

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

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

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

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

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

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

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

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

    
75
    ngOnInit() {
76
      this.route.data
77
        .subscribe((data: { envSpecific: EnvProperties }) => {
78
           this.properties = data.envSpecific;
79
           this.updateUrl(data.envSpecific.baseLink+this._router.url);
80

    
81
        });
82
        this.sub =  this.route.queryParams.subscribe(params => {
83
            this.projectId = params['projectId'];
84

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

    
92
            if(params['type'] && (params['type'] == "publication" || params['type'] == "dataset" || params['type'] == "software")){
93
              if(params['type'] == "publication") {
94
                this.resultsType = 'publication';
95
              } else if(params['type'] == "dataset") {
96
                this.resultsType = 'research data';
97
              } else if(params['type'] == "software") {
98
                this.resultsType = 'software';
99
              }
100

    
101
              var title = "Project's "+this.resultsType+" report";
102
              var description = "project, project "+ this.resultsType +" report, funding, open access, publications, research data, software";
103
              this.updateTitle(title);
104
              this.updateDescription(description);
105

    
106
            } else {
107
              this.showLoading = false;
108
    		      this.warningMessage="Requested type should be publication or research data or software";
109
            }
110

    
111
            //showLoading is true if no warnings
112
            if(this.showLoading) {
113
              if(this.projectId) {
114
                this.createHeaders();
115
              } else {
116
                this.showLoading = false;
117
      		      this.warningMessage="No valid project id";
118
              }
119
            }
120
        });
121
    }
122

    
123
    ngOnDestroy() {
124
      this.sub.unsubscribe();
125
      if(this.piwiksub){
126
        this.piwiksub.unsubscribe();
127
      }
128
      if(this.subHTML) {
129
        this.subHTML.unsubscribe();
130
      }
131
      if(this.subHTMLInfo) {
132
        this.subHTMLInfo.unsubscribe();
133
      }
134
    }
135

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

    
155
        if(this.resultsType == "publication") {
156
          this.header2 += this.totalResults.toLocaleString('en-US') + " publications";
157
        } else if(this.resultsType == "research data") {
158
          this.header2 += this.totalResults.toLocaleString('en-US') + " research data";
159
        } else if(this.resultsType == "software") {
160
          this.header2 += this.totalResults.toLocaleString('en-US') + " software";
161
        }
162
    }
163

    
164
    private createClipboard() {
165
        let intro: string = '<!doctype html>';
166
        intro += '<html lang="en-gb" dir="ltr" vocab="http://schema.org/">';
167
        intro += '<head>';
168
        intro += '<title>'+this.header1+'</title>'
169
        intro += '</head>';
170

    
171
        if (typeof window !== 'undefined') {
172
            this.subHTML = this.htmlService.getHTML(this.projectId, this.totalResults, this.resultsType, this.properties.csvAPIURL).subscribe(
173
                data => {
174
                    //let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>'+data+'</body></html>';
175
                    let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>';
176
                    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>";
177
                    body += '</body></html>';
178

    
179
                    //this.htmlResult = data;
180
                    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>";
181

    
182
                    let clipboard;
183
                    let Clipboard;
184
                    Clipboard = require('clipboard');
185
                    clipboard = new Clipboard('.clipBtn', {
186
                        /*target: function(trigger) {
187
                            return document.getElementById("clipboard");
188
                        }*/
189
                        text: function(trigger) {
190
                            return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo";
191
                        }
192
                    });
193

    
194
                    this.showLoading = false;
195
                },
196
                err => {
197
                    console.log(err);
198
                    this.errorMessage = 'Service not available';
199
                    this.showLoading = false;
200
                }
201
            );
202
        }
203
    }
204

    
205
    createHeader1(data: {"title": string, "acronym": string, "callIdentifier": string}) {
206
        if(this.resultsType == "publication") {
207
          this.header1 += "Publications";
208
        } else if(this.resultsType == "research data") {
209
          this.header1 += "Research Data";
210
        } else if(this.resultsType == "software") {
211
          this.header1 += "Software";
212
        }
213

    
214
        if(data != undefined) {
215
            if(data.title != undefined && data.title != "") {
216
                this.header1 += data.title;
217
            }
218
            if((data.title != undefined && data.title != "") &&
219
             ((data.acronym != undefined && data.acronym != "") ||
220
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
221
                    this.header1 += "(";
222
            }
223
            if(data.acronym != undefined && data.acronym != "") {
224
                this.header1 += data.acronym + " - ";
225
            }
226
            if(data.callIdentifier != undefined && data.callIdentifier != "") {
227
                this.header1 += data.callIdentifier;
228
            }
229
            if((data.title != undefined && data.title != "") &&
230
             ((data.acronym != undefined && data.acronym != "") ||
231
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
232
                    this.header1 += ")";
233
            }
234
        }
235

    
236
        this.createClipboard();
237
    }
238

    
239
    public copied() {
240
      UIkit.notification({
241
          message : '<strong>Raw html is copied. Please paste it on an html file.<strong>',
242
          status  : 'success',
243
          timeout : 3000,
244
          pos     : 'top-center'
245
      });
246
    }
247

    
248
    private updateDescription(description:string){
249
      this._meta.updateTag({content:description},"name='description'");
250
      this._meta.updateTag({content:description},"property='og:description'");
251
    }
252
    private updateTitle(title:string){
253
      var _prefix ="OpenAIRE | ";
254
      var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
255
      this._title.setTitle(_title);
256
      this._meta.updateTag({content:_title},"property='og:title'");
257
    }
258
    private updateUrl(url:string){
259
      this._meta.updateTag({content:url},"property='og:url'");
260
    }
261
}
(2-2/4)