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
    <div class= "searchDownload" *ngIf="totalResults > 0">
12
        <p class="uk-text-right" *ngIf="totalResults <= 10000">
13
            <!--a (click)="download()" href="{{downloadURLAPI}}{{type}}?format=csv&page=0&size={{totalResults}}{{csvParams}}" -->
14
            <span class="clickable" (click)="downloadfile(downloadURLAPI+type+'?format=csv&page=0&size='+totalResults+csvParams,type+'-report-'+totalResults)">
15
                <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
16
                <span uk-icon="icon: download"></span> Results (CSV)
17
            </span>
18
        </p>
19
    </div>
20
    <!--modal-alert></modal-alert-->
21
    <modal-loading></modal-loading>
22
    `
23
})
24

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

    
33
    @ViewChild (ModalLoading) loading : ModalLoading ;
34

    
35
    constructor ( private _reportsService: ReportsService) {}
36

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

    
41
    confirmClose(data){
42

    
43
    }
44
    download() {
45
        this.downloadClick.emit({
46
            value: true
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
          },
71
          error => console.log("Error downloading the file."),
72
          () => console.log('Completed file download.')
73
      );
74
    }
75

    
76

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

    
93
}
(10-10/28)