$(() => { let VALUEMAP_MAPPING_TYPE_DEFAULT = ; let type_placeholder = _('value'), VALUEMAP_MAPPING_TYPE_GREATER_EQUAL => _('value'), VALUEMAP_MAPPING_TYPE_LESS_EQUAL => _('value'), VALUEMAP_MAPPING_TYPE_IN_RANGE => _('value'), VALUEMAP_MAPPING_TYPE_REGEXP => _('regexp') ]) ?>; let table = document.querySelector('#mappings_table'); let observer = new MutationObserver(mutationHandler); // Observe changes for form fields: type, value. observer.observe(table, { childList: true, subtree: true, attributes: true, attributeFilter: ['value'] }); updateOnTypeChange(); function updateOnTypeChange() { let default_select = table.querySelector(`z-select[value="${VALUEMAP_MAPPING_TYPE_DEFAULT}"]`); table.querySelectorAll('tr').forEach((row) => { let zselect = row.querySelector('z-select[name$="[type]"]'); let input = row.querySelector('input[name$="[value]"]'); if (zselect) { zselect.getOptionByValue(VALUEMAP_MAPPING_TYPE_DEFAULT).disabled = (default_select && zselect !== default_select ); input.classList.toggle('visibility-hidden', (zselect === default_select)); input.disabled = (zselect === default_select); input.setAttribute('placeholder', type_placeholder[zselect.value]||''); } }); } function mutationHandler(mutation_records, observer) { let update = mutation_records.filter((mutation) => { return (mutation.target.tagName === 'INPUT' && mutation.target.getAttribute('name').substr(-6) === '[type]') || (mutation.target.tagName === 'TBODY' && mutation.removedNodes.length > 0); }); if (update.length) { updateOnTypeChange(); } } }); function submitValueMap(overlay) { const form = overlay.$dialogue.$body[0].querySelector('form'); jQuery(form).trimValues(['input[type="text"]']); const curl = new Curl(form.getAttribute('action')); fetch(curl.getUrl(), { method: 'POST', body: new URLSearchParams(new FormData(form)) }) .then(response => response.json()) .then(response => { if ('error' in response) { throw {error: response.error}; } new AddValueMap(response, response.edit ? overlay.element.closest('tr') : null); overlayDialogueDestroy(overlay.dialogueid); }) .catch((exception) => { for (const element of form.parentNode.children) { if (element.matches('.msg-good, .msg-bad, .msg-warning')) { element.parentNode.removeChild(element); } } let title, messages; if (typeof exception === 'object' && 'error' in exception) { title = exception.error.title; messages = exception.error.messages; } else { messages = []; } const message_box = makeMessageBox('bad', messages, title)[0]; form.parentNode.insertBefore(message_box, form); }) .finally(() => { overlay.unsetLoading(); }); }