2019-12-03 15:10:38 +01:00
|
|
|
import {Component, Input, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute} from '@angular/router';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {AlertModal} from '../../utils/modal/alert';
|
|
|
|
import {ReportsService} from '../../services/reports.service';
|
|
|
|
import {ModalLoading} from '../../utils/modal/loading.component';
|
|
|
|
import {PiwikService} from '../../utils/piwik/piwik.service';
|
2019-12-03 15:10:38 +01:00
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2019-02-12 12:15:21 +01:00
|
|
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
|
|
|
|
2019-12-03 15:10:38 +01:00
|
|
|
import 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'search-download',
|
|
|
|
template: `
|
2019-12-03 15:10:38 +01:00
|
|
|
<a [attr.uk-tooltip]="'title: Download'
|
|
|
|
+ ((totalResults > csvLimit)?' the first 2000 ':' ')
|
2020-03-28 01:33:55 +01:00
|
|
|
+ 'results'" [class]="isDisabled ? 'uk-disabled uk-link-muted' : 'uk-link-text'">
|
2020-02-24 14:33:34 +01:00
|
|
|
<!-- type: {{type}}-->
|
|
|
|
<span class="clickable" (click)="downloadfile(downloadURLAPI+'?format=csv'+csvParams,type+'-report-'+((totalResults > csvLimit)?'2000 ':totalResults))">
|
2017-12-19 13:53:46 +01:00
|
|
|
<span aria-hidden="true" class="glyphicon glyphicon-download"></span>
|
2020-02-24 14:33:34 +01:00
|
|
|
<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>
|
|
|
|
Download Results
|
2017-12-19 13:53:46 +01:00
|
|
|
</span>
|
2018-09-26 15:56:21 +02:00
|
|
|
</a>
|
2017-12-19 13:53:46 +01:00
|
|
|
<modal-loading></modal-loading>
|
|
|
|
<modal-alert #AlertModalCsvError></modal-alert>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
export class SearchDownloadComponent {
|
2020-03-28 01:33:55 +01:00
|
|
|
@Input() isDisabled: boolean = false;
|
2017-12-19 13:53:46 +01:00
|
|
|
@Input() totalResults:number = 0;
|
|
|
|
@Input() csvParams: string;
|
|
|
|
@Input() type: string;
|
2019-11-22 09:42:36 +01:00
|
|
|
@ViewChild(AlertModal) alertApplyAll: AlertModal;
|
2020-03-31 11:07:21 +02:00
|
|
|
downloadURLAPI: string;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
sub: any;
|
|
|
|
downloadFilePiwikSub: any;
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public csvLimit: number = 0;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@ViewChild (ModalLoading) loading : ModalLoading ;
|
|
|
|
// Alert box when something is wrong with CSV requests
|
|
|
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
2018-02-05 14:14:59 +01:00
|
|
|
public isPiwikEnabled;
|
2018-02-15 11:36:12 +01:00
|
|
|
public properties:EnvProperties;
|
2019-02-12 12:15:21 +01:00
|
|
|
public errorCodes:ErrorCodes = new ErrorCodes();
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
constructor (private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService:PiwikService) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
2018-02-05 14:14:59 +01:00
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
2018-11-08 16:27:19 +01:00
|
|
|
this.properties = data.envSpecific;
|
2018-02-05 14:14:59 +01:00
|
|
|
this.csvLimit = data.envSpecific.csvLimit;
|
|
|
|
this.downloadURLAPI = data.envSpecific.csvAPIURL;
|
|
|
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
if(this.downloadFilePiwikSub) {
|
|
|
|
this.downloadFilePiwikSub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
denialOfDownload() {
|
|
|
|
this.alertApplyAll.cancelButton = true;
|
|
|
|
this.alertApplyAll.okButton = false;
|
|
|
|
this.alertApplyAll.alertTitle = "Download Results in CSV";
|
|
|
|
this.alertApplyAll.message = "Sorry, but the results are too many! Use the api instead!";
|
|
|
|
this.alertApplyAll.cancelButtonText = "Ok";
|
2019-11-22 09:42:36 +01:00
|
|
|
this.alertApplyAll.open();
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-02-15 13:50:24 +01:00
|
|
|
console.error("Error downloading file. Results are too many!");
|
|
|
|
//this.handleError("Error downloading file. Results are too many!", err);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
downloadfile(url:string,filename:string){
|
2018-11-08 16:27:19 +01:00
|
|
|
//var newWindow = window.open("", "_parent");
|
|
|
|
//var newWindow = window.open();
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.log("Downloading file: "+ url);
|
2017-12-19 13:53:46 +01:00
|
|
|
this.openLoading();
|
|
|
|
this.setMessageLoading("Downloading CSV file");
|
|
|
|
|
|
|
|
this._reportsService.downloadCSVFile(url).subscribe(
|
|
|
|
data => {
|
|
|
|
this.closeLoading();
|
2018-11-08 16:27:19 +01:00
|
|
|
//window.open(window.URL.createObjectURL(data),filename+".csv");
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.info("Fill window with data for csv");
|
2019-02-15 16:03:48 +01:00
|
|
|
if(typeof document !== 'undefined'){
|
2018-11-08 16:27:19 +01:00
|
|
|
var url = window.URL.createObjectURL(data);
|
|
|
|
var a = window.document.createElement('a');
|
|
|
|
window.document.body.appendChild(a);
|
|
|
|
a.setAttribute('style', 'display: none');
|
|
|
|
a.href = url;
|
|
|
|
a.download = filename+".csv";
|
|
|
|
a.click();
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
a.remove(); // remove the element
|
2019-02-15 16:03:48 +01:00
|
|
|
}
|
2018-11-08 16:27:19 +01:00
|
|
|
//newWindow.location.assign(window.URL.createObjectURL(data));
|
|
|
|
|
|
|
|
//window.location.href = window.URL.createObjectURL(data);
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
if(this.isPiwikEnabled && (typeof document !== 'undefined')){
|
2018-02-15 11:36:12 +01:00
|
|
|
this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
2019-02-15 13:50:24 +01:00
|
|
|
//console.error("Error downloading the file.");
|
|
|
|
this.handleError("Error downloading file: "+filename, error);
|
|
|
|
|
2018-11-08 16:27:19 +01:00
|
|
|
//newWindow.close();
|
2017-12-19 13:53:46 +01:00
|
|
|
this.closeLoading();
|
|
|
|
this.confirmOpenCsvError();
|
2019-02-14 11:52:06 +01:00
|
|
|
}/*,
|
2018-11-08 16:27:19 +01:00
|
|
|
() => {
|
|
|
|
console.log('Completed file download.');
|
|
|
|
//setTimeout(function(){ newWindow.close(); }, 500);
|
2019-02-14 11:44:30 +01:00
|
|
|
}*/
|
2017-12-19 13:53:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public openLoading(){
|
|
|
|
if(this.loading){
|
|
|
|
this.loading.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public closeLoading(){
|
|
|
|
if(this.loading){
|
|
|
|
this.loading.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public setMessageLoading(message: string){
|
|
|
|
if(this.loading){
|
|
|
|
this.loading.message = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmOpenCsvError(){
|
|
|
|
this.alertCsvError.cancelButton = false;
|
|
|
|
this.alertCsvError.okButton = true;
|
|
|
|
this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
|
|
|
|
this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
|
|
|
|
this.alertCsvError.okButtonText = "OK";
|
|
|
|
this.alertCsvError.open();
|
|
|
|
}
|
|
|
|
|
2019-02-15 13:50:24 +01:00
|
|
|
private handleError(message: string, error) {
|
|
|
|
console.error("Search Download (component): "+message, error);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|