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 > 0 && 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
    <modal-alert #AlertModalCsvError></modal-alert>
21
    `
22
})
23

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

    
31
    sub: any;
32
    downloadFilePiwikSub: any;
33

    
34
    @ViewChild (ModalLoading) loading : ModalLoading ;
35
    // Alert box when something is wrong with CSV requests
36
    @ViewChild('AlertModalCsvError') alertCsvError;
37

    
38
    constructor ( private _reportsService: ReportsService, private _piwikService:PiwikService) {}
39

    
40
    ngOnInit() {
41
        this.downloadURLAPI = OpenaireProperties.getCsvAPIURL();
42
    }
43

    
44
    ngOnDestroy() {
45
      if(this.sub) {
46
        this.sub.unsubscribe();
47
      }
48
      if(this.downloadFilePiwikSub) {
49
        this.downloadFilePiwikSub.unsubscribe();
50
      }
51
    }
52

    
53
    denialOfDownload() {
54
        this.alertApplyAll.isOpen = true;
55
        this.alertApplyAll.cancelButton = true;
56
        this.alertApplyAll.okButton = false;
57
        this.alertApplyAll.alertTitle = "Download Results in CSV";
58
        this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
59
        this.alertApplyAll.cancelButtonText = "Ok";
60

    
61
        console.info("denial of Download");
62

    
63
    }
64
    downloadfile(url:string,filename:string){
65
      console.log("Downloading file: "+ url);
66
      this.openLoading();
67
      this.setMessageLoading("Downloading CSV file");
68

    
69
      this._reportsService.downloadCSVFile(url).subscribe(
70
          data => {
71
              this.closeLoading();
72
              window.open(window.URL.createObjectURL(data),filename+".csv");
73
              if(OpenaireProperties.isPiwikTrackEnabled() && (typeof document !== 'undefined')){
74
                this.downloadFilePiwikSub = this._piwikService.trackDownload(url).subscribe();
75
              }
76
          },
77
          error => {
78
            console.log("Error downloading the file.");
79
            this.closeLoading();
80
            this.confirmOpenCsvError();
81
          },
82
          () => console.log('Completed file download.')
83
      );
84
    }
85

    
86

    
87
    public openLoading(){
88
      if(this.loading){
89
        this.loading.open();
90
      }
91
    }
92
    public closeLoading(){
93
      if(this.loading){
94
        this.loading.close();
95
      }
96
    }
97
    public setMessageLoading(message: string){
98
      if(this.loading){
99
        this.loading.message = message;
100
      }
101
    }
102

    
103
    public confirmOpenCsvError(){
104
      this.alertCsvError.cancelButton = false;
105
      this.alertCsvError.okButton = true;
106
      this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
107
      this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
108
      this.alertCsvError.okButtonText = "OK";
109
      this.alertCsvError.open();
110
    }
111

    
112
}
(13-13/36)