From a64ee23afc2aaf01ab5578dd571248ee5b6538b2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 4 Aug 2021 23:16:11 +1000 Subject: [PATCH] Add more options for form rendering - "before" a field - "after" a field - pure "eye candy" field --- InvenTree/templates/js/translated/forms.js | 45 ++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 4801ec77eb..46b2b21a87 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -366,6 +366,10 @@ function constructFormBody(fields, options) { // 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 fields[field].secondary = field_options.secondary; @@ -560,10 +564,15 @@ function submitFormData(fields, options) { var has_files = false; // 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; + // Ignore visual fields + if (field && field.type == 'candy') continue; + if (field) { var value = getFormFieldValue(name, field, options); @@ -593,7 +602,7 @@ function submitFormData(fields, options) { } else { console.log(`WARNING: Could not find field matching '${name}'`); } - }); + } var upload_func = inventreePut; @@ -1279,6 +1288,11 @@ function renderModelData(name, model, data, 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}`; // Hidden inputs are rendered without label / help text / etc @@ -1292,7 +1306,14 @@ function constructField(name, parameters, options) { form_classes += ' has-error'; } - var html = `
`; + var html = ''; + + // Optional content to render before the field + if (parameters.before) { + html += parameters.before; + } + + html += `
`; // Add a label html += constructLabel(name, parameters); @@ -1352,6 +1373,10 @@ function constructField(name, parameters, options) { html += `
`; // controls html += `
`; // form-group + if (parameters.after) { + html += parameters.after; + } + return html; } @@ -1430,6 +1455,9 @@ function constructInput(name, parameters, options) { case 'date': func = constructDateInput; break; + case 'candy': + func = constructCandyInput; + break; default: // Unsupported field type! 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 *