Refactor 'DeleteManufacturerPart' form (#3067)

* Refactor 'DeleteManufacturerPart' form

- Remove duplicated forms
- Update style to match other forms
- Block on each deletion before progressing to the next one

* PEP fix

* Adds deleteSupplierParts function

* Refactor all instances of supplier part deletion

* Refactor tractor : use convenience function for bootstraptable.getSelections

* Add deleter for manufacturerpartparameter.

Refactor existing code into a single function

* Refactor deletion for stock items

* JS linting
This commit is contained in:
Oliver 2022-05-25 23:35:53 +10:00 committed by GitHub
parent 0e9ea1be0c
commit 2d1776a151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 371 additions and 291 deletions

View File

@ -527,11 +527,7 @@ $('#btn-unallocate').on('click', function() {
$('#allocate-selected-items').click(function() { $('#allocate-selected-items').click(function() {
var bom_items = $("#allocation-table-untracked").bootstrapTable("getSelections"); var bom_items = getTableData('#allocation-table-untracked');
if (bom_items.length == 0) {
bom_items = $("#allocation-table-untracked").bootstrapTable('getData');
}
allocateStockToBuild( allocateStockToBuild(
{{ build.pk }}, {{ build.pk }},

View File

@ -185,8 +185,7 @@ loadBuildTable($("#build-table"), {
{% if report_enabled %} {% if report_enabled %}
$('#multi-build-print').click(function() { $('#multi-build-print').click(function() {
var rows = $("#build-table").bootstrapTable('getSelections'); var rows = getTableData("#build-table");
var build_ids = []; var build_ids = [];
rows.forEach(function(row) { rows.forEach(function(row) {

View File

@ -28,8 +28,8 @@
<div class='button-toolbar container-fluid'> <div class='button-toolbar container-fluid'>
<div class='btn-group' role='group'> <div class='btn-group' role='group'>
<div class='btn-group'> <div class='btn-group'>
<button class="btn btn-primary dropdown-toggle" id='supplier-table-options' type="button" data-bs-toggle="dropdown">{% trans "Options" %} <button class="btn btn-primary dropdown-toggle" id='supplier-table-options' type="button" data-bs-toggle="dropdown">
<span class="caret"></span> <span class='fas fa-tools'></span> <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
{% if roles.purchase_order.add %} {% if roles.purchase_order.add %}
@ -75,7 +75,8 @@
<div class='button-toolbar container-fluid'> <div class='button-toolbar container-fluid'>
<div class='btn-group' role='group'> <div class='btn-group' role='group'>
<div class='btn-group' role='group'> <div class='btn-group' role='group'>
<button class="btn btn-primary dropdown-toggle" id='manufacturer-table-options' type="button" data-bs-toggle="dropdown">{% trans "Options" %} <button class="btn btn-primary dropdown-toggle" id='manufacturer-table-options' type="button" data-bs-toggle="dropdown">
<span class='fas fa-tools'></span>
<span class="caret"></span> <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
@ -307,20 +308,18 @@
} }
); );
linkButtonsToSelection($("#manufacturer-part-table"), ['#manufacturer-table-options']);
$("#multi-manufacturer-part-delete").click(function() { $("#multi-manufacturer-part-delete").click(function() {
var selections = $("#manufacturer-part-table").bootstrapTable("getSelections"); var selections = getTableData('#manufacturer-part-table');
deleteManufacturerParts(selections, { deleteManufacturerParts(selections, {
onSuccess: function() { success: function() {
$("#manufacturer-part-table").bootstrapTable("refresh"); $("#manufacturer-part-table").bootstrapTable("refresh");
} }
}); });
}); });
$("#multi-manufacturer-part-order").click(function() { $("#multi-manufacturer-part-order").click(function() {
var selections = $("#manufacturer-part-table").bootstrapTable("getSelections"); var selections = getTableData('#manufacturer-part-table');
var parts = []; var parts = [];
@ -364,34 +363,20 @@
} }
); );
linkButtonsToSelection($("#supplier-part-table"), ['#supplier-table-options']);
$("#multi-supplier-part-delete").click(function() { $("#multi-supplier-part-delete").click(function() {
var selections = $("#supplier-part-table").bootstrapTable("getSelections");
var requests = []; var selections = getTableData("#supplier-part-table");
showQuestionDialog( deleteSupplierParts(selections, {
'{% trans "Delete Supplier Parts?" %}', success: function() {
'{% trans "All selected supplier parts will be deleted" %}', $('#supplier-part-table').bootstrapTable('refresh');
{
accept: function() {
selections.forEach(function(part) {
var url = `/api/company/part/${part.pk}/`;
requests.push(inventreeDelete(url));
});
$.when.apply($, requests).done(function() {
$('#supplier-part-table').bootstrapTable('refresh');
});
}
} }
); });
}); });
$("#multi-supplier-part-order").click(function() { $("#multi-supplier-part-order").click(function() {
var selections = $("#supplier-part-table").bootstrapTable("getSelections");
var selections = getTableData('#supplier-part-table');
var parts = []; var parts = [];

View File

@ -129,7 +129,9 @@ src="{% static 'img/blank_image.png' %}"
<div id='supplier-button-toolbar'> <div id='supplier-button-toolbar'>
<div class='btn-group'> <div class='btn-group'>
<div id='opt-dropdown' class="btn-group"> <div id='opt-dropdown' class="btn-group">
<button id='supplier-part-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">{% trans "Options" %} <span class="caret"></span></button> <button id='supplier-part-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
<span class='fas fa-tools'></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a class='dropdown-item' href='#' id='supplier-part-delete' title='{% trans "Delete supplier parts" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete" %}</a></li> <li><a class='dropdown-item' href='#' id='supplier-part-delete' title='{% trans "Delete supplier parts" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete" %}</a></li>
</ul> </ul>
@ -174,11 +176,14 @@ src="{% static 'img/blank_image.png' %}"
<div id='parameter-toolbar'> <div id='parameter-toolbar'>
<div class='btn-group'> <div class='btn-group'>
<div id='opt-dropdown' class="btn-group"> <div id='opt-dropdown' class="btn-group">
<button id='parameter-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">{% trans "Options" %} <span class="caret"></span></button> <button id='parameter-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
<span class='fas fa-tools'></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a class='dropdown-item' href='#' id='multi-parameter-delete' title='{% trans "Delete parameters" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete" %}</a></li> <li><a class='dropdown-item' href='#' id='multi-parameter-delete' title='{% trans "Delete parameters" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete" %}</a></li>
</ul> </ul>
</div> </div>
{% include "filter_list.html" with id="manufacturer-part-parameters" %}
</div> </div>
</div> </div>
@ -256,67 +261,22 @@ $('#supplier-create').click(function () {
$("#supplier-part-delete").click(function() { $("#supplier-part-delete").click(function() {
var selections = $("#supplier-table").bootstrapTable("getSelections"); var selections = getTableData('#supplier-table');
var requests = []; deleteSupplierParts(selections, {
success: reloadSupplierPartTable,
showQuestionDialog( });
'{% trans "Delete Supplier Parts?" %}',
'{% trans "All selected supplier parts will be deleted" %}',
{
accept: function() {
selections.forEach(function(part) {
var url = `/api/company/part/${part.pk}/`;
requests.push(inventreeDelete(url));
});
$.when.apply($, requests).done(function() {
reloadSupplierPartTable();
});
}
}
);
}); });
$("#multi-parameter-delete").click(function() { $("#multi-parameter-delete").click(function() {
var selections = $("#parameter-table").bootstrapTable("getSelections"); var selections = getTableData('#parameter-table');
var text = ` deleteManufacturerPartParameters(selections, {
<div class ='alert alert-block alert-danger'> success: function() {
<p>{% trans "Selected parameters will be deleted" %}:</p> $('#parameter-table').bootstrapTable('refresh');
<ul>`;
selections.forEach(function(item) {
text += `<li>${item.name} - <em>${item.value}</em></li>`;
});
text += `
</ul>
</div>`;
showQuestionDialog(
'{% trans "Delete Parameters" %}',
text,
{
accept_text: '{% trans "Delete" %}',
accept: function() {
// Delete each parameter via the API
var requests = [];
selections.forEach(function(item) {
var url = `/api/company/part/manufacturer/parameter/${item.pk}/`;
requests.push(inventreeDelete(url));
});
$.when.apply($, requests).done(function() {
$('#parameter-table').bootstrapTable('refresh');
});
}
} }
); });
}); });
loadSupplierPartTable( loadSupplierPartTable(
@ -326,9 +286,9 @@ loadSupplierPartTable(
params: { params: {
part: {{ part.part.id }}, part: {{ part.part.id }},
manufacturer_part: {{ part.id }}, manufacturer_part: {{ part.id }},
part_detail: false, part_detail: true,
supplier_detail: true, supplier_detail: true,
manufacturer_detail: false, manufacturer_detail: true,
}, },
} }
); );
@ -343,10 +303,6 @@ loadManufacturerPartParameterTable(
} }
); );
linkButtonsToSelection($("#supplier-table"), ['#supplier-part-options']);
linkButtonsToSelection($("#parameter-table"), ['#parameter-options']);
$('#order-part, #order-part2').click(function() { $('#order-part, #order-part2').click(function() {
inventreeGet( inventreeGet(
@ -376,15 +332,26 @@ $('#edit-part').click(function () {
$('#delete-part').click(function() { $('#delete-part').click(function() {
deleteManufacturerPart({{ part.pk }}, { inventreeGet(
onSuccess: function() { '{% url "api-manufacturer-part-detail" part.pk %}',
{% if part.manufacturer %} {},
window.location.href = "{% url 'company-detail' part.manufacturer.id %}"; {
{% else%} success: function(data) {
window.location.href = "{% url 'index' %}"; deleteManufacturerParts(
{% endif %} [data],
{
success: function() {
{% if part.manufacturer %}
window.location.href = "{% url 'company-detail' part.manufacturer.id %}";
{% else%}
window.location.href = "{% url 'index' %}";
{% endif %}
}
}
);
}
} }
}); );
}); });
enableSidebar('manufacturerpart'); enableSidebar('manufacturerpart');

View File

@ -4,7 +4,7 @@
{% trans "Parameters" as text %} {% trans "Parameters" as text %}
{% include "sidebar_item.html" with label='parameters' text=text icon="fa-th-list" %} {% include "sidebar_item.html" with label='parameters' text=text icon="fa-th-list" %}
{% trans "Attachments" as text %}
{% include "sidebar_item.html" with label='attachments' text=text icon="fa-paperclip" %}
{% trans "Supplier Parts" as text %} {% trans "Supplier Parts" as text %}
{% include "sidebar_item.html" with label='supplier-parts' text=text icon="fa-building" %} {% include "sidebar_item.html" with label='supplier-parts' text=text icon="fa-building" %}
{% trans "Attachments" as text %}
{% include "sidebar_item.html" with label='attachments' text=text icon="fa-paperclip" %}

View File

@ -191,11 +191,7 @@ $('#new-po-line').click(function() {
{% elif order.status == PurchaseOrderStatus.PLACED %} {% elif order.status == PurchaseOrderStatus.PLACED %}
$('#receive-selected-items').click(function() { $('#receive-selected-items').click(function() {
var items = $("#po-line-table").bootstrapTable('getSelections'); var items = getTableData('#po-line-table');
if (items.length == 0) {
items = $("#po-line-table").bootstrapTable('getData');
}
receivePurchaseOrderItems( receivePurchaseOrderItems(
{{ order.id }}, {{ order.id }},

View File

@ -172,7 +172,7 @@ $("#view-list").click(function() {
{% if report_enabled %} {% if report_enabled %}
$("#order-print").click(function() { $("#order-print").click(function() {
var rows = $("#purchase-order-table").bootstrapTable('getSelections'); var rows = getTableData('#purchase-order-table');
var orders = []; var orders = [];

View File

@ -176,7 +176,7 @@ loadSalesOrderTable("#sales-order-table", {
{% if report_enabled %} {% if report_enabled %}
$("#order-print").click(function() { $("#order-print").click(function() {
var rows = $("#sales-order-table").bootstrapTable('getSelections'); var rows = getTableData('#sales-order-table');
var orders = []; var orders = [];

View File

@ -360,7 +360,9 @@
<div id='opt-dropdown' class="btn-group"> <div id='opt-dropdown' class="btn-group">
<button id='supplier-part-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">{% trans "Options" %} <span class="caret"></span></button> <button id='supplier-part-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">{% trans "Options" %} <span class="caret"></span></button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a class='dropdown-item' href='#' id='supplier-part-delete' title='{% trans "Delete supplier parts" %}'>{% trans "Delete" %}</a></li> <li><a class='dropdown-item' href='#' id='supplier-part-delete' title='{% trans "Delete supplier parts" %}'>
<span class='fas fa-trash-alt icon-red'></span> {% trans "Delete" %}
</a></li>
</ul> </ul>
</div> </div>
{% include "filter_list.html" with id="supplier-part" %} {% include "filter_list.html" with id="supplier-part" %}
@ -443,27 +445,11 @@
$("#supplier-part-delete").click(function() { $("#supplier-part-delete").click(function() {
var selections = $("#supplier-part-table").bootstrapTable("getSelections"); var selections = getTableData('#supplier-part-table');
var requests = []; deleteSupplierParts(selections, {
success: reloadSupplierPartTable,
showQuestionDialog( });
'{% trans "Delete Supplier Parts?" %}',
'{% trans "All selected supplier parts will be deleted" %}',
{
accept: function() {
selections.forEach(function(part) {
var url = `/api/company/part/${part.pk}/`;
requests.push(inventreeDelete(url));
});
$.when.apply($, requests).done(function() {
reloadSupplierPartTable();
});
}
}
);
}); });
loadSupplierPartTable( loadSupplierPartTable(
@ -472,7 +458,7 @@
{ {
params: { params: {
part: {{ part.id }}, part: {{ part.id }},
part_detail: false, part_detail: true,
supplier_detail: true, supplier_detail: true,
manufacturer_detail: true, manufacturer_detail: true,
}, },
@ -497,10 +483,10 @@
$("#manufacturer-part-delete").click(function() { $("#manufacturer-part-delete").click(function() {
var selections = $("#manufacturer-part-table").bootstrapTable("getSelections"); var selectionss = getTableData('#manufacturer-part-table');
deleteManufacturerParts(selections, { deleteManufacturerParts(selections, {
onSuccess: function() { success: function() {
$("#manufacturer-part-table").bootstrapTable("refresh"); $("#manufacturer-part-table").bootstrapTable("refresh");
} }
}); });
@ -587,11 +573,7 @@
$('#bom-item-delete').click(function() { $('#bom-item-delete').click(function() {
// Get a list of the selected BOM items // Get a list of the selected BOM items
var rows = $("#bom-table").bootstrapTable('getSelections'); var rows = getTableData('#bom-table');
if (rows.length == 0) {
rows = $('#bom-table').bootstrapTable('getData');
}
deleteBomItems(rows, { deleteBomItems(rows, {
success: function() { success: function() {

View File

@ -254,7 +254,7 @@
$('#multi-location-print-label').click(function() { $('#multi-location-print-label').click(function() {
var selections = $('#sublocation-table').bootstrapTable('getSelections'); var selections = getTableData('#sublocation-table');
var locations = []; var locations = [];

View File

@ -7,6 +7,7 @@
/* exported /* exported
inventreeGet, inventreeGet,
inventreeDelete, inventreeDelete,
inventreeMultiDelete,
inventreeFormDataUpload, inventreeFormDataUpload,
showApiError, showApiError,
*/ */
@ -171,6 +172,50 @@ function inventreeDelete(url, options={}) {
return inventreePut(url, {}, options); return inventreePut(url, {}, options);
} }
/*
* Perform a 'multi delete' operation:
*
* - Items are deleted sequentially from the database, rather than simultaneous requests
* - This prevents potential overload / transaction issues in the DB backend
*
* Notes:
* - Assumes that each item in the 'items' list has a parameter 'pk'
*/
function inventreeMultiDelete(url, items, options={}) {
if (!url.endsWith('/')) {
url += '/';
}
function doNextDelete() {
if (items.length > 0) {
var item = items.shift();
inventreeDelete(`${url}${item.pk}/`, {
complete: doNextDelete
});
} else {
if (options.modal) {
$(options.modal).modal('hide');
}
if (options.success) {
options.success();
}
}
}
if (options.modal) {
showModalSpinner(options.modal);
}
// Initiate the process
doNextDelete();
}
/* /*
* Display a notification with error information * Display a notification with error information
*/ */

View File

@ -693,41 +693,20 @@ function deleteBomItems(items, options={}) {
`; `;
constructFormBody({}, { constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete selected BOM items?" %}', title: '{% trans "Delete selected BOM items?" %}',
fields: {}, fields: {},
preFormContent: html, preFormContent: html,
submitText: '{% trans "Delete" %}',
submitClass: 'danger',
confirm: true,
onSubmit: function(fields, opts) { onSubmit: function(fields, opts) {
// Individually send DELETE requests for each BOM item
// We do *not* send these all at once, to prevent overloading the server
// Show the progress spinner inventreeMultiDelete(
$(opts.modal).find('#modal-progress-spinner').show(); '{% url "api-bom-list" %}',
items,
function deleteNextBomItem() { {
modal: opts.modal,
if (items.length > 0) { success: options.success,
var item = items.shift();
inventreeDelete(`/api/bom/${item.pk}/`,
{
complete: deleteNextBomItem,
}
);
} else {
// Destroy this modal once all items are deleted
$(opts.modal).modal('hide');
if (options.success) {
options.success();
}
} }
} );
deleteNextBomItem();
}, },
}); });
} }

View File

@ -834,12 +834,7 @@ function loadBuildOutputTable(build_info, options={}) {
var subtable = $(`#output-sub-table-${pk}`); var subtable = $(`#output-sub-table-${pk}`);
if (subtable.exists()) { if (subtable.exists()) {
var rows = subtable.bootstrapTable('getSelections'); var rows = getTableData(`#output-sub-table-${pk}`);
// None selected? Use all!
if (rows.length == 0) {
rows = subtable.bootstrapTable('getData');
}
allocateStockToBuild( allocateStockToBuild(
build_info.pk, build_info.pk,
@ -1291,11 +1286,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Complete multiple outputs // Complete multiple outputs
$('#multi-output-complete').click(function() { $('#multi-output-complete').click(function() {
var outputs = $(table).bootstrapTable('getSelections'); var outputs = getTableData(table);
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
completeBuildOutputs( completeBuildOutputs(
build_info.pk, build_info.pk,
@ -1314,11 +1305,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Delete multiple build outputs // Delete multiple build outputs
$('#multi-output-delete').click(function() { $('#multi-output-delete').click(function() {
var outputs = $(table).bootstrapTable('getSelections'); var outputs = getTableData(table);
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
deleteBuildOutputs( deleteBuildOutputs(
build_info.pk, build_info.pk,
@ -1337,11 +1324,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Print stock item labels // Print stock item labels
$('#incomplete-output-print-label').click(function() { $('#incomplete-output-print-label').click(function() {
var outputs = $(table).bootstrapTable('getSelections'); var outputs = getTableData(table);
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
var stock_id_values = []; var stock_id_values = [];

View File

@ -3,12 +3,11 @@
/* globals /* globals
constructForm, constructForm,
imageHoverIcon, imageHoverIcon,
inventreeDelete, inventreeMultiDelete,
loadTableFilters, loadTableFilters,
makeIconButton, makeIconButton,
renderLink, renderLink,
setupFilterList, setupFilterList,
showQuestionDialog,
*/ */
/* exported /* exported
@ -16,6 +15,8 @@
createManufacturerPart, createManufacturerPart,
createSupplierPart, createSupplierPart,
deleteManufacturerParts, deleteManufacturerParts,
deleteManufacturerPartParameters,
deleteSupplierParts,
editCompany, editCompany,
loadCompanyTable, loadCompanyTable,
loadManufacturerPartTable, loadManufacturerPartTable,
@ -101,15 +102,6 @@ function editManufacturerPart(part, options={}) {
}); });
} }
function deleteManufacturerPart(part, options={}) {
constructForm(`/api/company/part/manufacturer/${part}/`, {
method: 'DELETE',
title: '{% trans "Delete Manufacturer Part" %}',
onSuccess: options.onSuccess,
});
}
function supplierPartFields() { function supplierPartFields() {
@ -211,12 +203,76 @@ function editSupplierPart(part, options={}) {
} }
function deleteSupplierPart(part, options={}) { /*
* Delete one or more SupplierPart objects from the database.
* - User will be provided with a modal form, showing all the parts to be deleted.
* - Delete operations are performed sequentialy, not simultaneously
*/
function deleteSupplierParts(parts, options={}) {
constructForm(`/api/company/part/${part}/`, { if (parts.length == 0) {
return;
}
function renderPart(sup_part) {
var part = sup_part.part_detail;
var thumb = thumbnailImage(part.thumbnail || part.image);
var supplier = '-';
var MPN = '-';
if (sup_part.supplier_detail) {
supplier = sup_part.supplier_detail.name;
}
if (sup_part.manufacturer_part_detail) {
MPN = sup_part.manufacturer_part_detail.MPN;
}
return `
<tr>
<td>${thumb} ${part.full_name}</td>
<td>${sup_part.SKU}</td>
<td>${supplier}</td>
<td>${MPN}</td>
</tr>`;
}
var rows = '';
parts.forEach(function(sup_part) {
rows += renderPart(sup_part);
});
var html = `
<div class='alert alert-block alert-danger'>
{% trans "All selected supplier parts will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "SKU" %}</th>
<th>{% trans "Supplier" %}</th>
<th>{% trans "MPN" %}</th>
</tr>
${rows}
</table>
`;
constructFormBody({}, {
method: 'DELETE', method: 'DELETE',
title: '{% trans "Delete Supplier Part" %}', title: '{% trans "Delete Supplier Parts" %}',
onSuccess: options.onSuccess, preFormContent: html,
onSubmit: function(fields, opts) {
inventreeMultiDelete(
'{% url "api-supplier-part-list" %}',
parts,
{
modal: opts.modal,
success: options.success
}
);
}
}); });
} }
@ -393,58 +449,116 @@ function loadCompanyTable(table, url, options={}) {
} }
/* Delete one or more ManufacturerPart objects from the database.
* - User will be provided with a modal form, showing all the parts to be deleted.
* - Delete operations are performed sequentialy, not simultaneously
*/
function deleteManufacturerParts(selections, options={}) { function deleteManufacturerParts(selections, options={}) {
if (selections.length == 0) { if (selections.length == 0) {
return; return;
} }
var parts = []; function renderPart(man_part, opts={}) {
var part = man_part.part_detail;
var thumb = thumbnailImage(part.thumbnail || part.image);
var text = ` return `
<div class='alert alert-block alert-danger'> <tr>
<p>{% trans "The following manufacturer parts will be deleted" %}:</p> <td>${thumb} ${part.full_name}</td>
<ul>`; <td>${man_part.MPN}</td>
<td>${man_part.manufacturer_detail.name}</td>
</tr>`;
}
selections.forEach(function(item) { var rows = '';
parts.push(item.pk);
text += ` selections.forEach(function(man_part) {
<li> rows += renderPart(man_part);
<p>${item.MPN} - ${item.part_detail.full_name}</p>
</li>`;
}); });
text += ` var html = `
</ul> <div class='alert alert-block alert-danger'>
</div>`; {% trans "All selected manufacturer parts will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "MPN" %}</th>
<th>{% trans "Manufacturer" %}</th>
</tr>
${rows}
</table>
`;
showQuestionDialog( constructFormBody({}, {
'{% trans "Delete Manufacturer Parts" %}', method: 'DELETE',
text, title: '{% trans "Delete Manufacturer Parts" %}',
{ preFormContent: html,
accept_text: '{% trans "Delete" %}', onSubmit: function(fields, opts) {
accept: function() {
// Delete each manufacturer part inventreeMultiDelete(
var requests = []; '{% url "api-manufacturer-part-list" %}',
selections,
parts.forEach(function(pk) { {
var url = `/api/company/part/manufacturer/${pk}`; modal: opts.modal,
success: options.success,
requests.push(inventreeDelete(url)); }
}); );
// Wait for all the requests to complete
$.when.apply($, requests).done(function() {
if (options.onSuccess) {
options.onSuccess();
}
});
}
} }
); });
}
function deleteManufacturerPartParameters(selections, options={}) {
if (selections.length == 0) {
return;
}
function renderParam(param) {
return `
<tr>
<td>${param.name}</td>
<td>${param.units}</td>
</tr>`;
}
var rows = '';
selections.forEach(function(param) {
rows += renderParam(param);
});
var html = `
<div class='alert alert-block alert-danger'>
{% trans "All selected parameters will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Name" %}</th>
<th>{% trans "Value" %}</th>
</tr>
${rows}
</table>
`;
constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete Parameters" %}',
preFormContent: html,
onSubmit: function(fields, opts) {
inventreeMultiDelete(
'{% url "api-manufacturer-part-parameter-list" %}',
selections,
{
modal: opts.modal,
success: options.success,
}
);
}
});
} }
@ -473,6 +587,7 @@ function loadManufacturerPartTable(table, url, options) {
method: 'get', method: 'get',
original: params, original: params,
queryParams: filters, queryParams: filters,
uniqueId: 'pk',
sidePagination: 'server', sidePagination: 'server',
name: 'manufacturerparts', name: 'manufacturerparts',
groupBy: false, groupBy: false,
@ -588,11 +703,12 @@ function loadManufacturerPartTable(table, url, options) {
$(table).find('.button-manufacturer-part-delete').click(function() { $(table).find('.button-manufacturer-part-delete').click(function() {
var pk = $(this).attr('pk'); var pk = $(this).attr('pk');
var row = $(table).bootstrapTable('getRowByUniqueId', pk);
deleteManufacturerPart( deleteManufacturerParts(
pk, [row],
{ {
onSuccess: function() { success: function() {
$(table).bootstrapTable('refresh'); $(table).bootstrapTable('refresh');
} }
} }
@ -618,7 +734,7 @@ function loadManufacturerPartParameterTable(table, url, options) {
filters[key] = params[key]; filters[key] = params[key];
} }
// setupFilterList("manufacturer-part-parameters", $(table)); setupFilterList('manufacturer-part-parameters', $(table));
$(table).inventreeTable({ $(table).inventreeTable({
url: url, url: url,
@ -730,6 +846,7 @@ function loadSupplierPartTable(table, url, options) {
method: 'get', method: 'get',
original: params, original: params,
sidePagination: 'server', sidePagination: 'server',
uniqueId: 'pk',
queryParams: filters, queryParams: filters,
name: 'supplierparts', name: 'supplierparts',
groupBy: false, groupBy: false,
@ -886,11 +1003,12 @@ function loadSupplierPartTable(table, url, options) {
$(table).find('.button-supplier-part-delete').click(function() { $(table).find('.button-supplier-part-delete').click(function() {
var pk = $(this).attr('pk'); var pk = $(this).attr('pk');
var row = $(table).bootstrapTable('getRowByUniqueId', pk);
deleteSupplierPart( deleteSupplierParts(
pk, [row],
{ {
onSuccess: function() { success: function() {
$(table).bootstrapTable('refresh'); $(table).bootstrapTable('refresh');
} }
} }

View File

@ -247,11 +247,6 @@ function constructChangeForm(fields, options) {
*/ */
function constructDeleteForm(fields, options) { function constructDeleteForm(fields, options) {
// Force the "confirm" property if not set
if (!('confirm' in options)) {
options.confirm = true;
}
// Request existing data from the API endpoint // Request existing data from the API endpoint
// This data can be used to render some information on the form // This data can be used to render some information on the form
$.ajax({ $.ajax({
@ -430,6 +425,21 @@ function constructFormBody(fields, options) {
// otherwise *all* fields will be displayed // otherwise *all* fields will be displayed
var displayed_fields = options.fields || fields; var displayed_fields = options.fields || fields;
// Override default option values if a 'DELETE' form is specified
if (options.method == 'DELETE') {
if (!('confirm' in options)) {
options.confirm = true;
}
if (!('submitClass' in options)) {
options.submitClass = 'danger';
}
if (!('submitText' in options)) {
options.submitText = '{% trans "Delete" %}';
}
}
// Handle initial data overrides // Handle initial data overrides
if (options.data) { if (options.data) {
for (const field in options.data) { for (const field in options.data) {
@ -797,7 +807,7 @@ function submitFormData(fields, options) {
} }
// Show the progress spinner // Show the progress spinner
$(options.modal).find('#modal-progress-spinner').show(); showModalSpinner(options.modal);
// Submit data // Submit data
upload_func( upload_func(
@ -2625,7 +2635,7 @@ function selectImportFields(url, data={}, options={}) {
columns.push(getFormFieldValue(`column_${idx}`, {}, opts)); columns.push(getFormFieldValue(`column_${idx}`, {}, opts));
} }
$(opts.modal).find('#modal-progress-spinner').show(); showModalSpinner(opts.modal);
inventreePut( inventreePut(
opts.url, opts.url,

View File

@ -16,6 +16,7 @@
showModalImage, showModalImage,
removeRowFromModalForm, removeRowFromModalForm,
showQuestionDialog, showQuestionDialog,
showModalSpinner,
*/ */
/* /*
@ -1146,3 +1147,13 @@ function showModalImage(image_url) {
hideModalImage(); hideModalImage();
}); });
} }
/* Show (or hide) a progress spinner icon in the dialog */
function showModalSpinner(modal, show=true) {
if (show) {
$(modal).find('#modal-progress-spinner').show();
} else {
$(modal).find('#modal-progress-spinner').hide();
}
}

View File

@ -1560,7 +1560,7 @@ function loadPartTable(table, url, options={}) {
/* Button callbacks for part table buttons */ /* Button callbacks for part table buttons */
$('#multi-part-order').click(function() { $('#multi-part-order').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var parts = []; var parts = [];
@ -1594,7 +1594,7 @@ function loadPartTable(table, url, options={}) {
}); });
$('#multi-part-print-label').click(function() { $('#multi-part-print-label').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var items = []; var items = [];

View File

@ -11,8 +11,8 @@
global_settings, global_settings,
handleFormErrors, handleFormErrors,
imageHoverIcon, imageHoverIcon,
inventreeDelete,
inventreeGet, inventreeGet,
inventreeMultiDelete,
inventreePut, inventreePut,
launchModalForm, launchModalForm,
linkButtonsToSelection, linkButtonsToSelection,
@ -1106,25 +1106,15 @@ function adjustStock(action, items, options={}) {
// Delete action is handled differently // Delete action is handled differently
if (action == 'delete') { if (action == 'delete') {
var requests = [];
item_pk_values.forEach(function(pk) { inventreeMultiDelete(
requests.push( '{% url "api-stock-list" %}',
inventreeDelete( items,
`/api/stock/${pk}/`, {
) modal: opts.modal,
); success: options.success,
});
// Wait for *all* the requests to complete
$.when.apply($, requests).done(function() {
// Destroy the modal window
$(opts.modal).modal('hide');
if (options.success) {
options.success();
} }
}); );
return; return;
} }
@ -1955,7 +1945,7 @@ function loadStockTable(table, options) {
); );
function stockAdjustment(action) { function stockAdjustment(action) {
var items = $(table).bootstrapTable('getSelections'); var items = getTableData(table);
adjustStock(action, items, { adjustStock(action, items, {
success: function() { success: function() {
@ -1967,7 +1957,7 @@ function loadStockTable(table, options) {
// Automatically link button callbacks // Automatically link button callbacks
$('#multi-item-print-label').click(function() { $('#multi-item-print-label').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var items = []; var items = [];
@ -1979,7 +1969,7 @@ function loadStockTable(table, options) {
}); });
$('#multi-item-print-test-report').click(function() { $('#multi-item-print-test-report').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var items = []; var items = [];
@ -1992,7 +1982,7 @@ function loadStockTable(table, options) {
if (global_settings.BARCODE_ENABLE) { if (global_settings.BARCODE_ENABLE) {
$('#multi-item-barcode-scan-into-location').click(function() { $('#multi-item-barcode-scan-into-location').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var items = []; var items = [];
@ -2021,7 +2011,7 @@ function loadStockTable(table, options) {
}); });
$('#multi-item-merge').click(function() { $('#multi-item-merge').click(function() {
var items = $(table).bootstrapTable('getSelections'); var items = getTableData(table);
mergeStockItems(items, { mergeStockItems(items, {
success: function(response) { success: function(response) {
@ -2036,7 +2026,7 @@ function loadStockTable(table, options) {
$('#multi-item-assign').click(function() { $('#multi-item-assign').click(function() {
var items = $(table).bootstrapTable('getSelections'); var items = getTableData(table);
assignStockToCustomer(items, { assignStockToCustomer(items, {
success: function() { success: function() {
@ -2046,7 +2036,8 @@ function loadStockTable(table, options) {
}); });
$('#multi-item-order').click(function() { $('#multi-item-order').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var parts = []; var parts = [];
@ -2063,7 +2054,7 @@ function loadStockTable(table, options) {
$('#multi-item-set-status').click(function() { $('#multi-item-set-status').click(function() {
// Select and set the STATUS field for selected stock items // Select and set the STATUS field for selected stock items
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
// Select stock status // Select stock status
var modal = '#modal-form'; var modal = '#modal-form';
@ -2149,7 +2140,7 @@ function loadStockTable(table, options) {
}); });
$('#multi-item-delete').click(function() { $('#multi-item-delete').click(function() {
var selections = $(table).bootstrapTable('getSelections'); var selections = getTableData(table);
var stock = []; var stock = [];

View File

@ -8,6 +8,7 @@
/* exported /* exported
customGroupSorter, customGroupSorter,
downloadTableData, downloadTableData,
getTableData,
reloadtable, reloadtable,
renderLink, renderLink,
reloadTableFilters, reloadTableFilters,
@ -22,6 +23,23 @@ function reloadtable(table) {
} }
/*
* Return the 'selected' data rows from a bootstrap table.
* If allowEmpty = false, and the returned dataset is empty,
* then instead try to return *all* the data
*/
function getTableData(table, allowEmpty=false) {
var data = $(table).bootstrapTable('getSelections');
if (data.length == 0 && !allowEmpty) {
data = $(table).bootstrapTable('getData');
}
return data;
}
/** /**
* Download data from a table, via the API. * Download data from a table, via the API.
* This requires a number of conditions to be met: * This requires a number of conditions to be met: