mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 21:25:42 +00:00 
			
		
		
		
	Refactor attachment tables
This commit is contained in:
		| @@ -431,53 +431,17 @@ enableDragAndDrop( | |||||||
|     } |     } | ||||||
| ); | ); | ||||||
|  |  | ||||||
| // Callback for creating a new attachment | loadAttachmentTable('{% url "api-build-attachment-list" %}', { | ||||||
| $('#new-attachment').click(function() { |     filters: { | ||||||
|  |         build: {{ build.pk }}, | ||||||
|     constructForm('{% url "api-build-attachment-list" %}', { |     }, | ||||||
|     fields: { |     fields: { | ||||||
|             attachment: {}, |  | ||||||
|             comment: {}, |  | ||||||
|         build: { |         build: { | ||||||
|             value: {{ build.pk }}, |             value: {{ build.pk }}, | ||||||
|             hidden: true, |             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, |  | ||||||
|             }); |  | ||||||
|     } |     } | ||||||
|     } | }); | ||||||
| ); |  | ||||||
|  |  | ||||||
| $('#edit-notes').click(function() { | $('#edit-notes').click(function() { | ||||||
|     constructForm('{% url "api-build-detail" build.pk %}', { |     constructForm('{% url "api-build-detail" build.pk %}', { | ||||||
|   | |||||||
| @@ -124,51 +124,16 @@ | |||||||
|         } |         } | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|     loadAttachmentTable( |     loadAttachmentTable('{% url "api-po-attachment-list" %}', { | ||||||
|         '{% url "api-po-attachment-list" %}', |  | ||||||
|         { |  | ||||||
|         filters: { |         filters: { | ||||||
|             order: {{ order.pk }}, |             order: {{ order.pk }}, | ||||||
|         }, |         }, | ||||||
|             onEdit: function(pk) { |  | ||||||
|                 var url = `/api/order/po/attachment/${pk}/`; |  | ||||||
|  |  | ||||||
|                 constructForm(url, { |  | ||||||
|         fields: { |         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, |  | ||||||
|                 }); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     $("#new-attachment").click(function() { |  | ||||||
|  |  | ||||||
|         constructForm('{% url "api-po-attachment-list" %}', { |  | ||||||
|             method: 'POST', |  | ||||||
|             fields: { |  | ||||||
|                 attachment: {}, |  | ||||||
|                 comment: {}, |  | ||||||
|             order: { |             order: { | ||||||
|                 value: {{ order.pk }}, |                 value: {{ order.pk }}, | ||||||
|                 hidden: true, |                 hidden: true, | ||||||
|                 }, |             } | ||||||
|             }, |         } | ||||||
|             reload: true, |  | ||||||
|             title: '{% trans "Add Attachment" %}', |  | ||||||
|         }); |  | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     loadStockTable($("#stock-table"), { |     loadStockTable($("#stock-table"), { | ||||||
|   | |||||||
| @@ -110,55 +110,21 @@ | |||||||
|             }, |             }, | ||||||
|             label: 'attachment', |             label: 'attachment', | ||||||
|             success: function(data, status, xhr) { |             success: function(data, status, xhr) { | ||||||
|                 location.reload(); |                 reloadAttachmentTable(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|     loadAttachmentTable( |     loadAttachmentTable('{% url "api-so-attachment-list" %}', { | ||||||
|         '{% url "api-so-attachment-list" %}', |  | ||||||
|         { |  | ||||||
|         filters: { |         filters: { | ||||||
|             order: {{ order.pk }}, |             order: {{ order.pk }}, | ||||||
|         }, |         }, | ||||||
|             onEdit: function(pk) { |  | ||||||
|                 var url = `/api/order/so/attachment/${pk}/`; |  | ||||||
|  |  | ||||||
|                 constructForm(url, { |  | ||||||
|         fields: { |         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: { |             order: { | ||||||
|                 value: {{ order.pk }}, |                 value: {{ order.pk }}, | ||||||
|                     hidden: true |                 hidden: true, | ||||||
|                 } |  | ||||||
|             }, |             }, | ||||||
|             onSuccess: reloadAttachmentTable, |         } | ||||||
|             title: '{% trans "Add Attachment" %}' |  | ||||||
|         }); |  | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     loadBuildTable($("#builds-table"), { |     loadBuildTable($("#builds-table"), { | ||||||
|   | |||||||
| @@ -999,36 +999,17 @@ | |||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     onPanelLoad("part-attachments", function() { |     onPanelLoad("part-attachments", function() { | ||||||
|         loadAttachmentTable( |         loadAttachmentTable('{% url "api-part-attachment-list" %}', { | ||||||
|             '{% url "api-part-attachment-list" %}', |  | ||||||
|             { |  | ||||||
|             filters: { |             filters: { | ||||||
|                 part: {{ part.pk }}, |                 part: {{ part.pk }}, | ||||||
|             }, |             }, | ||||||
|                 onEdit: function(pk) { |  | ||||||
|                     var url = `/api/part/attachment/${pk}/`; |  | ||||||
|      |  | ||||||
|                     constructForm(url, { |  | ||||||
|             fields: { |             fields: { | ||||||
|                             filename: {}, |                 part: { | ||||||
|                             comment: {}, |                     value: {{ part.pk }}, | ||||||
|                         }, |                     hidden: true | ||||||
|                         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, |  | ||||||
|                     }); |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         ); |         }); | ||||||
|      |      | ||||||
|         enableDragAndDrop( |         enableDragAndDrop( | ||||||
|             '#attachment-dropzone', |             '#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" %}', |  | ||||||
|                 } |  | ||||||
|             ) |  | ||||||
|         }); |  | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -221,55 +221,16 @@ | |||||||
|             } |             } | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|     loadAttachmentTable( |     loadAttachmentTable('{% url "api-stock-attachment-list" %}', { | ||||||
|         '{% url "api-stock-attachment-list" %}', |  | ||||||
|         { |  | ||||||
|         filters: { |         filters: { | ||||||
|             stock_item: {{ item.pk }}, |             stock_item: {{ item.pk }}, | ||||||
|         }, |         }, | ||||||
|             onEdit: function(pk) { |  | ||||||
|                 var url = `/api/stock/attachment/${pk}/`; |  | ||||||
|  |  | ||||||
|                 constructForm(url, { |  | ||||||
|         fields: { |         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, |  | ||||||
|                 }); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     $("#new-attachment").click(function() { |  | ||||||
|  |  | ||||||
|         constructForm( |  | ||||||
|             '{% url "api-stock-attachment-list" %}', |  | ||||||
|             { |  | ||||||
|                 method: 'POST', |  | ||||||
|                 fields: { |  | ||||||
|                     attachment: {}, |  | ||||||
|                     comment: {}, |  | ||||||
|             stock_item: { |             stock_item: { | ||||||
|                 value: {{ item.pk }}, |                 value: {{ item.pk }}, | ||||||
|                 hidden: true, |                 hidden: true, | ||||||
|                     }, |  | ||||||
|                 }, |  | ||||||
|                 reload: true, |  | ||||||
|                 title: '{% trans "Add Attachment" %}', |  | ||||||
|             } |             } | ||||||
|         ); |         } | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     loadStockTestResultsTable( |     loadStockTestResultsTable( | ||||||
|   | |||||||
| @@ -1,5 +1,8 @@ | |||||||
| {% load i18n %} | {% load i18n %} | ||||||
|  |  | ||||||
|  | <button type='button' class='btn btn-outline-success' id='new-attachment-link'> | ||||||
|  |     <span class='fas fa-link'></span> {% trans "Add Link" %} | ||||||
|  | </button> | ||||||
| <button type='button' class='btn btn-success' id='new-attachment'> | <button type='button' class='btn btn-success' id='new-attachment'> | ||||||
|     <span class='fas fa-plus-circle'></span> {% trans "Add Attachment" %} |     <span class='fas fa-plus-circle'></span> {% trans "Add Attachment" %} | ||||||
| </button> | </button> | ||||||
| @@ -6,10 +6,57 @@ | |||||||
| */ | */ | ||||||
|  |  | ||||||
| /* exported | /* exported | ||||||
|  |     addAttachmentButtonCallbacks, | ||||||
|     loadAttachmentTable, |     loadAttachmentTable, | ||||||
|     reloadAttachmentTable, |     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() { | function reloadAttachmentTable() { | ||||||
|  |  | ||||||
|     $('#attachment-table').bootstrapTable('refresh'); |     $('#attachment-table').bootstrapTable('refresh'); | ||||||
| @@ -20,6 +67,8 @@ function loadAttachmentTable(url, options) { | |||||||
|  |  | ||||||
|     var table = options.table || '#attachment-table'; |     var table = options.table || '#attachment-table'; | ||||||
|  |  | ||||||
|  |     addAttachmentButtonCallbacks(url, options.fields || {}); | ||||||
|  |  | ||||||
|     $(table).inventreeTable({ |     $(table).inventreeTable({ | ||||||
|         url: url, |         url: url, | ||||||
|         name: options.name || 'attachments', |         name: options.name || 'attachments', | ||||||
| @@ -34,26 +83,35 @@ function loadAttachmentTable(url, options) { | |||||||
|             $(table).find('.button-attachment-edit').click(function() { |             $(table).find('.button-attachment-edit').click(function() { | ||||||
|                 var pk = $(this).attr('pk'); |                 var pk = $(this).attr('pk'); | ||||||
|  |  | ||||||
|                 if (options.onEdit) { |                 constructForm(`${url}${pk}/`, { | ||||||
|                     options.onEdit(pk); |                     fields: { | ||||||
|                 } |                         link: {}, | ||||||
|  |                         comment: {},  | ||||||
|  |                     }, | ||||||
|  |                     onSuccess: reloadAttachmentTable, | ||||||
|  |                     title: '{% trans "Edit Attachment" %}', | ||||||
|  |                 }); | ||||||
|             }); |             }); | ||||||
|              |              | ||||||
|             // Add callback for 'delete' button |             // Add callback for 'delete' button | ||||||
|             $(table).find('.button-attachment-delete').click(function() { |             $(table).find('.button-attachment-delete').click(function() { | ||||||
|                 var pk = $(this).attr('pk'); |                 var pk = $(this).attr('pk'); | ||||||
|  |  | ||||||
|                 if (options.onDelete) { |                 constructForm(`${url}${pk}/`, { | ||||||
|                     options.onDelete(pk); |                     method: 'DELETE', | ||||||
|                 } |                     confirmMessage: '{% trans "Confirm Delete" %}', | ||||||
|  |                     title: '{% trans "Delete Attachment" %}', | ||||||
|  |                     onSuccess: reloadAttachmentTable, | ||||||
|  |                 }); | ||||||
|             }); |             }); | ||||||
|         }, |         }, | ||||||
|         columns: [ |         columns: [ | ||||||
|             { |             { | ||||||
|                 field: 'attachment', |                 field: 'attachment', | ||||||
|                 title: '{% trans "File" %}', |                 title: '{% trans "Attachment" %}', | ||||||
|                 formatter: function(value) { |                 formatter: function(value, row) { | ||||||
|  |  | ||||||
|  |                     if (row.attachment) { | ||||||
|                         var icon = 'fa-file-alt'; |                         var icon = 'fa-file-alt'; | ||||||
|  |  | ||||||
|                         var fn = value.toLowerCase(); |                         var fn = value.toLowerCase(); | ||||||
| @@ -84,6 +142,11 @@ function loadAttachmentTable(url, options) { | |||||||
|                         var html = `<span class='fas ${icon}'></span> ${filename}`; |                         var html = `<span class='fas ${icon}'></span> ${filename}`; | ||||||
|  |  | ||||||
|                         return renderLink(html, value); |                         return renderLink(html, value); | ||||||
|  |                     } else if (row.link) { | ||||||
|  |                         return renderLink(row.link, row.link); | ||||||
|  |                     } else { | ||||||
|  |                         return '-'; | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user