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

    
8
@Component({
9
    selector: 'search-download',
10
    template: `
11
        <span class="uk-margin-large-right" *ngIf="totalResults <= 10000">
12
            <!--a (click)="download()" href="{{downloadURLAPI}}{{type}}?format=csv&page=0&size={{totalResults}}{{csvParams}}" -->
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
    @Output() downloadClick  = new EventEmitter();
29
    private downloadURLAPI: string;
30

    
31
    @ViewChild (ModalLoading) loading : ModalLoading ;
32

    
33
    constructor ( private _reportsService: ReportsService) {}
34

    
35
    ngOnInit() {
36
        this.downloadURLAPI = OpenaireProperties.getCsvAPIURL();
37
    }
38

    
39
    confirmClose(data){
40

    
41
    }
42
    download() {
43
        this.downloadClick.emit({
44
            value: true
45
        });
46
    }
47

    
48
    denialOfDownload() {
49
        this.alertApplyAll.isOpen = true;
50
        this.alertApplyAll.cancelButton = true;
51
        this.alertApplyAll.okButton = false;
52
        this.alertApplyAll.alertTitle = "Download Results in CSV";
53
        this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
54
        this.alertApplyAll.cancelButtonText = "Ok";
55

    
56
        console.info("denial of Download");
57

    
58
    }
59
    downloadfile(url:string,filename:string){
60
      console.log("Downloading file: "+ url);
61
      this.openLoading();
62
      this.setMessageLoading("Downloading CSV file");
63

    
64
      this._reportsService.downloadCSVFile(url).subscribe(
65
          data => {
66
              this.closeLoading();
67
              window.open(window.URL.createObjectURL(data),filename+".csv")
68
          },
69
          error => console.log("Error downloading the file."),
70
          () => console.log('Completed file download.')
71
      );
72
    }
73

    
74

    
75
    public openLoading(){
76
      if(this.loading){
77
        this.loading.open();
78
      }
79
    }
80
    public closeLoading(){
81
      if(this.loading){
82
        this.loading.close();
83
      }
84
    }
85
    public setMessageLoading(message: string){
86
      if(this.loading){
87
        this.loading.message = message;
88
      }
89
    }
90

    
91
}
(10-10/28)