Skip to content

Commit

Permalink
Reduced code repeatition.
Browse files Browse the repository at this point in the history
  • Loading branch information
ravise5 committed Dec 1, 2023
1 parent 4c550f7 commit ee89b14
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,60 +141,11 @@
return option;
}
updateEnum(newEnums) {
let currentEnumSize = this.getWidget().length;
if(currentEnumSize === 0) { // case 1: create option with new enums
newEnums.forEach(value => {
this.getWidgets().appendChild(this.#createCheckBoxItem(value, value));
});
} else if(currentEnumSize === newEnums.length) { // case 2: replace existing enums
this.widget.forEach((option, index) => {
option.value = newEnums[index];
})
} else if(currentEnumSize < newEnums.length) { // case 3: replace existing enums and create new options with remaining
this.widget.forEach((option, index) => {
option.value = newEnums[index];
})

newEnums.forEach((value, index) => {
if(index > currentEnumSize - 1) {
let newOption = this.#createCheckBoxItem(value, value);
this.getWidgets().appendChild(newOption);
}
})
} else {
this.widget.forEach((option, index) => { // case 4: replace existing enums and remove extra ones
if(index < newEnums.length){
option.value = newEnums[index];
} else {
let optionToRemove = option.parentElement.parentElement;
this.getWidgets().removeChild(optionToRemove);
}
})
}
super.updateEnumForRadioButtonAndCheckbox(newEnums, this.#createCheckBoxItem);
}

updateEnumNames(newEnumNames) {
let currentEnumNameSize = this.getWidget().length;
if(currentEnumNameSize === 0) {
newEnumNames.forEach((value) => {
this.getWidgets().appendChild(this.#createCheckBoxItem(value, value));
})
} else if(currentEnumNameSize > newEnumNames.length) {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
let input = option.querySelector('input');
if(index < newEnumNames.length) {
span.textContent = newEnumNames[index];
} else {
span.textContent = input.value;
}
});
} else {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
span.textContent = newEnumNames[index];
});
}
super.updateEnumNamesForRadioButtonAndCheckbox(newEnumNames, this.#createCheckBoxItem);
}

updateEnabled(enabled, state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,61 +154,12 @@
}

updateEnum(newEnums) {
let currentEnumSize = this.getWidget().length;
if(currentEnumSize === 0) { // case 1: create option with new enums
newEnums.forEach(value => {
this.getWidgets().appendChild(this.#createRadioOption(value, value));
});
} else if(currentEnumSize === newEnums.length) { // case 2: replace existing enums
this.widget.forEach((input, index) => {
input.value = newEnums[index];
})
} else if(currentEnumSize < newEnums.length) { // case 3: replace existing enums and create options with remaining
this.widget.forEach((input, index) => {
input.value = newEnums[index];
})

newEnums.forEach((value, index) => {
if(index > currentEnumSize - 1) {
this.getWidgets().appendChild(this.#createRadioOption(value, value));
}
});
} else {
this.widget.forEach((input, index) => { // case 4: replace existing enums and remove extra ones
if(index < newEnums.length){
input.value = newEnums[index];
} else {
let optionToRemove = input.parentElement.parentElement;
this.getWidgets().removeChild(optionToRemove);
}
})
}
super.updateEnumForRadioButtonAndCheckbox(newEnums, this.#createRadioOption);
}

updateEnumNames(newEnumNames) {
let currentEnumNameSize = this.getWidget().length;
if(currentEnumNameSize === 0) {
newEnumNames.forEach((value) => {
this.getWidgets().appendChild(this.#createRadioOption(value, value));
})
} else if(currentEnumNameSize > newEnumNames.length) {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
let input = option.querySelector('input');
if(index < newEnumNames.length) {
span.textContent = newEnumNames[index];
} else {
span.textContent = input.value;
}
});
} else {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
span.textContent = newEnumNames[index];
});
}
super.updateEnumNamesForRadioButtonAndCheckbox(newEnumNames, this.#createRadioOption)
}

}

FormView.Utils.setupField(({element, formContainer}) => {
Expand Down
69 changes: 69 additions & 0 deletions ui.frontend/src/view/FormFieldBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,75 @@ class FormFieldBase extends FormField {
}
}

/**
* Common method to update the enums of checkbox and radiobutton
* @param {Array} newEnums - updated enum values
* @param {CallableFunction} createItemCallback - function to create options
*/
updateEnumForRadioButtonAndCheckbox(newEnums, createItemCallback) {
let currentEnumSize = this.getWidget().length;
if(currentEnumSize === 0) { // case 1: create option with new enums
newEnums.forEach(value => {
this.getWidgets().appendChild(createItemCallback.call(this, value, value));
});
} else if(currentEnumSize === newEnums.length) { // case 2: replace existing enums
this.widget.forEach((input, index) => {
input.value = newEnums[index];
})
} else if(currentEnumSize < newEnums.length) { // case 3: replace existing enums and create options with remaining
this.widget.forEach((input, index) => {
input.value = newEnums[index];
})

newEnums.forEach((value, index) => {
if(index > currentEnumSize - 1) {
this.getWidgets().appendChild(createItemCallback.call(this, value, value));
}
});
} else {
this.widget.forEach((input, index) => { // case 4: replace existing enums and remove extra ones
if(index < newEnums.length){
input.value = newEnums[index];
} else {
let optionToRemove = input.parentElement.parentElement;
this.getWidgets().removeChild(optionToRemove);
}
})
}
}



/**
* Common method to update the enums Names of checkbox and radiobutton
* @param {Array} newEnumsNames - updated enum Names values
* @param {CallableFunction} createItemCallback - function to create options
*/
updateEnumNamesForRadioButtonAndCheckbox(newEnumNames, createItemCallback) {
let currentEnumNameSize = this.getWidget().length;
if(currentEnumNameSize === 0) {
newEnumNames.forEach((value) => {
this.getWidgets().appendChild(createItemCallback.call(this, value, value));
})
} else if(currentEnumNameSize > newEnumNames.length) {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
let input = option.querySelector('input');
if(index < newEnumNames.length) {
span.textContent = newEnumNames[index];
} else {
span.textContent = input.value;
}
});
} else {
[...this.getOptions()].forEach((option, index) => {
let span = option.querySelector('span');
span.textContent = newEnumNames[index];
});
}
}


/**
* Updates the active child of the form container.
* @param {Object} activeChild - The active child.
Expand Down

0 comments on commit ee89b14

Please sign in to comment.