mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 04:55:44 +00:00
Set part category (#3134)
* Refactor function to enable / disable submit button on modal forms * Category selection now just uses the AP * Remove unused forms / views * JS linting fixes * remove outdated unit test
This commit is contained in:
@ -642,7 +642,7 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) {
|
||||
addRemoveCallback(opts.modal, `#button-row-remove-${response.pk}`);
|
||||
|
||||
// Re-enable the "submit" button
|
||||
$(opts.modal).find('#modal-form-submit').prop('disabled', false);
|
||||
enableSubmitButton(opts, true);
|
||||
|
||||
// Reload the parent BOM table
|
||||
reloadParentTable();
|
||||
|
@ -596,7 +596,7 @@ function constructFormBody(fields, options) {
|
||||
|
||||
// Immediately disable the "submit" button,
|
||||
// to prevent the form being submitted multiple times!
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', true);
|
||||
enableSubmitButton(options, false);
|
||||
|
||||
// Run custom code before normal form submission
|
||||
if (options.beforeSubmit) {
|
||||
@ -639,13 +639,13 @@ function insertConfirmButton(options) {
|
||||
$(options.modal).find('#modal-footer-buttons').append(html);
|
||||
|
||||
// Disable the 'submit' button
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', true);
|
||||
enableSubmitButton(options, true);
|
||||
|
||||
// Trigger event
|
||||
$(options.modal).find('#modal-confirm').change(function() {
|
||||
var enabled = this.checked;
|
||||
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', !enabled);
|
||||
enableSubmitButton(options, !enabled);
|
||||
});
|
||||
}
|
||||
|
||||
@ -1063,7 +1063,7 @@ function handleFormSuccess(response, options) {
|
||||
|
||||
// Reset the status of the "submit" button
|
||||
if (options.modal) {
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', false);
|
||||
enableSubmitButton(options, true);
|
||||
}
|
||||
|
||||
// Remove any error flags from the form
|
||||
@ -1228,7 +1228,7 @@ function handleFormErrors(errors, fields={}, options={}) {
|
||||
|
||||
// Reset the status of the "submit" button
|
||||
if (options.modal) {
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', false);
|
||||
enableSubmitButton(options, true);
|
||||
}
|
||||
|
||||
// Remove any existing error messages from the form
|
||||
|
@ -11,10 +11,10 @@
|
||||
clearFieldOptions,
|
||||
closeModal,
|
||||
enableField,
|
||||
enableSubmitButton,
|
||||
getFieldValue,
|
||||
reloadFieldOptions,
|
||||
showModalImage,
|
||||
removeRowFromModalForm,
|
||||
showQuestionDialog,
|
||||
showModalSpinner,
|
||||
*/
|
||||
@ -146,6 +146,24 @@ function createNewModal(options={}) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Convenience function to enable (or disable) the "submit" button on a modal form
|
||||
*/
|
||||
function enableSubmitButton(options, enable=true) {
|
||||
|
||||
if (!options || !options.modal) {
|
||||
console.warn('enableSubmitButton() called without modal reference');
|
||||
return;
|
||||
}
|
||||
|
||||
if (enable) {
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', false);
|
||||
} else {
|
||||
$(options.modal).find('#modal-form-submit').prop('disabled', true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function makeOption(text, value, title) {
|
||||
/* Format an option for a select element
|
||||
*/
|
||||
@ -536,18 +554,6 @@ function modalSubmit(modal, callback) {
|
||||
}
|
||||
|
||||
|
||||
function removeRowFromModalForm(e) {
|
||||
/* Remove a row from a table in a modal form */
|
||||
e = e || window.event;
|
||||
|
||||
var src = e.target || e.srcElement;
|
||||
|
||||
var row = $(src).attr('row');
|
||||
|
||||
$('#' + row).remove();
|
||||
}
|
||||
|
||||
|
||||
function renderErrorMessage(xhr) {
|
||||
|
||||
var html = '<b>' + xhr.statusText + '</b><br>';
|
||||
|
@ -8,7 +8,6 @@
|
||||
imageHoverIcon,
|
||||
inventreeGet,
|
||||
inventreePut,
|
||||
launchModalForm,
|
||||
linkButtonsToSelection,
|
||||
loadTableFilters,
|
||||
makeIconBadge,
|
||||
@ -1604,6 +1603,7 @@ function loadPartTable(table, url, options={}) {
|
||||
|
||||
/* Button callbacks for part table buttons */
|
||||
|
||||
// Callback function for the "order parts" button
|
||||
$('#multi-part-order').click(function() {
|
||||
var selections = getTableData(table);
|
||||
|
||||
@ -1613,31 +1613,82 @@ function loadPartTable(table, url, options={}) {
|
||||
parts.push(part);
|
||||
});
|
||||
|
||||
orderParts(
|
||||
parts,
|
||||
{
|
||||
|
||||
}
|
||||
);
|
||||
orderParts(parts, {});
|
||||
});
|
||||
|
||||
// Callback function for the "set category" button
|
||||
$('#multi-part-category').click(function() {
|
||||
var selections = $(table).bootstrapTable('getSelections');
|
||||
|
||||
var selections = getTableData(table);
|
||||
var parts = [];
|
||||
|
||||
selections.forEach(function(item) {
|
||||
parts.push(item.pk);
|
||||
});
|
||||
|
||||
launchModalForm('/part/set-category/', {
|
||||
data: {
|
||||
parts: parts,
|
||||
var html = `
|
||||
<div class='alert alert-block alert-info'>
|
||||
{% trans "Set the part category for the selected parts" %}
|
||||
</div>
|
||||
`;
|
||||
|
||||
constructFormBody({}, {
|
||||
title: '{% trans "Set Part Category" %}',
|
||||
preFormContent: html,
|
||||
fields: {
|
||||
category: {
|
||||
label: '{% trans "Category" %}',
|
||||
help_text: '{% trans "Select Part Category" %}',
|
||||
required: true,
|
||||
type: 'related field',
|
||||
model: 'partcategory',
|
||||
api_url: '{% url "api-part-category-list" %}',
|
||||
}
|
||||
},
|
||||
onSubmit: function(fields, opts) {
|
||||
var category = getFormFieldValue('category', fields['category'], opts);
|
||||
|
||||
if (category == null) {
|
||||
handleFormErrors(
|
||||
{
|
||||
'category': ['{% trans "Category is required" %}']
|
||||
},
|
||||
opts.fields,
|
||||
opts
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the category for each part in sequence
|
||||
function setCategory() {
|
||||
if (parts.length > 0) {
|
||||
var part = parts.shift();
|
||||
|
||||
inventreePut(
|
||||
`/api/part/${part}/`,
|
||||
{
|
||||
category: category,
|
||||
},
|
||||
{
|
||||
method: 'PATCH',
|
||||
complete: setCategory,
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// We are done!
|
||||
$(opts.modal).modal('hide');
|
||||
|
||||
$(table).bootstrapTable('refresh');
|
||||
}
|
||||
};
|
||||
|
||||
// Start the ball rolling
|
||||
showModalSpinner(opts.modal);
|
||||
setCategory();
|
||||
},
|
||||
reload: true,
|
||||
});
|
||||
});
|
||||
|
||||
// Callback function for the "print label" button
|
||||
$('#multi-part-print-label').click(function() {
|
||||
var selections = getTableData(table);
|
||||
|
||||
|
Reference in New Issue
Block a user