class CategorySelect extends View { constructor(element) { super(element); this.options = { }; TimeCards.dataManager.addDataListener("card_category", null, this.onCategoryUpdate.bind(this)); } setOptionInformation(option, category) { option.value = category.category_id; option.innerText = (category.icon ? category.icon + " " : "") + category.name; } onCategoryUpdate(categoryId, category, sortedBefore) { if (!category) { //Handle the deleted category. if (!(categoryId in this.options)) { console.error("Trying to remove a category from a category select that was never added."); return; } var option = this.options[categoryId]; this.element.removeChild(option); delete this.options[categoryId]; } else if (!(categoryId in this.options)) { //Handle the added category. var option = document.createElement("OPTION"); this.setOptionInformation(option, category); this.element.insertBefore(option, sortedBefore ? this.options[sortedBefore] : null); this.options[categoryId] = option; } else { //Handle the changed category. var option = this.options[categoryId]; this.setOptionInformation(option, category); //Re-add the option to maintain the sort order in case of a name change. this.element.insertBefore(option, sortedBefore ? this.options[sortedBefore] : null); } } } UIKit.registerViewType(CategorySelect);