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