/** * A DataLabel shows a value from an entity from a specific type of the DataManager. * Will always be up to date because it subscribes to updates by the DataManager. */ class DataLabel extends View { constructor(element) { super(element); this.type = element.getAttribute("data-type"); this.primaryKey = element.getAttribute("data-primary-key"); this.fieldName = element.getAttribute("data-value"); this.useHtml = element.hasAttribute("data-html"); this.nullText = element.getAttribute("data-null-text"); } /** * Sets the primary key value of the entity from which this label should display a specific value. * Will display the null text if the key is null. * @param key The primary key value of the entity to display. */ setKey(key) { if (key) { //Reset the label. this.displayValue(""); if (!this.onDataUpdateBound) { this.onDataUpdateBound = this.onDataUpdate.bind(this); } else { //Remove the existing listener. TimeCards.dataManager.removeDataListener(this.type, this.onDataUpdateBound); } var filters = { }; filters[this.primaryKey] = key; TimeCards.dataManager.addDataListener(this.type, filters, this.onDataUpdateBound); } else { this.displayValue(this.nullText); } } onDataUpdate(key, entity) { var value = this.nullText; if (entity && (this.fieldName in entity) && entity[this.fieldName] != null) { value = entity[this.fieldName]; } this.displayValue(value); } displayValue(value) { if (this.useHtml) { this.element.innerHTML = value; } else { this.element.innerText = value; } } } UIKit.registerViewType(DataLabel);