1
|
import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
|
2
|
import {Observable} from 'rxjs/Observable';
|
3
|
import { ActivatedRoute} from '@angular/router';
|
4
|
import {AlertModal} from '../../utils/modal/alert';
|
5
|
import {ReportsService} from '../../services/reports.service';
|
6
|
import {ModalLoading} from '../../utils/modal/loading.component';
|
7
|
import {PiwikService} from '../../utils/piwik/piwik.service';
|
8
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
9
|
|
10
|
@Component({
|
11
|
selector: 'search-download',
|
12
|
template: `
|
13
|
<a class="uk-margin-large-right" *ngIf="totalResults > 0 && totalResults <= csvLimit">
|
14
|
<span class="clickable" (click)="downloadfile(downloadURLAPI+'?type='+type+'&format=csv'+csvParams,type+'-report-'+totalResults)">
|
15
|
<span aria-hidden="true" class="glyphicon glyphicon-download"></span>
|
16
|
<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)
|
17
|
</span>
|
18
|
</a>
|
19
|
<span class="uk-margin-large-right cursor-not-allowed" *ngIf="totalResults > csvLimit"
|
20
|
[attr.uk-tooltip]="'pos:right'"
|
21
|
[title]="'Download up to '+csvLimit+' results'">
|
22
|
<span aria-hidden="true" class="glyphicon glyphicon-download"></span>
|
23
|
<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)
|
24
|
</span>
|
25
|
<modal-loading></modal-loading>
|
26
|
<modal-alert #AlertModalCsvError></modal-alert>
|
27
|
`
|
28
|
})
|
29
|
|
30
|
export class SearchDownloadComponent {
|
31
|
@Input() totalResults:number = 0;
|
32
|
@Input() csvParams: string;
|
33
|
@Input() type: string;
|
34
|
@ViewChild(AlertModal) alertApplyAll;
|
35
|
private downloadURLAPI: string;
|
36
|
|
37
|
sub: any;
|
38
|
downloadFilePiwikSub: any;
|
39
|
|
40
|
public csvLimit: number = 0;
|
41
|
|
42
|
@ViewChild (ModalLoading) loading : ModalLoading ;
|
43
|
// Alert box when something is wrong with CSV requests
|
44
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
45
|
public isPiwikEnabled;
|
46
|
public properties:EnvProperties;
|
47
|
constructor (private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService:PiwikService) {}
|
48
|
|
49
|
ngOnInit() {
|
50
|
this.route.data
|
51
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
52
|
|
53
|
this.csvLimit = data.envSpecific.csvLimit;
|
54
|
this.downloadURLAPI = data.envSpecific.csvAPIURL;
|
55
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
56
|
});
|
57
|
}
|
58
|
|
59
|
ngOnDestroy() {
|
60
|
if(this.sub) {
|
61
|
this.sub.unsubscribe();
|
62
|
}
|
63
|
if(this.downloadFilePiwikSub) {
|
64
|
this.downloadFilePiwikSub.unsubscribe();
|
65
|
}
|
66
|
}
|
67
|
|
68
|
denialOfDownload() {
|
69
|
this.alertApplyAll.isOpen = true;
|
70
|
this.alertApplyAll.cancelButton = true;
|
71
|
this.alertApplyAll.okButton = false;
|
72
|
this.alertApplyAll.alertTitle = "Download Results in CSV";
|
73
|
this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
|
74
|
this.alertApplyAll.cancelButtonText = "Ok";
|
75
|
|
76
|
console.info("denial of Download");
|
77
|
|
78
|
}
|
79
|
downloadfile(url:string,filename:string){
|
80
|
console.log("Downloading file: "+ url);
|
81
|
this.openLoading();
|
82
|
this.setMessageLoading("Downloading CSV file");
|
83
|
|
84
|
this._reportsService.downloadCSVFile(url).subscribe(
|
85
|
data => {
|
86
|
this.closeLoading();
|
87
|
window.open(window.URL.createObjectURL(data),filename+".csv");
|
88
|
if(this.isPiwikEnabled && (typeof document !== 'undefined')){
|
89
|
this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
|
90
|
}
|
91
|
},
|
92
|
error => {
|
93
|
console.log("Error downloading the file.");
|
94
|
this.closeLoading();
|
95
|
this.confirmOpenCsvError();
|
96
|
},
|
97
|
() => console.log('Completed file download.')
|
98
|
);
|
99
|
}
|
100
|
|
101
|
|
102
|
public openLoading(){
|
103
|
if(this.loading){
|
104
|
this.loading.open();
|
105
|
}
|
106
|
}
|
107
|
public closeLoading(){
|
108
|
if(this.loading){
|
109
|
this.loading.close();
|
110
|
}
|
111
|
}
|
112
|
public setMessageLoading(message: string){
|
113
|
if(this.loading){
|
114
|
this.loading.message = message;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
public confirmOpenCsvError(){
|
119
|
this.alertCsvError.cancelButton = false;
|
120
|
this.alertCsvError.okButton = true;
|
121
|
this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
|
122
|
this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
|
123
|
this.alertCsvError.okButtonText = "OK";
|
124
|
this.alertCsvError.open();
|
125
|
}
|
126
|
|
127
|
}
|