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';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2020-11-18 17:06:27 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Component({
|
2020-07-10 10:37:17 +02:00
|
|
|
selector: 'search-download',
|
|
|
|
template: `
|
|
|
|
<a [attr.uk-tooltip]="'title: Download'
|
2019-12-03 15:10:38 +01:00
|
|
|
+ ((totalResults > csvLimit)?' the first 2000 ':' ')
|
2020-07-10 10:37:17 +02:00
|
|
|
+ 'results;'" [class]="isDisabled ? 'uk-disabled uk-link-muted' : 'uk-link-text'"
|
|
|
|
(click)="downloadfile(downloadURLAPI+'?format=csv'+csvParams,type+'-report-'+((totalResults > csvLimit)?'2000 ':totalResults))">
|
|
|
|
<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
|
|
|
|
</a>
|
2017-12-19 13:53:46 +01:00
|
|
|
<modal-loading></modal-loading>
|
|
|
|
<modal-alert #AlertModalCsvError></modal-alert>
|
2020-07-10 10:37:17 +02:00
|
|
|
`
|
2017-12-19 13:53:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export class SearchDownloadComponent {
|
2020-07-10 10:37:17 +02:00
|
|
|
@Input() isDisabled: boolean = false;
|
|
|
|
@Input() totalResults: number = 0;
|
|
|
|
@Input() csvParams: string;
|
|
|
|
@Input() type: string;
|
2020-12-18 16:09:38 +01:00
|
|
|
@Input() piwikSiteId = null;
|
2020-07-10 10:37:17 +02:00
|
|
|
@ViewChild(AlertModal) alertApplyAll: AlertModal;
|
|
|
|
downloadURLAPI: string;
|
2020-11-11 15:43:13 +01:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
public csvLimit: number = 0;
|
|
|
|
|
|
|
|
@ViewChild(ModalLoading) loading: ModalLoading;
|
|
|
|
// Alert box when something is wrong with CSV requests
|
|
|
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
|
|
|
public isPiwikEnabled;
|
|
|
|
public properties: EnvProperties;
|
|
|
|
public errorCodes: ErrorCodes = new ErrorCodes();
|
2020-11-11 15:43:13 +01:00
|
|
|
subscriptions = [];
|
2020-07-10 10:37:17 +02:00
|
|
|
constructor(private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService: PiwikService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-11-18 17:06:27 +01:00
|
|
|
|
|
|
|
this.properties = properties;
|
|
|
|
this.csvLimit = this.properties.csvLimit;
|
|
|
|
this.downloadURLAPI = this.properties.csvAPIURL;
|
|
|
|
this.isPiwikEnabled = this.properties.enablePiwikTrack;
|
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
ngOnDestroy() {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
this.alertApplyAll.open();
|
|
|
|
|
|
|
|
console.error("Error downloading file. Results are too many!");
|
|
|
|
//this.handleError("Error downloading file. Results are too many!", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadfile(url: string, filename: string) {
|
|
|
|
//var newWindow = window.open("", "_parent");
|
|
|
|
//var newWindow = window.open();
|
|
|
|
//console.log("Downloading file: "+ url);
|
|
|
|
this.openLoading();
|
|
|
|
this.setMessageLoading("Downloading CSV file");
|
2020-11-11 15:43:13 +01:00
|
|
|
|
|
|
|
this.subscriptions.push(this._reportsService.downloadCSVFile(url).subscribe(
|
2020-07-10 10:37:17 +02:00
|
|
|
data => {
|
|
|
|
this.closeLoading();
|
|
|
|
//window.open(window.URL.createObjectURL(data),filename+".csv");
|
|
|
|
//console.info("Fill window with data for csv");
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
//newWindow.location.assign(window.URL.createObjectURL(data));
|
|
|
|
|
|
|
|
//window.location.href = window.URL.createObjectURL(data);
|
|
|
|
|
|
|
|
if (this.isPiwikEnabled && (typeof document !== 'undefined')) {
|
2020-12-18 16:09:38 +01:00
|
|
|
this.subscriptions.push(this._piwikService.trackDownload(this.properties, url, this.piwikSiteId).subscribe());
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
//console.error("Error downloading the file.");
|
|
|
|
this.handleError("Error downloading file: " + filename, error);
|
|
|
|
|
|
|
|
//newWindow.close();
|
|
|
|
this.closeLoading();
|
|
|
|
this.confirmOpenCsvError();
|
|
|
|
}/*,
|
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
|
|
|
}*/
|
2020-11-11 15:43:13 +01:00
|
|
|
));
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public openLoading() {
|
|
|
|
if (this.loading) {
|
|
|
|
this.loading.open();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public closeLoading() {
|
|
|
|
if (this.loading) {
|
|
|
|
this.loading.close();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public setMessageLoading(message: string) {
|
|
|
|
if (this.loading) {
|
|
|
|
this.loading.message = message;
|
2019-02-15 13:50:24 +01:00
|
|
|
}
|
2020-07-10 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError(message: string, error) {
|
|
|
|
console.error("Search Download (component): " + message, error);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|