import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import { ActivatedRoute} from '@angular/router'; 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'; import{EnvProperties} from '../../utils/properties/env-properties'; @Component({ selector: 'search-download', template: ` (CSV) ` }) export class SearchDownloadComponent { @Input() totalResults:number = 0; @Input() csvParams: string; @Input() type: string; @ViewChild(AlertModal) alertApplyAll; private downloadURLAPI: string; sub: any; downloadFilePiwikSub: any; public csvLimit: number = 0; @ViewChild (ModalLoading) loading : ModalLoading ; // Alert box when something is wrong with CSV requests @ViewChild('AlertModalCsvError') alertCsvError; public isPiwikEnabled; constructor (private route: ActivatedRoute, private _reportsService: ReportsService, private _piwikService:PiwikService) {} ngOnInit() { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.csvLimit = data.envSpecific.csvLimit; this.downloadURLAPI = data.envSpecific.csvAPIURL; this.isPiwikEnabled = data.envSpecific.enablePiwikTrack; }); } ngOnDestroy() { if(this.sub) { this.sub.unsubscribe(); } if(this.downloadFilePiwikSub) { this.downloadFilePiwikSub.unsubscribe(); } } denialOfDownload() { this.alertApplyAll.isOpen = true; 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"; console.info("denial of Download"); } downloadfile(url:string,filename:string){ console.log("Downloading file: "+ url); this.openLoading(); this.setMessageLoading("Downloading CSV file"); this._reportsService.downloadCSVFile(url).subscribe( data => { this.closeLoading(); window.open(window.URL.createObjectURL(data),filename+".csv"); if(this.isPiwikEnabled && (typeof document !== 'undefined')){ this.downloadFilePiwikSub = this._piwikService.trackDownload(url).subscribe(); } }, error => { console.log("Error downloading the file."); this.closeLoading(); this.confirmOpenCsvError(); }, () => console.log('Completed file download.') ); } 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(); } }