class CardViewController extends TimeCardsViewController { constructor(element) { super(element); this.editMode = false; 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); } viewDidDisappear() { this.setEditMode(false); } setCardData(cardData) { if (!this.onCardUpdateBound) { this.onCardUpdateBound = this.onCardUpdate.bind(this); } else { TimeCards.dataManager.removeDataListener("card", this.onCardUpdateBound); } TimeCards.dataManager.addDataListener("card", { card_id: cardData.card_id }, this.onCardUpdateBound); } onCardUpdate(cardId, card) { if (!card) { //Dismiss the view if the card is deleted. this.dismissModally(); return; } //End the editing if there is updated data. if (this.savingCardData) { this.setEditMode(false); this.savingCardData = null; } this.cardData = card; this.cardTitleLabel.innerText = card.title; this.cardDescriptionBox.innerText = card.description; this.cardVisibilityLabel.innerText = card.visibility == 1 ? "Visible to: everyone" : "Visible to: only me"; this.createdAtLabel.innerText = card.created_at; this.updatedAtLabel.innerText = card.updated_at; this.cardCategoryLabelController.setKey(card.id_category); this.cardProjectLabelController.setKey(card.id_project); this.totalTimeLabel.innerText = (Math.round(card.time_used / 60 / 60 * 100) / 100) + "h"; this.totalTimeWithoutInvoiceLabel.innerText = (Math.round(card.time_used_without_invoice / 60 / 60 * 100) / 100) + "h"; } onCloseButtonPressed(event) { if (this.editMode) { this.setEditMode(false); } else { this.dismissModally(); } } 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.id_category; this.cardVisibilitySelectController.value = this.cardData.visibility; //this.cardProjectSelectController.setValue(this.cardData.id_project); 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; } onFormSubmitted(event) { //Just prevent form submission and do nothing further. event.preventDefault(); } /** * 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.setAttribute("disabled", "true"); } else { this.editButton.removeAttribute("disabled"); } } /** * Takes the values from the input fields and saves them to the database. */ applyCardData() { var inputData = { title: this.cardTitleField.value, description: this.cardDescriptionField.value, id_category: this.cardCategorySelect.value, visibility: parseInt(this.cardVisibilitySelectController.value)/*, id_project: this.cardProjectSelect.value*/ }; var existingData = { title: this.cardData.title, description: this.cardData.description, id_category: this.cardData.id_category, visibility: this.cardData.visibility/*, id_project: this.cardData.id_project*/ }; var differenceData = { }; for (var key in inputData) { var inputValue = inputData[key]; var existingValue = existingData[key]; if (inputValue != existingValue) { differenceData[key] = inputValue; } } //Only send data to the server if anything has changed at all. if (Object.keys(differenceData).length > 0) { this.savingCardData = differenceData; TimeCards.dataManager.store("card", this.cardData.card_id, differenceData); } } onDeleteButtonPressed(event) { this.deleteCardDialog.show(); } onDeleteDialogReturned(buttonId) { if (buttonId == "delete") { TimeCards.dataManager.store("card", this.cardData.card_id, null); } } } UIKit.registerViewControllerType(CardViewController);