Skip to content

Commit

Permalink
netteForms: refactoring, added expandRadioElement()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 4, 2024
1 parent bbf5f82 commit 5be52f1
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
};


/**
* @param {FormElement} elem
* @return {Array<FormElement>}
*/
let expandRadioElement = function(elem) {
let inputs = elem.form.elements.namedItem(elem.name);
return inputs instanceof RadioNodeList ? Array.from(inputs) : [inputs];
};


/**
* Function to execute when the DOM is fully loaded.
* @private
Expand All @@ -76,10 +86,9 @@
return elem[0] ? Nette.getValue(elem[0]) : null;

} else if (elem.type === 'radio') {
let elements = elem.form.elements; // prevents problem with name 'item' or 'namedItem'
for (let i = 0; i < elements.length; i++) {
if (elements[i].name === elem.name && elements[i].checked) {
return elements[i].value;
for (let input of expandRadioElement(elem)) {
if (input.checked) {
return input.value;
}
}
return null;
Expand All @@ -104,12 +113,10 @@
return values;

} else if (elem.name && elem.name.endsWith('[]')) { // multiple elements []
let elements = elem.form.elements[elem.name].tagName ? [elem] : elem.form.elements[elem.name],
values = [];

for (let i = 0; i < elements.length; i++) {
if (elements[i].type !== 'checkbox' || elements[i].checked) {
values.push(elements[i].value);
let values = [];
for (let input of expandRadioElement(elem)) {
if (input.type !== 'checkbox' || input.checked) {
values.push(input.value);
}
}
return values;
Expand Down Expand Up @@ -273,8 +280,8 @@
*/
Nette.isDisabled = function(elem) {
if (elem.type === 'radio') {
for (let i = 0, elements = elem.form.elements; i < elements.length; i++) {
if (elements[i].name === elem.name && !elements[i].disabled) {
for (let input of expandRadioElement(elem)) {
if (!input.disabled) {
return false;
}
}
Expand Down Expand Up @@ -670,11 +677,10 @@
if ((rule.condition && Nette.toggleControl(elem, rule.rules, curSuccess, firsttime, value, rule.op === ':blank' ? false : emptyOptional)) || rule.toggle) {
has = true;
if (firsttime) {
let els = elem.form.elements;
for (let i = 0; i < els.length; i++) {
if (els[i].name === curElem.name && !toggleListeners.has(els[i])) {
els[i].addEventListener('change', handler);
toggleListeners.set(els[i], null);
for (let el of expandRadioElement(curElem)) {
if (!toggleListeners.has(el)) {
el.addEventListener('change', handler);
toggleListeners.set(el, null);
}
}
}
Expand Down

0 comments on commit 5be52f1

Please sign in to comment.