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: `
|
2023-07-12 14:05:25 +02:00
|
|
|
<button [attr.uk-tooltip]="'title: Download' + ((totalResults > csvLimit)?' the first 2000 ':' ') + 'results;'"
|
|
|
|
class="uk-button uk-button-link uk-flex uk-flex-middle" [class.uk-disabled]="isDisabled"
|
|
|
|
[disabled]="isDisabled"
|
|
|
|
(click)="downloadfile(downloadURLAPI+'?format=csv'+csvParams,type+'-report-'+((totalResults > csvLimit)?'2000 ':totalResults))">
|
|
|
|
<icon name="download" [flex]="true"></icon>
|
|
|
|
<span class="uk-margin-small-left">Download Results</span>
|
|
|
|
</button>
|
|
|
|
<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;
|
|
|
|
@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;
|
2023-07-12 14:05:25 +02:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
@ViewChild(ModalLoading) loading: ModalLoading;
|
|
|
|
// Alert box when something is wrong with CSV requests
|
|
|
|
@ViewChild('AlertModalCsvError') alertCsvError;
|
|
|
|
public properties: EnvProperties;
|
|
|
|
public errorCodes: ErrorCodes = new ErrorCodes();
|
2020-11-11 15:43:13 +01:00
|
|
|
subscriptions = [];
|
2023-07-12 14:05:25 +02:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
constructor(private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService: PiwikService) {
|
|
|
|
}
|
2020-11-18 17:06:27 +01:00
|
|
|
|
2023-07-12 14:05:25 +02:00
|
|
|
ngOnInit() {
|
|
|
|
this.properties = properties;
|
|
|
|
this.csvLimit = this.properties.csvLimit;
|
|
|
|
this.downloadURLAPI = this.properties.csvAPIURL;
|
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
|
|
|
}
|
2023-07-12 14:05:25 +02:00
|
|
|
|
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();
|
2023-07-12 14:05:25 +02:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
console.error("Error downloading file. Results are too many!");
|
|
|
|
//this.handleError("Error downloading file. Results are too many!", err);
|
|
|
|
}
|
2023-07-12 14:05:25 +02:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
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(
|
2023-07-12 14:05:25 +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);
|
|
|
|
|
|
|
|
this.subscriptions.push(this._piwikService.trackDownload(this.properties, "searchDownload").subscribe());
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2023-07-12 14:05:25 +02: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
|
|
|
}
|
2023-07-12 14:05:25 +02: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
|
|
|
}
|
2023-07-12 14:05:25 +02: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
|
|
|
}
|
2023-07-12 14:05:25 +02: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();
|
|
|
|
}
|
2023-07-12 14:05:25 +02:00
|
|
|
|
2020-07-10 10:37:17 +02:00
|
|
|
private handleError(message: string, error) {
|
|
|
|
console.error("Search Download (component): " + message, error);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|