2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-03 04:00:57 +00:00

Merge remote-tracking branch 'inventree/master' into stock-item-forms

This commit is contained in:
Oliver
2021-11-04 10:57:40 +11:00
17 changed files with 238 additions and 141 deletions

View File

@ -1,317 +0,0 @@
{% load inventree_extras %}
/* globals
ClipboardJS,
inventreeFormDataUpload,
launchModalForm,
user_settings,
*/
/* exported
attachClipboard,
enableDragAndDrop,
exportFormatOptions,
inventreeDocReady,
inventreeLoad,
inventreeSave,
*/
function attachClipboard(selector, containerselector, textElement) {
// set container
if (containerselector) {
containerselector = document.getElementById(containerselector);
} else {
containerselector = document.body;
}
var text = null;
// set text-function
if (textElement) {
text = function() {
return document.getElementById(textElement).textContent;
};
} else {
text = function(trigger) {
var content = trigger.parentElement.parentElement.textContent;
return content.trim();
};
}
// create Clipboard
// eslint-disable-next-line no-unused-vars
var cis = new ClipboardJS(selector, {
text: text,
container: containerselector
});
}
/**
* Return a standard list of export format options *
*/
function exportFormatOptions() {
return [
{
value: 'csv',
display_name: 'CSV',
},
{
value: 'tsv',
display_name: 'TSV',
},
{
value: 'xls',
display_name: 'XLS',
},
{
value: 'xlsx',
display_name: 'XLSX',
},
];
}
function inventreeDocReady() {
/* Run this function when the HTML document is loaded.
* This will be called for every page that extends "base.html"
*/
window.addEventListener('dragover', function(e) {
e = e || event;
e.preventDefault();
}, false);
window.addEventListener('drop', function(e) {
e = e || event;
e.preventDefault();
}, false);
/* Add drag-n-drop functionality to any element
* marked with the class 'dropzone'
*/
$('.dropzone').on('dragenter', function(event) {
// TODO - Only indicate that a drop event will occur if a file is being dragged
var transfer = event.originalEvent.dataTransfer;
// eslint-disable-next-line no-constant-condition
if (true || isFileTransfer(transfer)) {
$(this).addClass('dragover');
}
});
$('.dropzone').on('dragleave drop', function() {
$(this).removeClass('dragover');
});
// Callback to launch the 'About' window
$('#launch-about').click(function() {
var modal = $('#modal-about');
modal.modal({
backdrop: 'static',
keyboard: true,
});
modal.modal('show');
});
// Callback to launch the 'Database Stats' window
$('#launch-stats').click(function() {
launchModalForm('/stats/', {
no_post: true,
});
});
// Initialize clipboard-buttons
attachClipboard('.clip-btn');
attachClipboard('.clip-btn', 'modal-about');
attachClipboard('.clip-btn-version', 'modal-about', 'about-copy-text');
// Add autocomplete to the search-bar
$('#search-bar').autocomplete({
source: function(request, response) {
$.ajax({
url: '/api/part/',
data: {
search: request.term,
limit: user_settings.SEARCH_PREVIEW_RESULTS,
offset: 0
},
success: function(data) {
var transformed = $.map(data.results, function(el) {
return {
label: el.full_name,
id: el.pk,
thumbnail: el.thumbnail,
data: el,
};
});
response(transformed);
},
error: function() {
response([]);
}
});
},
create: function() {
$(this).data('ui-autocomplete')._renderItem = function(ul, item) {
var html = `<a href='/part/${item.id}/'><span>`;
html += `<img class='hover-img-thumb' src='`;
html += item.thumbnail || `/static/img/blank_image.png`;
html += `'> `;
html += item.label;
html += '</span>';
if (user_settings.SEARCH_SHOW_STOCK_LEVELS) {
html += partStockLabel(
item.data,
{
classes: 'badge-right',
}
);
}
html += '</a>';
return $('<li>').append(html).appendTo(ul);
};
},
select: function( event, ui ) {
window.location = '/part/' + ui.item.id + '/';
},
minLength: 2,
classes: {
'ui-autocomplete': 'dropdown-menu search-menu',
},
});
// Generate brand-icons
$('.brand-icon').each(function(i, obj) {
loadBrandIcon($(this), $(this).attr('brand_name'));
});
// Callback for "admin view" button
$('#admin-button').click(function() {
var url = $(this).attr('url');
location.href = url;
});
}
function isFileTransfer(transfer) {
/* Determine if a transfer (e.g. drag-and-drop) is a file transfer
*/
return transfer.files.length > 0;
}
function enableDragAndDrop(element, url, options) {
/* Enable drag-and-drop file uploading for a given element.
Params:
element - HTML element lookup string e.g. "#drop-div"
url - URL to POST the file to
options - object with following possible values:
label - Label of the file to upload (default='file')
data - Other form data to upload
success - Callback function in case of success
error - Callback function in case of error
method - HTTP method
*/
var data = options.data || {};
$(element).on('drop', function(event) {
var transfer = event.originalEvent.dataTransfer;
var label = options.label || 'file';
var formData = new FormData();
// Add the extra data
for (var key in data) {
formData.append(key, data[key]);
}
if (isFileTransfer(transfer)) {
formData.append(label, transfer.files[0]);
inventreeFormDataUpload(
url,
formData,
{
success: function(data, status, xhr) {
console.log('Uploaded file via drag-and-drop');
if (options.success) {
options.success(data, status, xhr);
}
},
error: function(xhr, status, error) {
console.log('File upload failed');
if (options.error) {
options.error(xhr, status, error);
}
},
method: options.method || 'POST',
}
);
} else {
console.log('Ignoring drag-and-drop event (not a file)');
}
});
}
/**
* Save a key:value pair to local storage
* @param {String} name - settting key
* @param {String} value - setting value
*/
function inventreeSave(name, value) {
var key = `inventree-${name}`;
localStorage.setItem(key, value);
}
/**
* Retrieve a key:value pair from local storage
* @param {*} name - setting key
* @param {*} defaultValue - default value (returned if no matching key:value pair is found)
* @returns
*/
function inventreeLoad(name, defaultValue) {
var key = `inventree-${name}`;
var value = localStorage.getItem(key);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
function loadBrandIcon(element, name) {
// check if icon exists
var icon = window.FontAwesome.icon({prefix: 'fab', iconName: name});
if (icon) {
// add icon to button
element.addClass('fab fa-' + name);
}
}
// Convenience function to determine if an element exists
$.fn.exists = function() {
return this.length !== 0;
};

View File

@ -2,8 +2,6 @@
{% load inventree_extras %}
/* globals
renderErrorMessage,
showAlertDialog,
*/
/* exported
@ -68,6 +66,8 @@ function inventreeGet(url, filters={}, options={}) {
options.error({
error: thrownError
});
} else {
showApiError(xhr, url);
}
}
});
@ -104,6 +104,8 @@ function inventreeFormDataUpload(url, data, options={}) {
if (options.error) {
options.error(xhr, status, error);
} else {
showApiError(xhr, url);
}
}
});
@ -139,6 +141,8 @@ function inventreePut(url, data={}, options={}) {
} else {
console.error(`Error on ${method} to '${url}' - STATUS ${xhr.status}`);
console.error(thrownError);
showApiError(xhr, url);
}
},
complete: function(xhr, status) {
@ -162,8 +166,10 @@ function inventreeDelete(url, options={}) {
return inventreePut(url, {}, options);
}
function showApiError(xhr) {
/*
* Display a notification with error information
*/
function showApiError(xhr, url) {
var title = null;
var message = null;
@ -208,7 +214,11 @@ function showApiError(xhr) {
}
message += '<hr>';
message += renderErrorMessage(xhr);
message += `URL: ${url}`;
showAlertDialog(title, message);
showMessage(title, {
style: 'danger',
icon: 'fas fa-server icon-red',
details: message,
});
}

View File

@ -480,10 +480,10 @@ function barcodeCheckIn(location_id) {
$(modal).modal('hide');
if (status == 'success' && 'success' in response) {
showAlertOrCache('alert-success', response.success, true);
showAlertOrCache(response.success, 'success', true);
location.reload();
} else {
showAlertOrCache('alert-success', '{% trans "Error transferring stock" %}', false);
showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
}
}
}
@ -604,10 +604,10 @@ function scanItemsIntoLocation(item_id_list, options={}) {
$(modal).modal('hide');
if (status == 'success' && 'success' in response) {
showAlertOrCache('alert-success', response.success, true);
showAlertOrCache(response.success, 'success', true);
location.reload();
} else {
showAlertOrCache('alert-danger', '{% trans "Error transferring stock" %}', false);
showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
}
}
}

View File

@ -339,7 +339,7 @@ function completeBuildOutputs(build_id, outputs, options={}) {
break;
default:
$(opts.modal).modal('hide');
showApiError(xhr);
showApiError(xhr, opts.url);
break;
}
}
@ -1527,7 +1527,7 @@ function allocateStockToBuild(build_id, part_id, bom_items, options={}) {
break;
default:
$(opts.modal).modal('hide');
showApiError(xhr);
showApiError(xhr, opts.url);
break;
}
}

View File

@ -125,9 +125,10 @@ function getApiEndpointOptions(url, callback) {
json: 'application/json',
},
success: callback,
error: function() {
error: function(xhr) {
// TODO: Handle error
console.log(`ERROR in getApiEndpointOptions at '${url}'`);
showApiError(xhr, url);
}
});
}
@ -213,12 +214,14 @@ function constructChangeForm(fields, options) {
// Store the entire data object
options.instance = data;
constructFormBody(fields, options);
},
error: function() {
error: function(xhr) {
// TODO: Handle error here
console.log(`ERROR in constructChangeForm at '${options.url}'`);
showApiError(xhr, options.url);
}
});
}
@ -255,9 +258,11 @@ function constructDeleteForm(fields, options) {
constructFormBody(fields, options);
},
error: function() {
error: function(xhr) {
// TODO: Handle error here
console.log(`ERROR in constructDeleteForm at '${options.url}`);
showApiError(xhr, options.url);
}
});
}
@ -722,7 +727,7 @@ function submitFormData(fields, options) {
break;
default:
$(options.modal).modal('hide');
showApiError(xhr);
showApiError(xhr, options.url);
break;
}
}
@ -899,19 +904,19 @@ function handleFormSuccess(response, options) {
// Display any messages
if (response && response.success) {
showAlertOrCache('alert-success', response.success, cache);
showAlertOrCache(response.success, 'success', cache);
}
if (response && response.info) {
showAlertOrCache('alert-info', response.info, cache);
showAlertOrCache(response.info, 'info', cache);
}
if (response && response.warning) {
showAlertOrCache('alert-warning', response.warning, cache);
showAlertOrCache(response.warning, 'warning', cache);
}
if (response && response.danger) {
showAlertOrCache('alert-danger', response.danger, cache);
showAlertOrCache(response.danger, 'dagner', cache);
}
if (options.onSuccess) {

View File

@ -399,19 +399,19 @@ function afterForm(response, options) {
// Display any messages
if (response.success) {
showAlertOrCache('alert-success', response.success, cache);
showAlertOrCache(response.success, 'success', cache);
}
if (response.info) {
showAlertOrCache('alert-info', response.info, cache);
showAlertOrCache(response.info, 'info', cache);
}
if (response.warning) {
showAlertOrCache('alert-warning', response.warning, cache);
showAlertOrCache(response.warning, 'warning', cache);
}
if (response.danger) {
showAlertOrCache('alert-danger', response.danger, cache);
showAlertOrCache(response.danger, 'danger', cache);
}
// Was a callback provided?

View File

@ -555,7 +555,7 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) {
break;
default:
$(opts.modal).modal('hide');
showApiError(xhr);
showApiError(xhr, opts.url);
break;
}
}

View File

@ -680,7 +680,7 @@ function adjustStock(action, items, options={}) {
break;
default:
$(opts.modal).modal('hide');
showApiError(xhr);
showApiError(xhr, opts.url);
break;
}
}