class RoleSelect extends View { constructor(element) { super(element); this.options = { }; TimeCards.dataManager.addDataListener("user_role", null, this.onRoleUpdate.bind(this)); } setOptionInformation(option, role) { option.value = role.role_id; option.innerText = role.name; } onRoleUpdate(roleId, role, sortedBefore) { if (!role) { //Handle the deleted role. if (!(roleId in this.options)) { console.warn("Trying to remove a role from a role select that was never added."); return; } var option = this.options[roleId]; this.element.removeChild(option); delete this.options[roleId]; } else if (!(roleId in this.options)) { //Handle the added role. var option = document.createElement("OPTION"); this.setOptionInformation(option, role); this.element.insertBefore(option, sortedBefore ? this.options[sortedBefore] : null); this.options[roleId] = option; } else { //Handle the changed role. var option = this.options[roleId]; this.setOptionInformation(option, role); //Re-add to keep the sorting in case of a name change. this.element.insertBefore(option, sortedBefore ? this.options[sortedBefore] : null); } } } UIKit.registerViewType(RoleSelect);