From 759a3724b56ecc0fe9adeca3573cb3e2e5a1c519 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:09:56 +1000 Subject: [PATCH 1/6] Add function to extract all data from a displayed form --- InvenTree/templates/js/translated/forms.js | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 3b55802f38..a31da2529d 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -564,6 +564,30 @@ function insertConfirmButton(options) { } +/* + * Extract all specified form values as a single object + */ +function extractFormData(fields, options) { + + var data = {}; + + for (var idx = 0; idx < options.field_names.length; idx++) { + + var name = options.field_names[idx]; + + var field = fields[name] || null; + + if (!field) continue; + + if (field.type == 'candy') continue; + + data[name] = getFormFieldValue(name, field, options); + } + + return data; +} + + /* * Submit form data to the server. * From 25af8559baa14893eaf77de870f70ba41ce56091 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:27:31 +1000 Subject: [PATCH 2/6] Back-fill newly created model data into the parent modal form --- InvenTree/templates/js/translated/forms.js | 55 +++++++++++++++------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index a31da2529d..8e2d3a4c66 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -974,10 +974,10 @@ function initializeRelatedFields(fields, options) { switch (field.type) { case 'related field': - initializeRelatedField(name, field, options); + initializeRelatedField(field, fields, options); break; case 'choice': - initializeChoiceField(name, field, options); + initializeChoiceField(field, fields, options); break; } } @@ -992,7 +992,9 @@ function initializeRelatedFields(fields, options) { * - field: The field data object * - options: The options object provided by the client */ -function addSecondaryModal(name, field, options) { +function addSecondaryModal(field, fields, options) { + + var name = field.name; var secondary = field.secondary; @@ -1005,22 +1007,41 @@ function addSecondaryModal(name, field, options) { $(options.modal).find(`label[for="id_${name}"]`).append(html); - // TODO: Launch a callback + // Callback function when the secondary button is pressed $(options.modal).find(`#btn-new-${name}`).click(function() { - if (secondary.callback) { - // A "custom" callback can be specified for the button - secondary.callback(field, options); - } else if (secondary.api_url) { - // By default, a new modal form is created, with the parameters specified - // The parameters match the "normal" form creation parameters + // Determine the API query URL + var url = secondary.api_url || field.api_url; - secondary.onSuccess = function(data, opts) { - setRelatedFieldData(name, data, options); - }; + // Extract form values at time of button press + var data = extractFormData(fields, options) - constructForm(secondary.api_url, secondary); + // Allow the secondary form to be "prefilled" with a custom function + if (secondary.prefill) { + secondary.fields = secondary.prefill(data); } + + // If no onSuccess function is defined, provide a default one + if (!secondary.onSuccess) { + secondary.onSuccess = function(data, opts) { + + // Force refresh from the API, to get full detail + inventreeGet(`${url}${data.pk}/`, {}, { + success: function(responseData) { + + setRelatedFieldData(name, responseData, options); + } + }); + }; + } + + // Method should be "POST" for creation + secondary.method = secondary.method || 'POST'; + + constructForm( + url, + secondary + ); }); } @@ -1034,7 +1055,9 @@ function addSecondaryModal(name, field, options) { * - field: Field definition from the OPTIONS request * - options: Original options object provided by the client */ -function initializeRelatedField(name, field, options) { +function initializeRelatedField(field, fields, options) { + + var name = field.name; if (!field.api_url) { // TODO: Provide manual api_url option? @@ -1047,7 +1070,7 @@ function initializeRelatedField(name, field, options) { // Add a button to launch a 'secondary' modal if (field.secondary != null) { - addSecondaryModal(name, field, options); + addSecondaryModal(field, fields, options); } // TODO: Add 'placeholder' support for entry select2 fields From fd917b2e41e8039c30cace1bec15312cc9ffbdf5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:30:53 +1000 Subject: [PATCH 3/6] Simple refactoring --- InvenTree/templates/js/translated/forms.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 8e2d3a4c66..9ca0ddccec 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1016,9 +1016,10 @@ function addSecondaryModal(field, fields, options) { // Extract form values at time of button press var data = extractFormData(fields, options) - // Allow the secondary form to be "prefilled" with a custom function - if (secondary.prefill) { - secondary.fields = secondary.prefill(data); + // If the "fields" attribute is a function, call it with data + if (secondary.fields instanceof Function) { + console.log("Fields is a function!"); + secondary.fields = secondary.fields(data); } // If no onSuccess function is defined, provide a default one From b960ce839add52ce3afda573f9dd0aa198eb1419 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:31:21 +1000 Subject: [PATCH 4/6] remove debug statement --- InvenTree/templates/js/translated/forms.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 9ca0ddccec..8f8ea71d54 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1013,12 +1013,12 @@ function addSecondaryModal(field, fields, options) { // Determine the API query URL var url = secondary.api_url || field.api_url; - // Extract form values at time of button press - var data = extractFormData(fields, options) - // If the "fields" attribute is a function, call it with data if (secondary.fields instanceof Function) { - console.log("Fields is a function!"); + + // Extract form values at time of button press + var data = extractFormData(fields, options) + secondary.fields = secondary.fields(data); } From 94c1ed882fdd934a92db633f3d9ec6ace522d160 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:42:42 +1000 Subject: [PATCH 5/6] Add secondary field options for "Create Supplier Part" form - Supplier - Manufacturer Part --- InvenTree/templates/js/translated/company.js | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/js/translated/company.js b/InvenTree/templates/js/translated/company.js index f671fb616f..2124f5f344 100644 --- a/InvenTree/templates/js/translated/company.js +++ b/InvenTree/templates/js/translated/company.js @@ -72,7 +72,7 @@ function supplierPartFields() { filters: { part_detail: true, manufacturer_detail: true, - } + }, }, description: {}, link: { @@ -108,6 +108,33 @@ function createSupplierPart(options={}) { fields.manufacturer_part.value = options.manufacturer_part; } + // Add a secondary modal for the supplier + fields.supplier.secondary = { + title: '{% trans "Add Supplier" %}', + fields: function(data) { + var company_fields = companyFormFields(); + + company_fields.is_supplier.value = true; + + return company_fields; + } + }; + + // Add a secondary modal for the manufacturer part + fields.manufacturer_part.secondary = { + title: '{% trans "Add Manufacturer Part" %}', + fields: function(data) { + var mp_fields = manufacturerPartFields(); + + if (data.part) { + mp_fields.part.value = data.part; + mp_fields.part.hidden = true; + } + + return mp_fields; + } + }; + constructForm('{% url "api-supplier-part-list" %}', { fields: fields, method: 'POST', From 720b332f711d938de4b15a3886b9470df8fc34f7 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 7 Aug 2021 23:49:50 +1000 Subject: [PATCH 6/6] Adds secondary to manufacturerpart form --- InvenTree/templates/js/translated/company.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/InvenTree/templates/js/translated/company.js b/InvenTree/templates/js/translated/company.js index 2124f5f344..9c73fcc111 100644 --- a/InvenTree/templates/js/translated/company.js +++ b/InvenTree/templates/js/translated/company.js @@ -30,6 +30,17 @@ function createManufacturerPart(options={}) { fields.manufacturer.value = options.manufacturer; } + fields.manufacturer.secondary = { + title: '{% trans "Add Manufacturer" %}', + fields: function(data) { + var company_fields = companyFormFields(); + + company_fields.is_manufacturer.value = true; + + return company_fields; + } + } + constructForm('{% url "api-manufacturer-part-list" %}', { fields: fields, method: 'POST',