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
|
import {Subscriber} from "rxjs";
|
12
|
import {properties} from "../../../../environments/environment";
|
13
|
|
14
|
@Component({
|
15
|
selector: 'search-download',
|
16
|
template: `
|
17
|
<a [attr.uk-tooltip]="'title: Download'
|
18
|
+ ((totalResults > csvLimit)?' the first 2000 ':' ')
|
19
|
+ 'results;'" [class]="isDisabled ? 'uk-disabled uk-link-muted' : 'uk-link-text'"
|
20
|
(click)="downloadfile(downloadURLAPI+'?format=csv'+csvParams,type+'-report-'+((totalResults > csvLimit)?'2000 ':totalResults))">
|
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"
|
23
|
ratio="1">
|
24
|
<polyline fill="none" stroke="#000" points="14,10 9.5,14.5 5,10"></polyline>
|
25
|
<rect x="3" y="17" width="13" height="1"></rect>
|
26
|
<line fill="none" stroke="#000" x1="9.5" y1="13.91" x2="9.5" y2="3"></line>
|
27
|
</svg>
|
28
|
</span>
|
29
|
Download Results
|
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
|
@Input() piwikSiteId = null;
|
42
|
@ViewChild(AlertModal) alertApplyAll: AlertModal;
|
43
|
downloadURLAPI: string;
|
44
|
|
45
|
public csvLimit: number = 0;
|
46
|
|
47
|
@ViewChild(ModalLoading) loading: ModalLoading;
|
48
|
// Alert box when something is wrong with CSV requests
|
49
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
50
|
public isPiwikEnabled;
|
51
|
public properties: EnvProperties;
|
52
|
public errorCodes: ErrorCodes = new ErrorCodes();
|
53
|
subscriptions = [];
|
54
|
constructor(private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService: PiwikService) {
|
55
|
}
|
56
|
|
57
|
ngOnInit() {
|
58
|
|
59
|
this.properties = properties;
|
60
|
this.csvLimit = this.properties.csvLimit;
|
61
|
this.downloadURLAPI = this.properties.csvAPIURL;
|
62
|
this.isPiwikEnabled = this.properties.enablePiwikTrack;
|
63
|
|
64
|
}
|
65
|
|
66
|
ngOnDestroy() {
|
67
|
this.subscriptions.forEach(subscription => {
|
68
|
if (subscription instanceof Subscriber) {
|
69
|
subscription.unsubscribe();
|
70
|
}
|
71
|
});
|
72
|
}
|
73
|
|
74
|
denialOfDownload() {
|
75
|
this.alertApplyAll.cancelButton = true;
|
76
|
this.alertApplyAll.okButton = false;
|
77
|
this.alertApplyAll.alertTitle = "Download Results in CSV";
|
78
|
this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
|
79
|
this.alertApplyAll.cancelButtonText = "Ok";
|
80
|
this.alertApplyAll.open();
|
81
|
|
82
|
console.error("Error downloading file. Results are too many!");
|
83
|
//this.handleError("Error downloading file. Results are too many!", err);
|
84
|
}
|
85
|
|
86
|
downloadfile(url: string, filename: string) {
|
87
|
//var newWindow = window.open("", "_parent");
|
88
|
//var newWindow = window.open();
|
89
|
//console.log("Downloading file: "+ url);
|
90
|
this.openLoading();
|
91
|
this.setMessageLoading("Downloading CSV file");
|
92
|
|
93
|
this.subscriptions.push(this._reportsService.downloadCSVFile(url).subscribe(
|
94
|
data => {
|
95
|
this.closeLoading();
|
96
|
//window.open(window.URL.createObjectURL(data),filename+".csv");
|
97
|
//console.info("Fill window with data for csv");
|
98
|
if (typeof document !== 'undefined') {
|
99
|
var url = window.URL.createObjectURL(data);
|
100
|
var a = window.document.createElement('a');
|
101
|
window.document.body.appendChild(a);
|
102
|
a.setAttribute('style', 'display: none');
|
103
|
a.href = url;
|
104
|
a.download = filename + ".csv";
|
105
|
a.click();
|
106
|
window.URL.revokeObjectURL(url);
|
107
|
a.remove(); // remove the element
|
108
|
}
|
109
|
//newWindow.location.assign(window.URL.createObjectURL(data));
|
110
|
|
111
|
//window.location.href = window.URL.createObjectURL(data);
|
112
|
|
113
|
if (this.isPiwikEnabled && (typeof document !== 'undefined')) {
|
114
|
this.subscriptions.push(this._piwikService.trackDownload(this.properties, "searchDownload", this.piwikSiteId).subscribe());
|
115
|
}
|
116
|
},
|
117
|
error => {
|
118
|
//console.error("Error downloading the file.");
|
119
|
this.handleError("Error downloading file: " + filename, error);
|
120
|
|
121
|
//newWindow.close();
|
122
|
this.closeLoading();
|
123
|
this.confirmOpenCsvError();
|
124
|
}/*,
|
125
|
() => {
|
126
|
console.log('Completed file download.');
|
127
|
//setTimeout(function(){ newWindow.close(); }, 500);
|
128
|
}*/
|
129
|
));
|
130
|
}
|
131
|
|
132
|
|
133
|
public openLoading() {
|
134
|
if (this.loading) {
|
135
|
this.loading.open();
|
136
|
}
|
137
|
}
|
138
|
|
139
|
public closeLoading() {
|
140
|
if (this.loading) {
|
141
|
this.loading.close();
|
142
|
}
|
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
|
}
|