Project

General

Profile

1
import {Component} from '@angular/core';
2
import {Observable} from 'rxjs/Observable';
3
import {ActivatedRoute, Params} from '@angular/router';
4
import {isBrowser, isNode} from 'angular2-universal';
5
import {HtmlProjectReportService} from './htmlProjectReport.service';
6
import {ProjectService} from '../project/project.service';
7

    
8
@Component({
9
    selector: 'htmlProjectReport',
10
    template: `
11
        <div class="uk-container uk-margin-top">
12
            <button class="uk-icon-clipboard uk-button uk-button-primary btn" (click)="copied = true;">
13
                Copy to clipboard
14
            </button>
15
            <!--span *ngIf="copied" class="uk-badge uk-badge-success">Success</span-->
16
            <div *ngIf="copied" class="uk-alert" data-uk-alert>
17
                <p>Raw html is copied. Please paste it on an html file.</p>
18
            </div>
19

    
20
            <h1>{{header1}}</h1>
21
            <h2>{{header2}}</h2>
22
            <div id="clipboard" [innerHTML]="htmlResult"></div>
23
        </div>
24
    `
25
 })
26
export class HtmlProjectReportComponent{
27
    public projectId: string;
28
    public totalResults: number;
29
    public resultsType: string = "publication";
30

    
31
    public header1: string;
32
    public header2: string;
33
    public htmlResult: string = "";
34

    
35
    public sub;
36
    public subHTML;
37
    public subHTMLInfo;
38

    
39
    public copied: boolean = false;
40

    
41
    constructor ( private  route: ActivatedRoute,
42
                private htmlService: HtmlProjectReportService,
43
                private _projectService: ProjectService) {
44
    }
45

    
46
    ngOnInit() {
47
        this.sub =  this.route.queryParams.subscribe(params => {
48
            this.projectId = params['projectId'];
49
            this.totalResults = params['size'];
50
            if(params['type'] && (params['type'] == "dataset" || params['type'] == "publication")){
51
              this.resultsType = params['type'];
52
            }
53

    
54
            this.createHeaders();
55
            this.createClipboard();
56
        });
57
    }
58

    
59
    ngOnDestroy() {}
60

    
61
    createHeaders() {
62
        this._projectService.getHTMLInfo(this.projectId).subscribe(
63
            data => {
64
                this.createHeader1(data);
65
            },
66
            err => {
67
                console.log(err);
68
            }
69
        );
70

    
71
        this.header2 = this.totalResults+((this.resultsType == "publication")?" publications":" datasets");
72
    }
73

    
74
    createClipboard() {
75
        let intro: string = '<!doctype html>';
76
        intro += '<html lang="en-gb" dir="ltr" vocab="http://schema.org/">';
77
        intro += '<head>';
78
        intro += '<title>'+this.header1+'</title>'
79
        intro += '</head>';
80
        intro += '<body>';
81

    
82
        if (typeof window !== 'undefined') {
83
            this.htmlService.getHTML(this.projectId, this.totalResults, this.resultsType).subscribe(
84
                data => {
85
                    let body: string = intro+'<body><h1>'+this.header1+'</h1><h2>'+this.header2+'</h2>'+data+'</body></html>';
86

    
87
                    this.htmlResult = data;
88

    
89
                    let clipboard;
90
                    let Clipboard;
91
                    Clipboard = require('clipboard');
92
                    clipboard = new Clipboard('.btn', {
93
                        /*target: function(trigger) {
94
                            return document.getElementById("clipboard");
95
                        }*/
96
                        text: function(trigger) {
97
                            return body;//document.getElementById("clipboard").getAttribute('innerHTML');//"aaaa"+tmp+"oo";
98
                        }
99
                    });
100
                },
101
                err => {
102
                    console.log(err);
103
                }
104
            );
105
        }
106
    }
107

    
108
    createHeader1(data: {"title": string, "acronym": string, "callIdentifier": string}) {
109
        console.info(data);
110
        this.header1 = ((this.resultsType == "publication")?"Publications":"Datasets") + " of Project ";
111

    
112
        if(data != undefined) {
113
            if(data.title != undefined && data.title != "") {
114
                this.header1 += data.title;
115
            }
116
            if((data.title != undefined && data.title != "") &&
117
             ((data.acronym != undefined && data.acronym != "") ||
118
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
119
                    this.header1 += "(";
120
            }
121
            if(data.acronym != undefined && data.acronym != "") {
122
                this.header1 += data.acronym + " - ";
123
            }
124
            if(data.callIdentifier != undefined && data.callIdentifier != "") {
125
                this.header1 += data.callIdentifier;
126
            }
127
            if((data.title != undefined && data.title != "") &&
128
             ((data.acronym != undefined && data.acronym != "") ||
129
              (data.callIdentifier != undefined && data.callIdentifier != ""))) {
130
                    this.header1 += ")";
131
            }
132
        }
133
    }
134
}
(2-2/4)