/** * 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, type = null, primaryKey = null, fieldName = null, useHtml = null, nullText = null, unknownText = null) { super(element); this.type = type ?? element.getAttribute("data-type"); this.primaryKey = primaryKey ?? element.getAttribute("data-primary-key"); this.fieldName = fieldName ?? element.getAttribute("data-value"); this.useHtml = useHtml ?? element.hasAttribute("data-html") ?? false; this.nullText = nullText ?? element.getAttribute("data-null-text") ?? ""; this.unknownText = unknownText ?? element.getAttribute("data-unknown-text") ?? DataManager.UNKNOWN_LABEL_GENERIC; } /** * 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, null, true, true); } else { this.displayValue(this.nullText); } } onDataUpdate(key, entity) { var value = this.unknownText; 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);