class CardViewController extends OnTimeViewController { constructor(element) { super(element); this.editMode = false; OnTime.projectManager.addEventListener("projectchanged", this.onProjectChanged.bind(this)); OnTime.cardManager.addEventListener("cardchanged", this.onCardChanged.bind(this)); OnTime.cardManager.addEventListener("categorychanged", this.onCategoryChanged.bind(this)); OnTime.cardManager.addEventListener("cardremoved", this.onCardRemoved.bind(this)); this.deleteCardDialog = new Dialog("

Are you sure you want to delete this card?

Already recorded time periods will not be affected by the deletion of this card.

", this.onDeleteDialogReturned.bind(this), Dialog.BUTTONS_DESTRUCTIVE); } setCardData(cardData) { this.cardData = cardData; this.cardTitleLabel.innerText = this.cardData.title; this.cardDescriptionBox.innerText = this.cardData.description; this.createdAtLabel.innerText = this.cardData.created_at; this.updatedAtLabel.innerText = this.cardData.updated_at; var project = OnTime.projectManager.getProjectByKey(this.cardData.project_key); if (project) { this.cardProjectLabel.innerText = project.name; } else { this.cardProjectLabel.innerText = ""; } var category = OnTime.cardManager.getCategoryByKey(this.cardData.category_key); if (category) { this.cardCategoryLabel.innerText = category.name; } this.totalTimeLabel.innerText = (Math.round(this.cardData.time_used / 60 / 60 * 100) / 100) + "h"; this.totalTimeWithoutInvoiceLabel.innerText = (Math.round(this.cardData.time_used_without_invoice / 60 / 60 * 100) / 100) + "h"; } onCategoryChanged(event) { if (!this.cardData) { return; } if (event.category.category_key == this.cardData.category_key) { this.cardCategoryLabel.innerText = event.category.name; } } onCardChanged(event) { if (!this.cardData) { return; } if (event.card.card_key == this.cardData.card_key) { /*this.cardTitleLabel.innerText = event.card.title; this.cardDescriptionBox.innerText = event.card.description;*/ this.setCardData(event.card); } } onProjectChanged(event) { if (!this.cardData) { return; } if (event.project.project_key == this.cardData.project_key) { this.cardProjectLabel.innerText = event.project.name; } } onCardRemoved(event) { if (event.card.card_key == this.cardData.card_key) { this.dismissModally(); } } onCloseButtonPressed(event) { this.dismissModally(); this.setEditMode(false); } onEditButtonPressed(event) { if (this.editMode) { this.applyCardData(); this.setEditMode(false); } else { this.setEditMode(true); } } setEditMode(editMode) { if (editMode) { this.cardTitleField.value = this.cardData.title; this.cardDescriptionField.value = this.cardData.description; this.cardCategorySelect.value = this.cardData.category_key; this.cardProjectSelect.value = this.cardData.project_key; this.element.classList.add("edit"); this.editButton.innerText = "Done"; this.closeButton.innerText = "Cancel"; } else { this.element.classList.remove("edit"); this.editButton.innerText = "Edit"; this.closeButton.innerText = "Close"; this.editButton.removeAttribute("disabled"); } this.editMode = editMode; } /** * Called when any change was made to the input fields in the edit view. * Disables the done button as long as there are invalid inputs. */ checkInputFields(event) { if (!event.currentTarget.value) { this.editButton.disabled = true; } else { this.editButton.disabled = false; } } /** * Takes the values from the input fields and saves them to the database. */ applyCardData() { var inputData = { title: this.cardTitleField.value, description: this.cardDescriptionField.value, category_key: this.cardCategorySelect.value, project_key: this.cardProjectSelect.value }; var existingData = { title: this.cardData.title, description: this.cardData.description, category_key: this.cardData.category_key, project_key: this.cardData.project_key }; var differenceData = { }; for (var key in inputData) { var inputValue = inputData[key]; var existingValue = existingData[key]; if (inputValue != existingValue) { differenceData[key] = inputValue; } } if (Object.keys(differenceData).length > 0) { differenceData.card_key = this.cardData.card_key; this.savingCardData = differenceData; OnTime.cardManager.saveCardData(differenceData, this.onCardSaved.bind(this), this.onCardSaveError.bind(this)); } } onCardSaved(data) { this.setEditMode(false); OnTime.cardManager.updateCard(this.savingCardData); this.savingCardData = null; } onCardSaveError(request, status, error) { alert("Could not save the card.\n\n" + error); } onDeleteButtonPressed(event) { this.deleteCardDialog.show(); } onDeleteDialogReturned(buttonId) { if (buttonId == "delete") { OnTime.cardManager.deleteCard(this.cardData.card_key); } } } UIKit.registerViewControllerType(CardViewController);