class DeleteCategoryViewController extends TimeCardsViewController { constructor(element) { super(element); TimeCards.dataManager.addDataListener("card_category", null, this.onCategoryUpdate.bind(this)); TimeCards.dataManager.addStoreErrorListener("card_category", this.onCategoryStoreError.bind(this)); } setCategory(category) { this.category = category; this.categoryNameLabel1.innerText = category.name; this.categoryNameLabel2.innerText = category.name; //Show or hide the migration box depending on whether there are cards in the category or not. this.migrationBox.style.display = (Object.keys(TimeCards.dataManager.getEntities("card", { id_category: this.category.category_id })).length > 0 ? "" : "none"); var previouslySelectedCategory = this.categoryDropdown.value; //Remove all but the first option of the category dropdown. for (var i = this.categoryDropdown.children.length - 1; i > 0; i--) { this.categoryDropdown.removeChild(this.categoryDropdown.children[i]); } //Add the names of the other categories to the migration dropdown. var allCategories = TimeCards.dataManager.getEntities("card_category"); for (var categoryId in allCategories) { if (categoryId != category.category_id) { var optionElement = document.createElement("OPTION"); optionElement.value = categoryId; optionElement.innerText = "Move to \"" + allCategories[categoryId].name + "\""; this.categoryDropdown.appendChild(optionElement); this.categoryDropdown.value = categoryId; } } //Preselect the category that was selected before the update. if (previouslySelectedCategory in allCategories) { this.categoryDropdown.value = previouslySelectedCategory; } } onCancelButtonPressed(event) { this.dismissModally(); } onDeleteButtonPressed(event) { this.deleteButton.disabled = true; TimeCards.dataManager.store("card_category", this.category.category_id, { migration_target: this.categoryDropdown.value }); } onCategoryUpdate(categoryId, category) { //Dismiss the view when the deleted category is the one that the view is currently about to delete. if (!category && categoryId == this.category.category_id) { this.deleteButton.disabled = false; this.dismissModally(); return; } //Refresh the dropdown if any other category has changed. if (this.category) { this.setCategory(this.category); } } onCategoryStoreError(categoryId, category, error) { if (!this.category || this.category.category_id != categoryId) { return; } this.deleteButton.disabled = false; } } UIKit.registerViewControllerType(DeleteCategoryViewController);