UNPKG

pragma-views2

Version:

48 lines (42 loc) 1.52 kB
/** * Some controls have odd behaviors where you can't set certain properties on the element. * The select is a example of this. * In order for these controls to work you need to perform some post process operations. * These functions do that for us * @type {Map<any, any>} */ const processMap = new Map([ ["select-value", updateSelectValue], ["select-repeat", updateSelectItemsAdded] ]); export function postProcessPropertyChange(element, property, result) { const key = `${element.nodeName.toLowerCase()}-${property}`; if (processMap.has(key)) { processMap.get(key)(element, property, result); } } /** * Set the selected option of a select based on the value property * @param element * @param property * @param result */ function updateSelectValue(element, property, result) { element.setAttribute("value", result); const currentSelected = element.querySelector("[selected]"); if (currentSelected != undefined) { currentSelected.removeAttribute("selected"); } const selected = element.querySelector(`[value="${result}"`); if (selected != undefined) { selected.setAttribute("selected", "selected"); } } /** * When items are added check the value attribute for the default option and set it accordingly * @param element */ function updateSelectItemsAdded(element) { const selectedValue = element.getAttribute("value"); updateSelectValue(element, null, selectedValue); }