From 3eb489801951e6739d6e41cf2fccf16b21dcf136 Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Sat, 21 May 2022 00:52:45 +1000 Subject: [PATCH] Added support for secondary buttons on modals Secondary buttons can be added to modal footers beside Close and Submit Buttons can be linked to callback functions Callback functions can access options arguments passed to constructForm --- InvenTree/templates/js/translated/forms.js | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index d45a2de78c..9e1a7d154d 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -561,6 +561,11 @@ function constructFormBody(fields, options) { insertPersistButton(options); } + // Insert secondary buttons (if required) + if (options.buttons) { + insertSecondaryButtons(options); + } + // Display the modal $(modal).modal('show'); @@ -650,6 +655,31 @@ function insertPersistButton(options) { $(options.modal).find('#modal-footer-buttons').append(html); } +/* + * Add secondary buttons to the left of the close and submit buttons + * with callback functions + */ +function insertSecondaryButtons(options) { + for (var idx = 0; idx < options.buttons.length; idx++) { + + var html = ` + + `; + + $(options.modal).find('#modal-footer-secondary-buttons').append(html); + + if (options.buttons[idx].onClick instanceof Function) { + // Copy callback reference to prevent errors if `idx` changes value before execution + var onclick_callback = options.buttons[idx].onClick; + + $(options.modal).find(`#modal-form-${options.buttons[idx].name}`).click(function() { + onclick_callback(options); + }); + } + } +} /* * Extract all specified form values as a single object