Add more options for form rendering

- "before" a field
- "after" a field
- pure "eye candy" field
This commit is contained in:
Oliver Walters 2021-08-04 23:16:11 +10:00
parent 5aa111b0aa
commit a64ee23afc

View File

@ -366,6 +366,10 @@ function constructFormBody(fields, options) {
// TODO: Refactor the following code with Object.assign (see above) // TODO: Refactor the following code with Object.assign (see above)
// "before" and "after" renders
fields[field].before = field_options.before;
fields[field].after = field_options.after;
// Secondary modal options // Secondary modal options
fields[field].secondary = field_options.secondary; fields[field].secondary = field_options.secondary;
@ -560,10 +564,15 @@ function submitFormData(fields, options) {
var has_files = false; var has_files = false;
// Extract values for each field // Extract values for each field
options.field_names.forEach(function(name) { for (var idx = 0; idx < options.fields_names.length; idx++) {
var name = options.field_names[idx];
var field = fields[name] || null; var field = fields[name] || null;
// Ignore visual fields
if (field && field.type == 'candy') continue;
if (field) { if (field) {
var value = getFormFieldValue(name, field, options); var value = getFormFieldValue(name, field, options);
@ -593,7 +602,7 @@ function submitFormData(fields, options) {
} else { } else {
console.log(`WARNING: Could not find field matching '${name}'`); console.log(`WARNING: Could not find field matching '${name}'`);
} }
}); }
var upload_func = inventreePut; var upload_func = inventreePut;
@ -1279,6 +1288,11 @@ function renderModelData(name, model, data, parameters, options) {
*/ */
function constructField(name, parameters, options) { function constructField(name, parameters, options) {
// Shortcut for simple visual fields
if (parameters.type == 'candy') {
return constructCandyInput(name, parameters, options);
}
var field_name = `id_${name}`; var field_name = `id_${name}`;
// Hidden inputs are rendered without label / help text / etc // Hidden inputs are rendered without label / help text / etc
@ -1292,7 +1306,14 @@ function constructField(name, parameters, options) {
form_classes += ' has-error'; form_classes += ' has-error';
} }
var html = `<div id='div_${field_name}' class='${form_classes}'>`; var html = '';
// Optional content to render before the field
if (parameters.before) {
html += parameters.before;
}
html += `<div id='div_${field_name}' class='${form_classes}'>`;
// Add a label // Add a label
html += constructLabel(name, parameters); html += constructLabel(name, parameters);
@ -1352,6 +1373,10 @@ function constructField(name, parameters, options) {
html += `</div>`; // controls html += `</div>`; // controls
html += `</div>`; // form-group html += `</div>`; // form-group
if (parameters.after) {
html += parameters.after;
}
return html; return html;
} }
@ -1430,6 +1455,9 @@ function constructInput(name, parameters, options) {
case 'date': case 'date':
func = constructDateInput; func = constructDateInput;
break; break;
case 'candy':
func = constructCandyInput;
break;
default: default:
// Unsupported field type! // Unsupported field type!
break; break;
@ -1658,6 +1686,17 @@ function constructDateInput(name, parameters, options) {
} }
/*
* Construct a "candy" field input
* No actual field data!
*/
function constructCandyInput(name, parameters, options) {
return parameters.html;
}
/* /*
* Construct a 'help text' div based on the field parameters * Construct a 'help text' div based on the field parameters
* *