Project

General

Profile

1
import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {AlertModal} from '../../utils/modal/alert';
4
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
5
import {ReportsService} from '../../services/reports.service';
6
import {ModalLoading} from '../../utils/modal/loading.component';
7
import {PiwikService} from '../../utils/piwik/piwik.service';
8

    
9
@Component({
10
    selector: 'search-download',
11
    template: `
12
        <span class="uk-margin-large-right" *ngIf="totalResults <= 10000">
13
            <span class="clickable" (click)="downloadfile(downloadURLAPI+type+'?format=csv&page=0&size='+totalResults+csvParams,type+'-report-'+totalResults)">
14
                <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
15
                <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
            </span>
17
        </span>
18
    <!--modal-alert></modal-alert-->
19
    <modal-loading></modal-loading>
20
    `
21
})
22

    
23
export class SearchDownloadComponent {
24
    @Input() totalResults:number = 0;
25
    @Input() csvParams: string;
26
    @Input() type: string;
27
    @ViewChild(AlertModal) alertApplyAll;
28
    private downloadURLAPI: string;
29

    
30
    sub: any;
31
    downloadFilePiwikSub: any;
32

    
33
    @ViewChild (ModalLoading) loading : ModalLoading ;
34

    
35
    constructor ( private _reportsService: ReportsService, private _piwikService:PiwikService) {}
36

    
37
    ngOnInit() {
38
        this.downloadURLAPI = OpenaireProperties.getCsvAPIURL();
39
    }
40

    
41
    ngOnDestroy() {
42
      if(this.sub) {
43
        this.sub.unsubscribe();
44
      }
45
      if(this.downloadFilePiwikSub) {
46
        this.downloadFilePiwikSub.unsubscribe();
47
      }
48
    }
49

    
50
    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
    downloadfile(url:string,filename:string){
62
      console.log("Downloading file: "+ url);
63
      this.openLoading();
64
      this.setMessageLoading("Downloading CSV file");
65

    
66
      this._reportsService.downloadCSVFile(url).subscribe(
67
          data => {
68
              this.closeLoading();
69
              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
          },
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
      }
84
    }
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
}
(10-10/28)