class ProjectsViewController extends TimeCardsViewController { constructor(element) { super(element); this.projectCells = { }; TimeCards.dataManager.addDataListener("project", { deleted: 0 }, this.onProjectUpdate.bind(this)); this.deleteProjectDialog = new Dialog("Are you sure you want to delete this project?

Already recorded time periods will not be deleted, but they will no longer be associated with this project.", this.onDeleteDialogReturned.bind(this), Dialog.BUTTONS_DESTRUCTIVE); } viewDidLoad() { this.projectsTableController.addEventListener("cellselected", this.onCellSelected.bind(this)); this.newProjectCell = new TableViewCell(); this.newProjectCell.titleLabel.innerText = "New Project"; //Give the report view a reference to the view controller. this.reportsViewController.projectsViewController = this; } onProjectUpdate(projectId, project, sortedBefore) { if (!project) { //Handle the deleted project. if (!(projectId in this.projectCells)) { return; } var projectCell = this.projectCells[projectId]; this.projectsTableController.removeCell(projectCell); delete this.projectCells[projectId]; } else if (!(projectId in this.projectCells)) { //Handle the added project. var projectCell = new TableViewCell(); projectCell.titleLabel.innerText = project.name; projectCell.subtitleLabel.innerText = project.description; projectCell.project = project; this.projectsTableController.addCell(projectCell, project.completed, (sortedBefore && TimeCards.dataManager.getEntity("project", sortedBefore).completed == project.completed ? this.projectCells[sortedBefore] : null)); this.projectCells[projectId] = projectCell; if (this.waitingForProjectCreation) { this.waitingForProjectCreation = false; this.projectsTableController.selectCell(projectCell); } } else { //Handle the changed project. var projectCell = this.projectCells[projectId]; projectCell.titleLabel.innerText = project.name; projectCell.subtitleLabel.innerText = project.description; projectCell.project = project; //Move the cell to the appropriate section. this.projectsTableController.addCell(projectCell, project.completed, (sortedBefore && TimeCards.dataManager.getEntity("project", sortedBefore).completed == project.completed ? this.projectCells[sortedBefore] : null)); } } onCellSelected(event) { if (!event.cell) { this.projectSettingsSheet.style.display = "none"; this.selectedProject = null; } else { //Hide the new project cell if another cell was selected. if (this.isNewProject && event.cell != this.newProjectCell) { this.onDeleteButtonPressed(event); } this.setProjectData(event.cell.project); this.projectSettingsSheet.style.display = ""; } } onFieldChanged(event) { if (this.projectNameField.value && this.projectNameField.value.length > 0 && this.projectOwnerSelect.value) { var project = { name: this.projectNameField.value, description: this.projectDescriptionField.value, id_user: this.projectOwnerSelect.value, completed: this.projectCompleteCheckbox.checked ? 1 : 0 }; if (this.isNewProject) { if (!this.waitingForProjectCreation) { TimeCards.dataManager.store("project", project); this.waitingForProjectCreation = true; } } else { TimeCards.dataManager.store("project", this.selectedProject.project_id, project); } } } setProjectData(project) { this.selectedProject = project; this.projectNameField.value = project.name; this.projectDescriptionField.value = project.description; this.projectOwnerSelect.value = project.id_user; this.projectCompleteCheckbox.checked = project.completed; //Disable the fields if the session user has only read access to them. var disabled = !(Authentication.isResourceAvailable("project_all", "w") || project.id_user == Authentication.currentUser.user_id); this.projectNameField.disabled = disabled; this.projectDescriptionField.disabled = disabled; this.projectOwnerSelect.disabled = disabled; this.projectCompleteCheckbox.disabled = disabled; this.deleteProjectButton.disabled = disabled; //Display the correct access info labels. this.readInfoLabelOwnProject.style.display = (project.id_user == Authentication.currentUser.user_id ? "" : "none"); this.readInfoLabelAssignedProject.style.display = (project.id_user != Authentication.currentUser.user_id && (project.project_id in Authentication.currentUser.projects) ? "" : "none"); this.readInfoLabelOtherProject.style.display = (project.id_user != Authentication.currentUser.user_id && !(project.project_id in Authentication.currentUser.projects) ? "" : "none"); this.writeInfoLabelOwnProject.style.display = (project.id_user == Authentication.currentUser.user_id && !Authentication.isResourceAvailable("project", "w") ? "" : "none"); this.writeInfoLabelAssignedProject.style.display = (project.id_user != Authentication.currentUser.user_id && (project.project_id in Authentication.currentUser.projects) && !Authentication.isResourceAvailable("project_assigned", "w") && !Authentication.isResourceAvailable("project_all", "w") ? "" : "none"); this.writeInfoLabelOtherProject.style.display = (project.id_user != Authentication.currentUser.user_id && !(project.project_id in Authentication.currentUser.projects) && !Authentication.isResourceAvailable("project_all", "w") ? "" : "none"); //Load the report automatically if the user has the permission to do so. /*if (!this.isNewProject && Authentication.isResourceAvailable("report_project", "r")) { this.reportsViewController.loadButton.click(); }*/ //NOTE: Disabled because this causes a lot of unnneccessary report requests. Maybe this might be re-enabled in the future, but only when the user is in the reports tab. //Show the settings tab if it is a new project. if (this.isNewProject) { this.tabViewController.selectTab("settings"); } } onDeleteButtonPressed(event) { if (this.isNewProject) { this.isNewProject = false; this.projectSettingsSheet.classList.remove("new-project"); this.projectsTableController.removeCell(this.newProjectCell); } else { this.deleteProjectDialog.show(); } } onDeleteDialogReturned(buttonId) { if (buttonId == "delete") { TimeCards.dataManager.delete("project", this.selectedProject.project_id); } } onCreateButtonPressed(event) { this.isNewProject = true; this.projectSettingsSheet.classList.add("new-project"); this.newProjectCell.project = { name: "", description: "", id_user: Authentication.currentUser.user_id, completed: false }; this.projectsTableController.addCell(this.newProjectCell); this.projectsTableController.selectCell(this.newProjectCell); this.projectNameField.focus(); } } UIKit.registerViewControllerType(ProjectsViewController);