From 4ee55847f12030e5179fd6bd517b4164fefa34a2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 28 Nov 2021 14:21:20 +1100 Subject: [PATCH] Refactor attachment tables --- InvenTree/build/templates/build/detail.html | 54 ++------ .../order/purchase_order_detail.html | 51 ++----- .../templates/order/sales_order_detail.html | 52 ++----- InvenTree/part/templates/part/detail.html | 57 ++------ InvenTree/stock/templates/stock/item.html | 55 ++------ InvenTree/templates/attachment_button.html | 3 + .../templates/js/translated/attachment.js | 131 +++++++++++++----- 7 files changed, 143 insertions(+), 260 deletions(-) diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index 31e9f38080..8479c2819f 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -431,53 +431,17 @@ enableDragAndDrop( } ); -// Callback for creating a new attachment -$('#new-attachment').click(function() { - - constructForm('{% url "api-build-attachment-list" %}', { - fields: { - attachment: {}, - comment: {}, - build: { - value: {{ build.pk }}, - hidden: true, - } - }, - method: 'POST', - onSuccess: reloadAttachmentTable, - title: '{% trans "Add Attachment" %}', - }); -}); - -loadAttachmentTable( - '{% url "api-build-attachment-list" %}', - { - filters: { - build: {{ build.pk }}, - }, - onEdit: function(pk) { - var url = `/api/build/attachment/${pk}/`; - - constructForm(url, { - fields: { - filename: {}, - comment: {}, - }, - onSuccess: reloadAttachmentTable, - title: '{% trans "Edit Attachment" %}', - }); - }, - onDelete: function(pk) { - - constructForm(`/api/build/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); +loadAttachmentTable('{% url "api-build-attachment-list" %}', { + filters: { + build: {{ build.pk }}, + }, + fields: { + build: { + value: {{ build.pk }}, + hidden: true, } } -); +}); $('#edit-notes').click(function() { constructForm('{% url "api-build-detail" build.pk %}', { diff --git a/InvenTree/order/templates/order/purchase_order_detail.html b/InvenTree/order/templates/order/purchase_order_detail.html index 257707347a..3a6ea090d5 100644 --- a/InvenTree/order/templates/order/purchase_order_detail.html +++ b/InvenTree/order/templates/order/purchase_order_detail.html @@ -124,51 +124,16 @@ } ); - loadAttachmentTable( - '{% url "api-po-attachment-list" %}', - { - filters: { - order: {{ order.pk }}, - }, - onEdit: function(pk) { - var url = `/api/order/po/attachment/${pk}/`; - - constructForm(url, { - fields: { - filename: {}, - comment: {}, - }, - onSuccess: reloadAttachmentTable, - title: '{% trans "Edit Attachment" %}', - }); - }, - onDelete: function(pk) { - - constructForm(`/api/order/po/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); + loadAttachmentTable('{% url "api-po-attachment-list" %}', { + filters: { + order: {{ order.pk }}, + }, + fields: { + order: { + value: {{ order.pk }}, + hidden: true, } } - ); - - $("#new-attachment").click(function() { - - constructForm('{% url "api-po-attachment-list" %}', { - method: 'POST', - fields: { - attachment: {}, - comment: {}, - order: { - value: {{ order.pk }}, - hidden: true, - }, - }, - reload: true, - title: '{% trans "Add Attachment" %}', - }); }); loadStockTable($("#stock-table"), { diff --git a/InvenTree/order/templates/order/sales_order_detail.html b/InvenTree/order/templates/order/sales_order_detail.html index 887ecd390c..1cf2ce06cc 100644 --- a/InvenTree/order/templates/order/sales_order_detail.html +++ b/InvenTree/order/templates/order/sales_order_detail.html @@ -110,55 +110,21 @@ }, label: 'attachment', success: function(data, status, xhr) { - location.reload(); + reloadAttachmentTable(); } } ); - loadAttachmentTable( - '{% url "api-so-attachment-list" %}', - { - filters: { - order: {{ order.pk }}, + loadAttachmentTable('{% url "api-so-attachment-list" %}', { + filters: { + order: {{ order.pk }}, + }, + fields: { + order: { + value: {{ order.pk }}, + hidden: true, }, - onEdit: function(pk) { - var url = `/api/order/so/attachment/${pk}/`; - - constructForm(url, { - fields: { - filename: {}, - comment: {}, - }, - onSuccess: reloadAttachmentTable, - title: '{% trans "Edit Attachment" %}', - }); - }, - onDelete: function(pk) { - constructForm(`/api/order/so/attachment/${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); - } } - ); - - $("#new-attachment").click(function() { - - constructForm('{% url "api-so-attachment-list" %}', { - method: 'POST', - fields: { - attachment: {}, - comment: {}, - order: { - value: {{ order.pk }}, - hidden: true - } - }, - onSuccess: reloadAttachmentTable, - title: '{% trans "Add Attachment" %}' - }); }); loadBuildTable($("#builds-table"), { diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index a737bfa6fc..4cf6f5e824 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -999,36 +999,17 @@ }); onPanelLoad("part-attachments", function() { - loadAttachmentTable( - '{% url "api-part-attachment-list" %}', - { - filters: { - part: {{ part.pk }}, - }, - onEdit: function(pk) { - var url = `/api/part/attachment/${pk}/`; - - constructForm(url, { - fields: { - filename: {}, - comment: {}, - }, - title: '{% trans "Edit Attachment" %}', - onSuccess: reloadAttachmentTable, - }); - }, - onDelete: function(pk) { - var url = `/api/part/attachment/${pk}/`; - - constructForm(url, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); + loadAttachmentTable('{% url "api-part-attachment-list" %}', { + filters: { + part: {{ part.pk }}, + }, + fields: { + part: { + value: {{ part.pk }}, + hidden: true } } - ); + }); enableDragAndDrop( '#attachment-dropzone', @@ -1043,26 +1024,6 @@ } } ); - - $("#new-attachment").click(function() { - - constructForm( - '{% url "api-part-attachment-list" %}', - { - method: 'POST', - fields: { - attachment: {}, - comment: {}, - part: { - value: {{ part.pk }}, - hidden: true, - } - }, - onSuccess: reloadAttachmentTable, - title: '{% trans "Add Attachment" %}', - } - ) - }); }); diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 9bafc2633c..9cc6d85aeb 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -221,55 +221,16 @@ } ); - loadAttachmentTable( - '{% url "api-stock-attachment-list" %}', - { - filters: { - stock_item: {{ item.pk }}, - }, - onEdit: function(pk) { - var url = `/api/stock/attachment/${pk}/`; - - constructForm(url, { - fields: { - filename: {}, - comment: {}, - }, - title: '{% trans "Edit Attachment" %}', - onSuccess: reloadAttachmentTable - }); - }, - onDelete: function(pk) { - var url = `/api/stock/attachment/${pk}/`; - - constructForm(url, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete Operation" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); + loadAttachmentTable('{% url "api-stock-attachment-list" %}', { + filters: { + stock_item: {{ item.pk }}, + }, + fields: { + stock_item: { + value: {{ item.pk }}, + hidden: true, } } - ); - - $("#new-attachment").click(function() { - - constructForm( - '{% url "api-stock-attachment-list" %}', - { - method: 'POST', - fields: { - attachment: {}, - comment: {}, - stock_item: { - value: {{ item.pk }}, - hidden: true, - }, - }, - reload: true, - title: '{% trans "Add Attachment" %}', - } - ); }); loadStockTestResultsTable( diff --git a/InvenTree/templates/attachment_button.html b/InvenTree/templates/attachment_button.html index e1561010c0..d220f4829d 100644 --- a/InvenTree/templates/attachment_button.html +++ b/InvenTree/templates/attachment_button.html @@ -1,5 +1,8 @@ {% load i18n %} + \ No newline at end of file diff --git a/InvenTree/templates/js/translated/attachment.js b/InvenTree/templates/js/translated/attachment.js index 5ff5786588..87f2fbdcc0 100644 --- a/InvenTree/templates/js/translated/attachment.js +++ b/InvenTree/templates/js/translated/attachment.js @@ -6,10 +6,57 @@ */ /* exported + addAttachmentButtonCallbacks, loadAttachmentTable, reloadAttachmentTable, */ + +/* + * Add callbacks to buttons for creating new attachments. + * + * Note: Attachments can also be external links! + */ +function addAttachmentButtonCallbacks(url, fields={}) { + + // Callback for 'new attachment' button + $('#new-attachment').click(function() { + + var file_fields = { + attachment: {}, + comment: {}, + }; + + Object.assign(file_fields, fields); + + constructForm(url, { + fields: file_fields, + method: 'POST', + onSuccess: reloadAttachmentTable, + title: '{% trans "Add Attachment" %}', + }); + }); + + // Callback for 'new link' button + $('#new-attachment-link').click(function() { + + var link_fields = { + link: {}, + comment: {}, + }; + + Object.assign(link_fields, fields); + + constructForm(url, { + fields: link_fields, + method: 'POST', + onSuccess: reloadAttachmentTable, + title: '{% trans "Add Link" %}', + }); + }); +} + + function reloadAttachmentTable() { $('#attachment-table').bootstrapTable('refresh'); @@ -20,6 +67,8 @@ function loadAttachmentTable(url, options) { var table = options.table || '#attachment-table'; + addAttachmentButtonCallbacks(url, options.fields || {}); + $(table).inventreeTable({ url: url, name: options.name || 'attachments', @@ -34,56 +83,70 @@ function loadAttachmentTable(url, options) { $(table).find('.button-attachment-edit').click(function() { var pk = $(this).attr('pk'); - if (options.onEdit) { - options.onEdit(pk); - } + constructForm(`${url}${pk}/`, { + fields: { + link: {}, + comment: {}, + }, + onSuccess: reloadAttachmentTable, + title: '{% trans "Edit Attachment" %}', + }); }); // Add callback for 'delete' button $(table).find('.button-attachment-delete').click(function() { var pk = $(this).attr('pk'); - if (options.onDelete) { - options.onDelete(pk); - } + constructForm(`${url}${pk}/`, { + method: 'DELETE', + confirmMessage: '{% trans "Confirm Delete" %}', + title: '{% trans "Delete Attachment" %}', + onSuccess: reloadAttachmentTable, + }); }); }, columns: [ { field: 'attachment', - title: '{% trans "File" %}', - formatter: function(value) { + title: '{% trans "Attachment" %}', + formatter: function(value, row) { - var icon = 'fa-file-alt'; + if (row.attachment) { + var icon = 'fa-file-alt'; - var fn = value.toLowerCase(); + var fn = value.toLowerCase(); - if (fn.endsWith('.csv')) { - icon = 'fa-file-csv'; - } else if (fn.endsWith('.pdf')) { - icon = 'fa-file-pdf'; - } else if (fn.endsWith('.xls') || fn.endsWith('.xlsx')) { - icon = 'fa-file-excel'; - } else if (fn.endsWith('.doc') || fn.endsWith('.docx')) { - icon = 'fa-file-word'; - } else if (fn.endsWith('.zip') || fn.endsWith('.7z')) { - icon = 'fa-file-archive'; + if (fn.endsWith('.csv')) { + icon = 'fa-file-csv'; + } else if (fn.endsWith('.pdf')) { + icon = 'fa-file-pdf'; + } else if (fn.endsWith('.xls') || fn.endsWith('.xlsx')) { + icon = 'fa-file-excel'; + } else if (fn.endsWith('.doc') || fn.endsWith('.docx')) { + icon = 'fa-file-word'; + } else if (fn.endsWith('.zip') || fn.endsWith('.7z')) { + icon = 'fa-file-archive'; + } else { + var images = ['.png', '.jpg', '.bmp', '.gif', '.svg', '.tif']; + + images.forEach(function(suffix) { + if (fn.endsWith(suffix)) { + icon = 'fa-file-image'; + } + }); + } + + var split = value.split('/'); + var filename = split[split.length - 1]; + + var html = ` ${filename}`; + + return renderLink(html, value); + } else if (row.link) { + return renderLink(row.link, row.link); } else { - var images = ['.png', '.jpg', '.bmp', '.gif', '.svg', '.tif']; - - images.forEach(function(suffix) { - if (fn.endsWith(suffix)) { - icon = 'fa-file-image'; - } - }); + return '-'; } - - var split = value.split('/'); - var filename = split[split.length - 1]; - - var html = ` ${filename}`; - - return renderLink(html, value); } }, {