mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-03 12:10:59 +00:00
Make barcode javascript translatable
This commit is contained in:
289
InvenTree/templates/js/barcode.html
Normal file
289
InvenTree/templates/js/barcode.html
Normal file
@ -0,0 +1,289 @@
|
||||
{% load i18n %}
|
||||
|
||||
/*
|
||||
* Pass barcode data to the server.
|
||||
*/
|
||||
function scanBarcode(barcode, options={}) {
|
||||
|
||||
inventreePut(
|
||||
'/api/barcode/',
|
||||
{
|
||||
'barcode': barcode,
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
success: function(response, status) {
|
||||
console.log(response);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function makeBarcodeInput(placeholderText='') {
|
||||
/*
|
||||
* Generate HTML for a barcode input
|
||||
*/
|
||||
|
||||
placeholderText = placeholderText || '{% trans "Scan barcode data here using wedge scanner" %}';
|
||||
|
||||
var html = `
|
||||
<div id='barcode-error-message'></div>
|
||||
<form class='js-modal-form' method='post'>
|
||||
<div class='form-group'>
|
||||
<label class='control-label' for='barcode'>{% trans "Barcode" %}</label>
|
||||
<div class='controls'>
|
||||
<div class='input-group'>
|
||||
<span class='input-group-addon'>
|
||||
<span class='fas fa-qrcode'></span>
|
||||
</span>
|
||||
<input id='barcode' class='textinput textInput form-control' type='text' name='barcode' placeholder='${placeholderText}'>
|
||||
</div>
|
||||
<div id='hint_barcode_data' class='help-block'>{% trans "Enter barcode data" %}</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
`;
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
|
||||
function showBarcodeError(modal, message, style='danger') {
|
||||
|
||||
var html = `<div class='alert alert-block alert-${style}'>`;
|
||||
|
||||
html += message;
|
||||
|
||||
html += "</div>";
|
||||
|
||||
$(modal + ' #barcode-error-message').html(html);
|
||||
}
|
||||
|
||||
function clearBarcodeError(modal, message) {
|
||||
|
||||
$(modal + ' #barcode-error-message').html('');
|
||||
}
|
||||
|
||||
|
||||
function enableBarcodeInput(modal, enabled=true) {
|
||||
|
||||
var barcode = $(modal + ' #barcode');
|
||||
|
||||
barcode.prop('disabled', !enabled);
|
||||
|
||||
modalEnable(modal, enabled);
|
||||
}
|
||||
|
||||
function getBarcodeData(modal) {
|
||||
|
||||
modal = modal || '#modal-form';
|
||||
|
||||
var el = $(modal + ' #barcode');
|
||||
|
||||
var barcode = el.val();
|
||||
|
||||
el.val('');
|
||||
el.focus();
|
||||
|
||||
return barcode;
|
||||
}
|
||||
|
||||
|
||||
function barcodeDialog(title, options={}) {
|
||||
/*
|
||||
* Handle a barcode display dialog.
|
||||
*/
|
||||
|
||||
var modal = '#modal-form';
|
||||
|
||||
$(modal).on('shown.bs.modal', function() {
|
||||
$(modal + ' .modal-form-content').scrollTop(0);
|
||||
|
||||
// Ensure the barcode field has focus
|
||||
$(modal + ' #barcode').focus();
|
||||
|
||||
var form = $(modal).find('.js-modal-form');
|
||||
|
||||
// Override form submission
|
||||
form.submit(function() {
|
||||
|
||||
var barcode = getBarcodeData(modal);
|
||||
|
||||
if (options.submit) {
|
||||
options.submit(barcode);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
modalSubmit(modal, function() {
|
||||
|
||||
var barcode = getBarcodeData(modal);
|
||||
|
||||
if (options.submit) {
|
||||
options.submit(barcode);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
modalSetTitle(modal, title);
|
||||
modalShowSubmitButton(modal, true);
|
||||
|
||||
var content = '';
|
||||
|
||||
if (options.headerContent) {
|
||||
content += options.headerContent;
|
||||
}
|
||||
|
||||
content += `<div class='alert alert-info alert-block'>{% trans "Scan barcode data below" %}</div>`;
|
||||
|
||||
content += makeBarcodeInput();
|
||||
|
||||
if (options.footerContent) {
|
||||
content += options.footerContent;
|
||||
}
|
||||
|
||||
modalSetContent(modal, content);
|
||||
|
||||
$(modal).modal({
|
||||
backdrop: 'static',
|
||||
keyboard: false,
|
||||
});
|
||||
|
||||
$(modal).modal('show');
|
||||
}
|
||||
|
||||
|
||||
function barcodeScanDialog() {
|
||||
/*
|
||||
* Perform a barcode scan,
|
||||
* and (potentially) redirect the browser
|
||||
*/
|
||||
|
||||
var modal = '#modal-form';
|
||||
|
||||
barcodeDialog(
|
||||
"Scan Barcode",
|
||||
{
|
||||
submit: function(barcode) {
|
||||
enableBarcodeInput(modal, false);
|
||||
inventreePut(
|
||||
'/api/barcode/',
|
||||
{
|
||||
barcode: barcode,
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
success: function(response, status) {
|
||||
|
||||
enableBarcodeInput(modal, true);
|
||||
|
||||
if (status == 'success') {
|
||||
|
||||
if ('success' in response) {
|
||||
if ('url' in response) {
|
||||
// Redirect to the URL!
|
||||
$(modal).modal('hide');
|
||||
window.location.href = response.url;
|
||||
}
|
||||
|
||||
} else if ('error' in response) {
|
||||
showBarcodeError(modal, response.error, 'warning');
|
||||
} else {
|
||||
showBarcodeError(modal, "{% trans 'Unknown response from server' %}", 'warning');
|
||||
}
|
||||
} else {
|
||||
showBarcodeError(modal, `{% trans "Invalid server response" %}.<br>Status code: '${status}'`);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Dialog for linking a particular barcode to a stock item.
|
||||
*/
|
||||
function linkBarcodeDialog(stockitem, options={}) {
|
||||
|
||||
var modal = '#modal-form';
|
||||
|
||||
barcodeDialog(
|
||||
"Link Barcode",
|
||||
{
|
||||
submit: function(barcode) {
|
||||
enableBarcodeInput(modal, false);
|
||||
inventreePut(
|
||||
'/api/barcode/link/',
|
||||
{
|
||||
barcode: barcode,
|
||||
stockitem: stockitem,
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
success: function(response, status) {
|
||||
|
||||
console.log(response);
|
||||
|
||||
enableBarcodeInput(modal, true);
|
||||
|
||||
if (status == 'success') {
|
||||
|
||||
if ('success' in response) {
|
||||
$(modal).modal('hide');
|
||||
location.reload();
|
||||
} else if ('error' in response) {
|
||||
showBarcodeError(modal, response.error, 'warning');
|
||||
} else {
|
||||
showBarcodeError(modal, "{% trans 'Unknown response from server' %}", warning);
|
||||
}
|
||||
|
||||
} else {
|
||||
showBarcodeError(modal, `{% trans "Invalid server response" %}.<br>Status code: '${status}'`);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Remove barcode association from a device.
|
||||
*/
|
||||
function unlinkBarcode(stockitem) {
|
||||
|
||||
var html = `<b>{% trans "Unlink Barcode" %}</b><br>`;
|
||||
|
||||
html += "{% trans 'This will remove the association between this stock item and the barcode' %}";
|
||||
|
||||
showQuestionDialog(
|
||||
"{% trans 'Unlink Barcode' %}",
|
||||
html,
|
||||
{
|
||||
accept_text: "{% trans 'Unlink' %}",
|
||||
accept: function() {
|
||||
inventreePut(
|
||||
`/api/stock/${stockitem}/`,
|
||||
{
|
||||
// Clear the UID field
|
||||
uid: '',
|
||||
},
|
||||
{
|
||||
method: 'PATCH',
|
||||
success: function(response, status) {
|
||||
location.reload();
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user