From 2df7c520be6ce87a5c4e08f377f2b7a118b2fedd Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 9 Jul 2021 11:14:10 +1000 Subject: [PATCH] Improve rendering of "Company" in API form - Add a thumbnail image - Refactor select2 thumbnail code --- InvenTree/templates/js/model_renderers.js | 52 +++++++++++------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/InvenTree/templates/js/model_renderers.js b/InvenTree/templates/js/model_renderers.js index 9e98199bfa..f13388237d 100644 --- a/InvenTree/templates/js/model_renderers.js +++ b/InvenTree/templates/js/model_renderers.js @@ -1,5 +1,20 @@ {% load i18n %} + +function blankImage() { + return `/static/img/blank_image.png`; +} + +// Render a select2 thumbnail image +function select2Thumbnail(image) { + if (!image) { + image = blankImage(); + } + + return ``; +} + + /* * This file contains functions for rendering various InvenTree database models, * in particular for displaying them in modal forms in a 'select2' context. @@ -15,8 +30,10 @@ // Renderer for "Company" model function renderCompany(name, data, parameters, options) { + + var html = select2Thumbnail(data.image); - var html = `${data.name} - ${data.description}`; + html += `${data.name} - ${data.description}`; html += `{% trans "Company ID" %}: ${data.pk}`; @@ -27,11 +44,7 @@ function renderCompany(name, data, parameters, options) { // Renderer for "StockItem" model function renderStockItem(name, data, parameters, options) { - var image = data.part_detail.thumbnail || data.part_detail.image; - - if (!image) { - image = `/static/img/blank_image.png`; - } + var image = data.part_detail.thumbnail || data.part_detail.image || blankImage(); var html = ``; @@ -72,15 +85,13 @@ function renderStockLocation(name, data, parameters, options) { function renderBuild(name, data, parameters, options) { - var image = ''; + var image = null; if (data.part_detail && data.part_detail.thumbnail) { image = data.part_detail.thumbnail; - } else { - image = `/static/img/blank_image.png`; - } + } - var html = ``; + var html = select2Thumbnail(image); html += `${data.reference} - ${data.quantity} x ${data.part_detail.full_name}`; html += `{% trans "Build ID" %}: ${data.pk}`; @@ -94,13 +105,7 @@ function renderBuild(name, data, parameters, options) { // Renderer for "Part" model function renderPart(name, data, parameters, options) { - var image = data.image; - - if (!image) { - image = `/static/img/blank_image.png`; - } - - var html = ``; + var html = select2Thumbnail(data.image); html += ` ${data.full_name || data.name}`; @@ -131,7 +136,6 @@ function renderOwner(name, data, parameters, options) { var html = `${data.name}`; - switch (data.label) { case 'user': html += ``; @@ -177,13 +181,7 @@ function renderPartParameterTemplate(name, data, parameters, options) { // Rendered for "SupplierPart" model function renderSupplierPart(name, data, parameters, options) { - var image = data.supplier_detail.image; - - if (!image) { - image = `/static/img/blank_image.png`; - } - - var html = ``; + var html = select2Thumbnail(data.supplier_detail.image); html += ` ${data.supplier_detail.name} - ${data.SKU}`; html += ` - ${data.part_detail.full_name}`; @@ -193,4 +191,4 @@ function renderSupplierPart(name, data, parameters, options) { return html; -} \ No newline at end of file +}