Project

General

Profile

1 44583 konstantin
import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {AlertModal} from '../../utils/modal/alert';
4 45408 konstantin
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
5 45518 argiro.kok
import {ReportsService} from '../../services/reports.service';
6 46413 konstantin
import {ModalLoading} from '../../utils/modal/loading.component';
7 48880 konstantin
import {PiwikService} from '../../utils/piwik/piwik.service';
8 44583 konstantin
9
@Component({
10
    selector: 'search-download',
11
    template: `
12 49174 konstantin
        <span class="uk-margin-large-right" *ngIf="totalResults > 0 && totalResults <= 10000">
13 46413 konstantin
            <span class="clickable" (click)="downloadfile(downloadURLAPI+type+'?format=csv&page=0&size='+totalResults+csvParams,type+'-report-'+totalResults)">
14 45518 argiro.kok
                <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
15 48792 argiro.kok
                <span  class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="download" ratio="1"><polyline fill="none" stroke="#000" points="14,10 9.5,14.5 5,10"></polyline><rect x="3" y="17" width="13" height="1"></rect><line fill="none" stroke="#000" x1="9.5" y1="13.91" x2="9.5" y2="3"></line></svg></span> (CSV)
16 45518 argiro.kok
            </span>
17 48793 argiro.kok
        </span>
18 45484 konstantin
    <!--modal-alert></modal-alert-->
19 46413 konstantin
    <modal-loading></modal-loading>
20 44583 konstantin
    `
21
})
22
23
export class SearchDownloadComponent {
24
    @Input() totalResults:number = 0;
25 45484 konstantin
    @Input() csvParams: string;
26
    @Input() type: string;
27 44583 konstantin
    @ViewChild(AlertModal) alertApplyAll;
28 45408 konstantin
    private downloadURLAPI: string;
29 44583 konstantin
30 48880 konstantin
    sub: any;
31
    downloadFilePiwikSub: any;
32
33 46413 konstantin
    @ViewChild (ModalLoading) loading : ModalLoading ;
34
35 48880 konstantin
    constructor ( private _reportsService: ReportsService, private _piwikService:PiwikService) {}
36 44583 konstantin
37
    ngOnInit() {
38 45429 konstantin
        this.downloadURLAPI = OpenaireProperties.getCsvAPIURL();
39 44583 konstantin
    }
40
41 48880 konstantin
    ngOnDestroy() {
42
      if(this.sub) {
43
        this.sub.unsubscribe();
44
      }
45
      if(this.downloadFilePiwikSub) {
46
        this.downloadFilePiwikSub.unsubscribe();
47
      }
48
    }
49
50 44583 konstantin
    denialOfDownload() {
51
        this.alertApplyAll.isOpen = true;
52
        this.alertApplyAll.cancelButton = true;
53
        this.alertApplyAll.okButton = false;
54
        this.alertApplyAll.alertTitle = "Download Results in CSV";
55
        this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
56
        this.alertApplyAll.cancelButtonText = "Ok";
57
58
        console.info("denial of Download");
59
60
    }
61 45518 argiro.kok
    downloadfile(url:string,filename:string){
62
      console.log("Downloading file: "+ url);
63 46413 konstantin
      this.openLoading();
64
      this.setMessageLoading("Downloading CSV file");
65
66
      this._reportsService.downloadCSVFile(url).subscribe(
67
          data => {
68
              this.closeLoading();
69 48880 konstantin
              window.open(window.URL.createObjectURL(data),filename+".csv");
70
              if(OpenaireProperties.isPiwikTrackEnabled() && (typeof document !== 'undefined')){
71
                this.downloadFilePiwikSub = this._piwikService.trackDownload(url).subscribe();
72
              }
73 46413 konstantin
          },
74
          error => console.log("Error downloading the file."),
75
          () => console.log('Completed file download.')
76
      );
77
    }
78
79
80
    public openLoading(){
81
      if(this.loading){
82
        this.loading.open();
83 45518 argiro.kok
      }
84 46413 konstantin
    }
85
    public closeLoading(){
86
      if(this.loading){
87
        this.loading.close();
88
      }
89
    }
90
    public setMessageLoading(message: string){
91
      if(this.loading){
92
        this.loading.message = message;
93
      }
94
    }
95
96 44583 konstantin
}