class ReportView extends View { constructor(element = null) { super(element); this.selectedReport = "project"; } viewDidLoad() { var currentDate = new Date(); currentDate.setHours(0); currentDate.setMinutes(0); currentDate.setSeconds(0); currentDate.setMonth(currentDate.getMonth() - 1); currentDate.setDate(1); var lastMonthStartDate = new Date(currentDate.getTime()); currentDate.setMonth(currentDate.getMonth() + 1); currentDate.setSeconds(-1); var lastMonthEndDate = new Date(currentDate.getTime()); this.fromDateField.value = lastMonthStartDate.getFullYear() + "-" + this.addLeadingZero(lastMonthStartDate.getMonth() + 1) + "-" + this.addLeadingZero(lastMonthStartDate.getDate()); this.toDateField.value = lastMonthEndDate.getFullYear() + "-" + this.addLeadingZero(lastMonthEndDate.getMonth() + 1) + "-" + this.addLeadingZero(lastMonthEndDate.getDate()); OnTime.reportManager.addEventListener("reportloaded", this.onReportLoaded.bind(this)); OnTime.reportManager.addEventListener("reporterror", this.onReportLoadingError.bind(this)); } addLeadingZero(value) { value = "" + value; if (value.length < 2) { value = "0" + value; } return value; } onLoadButtonPressed() { this.loadButton.disabled = true; this.downloadButton.disabled = true; var options = { from_date: this.fromDateField.value, to_date: this.toDateField.value, project_key: this.projectsViewController.selectedProject.project_key }; OnTime.reportManager.loadReport(this.selectedReport, options); } onReportLoaded(event) { this.loadButton.disabled = false; this.downloadButton.disabled = false; this.reportTableController.populateWithReport(event.report); } onReportLoadingError(event) { alert("An error occurred while loading the requested report."); this.loadButton.disabled = false; //Only reenable the download button if there is data inside the table from a previous report request. if (this.reportTableController.data) { this.downloadButton.disabled = false; } } onDownloadButtonPressed(event) { var csvString = this.reportTableController.getCSVData(); var blob = new Blob([csvString], { type: "text/csv;charset=utf-8" }); var link = document.createElement("a"); var url = URL.createObjectURL(blob); link.href = url; link.download = "Work Time Report (" + this.fromDateField.value + " – " + this.toDateField.value + ").csv"; document.body.appendChild(link); link.click(); setTimeout(function() { document.body.removeChild(link); URL.revokeObjectURL(url); }, 0); } } UIKit.registerViewType(ReportView);