From e62b6063bb6863992db82199208dbfc38b2a2fbe Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 24 Mar 2022 13:20:26 +1100 Subject: [PATCH] If printing plugins are available, let the user select them when printing --- InvenTree/templates/js/translated/label.js | 92 ++++++++++++++++++---- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/InvenTree/templates/js/translated/label.js b/InvenTree/templates/js/translated/label.js index 1c843917e6..2e1a477387 100644 --- a/InvenTree/templates/js/translated/label.js +++ b/InvenTree/templates/js/translated/label.js @@ -57,13 +57,20 @@ function printStockItemLabels(items) { response, items, { - success: function(pk) { + success: function(data) { + + var pk = data.label; + var href = `/api/label/stock/${pk}/print/?`; items.forEach(function(item) { href += `items[]=${item}&`; }); + if (data.plugin) { + href += `plugin=${data.plugin}`; + } + window.location.href = href; } } @@ -107,13 +114,20 @@ function printStockLocationLabels(locations) { response, locations, { - success: function(pk) { + success: function(data) { + + var pk = data.label; + var href = `/api/label/location/${pk}/print/?`; locations.forEach(function(location) { href += `locations[]=${location}&`; }); + if (data.plugin) { + href += `plugin=${data.plugin}`; + } + window.location.href = href; } } @@ -162,13 +176,20 @@ function printPartLabels(parts) { response, parts, { - success: function(pk) { + success: function(data) { + + var pk = data.label; + var url = `/api/label/part/${pk}/print/?`; parts.forEach(function(part) { url += `parts[]=${part}&`; }); + if (data.plugin) { + href += `plugin=${data.plugin}`; + } + window.location.href = url; } } @@ -188,18 +209,53 @@ function selectLabel(labels, items, options={}) { * (via AJAX) from the server. */ - // If only a single label template is provided, - // just run with that! + // Array of available plugins for label printing + var plugins = []; - if (labels.length == 1) { - if (options.success) { - options.success(labels[0].pk); + // Request a list of available label printing plugins from the server + inventreeGet( + '{% url "api-plugin-list" %}', + { + + }, + { + async: false, + success: function(response) { + response.forEach(function(plugin) { + if (plugin.mixins && plugin.mixins.labels) { + // This plugin supports label printing + plugins.push(plugin); + } + }); + } } + ); - return; + var plugin_selection = ''; + + + if (plugins.length > 0) { + plugin_selection =` +
+ +
+ +
+
+ `; } - var modal = options.modal || '#modal-form'; var label_list = makeOptionsList( @@ -233,14 +289,15 @@ function selectLabel(labels, items, options={}) {
- ${label_list}
+ ${plugin_selection}
`; openModal({ @@ -255,14 +312,17 @@ function selectLabel(labels, items, options={}) { modalSubmit(modal, function() { - var label = $(modal).find('#id_label'); - - var pk = label.val(); + var label = $(modal).find('#id_label').val(); + var plugin = $(modal).find('#id_plugin').val(); closeModal(modal); if (options.success) { - options.success(pk); + options.success({ + // Return the selected label template and plugin + label: label, + plugin: plugin, + }); } }); }