mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor javascript for label printing
- Consolidate into a single function - Similar to refactor of report functions
This commit is contained in:
parent
cdbfcfa1cf
commit
4c03470e4a
InvenTree
part/templates/part
stock/templates/stock
templates/js/translated
@ -474,7 +474,11 @@
|
||||
|
||||
{% if labels_enabled %}
|
||||
$('#print-label').click(function() {
|
||||
printPartLabels([{{ part.pk }}]);
|
||||
printLabels({
|
||||
items: [{{ part.pk }}],
|
||||
key: 'part',
|
||||
url: '{% url "api-part-label-list" %}',
|
||||
});
|
||||
});
|
||||
{% endif %}
|
||||
|
||||
|
@ -501,7 +501,11 @@ $("#stock-test-report").click(function() {
|
||||
});
|
||||
|
||||
$("#print-label").click(function() {
|
||||
printStockItemLabels([{{ item.pk }}]);
|
||||
printLabels({
|
||||
items: [{{ item.pk }}],
|
||||
url: '{% url "api-stockitem-label-list" %}',
|
||||
key: 'item',
|
||||
});
|
||||
});
|
||||
|
||||
{% if roles.stock.change %}
|
||||
|
@ -299,8 +299,11 @@
|
||||
|
||||
var locs = [{{ location.pk }}];
|
||||
|
||||
printStockLocationLabels(locs);
|
||||
|
||||
printLabels({
|
||||
items: locs,
|
||||
key: 'location',
|
||||
url: '{% url "api-stocklocation-label-list" %}',
|
||||
});
|
||||
});
|
||||
|
||||
$('#multi-location-print-label').click(function() {
|
||||
@ -313,7 +316,11 @@
|
||||
locations.push(loc.pk);
|
||||
});
|
||||
|
||||
printStockLocationLabels(locations);
|
||||
printLabels({
|
||||
items: locations,
|
||||
key: 'location',
|
||||
url: '{% url "api-stocklocation-label-list" %}',
|
||||
});
|
||||
});
|
||||
{% endif %}
|
||||
|
||||
|
@ -1417,8 +1417,11 @@ function loadBuildOutputTable(build_info, options={}) {
|
||||
stock_id_values.push(output.pk);
|
||||
});
|
||||
|
||||
printStockItemLabels(stock_id_values);
|
||||
});
|
||||
printLabels({
|
||||
items: stock_id_values,
|
||||
key: 'item',
|
||||
url: '{% url "api-stockitem-label-list" %}',
|
||||
}); });
|
||||
|
||||
$('#outputs-expand').click(function() {
|
||||
$(table).bootstrapTable('expandAllRows');
|
||||
|
@ -16,218 +16,16 @@
|
||||
|
||||
/* exported
|
||||
printLabels,
|
||||
printPartLabels,
|
||||
printStockItemLabels,
|
||||
printStockLocationLabels,
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Perform the "print" action.
|
||||
/**
|
||||
* Present the user with the available labels,
|
||||
* and allow them to select which label to print.
|
||||
*
|
||||
* The intent is that the available labels have been requested
|
||||
* (via AJAX) from the server.
|
||||
*/
|
||||
function printLabels(url, plugin=null) {
|
||||
|
||||
if (plugin) {
|
||||
// If a plugin is provided, do not redirect the browser.
|
||||
// Instead, perform an API request and display a message
|
||||
|
||||
url = url + `plugin=${plugin}`;
|
||||
|
||||
inventreeGet(url, {}, {
|
||||
success: function(response) {
|
||||
showMessage(
|
||||
'{% trans "Labels sent to printer" %}',
|
||||
{
|
||||
style: 'success',
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
window.open(url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function printStockItemLabels(items) {
|
||||
/**
|
||||
* Print stock item labels for the given stock items
|
||||
*/
|
||||
|
||||
if (items.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "Select Stock Items" %}',
|
||||
'{% trans "Stock item(s) must be selected before printing labels" %}'
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Request available labels from the server
|
||||
inventreeGet(
|
||||
'{% url "api-stockitem-label-list" %}',
|
||||
{
|
||||
enabled: true,
|
||||
items: items,
|
||||
},
|
||||
{
|
||||
success: function(response) {
|
||||
|
||||
if (response.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "No Labels Found" %}',
|
||||
'{% trans "No labels found which match selected stock item(s)" %}',
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Select label to print
|
||||
selectLabel(
|
||||
response,
|
||||
items,
|
||||
{
|
||||
success: function(data) {
|
||||
|
||||
var pk = data.label;
|
||||
|
||||
var href = `/api/label/stock/${pk}/print/?`;
|
||||
|
||||
items.forEach(function(item) {
|
||||
href += `items[]=${item}&`;
|
||||
});
|
||||
|
||||
printLabels(href, data.plugin);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function printStockLocationLabels(locations) {
|
||||
|
||||
if (locations.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "Select Stock Locations" %}',
|
||||
'{% trans "Stock location(s) must be selected before printing labels" %}'
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Request available labels from the server
|
||||
inventreeGet(
|
||||
'{% url "api-stocklocation-label-list" %}',
|
||||
{
|
||||
enabled: true,
|
||||
locations: locations,
|
||||
},
|
||||
{
|
||||
success: function(response) {
|
||||
if (response.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "No Labels Found" %}',
|
||||
'{% trans "No labels found which match selected stock location(s)" %}',
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Select label to print
|
||||
selectLabel(
|
||||
response,
|
||||
locations,
|
||||
{
|
||||
success: function(data) {
|
||||
|
||||
var pk = data.label;
|
||||
|
||||
var href = `/api/label/location/${pk}/print/?`;
|
||||
|
||||
locations.forEach(function(location) {
|
||||
href += `locations[]=${location}&`;
|
||||
});
|
||||
|
||||
printLabels(href, data.plugin);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function printPartLabels(parts) {
|
||||
/**
|
||||
* Print labels for the provided parts
|
||||
*/
|
||||
|
||||
if (parts.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "Select Parts" %}',
|
||||
'{% trans "Part(s) must be selected before printing labels" %}',
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Request available labels from the server
|
||||
inventreeGet(
|
||||
'{% url "api-part-label-list" %}',
|
||||
{
|
||||
enabled: true,
|
||||
parts: parts,
|
||||
},
|
||||
{
|
||||
success: function(response) {
|
||||
|
||||
if (response.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "No Labels Found" %}',
|
||||
'{% trans "No labels found which match the selected part(s)" %}',
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Select label to print
|
||||
selectLabel(
|
||||
response,
|
||||
parts,
|
||||
{
|
||||
success: function(data) {
|
||||
|
||||
var pk = data.label;
|
||||
|
||||
var href = `/api/label/part/${pk}/print/?`;
|
||||
|
||||
parts.forEach(function(part) {
|
||||
href += `parts[]=${part}&`;
|
||||
});
|
||||
|
||||
printLabels(href, data.plugin);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function selectLabel(labels, items, options={}) {
|
||||
/**
|
||||
* Present the user with the available labels,
|
||||
* and allow them to select which label to print.
|
||||
*
|
||||
* The intent is that the available labels have been requested
|
||||
* (via AJAX) from the server.
|
||||
*/
|
||||
|
||||
// Array of available plugins for label printing
|
||||
var plugins = [];
|
||||
@ -347,3 +145,71 @@ function selectLabel(labels, items, options={}) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Print label(s) for the selected items:
|
||||
*
|
||||
* - Retrieve a list of matching label templates from the server
|
||||
* - Present the available templates to the user (if more than one available)
|
||||
* - Request printed labels
|
||||
*
|
||||
* Required options:
|
||||
* - url: The list URL for the particular template type
|
||||
* - items: The list of items to be printed
|
||||
* - key: The key to use in the query parameters
|
||||
*/
|
||||
function printLabels(options) {
|
||||
|
||||
if (!options.items || options.items.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "Select Items" %}',
|
||||
'{% trans "No items selected for printing" }',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let params = {
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
params[options.key] = options.items;
|
||||
|
||||
// Request a list of available label templates
|
||||
inventreeGet(options.url, params, {
|
||||
success: function(response) {
|
||||
if (response.length == 0) {
|
||||
showAlertDialog(
|
||||
'{% trans "No Labels Found" %}',
|
||||
'{% trans "No label templates found which match the selected items" %}',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Select label template for printing
|
||||
selectLabel(response, options.items, {
|
||||
success: function(data) {
|
||||
let href = `${options.url}${data.label}/print/?`;
|
||||
|
||||
options.items.forEach(function(item) {
|
||||
href += `${options.key}=${item}&`;
|
||||
});
|
||||
|
||||
if (data.plugin) {
|
||||
href += `plugin=${data.plugin}`;
|
||||
|
||||
inventreeGet(href, {}, {
|
||||
success: function(response) {
|
||||
showMessage('{% trans "Labels sent to printer" %}', {
|
||||
style: 'success',
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
window.open(href);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
loadTableFilters,
|
||||
makeIconBadge,
|
||||
makeIconButton,
|
||||
printPartLabels,
|
||||
printLabels,
|
||||
renderLink,
|
||||
setFormGroupVisibility,
|
||||
setupFilterList,
|
||||
@ -2182,7 +2182,11 @@ function loadPartTable(table, url, options={}) {
|
||||
items.push(item.pk);
|
||||
});
|
||||
|
||||
printPartLabels(items);
|
||||
printLabels({
|
||||
items: items,
|
||||
key: 'part',
|
||||
url: '{% url "api-part-label-list" %}',
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -17,14 +17,14 @@
|
||||
printReports,
|
||||
*/
|
||||
|
||||
/**
|
||||
* Present the user with the available reports,
|
||||
* and allow them to select which report to print.
|
||||
*
|
||||
* The intent is that the available report templates have been requested
|
||||
* (via AJAX) from the server.
|
||||
*/
|
||||
function selectReport(reports, items, options={}) {
|
||||
/**
|
||||
* Present the user with the available reports,
|
||||
* and allow them to select which report to print.
|
||||
*
|
||||
* The intent is that the available report templates have been requested
|
||||
* (via AJAX) from the server.
|
||||
*/
|
||||
|
||||
// If there is only a single report available, just print!
|
||||
if (reports.length == 1) {
|
||||
@ -112,7 +112,7 @@ function selectReport(reports, items, options={}) {
|
||||
* - Request printed document
|
||||
*
|
||||
* Required options:
|
||||
* - url: The list URL for the particular template
|
||||
* - url: The list URL for the particular template type
|
||||
* - items: The list of objects to print
|
||||
* - key: The key to use in the query parameters
|
||||
*/
|
||||
@ -132,7 +132,7 @@ function printReports(options) {
|
||||
|
||||
params[options.key] = options.items;
|
||||
|
||||
// Request a list of available reports
|
||||
// Request a list of available report templates
|
||||
inventreeGet(options.url, params, {
|
||||
success: function(response) {
|
||||
if (response.length == 0) {
|
||||
|
@ -24,7 +24,7 @@
|
||||
modalSetTitle,
|
||||
modalSubmit,
|
||||
openModal,
|
||||
printStockItemLabels,
|
||||
printLabels,
|
||||
printReports,
|
||||
renderLink,
|
||||
scanItemsIntoLocation,
|
||||
@ -2204,7 +2204,11 @@ function loadStockTable(table, options) {
|
||||
items.push(item.pk);
|
||||
});
|
||||
|
||||
printStockItemLabels(items);
|
||||
printLabels({
|
||||
items: items,
|
||||
key: 'item',
|
||||
url: '{% url "api-stockitem-label-list" %}',
|
||||
});
|
||||
});
|
||||
|
||||
$('#multi-item-print-test-report').click(function() {
|
||||
|
Loading…
Reference in New Issue
Block a user