From e0f8310ca8ee17cc8a942fc6a14f4d5217ba6b9d Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 1 Jul 2021 21:57:07 +1000 Subject: [PATCH] Adds the ability to "clear" a non-required field with an obvious button --- InvenTree/templates/js/forms.js | 103 ++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 18 deletions(-) diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 76f0dba2dd..a4a1d8e502 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -423,6 +423,9 @@ function constructFormBody(fields, options) { // Attach edit callbacks (if required) addFieldCallbacks(fields, options); + // Attach clear callbacks (if required) + addClearCallbacks(fields, options); + attachToggle(modal); $(modal + ' .select2-container').addClass('select-full-width'); @@ -571,22 +574,31 @@ function updateFieldValues(fields, options) { if (value == null) { continue; } - var el = $(options.modal).find(`#id_${name}`); + updateFieldValue(name, value, field, options); + } +} - switch (field.type) { - case 'boolean': - el.prop('checked', value); - break; - case 'related field': - // TODO? - break; - case 'file upload': - case 'image upload': - break; - default: - el.val(value); - break; - } + +function updateFieldValue(name, value, field, options) { + var el = $(options.modal).find(`#id_${name}`); + + switch (field.type) { + case 'boolean': + el.prop('checked', value); + break; + case 'related field': + // Clear? + if (value == null && !field.required) { + el.val(null).trigger('change'); + } + // TODO - Specify an actual value! + break; + case 'file upload': + case 'image upload': + break; + default: + el.val(value); + break; } } @@ -777,6 +789,29 @@ function addFieldCallback(name, field, options) { } +function addClearCallbacks(fields, options) { + + for (var idx = 0; idx < options.field_names.length; idx++) { + + var name = options.field_names[idx]; + + var field = fields[name]; + + if (!field || field.required) continue; + + addClearCallback(name, field, options); + } +} + + +function addClearCallback(name, field, options) { + + $(options.modal).find(`#clear_${name}`).click(function() { + updateFieldValue(name, null, field, options); + }); +} + + function initializeRelatedFields(fields, options) { var field_names = options.field_names; @@ -1114,14 +1149,46 @@ function constructField(name, parameters, options) { html += constructLabel(name, parameters); html += `
`; + + // Does this input deserve "extra" decorators? + var extra = parameters.prefix != null; - if (parameters.prefix) { - html += `
${parameters.prefix}`; + // Some fields can have 'clear' inputs associated with them + if (!parameters.required) { + switch (parameters.type) { + case 'string': + case 'url': + case 'email': + case 'integer': + case 'float': + case 'decimal': + case 'related field': + extra = true; + break; + default: + break; + } + } + + if (extra) { + html += `
`; + + if (parameters.prefix) { + html += `${parameters.prefix}`; + } } html += constructInput(name, parameters, options); - if (parameters.prefix) { + if (extra) { + + if (!parameters.required) { + html += ` + + + `; + } + html += `
`; // input-group }