class ReportView extends View { constructor(element = null) { super(element); this.selectedReport = "project"; } viewDidLoad() { TimeCards.reportManager.addEventListener("reportloaded", this.onReportLoaded.bind(this)); TimeCards.reportManager.addEventListener("reporterror", this.onReportLoadingError.bind(this)); this.selectedReport = this.reportSelect.value; //Construct the filters. var filterGroups = TimeCards.reportManager.reports[this.selectedReport].filterParameters; this.filterInputs = { }; for (var i = 0; i < filterGroups.length; i++) { //Create the table row and cell for this group. var row = document.createElement("tr"); var cell = document.createElement("td"); row.appendChild(cell); var filters = filterGroups[i]; for (var key in filters) { var filter = filters[key]; //Create the label. var label = document.createElement("label"); label.for = "filter-" + key; label.innerText = (filter.type == "checkbox" || cell.children.length > 0 ? " " : "") + filter.label + (filter.type == "checkbox" ? "" : " "); //Create the element itself. var input = document.createElement(filter.type.indexOf("select") != -1 ? "select" : "input"); input.id = "filter-" + key; if (filter.type == "select") { //Create the options. for (var value in filter.values) { var option = document.createElement("option"); option.value = value; option.innerText = filter.values[value]; input.appendChild(option); } } else if (filter.type == "project_select") { //Create a project select controller for the dropdown. var projectSelectController = new ProjectSelect(input); projectSelectController.addOption("all", "all"); } else if (filter.type == "date") { input.type = "date"; } if (filter.default) { if (filter.type == "checkbox") { input.checked = filter.default; } else { input.value = filter.default; } } //Add the input first if it is a checkbox. Otherwise, the order is the other way round. if (filter.type == "checkbox") { cell.appendChild(input); cell.appendChild(label); } else { cell.appendChild(label); cell.appendChild(input); } this.filterInputs[key] = input; } this.reportSettingsTableBody.appendChild(row); } } onLoadButtonPressed() { this.setLoading(true); //Construct the options from the filter inputs. this.options = { }; for (var key in this.filterInputs) { if (this.filterInputs[key].nodeName == "input" && this.filterInputs[key].type == "checkbox") { this.options[key] = this.filterInputs[key].checked; } else { this.options[key] = this.filterInputs[key].value; } } //Add the corresponding key for the report. //TODO: This needs to be moved somewhere else! if (this.selectedReport == "project") { this.options.project_id = this.projectsViewController.selectedProject.project_id; } else if (this.selectedReport == "user") { this.options.user_id = this.usersViewController.selectedUser.user_id; } TimeCards.reportManager.loadReport(this.selectedReport, this.options); } setLoading(loading) { this.loadingIndicator.style.display = loading ? "" : "none"; this.loadButton.disabled = loading; this.downloadButton.disabled = loading; } onReportLoaded(event) { //Do nothing if this is not the correct report view. if (!this.reportSelect.value || event.report.name != TimeCards.reportManager.reports[this.reportSelect.value].name) { return; } this.setLoading(false); this.reportTableController.populateWithReport(event.report); } onReportLoadingError(event) { alert("An error occurred while loading the requested report."); this.setLoading(false); //Only reenable the download button if there is data inside the table from a previous report request. this.downloadButton.disabled = (this.reportTableController.data ? false : true); } 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; var fileName = "Work Time Report"; if (this.selectedReport == "project") { fileName += " for " + this.projectsViewController.selectedProject.name; } else if (this.selectedReport == "user") { fileName += " for " + this.usersViewController.selectedUser.full_name; } if (("from_date" in this.options) && ("to_date" in this.options)) { fileName += " (" + this.options["from_date"] + "~" + this.options["to_date"] + ")"; } fileName += ".csv"; link.download = fileName; document.body.appendChild(link); link.click(); setTimeout(function() { document.body.removeChild(link); URL.revokeObjectURL(url); }, 0); } } UIKit.registerViewType(ReportView);