Refactor form field definition copying

This commit is contained in:
Oliver Walters 2021-08-14 00:08:26 +10:00
parent 5b42ab7332
commit 1396c349c8

View File

@ -366,6 +366,14 @@ function constructFormBody(fields, options) {
}
}
// Initialize an "empty" field for each specified field
for (field in displayed_fields) {
if (!(field in fields)) {
console.log("adding blank field for ", field);
fields[field] = {};
}
}
// Provide each field object with its own name
for(field in fields) {
fields[field].name = field;
@ -383,57 +391,18 @@ function constructFormBody(fields, options) {
// Override existing query filters (if provided!)
fields[field].filters = Object.assign(fields[field].filters || {}, field_options.filters);
// TODO: Refactor the following code with Object.assign (see above)
for (var opt in field_options) {
// "before" and "after" renders
fields[field].before = field_options.before;
fields[field].after = field_options.after;
var val = field_options[opt];
// Secondary modal options
fields[field].secondary = field_options.secondary;
// Edit callback
fields[field].onEdit = field_options.onEdit;
fields[field].multiline = field_options.multiline;
// Custom help_text
if (field_options.help_text) {
fields[field].help_text = field_options.help_text;
}
// Custom label
if (field_options.label) {
fields[field].label = field_options.label;
}
// Custom placeholder
if (field_options.placeholder) {
fields[field].placeholder = field_options.placeholder;
}
// Choices
if (field_options.choices) {
fields[field].choices = field_options.choices;
}
// Group
if (field_options.group) {
fields[field].group = field_options.group;
}
// Field prefix
if (field_options.prefix) {
fields[field].prefix = field_options.prefix;
} else if (field_options.icon) {
// Specify icon like 'fa-user'
fields[field].prefix = `<span class='fas ${field_options.icon}'></span>`;
}
fields[field].hidden = field_options.hidden;
if (field_options.read_only != null) {
fields[field].read_only = field_options.read_only;
if (opt == 'filters') {
// ignore filters (see above)
} else if (opt == 'icon') {
// Specify custom icon
fields[field].prefix = `<span class='fas ${val}'></span>`;
} else {
fields[field][opt] = field_options[opt];
}
}
}
}
@ -477,8 +446,6 @@ function constructFormBody(fields, options) {
if (options.current_group) {
// Close out the current group
html += `</div></div>`;
console.log(`finally, ending group '${console.current_group}'`);
}
// Create a new modal if one does not exists
@ -878,6 +845,7 @@ function handleFormErrors(errors, fields, options) {
non_field_errors.append(
`<div class='alert alert-block alert-danger'>
<b>{% trans "Form errors exist" %}</b>
<span id='form-errors-info' class='float-right fas fa-info-circle icon-red'></span>
</div>`
);
@ -947,7 +915,10 @@ function addFieldCallbacks(fields, options) {
function addFieldCallback(name, field, options) {
$(options.modal).find(`#id_${name}`).change(function() {
field.onEdit(name, field, options);
var value = getFormFieldValue(name, field, options);
field.onEdit(value, name, field, options);
});
}
@ -1429,8 +1400,6 @@ function constructField(name, parameters, options) {
if (options.current_group && parameters.group != options.current_group) {
html += `</div></div>`;
console.log(`ending group '${options.current_group}'`);
// Null out the current "group" so we can start a new one
options.current_group = null;
}
@ -1446,8 +1415,6 @@ function constructField(name, parameters, options) {
// Add HTML for the start of a separate panel
if (parameters.group != options.current_group) {
console.log(`starting group '${group}'`);
html += `
<div class='panel form-panel' id='form-panel-${group}' group='${group}'>
<div class='panel-heading form-panel-heading' id='form-panel-heading-${group}'>`;