Refactor the way that field options are passed to a form

This commit is contained in:
Oliver 2021-06-28 13:03:34 +10:00
parent 6335372208
commit ba2537d125

View File

@ -243,21 +243,20 @@ function constructForm(url, options) {
} }
/*
* Construct a modal form based on the provided options
*
* arguments:
* - fields: The endpoint description returned from the OPTIONS request
* - options: form options object provided by the client.
*/
function constructFormBody(fields, options) { function constructFormBody(fields, options) {
var html = ''; var html = '';
var allowed_fields = options.fields || null; // Client must provide set of fields to be displayed,
var ignored_fields = options.ignore || []; // otherwise *all* fields will be displayed
var displayed_fields = options.fields || fields;
if (!ignored_fields.includes('pk')) {
ignored_fields.push('pk');
}
if (!ignored_fields.includes('id')) {
ignored_fields.push('id');
}
// Provide each field object with its own name // Provide each field object with its own name
for(field in fields) { for(field in fields) {
@ -267,26 +266,14 @@ function constructFormBody(fields, options) {
// Construct an ordered list of field names // Construct an ordered list of field names
var field_names = []; var field_names = [];
if (allowed_fields) { for (var name in displayed_fields) {
allowed_fields.forEach(function(name) {
// Only push names which are actually in the set of fields // Only push names which are actually in the set of fields
if (name in fields) { if (name in fields) {
if (!ignored_fields.includes(name) && !field_names.includes(name)) {
field_names.push(name); field_names.push(name);
}
} else { } else {
console.log(`WARNING: '${name}' does not match a valid field name.`); console.log(`WARNING: '${name}' does not match a valid field name.`);
} }
});
} else {
for (const name in fields) {
if (!ignored_fields.includes(name) && !field_names.includes(name)) {
field_names.push(name);
}
}
} }
// Push the ordered field names into the options, // Push the ordered field names into the options,
@ -429,6 +416,10 @@ function updateFieldValues(fields, options) {
case 'boolean': case 'boolean':
el.prop('checked', value); el.prop('checked', value);
break; break;
case 'related field':
// TODO
console.log(`related field '${name}'`);
break;
default: default:
el.val(value); el.val(value);
break; break;