From 9d50f2a6acaa03e6de1c84da7962102309e6a867 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 May 2019 23:27:45 +1000 Subject: [PATCH] Toot toot all aboard the refactor tractor - launchModalForm now accepts a 'secondary' parameter which is a list of secondary modals to attach --- InvenTree/part/templates/part/category.html | 33 ++++++++---------- InvenTree/static/script/inventree/modals.js | 18 ++++++++++ InvenTree/stock/templates/stock/location.html | 34 ++++++++----------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index 82cf5e5833..44a767b1d9 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -90,25 +90,20 @@ category: {{ category.id }} {% endif %} }, - after_render: function(modal, response) { - attachSecondaryModal(modal, - { - field: 'category', - label: 'New Category', - title: 'Create new Part Category', - url: "{% url 'category-create' %}", - } - ); - - attachSecondaryModal(modal, - { - field: 'default_location', - label: 'New Location', - title: 'Create new Stock Location', - url: "{% url 'stock-location-create' %}", - } - ); - } + secondary: [ + { + field: 'category', + label: 'New Category', + title: 'Create new Part Category', + url: "{% url 'category-create' %}", + }, + { + field: 'default_location', + label: 'New Location', + title: 'Create new Stock Location', + url: "{% url 'stock-location-create' %}", + } + ] } ); }); diff --git a/InvenTree/static/script/inventree/modals.js b/InvenTree/static/script/inventree/modals.js index 4f9cb70068..afb14ace9f 100644 --- a/InvenTree/static/script/inventree/modals.js +++ b/InvenTree/static/script/inventree/modals.js @@ -420,6 +420,15 @@ function attachSecondaryModal(modal, options) { } +function attachSecondaries(modal, secondaries) { + /* Attach a provided list of secondary modals */ + + for (var i = 0; i < secondaries.length; i++) { + attachSecondaryModal(modal, secondaries[i]); + } +} + + function handleModalForm(url, options) { /* Update a modal form after data are received from the server. * Manages POST requests until the form is successfully submitted. @@ -471,6 +480,10 @@ function handleModalForm(url, options) { if (options.after_render) { options.after_render(modal, response); } + + if (options.secondary) { + attachSecondaries(modal, options.secondary); + } } else { $(modal).modal('hide'); @@ -514,6 +527,7 @@ function launchModalForm(url, options = {}) { * close_text - Text for the close button (default = 'Close') * no_post - If true, only display form data, hide submit button, and disallow POST * after_render - Callback function to run after form is rendered + * secondary - List of secondary modals to attach */ var modal = options.modal || '#modal-form'; @@ -550,6 +564,10 @@ function launchModalForm(url, options = {}) { options.after_render(modal, response); } + if (options.secondary) { + attachSecondaries(modal, options.secondary); + } + if (options.no_post) { modalShowSubmitButton(modal, false); } else { diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index 18aa369d0d..786298d407 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -124,26 +124,20 @@ location: {{ location.id }} {% endif %} }, - after_render: function(modal, response) { - - attachSecondaryModal(modal, - { - field: 'part', - label: 'New Part', - title: 'Create New Part', - url: "{% url 'part-create' %}", - } - ); - - attachSecondaryModal(modal, - { - field: 'location', - label: 'New Location', - title: 'Create New Location', - url: "{% url 'stock-location-create' %}", - } - ); - } + secondary: [ + { + field: 'part', + label: 'New Part', + title: 'Create New Part', + url: "{% url 'part-create' %}", + }, + { + field: 'location', + label: 'New Location', + title: 'Create New Location', + url: "{% url 'stock-location-create' %}", + } + ] });