Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AlertModal} from '../../utils/modal/alert';
4
import {ReportsService} from '../../services/reports.service';
5
import {ModalLoading} from '../../utils/modal/loading.component';
6
import {PiwikService} from '../../utils/piwik/piwik.service';
7
import {EnvProperties} from '../../utils/properties/env-properties';
8
import {ErrorCodes} from '../../utils/properties/errorCodes';
9

    
10
import 'rxjs';
11

    
12
@Component({
13
    selector: 'search-download',
14
    template: `
15
        <a [attr.uk-tooltip]="'title: Download'
16
        + ((totalResults > csvLimit)?' the first 2000 ':' ')
17
        + 'results'" class="uk-link-text" *ngIf="totalResults > 0">
18
<!--          type: {{type}}-->
19
            <span class="clickable" (click)="downloadfile(downloadURLAPI+'?format=csv'+csvParams,type+'-report-'+((totalResults > csvLimit)?'2000 ':totalResults))">
20
                <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
21
                <span  class="uk-icon">
22
                  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="download" ratio="1">
23
                    <polyline fill="none" stroke="#000" points="14,10 9.5,14.5 5,10"></polyline>
24
                    <rect x="3" y="17" width="13" height="1"></rect>
25
                    <line fill="none" stroke="#000" x1="9.5" y1="13.91" x2="9.5" y2="3"></line>
26
                  </svg>
27
                </span> 
28
                Download Results
29
            </span>
30
        </a>
31
    <modal-loading></modal-loading>
32
    <modal-alert #AlertModalCsvError></modal-alert>
33
    `
34
})
35

    
36
export class SearchDownloadComponent {
37
    @Input() searchUtils;
38
    @Input() oldTotalResults:number = 0;
39
    @Input() loadPaging: boolean = true;
40
    @Input() totalResults:number = 0;
41
    @Input() csvParams: string;
42
    @Input() type: string;
43
    @ViewChild(AlertModal) alertApplyAll: AlertModal;
44
    private downloadURLAPI: string;
45

    
46
    sub: any;
47
    downloadFilePiwikSub: any;
48

    
49
    public csvLimit: number = 0;
50

    
51
    @ViewChild (ModalLoading) loading : ModalLoading ;
52
    // Alert box when something is wrong with CSV requests
53
    @ViewChild('AlertModalCsvError') alertCsvError;
54
    public isPiwikEnabled;
55
    public properties:EnvProperties;
56
    public errorCodes:ErrorCodes = new ErrorCodes();
57

    
58
    constructor (private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService:PiwikService) {}
59

    
60
    ngOnInit() {
61
      this.route.data
62
        .subscribe((data: { envSpecific: EnvProperties }) => {
63
          this.properties = data.envSpecific;
64
          this.csvLimit = data.envSpecific.csvLimit;
65
          this.downloadURLAPI = data.envSpecific.csvAPIURL;
66
          this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
67
        });
68
    }
69

    
70
    ngOnDestroy() {
71
      if(this.sub) {
72
        this.sub.unsubscribe();
73
      }
74
      if(this.downloadFilePiwikSub) {
75
        this.downloadFilePiwikSub.unsubscribe();
76
      }
77
    }
78

    
79
    denialOfDownload() {
80
        this.alertApplyAll.cancelButton = true;
81
        this.alertApplyAll.okButton = false;
82
        this.alertApplyAll.alertTitle = "Download Results in CSV";
83
        this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
84
        this.alertApplyAll.cancelButtonText = "Ok";
85
        this.alertApplyAll.open();
86

    
87
        console.error("Error downloading file. Results are too many!");
88
        //this.handleError("Error downloading file. Results are too many!", err);
89
    }
90
    downloadfile(url:string,filename:string){
91
      //var newWindow = window.open("", "_parent");
92
      //var newWindow = window.open();
93
      //console.log("Downloading file: "+ url);
94
      this.openLoading();
95
      this.setMessageLoading("Downloading CSV file");
96

    
97
      this._reportsService.downloadCSVFile(url).subscribe(
98
          data => {
99
              this.closeLoading();
100
              //window.open(window.URL.createObjectURL(data),filename+".csv");
101
              //console.info("Fill window with data for csv");
102
            if(typeof document !== 'undefined'){
103
              var url = window.URL.createObjectURL(data);
104
              var a = window.document.createElement('a');
105
              window.document.body.appendChild(a);
106
              a.setAttribute('style', 'display: none');
107
              a.href = url;
108
              a.download = filename+".csv";
109
              a.click();
110
              window.URL.revokeObjectURL(url);
111
              a.remove(); // remove the element
112
            }
113
              //newWindow.location.assign(window.URL.createObjectURL(data));
114

    
115
              //window.location.href = window.URL.createObjectURL(data);
116

    
117
              if(this.isPiwikEnabled && (typeof document !== 'undefined')){
118
                this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
119
              }
120
          },
121
          error => {
122
            //console.error("Error downloading the file.");
123
            this.handleError("Error downloading file: "+filename, error);
124

    
125
            //newWindow.close();
126
            this.closeLoading();
127
            this.confirmOpenCsvError();
128
          }/*,
129
          () => {
130
            console.log('Completed file download.');
131
            //setTimeout(function(){ newWindow.close(); }, 500);
132
          }*/
133
      );
134
    }
135

    
136

    
137
    public openLoading(){
138
      if(this.loading){
139
        this.loading.open();
140
      }
141
    }
142
    public closeLoading(){
143
      if(this.loading){
144
        this.loading.close();
145
      }
146
    }
147
    public setMessageLoading(message: string){
148
      if(this.loading){
149
        this.loading.message = message;
150
      }
151
    }
152

    
153
    public confirmOpenCsvError(){
154
      this.alertCsvError.cancelButton = false;
155
      this.alertCsvError.okButton = true;
156
      this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
157
      this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
158
      this.alertCsvError.okButtonText = "OK";
159
      this.alertCsvError.open();
160
    }
161

    
162
    private handleError(message: string, error) {
163
      console.error("Search Download (component): "+message, error);
164
    }
165
}
(28-28/55)