-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdynamicFields.js
39 lines (32 loc) · 1.25 KB
/
dynamicFields.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import "child-replace-with-polyfill";
import {addDynamicChoiceSelect, clickSendConfirmation} from "./modules/eventListeners";
export function addDynamicFields(form, emsForm) {
Array.from(form.getElementsByClassName("dynamic-choice-select")).forEach(function(item) {
addDynamicChoiceSelect(item, emsForm);
});
}
export function replaceFormFields(response, fieldIds) {
let parser = new DOMParser;
let dom = parser.parseFromString('<!doctype html><body>' + response, 'text/html');
let formElement;
let newElement;
Array.prototype.forEach.call(fieldIds, function(fieldId) {
formElement = document.getElementById(fieldId);
newElement = dom.getElementById(fieldId);
if (formElement.tagName === 'SELECT') {
formElement = formElement.parentElement;
}
if (newElement.tagName === 'SELECT') {
newElement = newElement.parentElement;
}
formElement.replaceWith(newElement);
});
}
export function addEventListeners(form, emsForm) {
Array.from(form.getElementsByClassName("btn-send-confirmation")).forEach(function (item) {
clickSendConfirmation(item, emsForm);
})
}
window.dynamicFields = function (form, emsForm) {
addDynamicFields(form, emsForm);
};