Project

General

Profile

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

    
5
import {EnvProperties} from '../../utils/properties/env-properties';
6

    
7
import {HtmlProjectReportService} from './htmlProjectReport.service';
8
import {ProjectService} from '../project/project.service';
9
import {PiwikService} from '../../utils/piwik/piwik.service';
10
import {SEOService} from '../../sharedComponents/SEO/SEO.service';
11
import {HelperService} from "../../utils/helper/helper.service";
12
import {Subscriber} from "rxjs";
13

    
14
declare var UIkit: any;
15

    
16
@Component({
17
  selector: 'htmlProjectReport',
18
  template: `
19
    <div id="tm-main" class=" uk-section  uk-padding-remove-top tm-middle">
20
      <div 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
          <helper *ngIf="pageContents && pageContents['top'] && pageContents['top'].length > 0"
23
                  [texts]="pageContents['top']" styleName="uk-width-1-1"></helper>
24
          <div class="uk-container uk-margin-top uk-width-1-1">
25
            <div *ngIf="warningMessage" class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
26
            <div [style.display]="showLoading ? 'inline' : 'none'"
27
                 class="uk-animation-fade uk-margin-large-top  uk-width-1-1" role="alert"><span
28
              class="loading-gif  uk-align-center"></span></div>
29
            
30
            <div *ngIf="!showLoading && !warningMessage">
31
              <div *ngIf="header1" class="uk-h4 uk-text-bold ">{{header1}}</div>
32
              <div *ngIf="header1 || htmlResult" class=" ">{{header2}}</div>
33

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

    
44
              <div *ngIf="errorMessage" class="uk-alert uk-alert-danger" role="alert">{{errorMessage}}</div>
45
            </div>
46
          </div>
47
          <helper *ngIf="pageContents && pageContents['bottom'] && pageContents['bottom'].length > 0"
48
                  [texts]="pageContents['bottom']" styleName="uk-width-1-1"></helper>
49
        </div>
50
      </div>
51
    </div>
52

    
53
  `
54
})
55
export class HtmlProjectReportComponent {
56
  @Input() piwikSiteId = null;
57
  @Input() communityId = null;
58
  private projectId: string;
59
  private totalResults: number = 10;
60
  private resultsType: string = "publication";
61

    
62
  public header1: string = "";
63
  public header2: string = "";
64
  public htmlResult: string = "";
65

    
66
  subscriptions = [];
67

    
68
  public warningMessage: string = "";
69
  public errorMessage: string = "";
70
  public showLoading: boolean = true;
71
  properties: EnvProperties;
72
  public pageContents = null;
73
  public divContents = null;
74

    
75
  constructor(private  route: ActivatedRoute,
76
              private htmlService: HtmlProjectReportService,
77
              private _piwikService: PiwikService,
78
              private _projectService: ProjectService,
79
              private _meta: Meta,
80
              private _title: Title,
81
              private _router: Router,
82
              private helper: HelperService,
83
              private seoService: SEOService) {
84
  }
85

    
86
  ngOnInit() {
87
    this.subscriptions.push(this.route.data
88
      .subscribe((data: { envSpecific: EnvProperties }) => {
89
        this.properties = data.envSpecific;
90
        //this.getDivContents();
91
        this.getPageContents();
92
        this.updateUrl(this.properties.domain + this.properties.baseLink + this._router.url);
93
        this.seoService.createLinkForCanonicalURL(this.properties.domain + this.properties.baseLink + this._router.url);
94

    
95
      }));
96
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
97
      this.projectId = params['projectId'];
98

    
99
      if (params['size'] == parseInt(params['size'], 10)) {
100
        this.totalResults = params['size'];
101
      } else {
102
        this.showLoading = false;
103
        this.warningMessage = "Requested size is not an integer";
104
      }
105

    
106
      if (params['type'] && (params['type'] == "publication" || params['type'] == "dataset" || params['type'] == "software" || params['type'] == "other")) {
107
        if (params['type'] == "publication") {
108
          this.resultsType = 'publication';
109
        } else if (params['type'] == "dataset") {
110
          this.resultsType = 'research data';
111
        } else if (params['type'] == "software") {
112
          this.resultsType = 'software';
113
        } else if (params['type'] == "other") {
114
          this.resultsType = "other research product";
115
        }
116

    
117
        var title = "Project's " + this.resultsType + " report";
118
        var description = "project, project " + this.resultsType + " report, funding, open access, publications, research data, software, other research products";
119
        this.updateTitle(title);
120
        this.updateDescription(description);
121

    
122

    
123
      } else {
124
        this.showLoading = false;
125
        this.warningMessage = "Requested type should be publication or research data or software or other research product";
126
      }
127

    
128
      //showLoading is true if no warnings
129
      if (this.showLoading) {
130
        if (this.projectId) {
131
          this.createHeaders();
132
        } else {
133
          this.showLoading = false;
134
          this.warningMessage = "No valid project id";
135
        }
136
      }
137
    }));
138
  }
139

    
140
  private getPageContents() {
141
    if(this.communityId) {
142
      this.subscriptions.push(this.helper.getPageHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
143
        this.pageContents = contents;
144
      }));
145
    }
146
  }
147

    
148
  private getDivContents() {
149
    if(this.communityId) {
150
      this.subscriptions.push(this.helper.getDivHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
151
        this.divContents = contents;
152
      }));
153
    }
154
  }
155

    
156
  ngOnDestroy() {
157
    this.subscriptions.forEach(subscription => {
158
      if (subscription instanceof Subscriber) {
159
        subscription.unsubscribe();
160
      }
161
    });
162
  }
163

    
164
  private createHeaders() {
165
    this.subscriptions.push(this._projectService.getHTMLInfo(this.projectId, this.properties).subscribe(
166
      data => {
167
        this.createHeader1(data);
168
        if (data.acronym) {
169
          this.updateTitle(data.acronym + " " + this.resultsType + " report");
170
        } else if (data.title) {
171
          this.updateTitle(data.title + " " + this.resultsType + " report");
172
        }
173
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
174
          this.subscriptions.push(this._piwikService.trackView(this.properties, ((data.acronym) ? data.acronym : data.title) + " " + this.resultsType + " report", this.piwikSiteId).subscribe());
175
        }
176
      },
177
      err => {
178
        this.handleError("Error getting html information for project id: " + this.projectId, err);
179
        //console.log(err);
180
        this.createClipboard();
181
      }
182
    ));
183

    
184
    if (this.resultsType == "publication") {
185
      this.header2 += this.totalResults.toLocaleString('en-US') + " publications";
186
    } else if (this.resultsType == "research data") {
187
      this.header2 += this.totalResults.toLocaleString('en-US') + " research data";
188
    } else if (this.resultsType == "software") {
189
      this.header2 += this.totalResults.toLocaleString('en-US') + " software";
190
    } else if (this.resultsType == "other research product") {
191
      this.header2 += this.totalResults.toLocaleString('en-US') + " other";
192
    }
193
  }
194

    
195
  private createClipboard() {
196
    let intro: string = '<!doctype html>';
197
    intro += '<html lang="en-gb" dir="ltr" vocab="http://schema.org/">';
198
    intro += '<head>';
199
    intro += '<title>' + this.header1 + '</title>'
200
    intro += '</head>';
201

    
202
    if (typeof window !== 'undefined') {
203
      this.subscriptions.push(this.htmlService.getHTML(this.projectId, this.resultsType, this.properties.csvAPIURL).subscribe(
204
        data => {
205
          //let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>'+data+'</body></html>';
206
          let body: string = intro + '<body><h1>' + this.header1 + '</h1><h2>' + this.header2 + '</h2>';
207
          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>";
208
          body += '</body></html>';
209

    
210
          //this.htmlResult = data;
211
          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>";
212

    
213
          let clipboard;
214
          let Clipboard;
215
          Clipboard = require('clipboard');
216
          clipboard = new Clipboard('.clipBtn', {
217
            /*target: function(trigger) {
218
                return document.getElementById("clipboard");
219
            }*/
220
            text: function (trigger) {
221
              return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo";
222
            }
223
          });
224

    
225
          this.showLoading = false;
226
        },
227
        err => {
228
          //console.log(err);
229
          this.handleError("Error getting html for id: " + this.projectId, err);
230

    
231
          this.errorMessage = 'Service not available';
232
          this.showLoading = false;
233
        }
234
      ));
235
    }
236
  }
237

    
238
  createHeader1(data: { "title": string, "acronym": string, "callIdentifier": string }) {
239
    if (this.resultsType == "publication") {
240
      this.header1 += "Publications";
241
    } else if (this.resultsType == "research data") {
242
      this.header1 += "Research Data";
243
    } else if (this.resultsType == "software") {
244
      this.header1 += "Software";
245
    } else if (this.resultsType == "other research product") {
246
      this.header1 += "Other Research Products";
247
    }
248

    
249
    if (data != undefined) {
250
      if (data.title != undefined && data.title != "") {
251
        this.header1 += data.title;
252
      }
253
      if ((data.title != undefined && data.title != "") &&
254
        ((data.acronym != undefined && data.acronym != "") ||
255
          (data.callIdentifier != undefined && data.callIdentifier != ""))) {
256
        this.header1 += "(";
257
      }
258
      if (data.acronym != undefined && data.acronym != "") {
259
        this.header1 += data.acronym + " - ";
260
      }
261
      if (data.callIdentifier != undefined && data.callIdentifier != "") {
262
        this.header1 += data.callIdentifier;
263
      }
264
      if ((data.title != undefined && data.title != "") &&
265
        ((data.acronym != undefined && data.acronym != "") ||
266
          (data.callIdentifier != undefined && data.callIdentifier != ""))) {
267
        this.header1 += ")";
268
      }
269
    }
270

    
271
    this.createClipboard();
272
  }
273

    
274
  public copied() {
275
    UIkit.notification({
276
      message: '<strong>Raw html is copied. Please paste it on an html file.<strong>',
277
      status: 'success',
278
      timeout: 3000,
279
      pos: 'top-center'
280
    });
281
  }
282

    
283
  private updateDescription(description: string) {
284
    this._meta.updateTag({content: description}, "name='description'");
285
    this._meta.updateTag({content: description}, "property='og:description'");
286
  }
287

    
288
  private updateTitle(title: string) {
289
    var _prefix = "";
290
    if(!this.communityId) {
291
      _prefix = "OpenAIRE | ";
292
    }
293
    var _title = _prefix + ((title.length > 50) ? title.substring(0, 50) : title);
294
    this._title.setTitle(_title);
295
    this._meta.updateTag({content: _title}, "property='og:title'");
296
  }
297

    
298
  private updateUrl(url: string) {
299
    this._meta.updateTag({content: url}, "property='og:url'");
300
  }
301

    
302
  private handleError(message: string, error) {
303
    console.error("Html Project Report Page: " + message, error);
304
  }
305
}
(1-1/3)