Project

General

Profile

1
import {Component} from '@angular/core';
2
import {Observable} from 'rxjs/Observable';
3
import {ActivatedRoute, Params, Router} from '@angular/router';
4
import {HtmlProjectReportService} from './htmlProjectReport.service';
5
import {ProjectService} from '../project/project.service';
6
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
7
import { Meta} from '../../../angular2-meta';
8
import {PiwikService} from '../../utils/piwik/piwik.service';
9

    
10
declare var UIkit: any;
11

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

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

    
23
          <div *ngIf="!showLoading && !warningMessage">
24
            <h1 *ngIf="header1">{{header1}}</h1>
25
            <h2 *ngIf="header1 || htmlResult">{{header2}}</h2>
26

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

    
35
            <div *ngIf="errorMessage" class="uk-alert uk-alert-danger" role="alert">{{errorMessage}}</div>
36
          </div>
37
      </div>
38
      </div>
39
    </div>
40
  </div>
41

    
42
    `
43
 })
44
export class HtmlProjectReportComponent{
45
    private projectId: string;
46
    private totalResults: number = 10;
47
    private resultsType: string = "publication";
48

    
49
    public header1: string = "";
50
    public header2: string = "";
51
    public htmlResult: string = "";
52

    
53
    public sub: any; piwiksub: any;
54
    public subHTML: any;
55
    public subHTMLInfo: any;
56

    
57
    public warningMessage: string = "";
58
    public errorMessage: string = "";
59
    public showLoading: boolean = true;
60

    
61
    constructor ( private  route: ActivatedRoute,
62
                  private htmlService: HtmlProjectReportService,
63
                  private _piwikService:PiwikService,
64
                  private _projectService: ProjectService,
65
                  private _meta: Meta, private _router: Router) {
66
          this.updateUrl(OpenaireProperties.getBaseLink()+this._router.url);
67
    }
68

    
69
    ngOnInit() {
70
        this.sub =  this.route.queryParams.subscribe(params => {
71
            this.projectId = params['projectId'];
72

    
73
            if (params['size'] == parseInt(params['size'], 10)) {
74
              this.totalResults = params['size'];
75
            } else {
76
              this.showLoading = false;
77
    		      this.warningMessage="Requested size is not an integer";
78
            }
79
console.info(params['type']);
80
            if(params['type'] && (params['type'] == "dataset" || params['type'] == "publication")){
81
              this.resultsType = (params['type'] == "dataset") ? 'research data' : params['type'];
82
              this.updateTitle("Project's "+this.resultsType+" report");
83
              this.updateDescription("project, project "+ this.resultsType +" report, funding, open access, publications, research data");
84
            } else {
85
              this.showLoading = false;
86
    		      this.warningMessage="Requested type should be publication or research data";
87
            }
88

    
89
            //showLoading is true if no warnings
90
            if(this.showLoading) {
91
              if(this.projectId) {
92
                this.createHeaders();
93
              } else {
94
                this.showLoading = false;
95
      		      this.warningMessage="No valid project id";
96
              }
97
            }
98
        });
99
    }
100

    
101
    ngOnDestroy() {
102
      this.sub.unsubscribe();
103
      if(this.piwiksub){
104
        this.piwiksub.unsubscribe();
105
      }
106
      if(this.subHTML) {
107
        this.subHTML.unsubscribe();
108
      }
109
      if(this.subHTMLInfo) {
110
        this.subHTMLInfo.unsubscribe();
111
      }
112
    }
113

    
114
    private createHeaders() {
115
        this.subHTMLInfo = this._projectService.getHTMLInfo(this.projectId).subscribe(
116
            data => {
117
                this.createHeader1(data);
118
                if(data.acronym) {
119
                  this.updateTitle(data.acronym+" "+this.resultsType+" report");
120
                } else if(data.title){
121
                  this.updateTitle(data.title+" "+this.resultsType+" report");
122
                }
123
                if(OpenaireProperties.isPiwikTrackEnabled() && (typeof document !== 'undefined')){
124
                  this.piwiksub = this._piwikService.trackView(((data.acronym)?data.acronym:data.title)+" "+this.resultsType+" report").subscribe();
125
                }
126
            },
127
            err => {
128
                console.log(err);
129
                this.createClipboard();
130
            }
131
        );
132

    
133
        this.header2 = this.totalResults+((this.resultsType == "publication")?" publications":" research data");
134
    }
135

    
136
    private createClipboard() {
137
        let intro: string = '<!doctype html>';
138
        intro += '<html lang="en-gb" dir="ltr" vocab="http://schema.org/">';
139
        intro += '<head>';
140
        intro += '<title>'+this.header1+'</title>'
141
        intro += '</head>';
142

    
143
        if (typeof window !== 'undefined') {
144
            this.subHTML = this.htmlService.getHTML(this.projectId, this.totalResults, this.resultsType).subscribe(
145
                data => {
146
                    let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>'+data+'</body></html>';
147
                    this.htmlResult = data;
148

    
149
                    let clipboard;
150
                    let Clipboard;
151
                    Clipboard = require('clipboard');
152
                    clipboard = new Clipboard('.clipBtn', {
153
                        /*target: function(trigger) {
154
                            return document.getElementById("clipboard");
155
                        }*/
156
                        text: function(trigger) {
157
                            return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo";
158
                        }
159
                    });
160

    
161
                    this.showLoading = false;
162
                },
163
                err => {
164
                    console.log(err);
165
                    this.errorMessage = 'Service not available';
166
                    this.showLoading = false;
167
                }
168
            );
169
        }
170
    }
171

    
172
    createHeader1(data: {"title": string, "acronym": string, "callIdentifier": string}) {
173
        this.header1 = ((this.resultsType == "publication")?"Publications":"Research Data") + " of Project ";
174

    
175
        if(data != undefined) {
176
            if(data.title != undefined && data.title != "") {
177
                this.header1 += data.title;
178
            }
179
            if((data.title != undefined && data.title != "") &&
180
             ((data.acronym != undefined && data.acronym != "") ||
181
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
182
                    this.header1 += "(";
183
            }
184
            if(data.acronym != undefined && data.acronym != "") {
185
                this.header1 += data.acronym + " - ";
186
            }
187
            if(data.callIdentifier != undefined && data.callIdentifier != "") {
188
                this.header1 += data.callIdentifier;
189
            }
190
            if((data.title != undefined && data.title != "") &&
191
             ((data.acronym != undefined && data.acronym != "") ||
192
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
193
                    this.header1 += ")";
194
            }
195
        }
196

    
197
        this.createClipboard();
198
    }
199

    
200
    public copied() {
201
      UIkit.notification({
202
          message : '<strong>Raw html is copied. Please paste it on an html file.<strong>',
203
          status  : 'success',
204
          timeout : 3000,
205
          pos     : 'top-center'
206
      });
207
    }
208

    
209
    private updateDescription(description:string){
210
      this._meta.updateMeta("description", description);
211
      this._meta.updateProperty("og:description", description);
212
    }
213
    private updateTitle(title:string){
214
      var _prefix ="OpenAIRE | ";
215
      var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
216
      this._meta.setTitle(_title );
217
      this._meta.updateProperty("og:title",_title);
218
    }
219
    private updateUrl(url:string){
220
      this._meta.updateProperty("og:url", url);
221
    }
222
}
(2-2/4)