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]="isDisabled ? 'uk-disabled uk-link-muted' : 'uk-link-text'">
|
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() isDisabled: boolean = false;
|
38
|
@Input() totalResults:number = 0;
|
39
|
@Input() csvParams: string;
|
40
|
@Input() type: string;
|
41
|
@ViewChild(AlertModal) alertApplyAll: AlertModal;
|
42
|
downloadURLAPI: string;
|
43
|
|
44
|
sub: any;
|
45
|
downloadFilePiwikSub: any;
|
46
|
|
47
|
public csvLimit: number = 0;
|
48
|
|
49
|
@ViewChild (ModalLoading) loading : ModalLoading ;
|
50
|
// Alert box when something is wrong with CSV requests
|
51
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
52
|
public isPiwikEnabled;
|
53
|
public properties:EnvProperties;
|
54
|
public errorCodes:ErrorCodes = new ErrorCodes();
|
55
|
|
56
|
constructor (private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService:PiwikService) {}
|
57
|
|
58
|
ngOnInit() {
|
59
|
this.route.data
|
60
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
61
|
this.properties = data.envSpecific;
|
62
|
this.csvLimit = data.envSpecific.csvLimit;
|
63
|
this.downloadURLAPI = data.envSpecific.csvAPIURL;
|
64
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
65
|
});
|
66
|
}
|
67
|
|
68
|
ngOnDestroy() {
|
69
|
if(this.sub) {
|
70
|
this.sub.unsubscribe();
|
71
|
}
|
72
|
if(this.downloadFilePiwikSub) {
|
73
|
this.downloadFilePiwikSub.unsubscribe();
|
74
|
}
|
75
|
}
|
76
|
|
77
|
denialOfDownload() {
|
78
|
this.alertApplyAll.cancelButton = true;
|
79
|
this.alertApplyAll.okButton = false;
|
80
|
this.alertApplyAll.alertTitle = "Download Results in CSV";
|
81
|
this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
|
82
|
this.alertApplyAll.cancelButtonText = "Ok";
|
83
|
this.alertApplyAll.open();
|
84
|
|
85
|
console.error("Error downloading file. Results are too many!");
|
86
|
//this.handleError("Error downloading file. Results are too many!", err);
|
87
|
}
|
88
|
downloadfile(url:string,filename:string){
|
89
|
//var newWindow = window.open("", "_parent");
|
90
|
//var newWindow = window.open();
|
91
|
//console.log("Downloading file: "+ url);
|
92
|
this.openLoading();
|
93
|
this.setMessageLoading("Downloading CSV file");
|
94
|
|
95
|
this._reportsService.downloadCSVFile(url).subscribe(
|
96
|
data => {
|
97
|
this.closeLoading();
|
98
|
//window.open(window.URL.createObjectURL(data),filename+".csv");
|
99
|
//console.info("Fill window with data for csv");
|
100
|
if(typeof document !== 'undefined'){
|
101
|
var url = window.URL.createObjectURL(data);
|
102
|
var a = window.document.createElement('a');
|
103
|
window.document.body.appendChild(a);
|
104
|
a.setAttribute('style', 'display: none');
|
105
|
a.href = url;
|
106
|
a.download = filename+".csv";
|
107
|
a.click();
|
108
|
window.URL.revokeObjectURL(url);
|
109
|
a.remove(); // remove the element
|
110
|
}
|
111
|
//newWindow.location.assign(window.URL.createObjectURL(data));
|
112
|
|
113
|
//window.location.href = window.URL.createObjectURL(data);
|
114
|
|
115
|
if(this.isPiwikEnabled && (typeof document !== 'undefined')){
|
116
|
this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
|
117
|
}
|
118
|
},
|
119
|
error => {
|
120
|
//console.error("Error downloading the file.");
|
121
|
this.handleError("Error downloading file: "+filename, error);
|
122
|
|
123
|
//newWindow.close();
|
124
|
this.closeLoading();
|
125
|
this.confirmOpenCsvError();
|
126
|
}/*,
|
127
|
() => {
|
128
|
console.log('Completed file download.');
|
129
|
//setTimeout(function(){ newWindow.close(); }, 500);
|
130
|
}*/
|
131
|
);
|
132
|
}
|
133
|
|
134
|
|
135
|
public openLoading(){
|
136
|
if(this.loading){
|
137
|
this.loading.open();
|
138
|
}
|
139
|
}
|
140
|
public closeLoading(){
|
141
|
if(this.loading){
|
142
|
this.loading.close();
|
143
|
}
|
144
|
}
|
145
|
public setMessageLoading(message: string){
|
146
|
if(this.loading){
|
147
|
this.loading.message = message;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
public confirmOpenCsvError(){
|
152
|
this.alertCsvError.cancelButton = false;
|
153
|
this.alertCsvError.okButton = true;
|
154
|
this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
|
155
|
this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
|
156
|
this.alertCsvError.okButtonText = "OK";
|
157
|
this.alertCsvError.open();
|
158
|
}
|
159
|
|
160
|
private handleError(message: string, error) {
|
161
|
console.error("Search Download (component): "+message, error);
|
162
|
}
|
163
|
}
|