diff --git a/InvenTree/part/templates/part/part_app_base.html b/InvenTree/part/templates/part/part_app_base.html index 30c9b30b6d..8c034dfa6e 100644 --- a/InvenTree/part/templates/part/part_app_base.html +++ b/InvenTree/part/templates/part/part_app_base.html @@ -1,5 +1,7 @@ {% extends "base.html" %} +{% load static %} + {% block sidenav %}
{% endblock %} @@ -14,6 +16,11 @@ {% endblock %} +{% block js_load %} +{{ block.super }} + +{% endblock %} + {% block js_ready %} {{ block.super }} loadTree("{% url 'api-part-tree' %}", diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index 0cac700857..44e2d04d7f 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -105,6 +105,14 @@ ); }); + $("#toggle-starred").click(function() { + toggleStar({ + part: {{ part.id }}, + user: {{ user.id }}, + button: '#part-star-icon' + }); + }); + $('#toggle-starred').click(function() { }); diff --git a/InvenTree/static/script/inventree/api.js b/InvenTree/static/script/inventree/api.js index 530e816f68..27feec8b79 100644 --- a/InvenTree/static/script/inventree/api.js +++ b/InvenTree/static/script/inventree/api.js @@ -43,9 +43,6 @@ function inventreeGet(url, filters={}, options={}) { } function inventreeUpdate(url, data={}, options={}) { - if ('final' in options && options.final) { - data["_is_final"] = true; - } var method = options.method || 'PUT'; @@ -63,8 +60,7 @@ function inventreeUpdate(url, data={}, options={}) { dataType: 'json', contentType: 'application/json', success: function(response, status) { - response['_status_code'] = status; - console.log('UPDATE object to ' + url + ' - result = ' + status); + console.log(method + ' - ' + url + ' : result = ' + status); if (options.success) { options.success(response, status); } diff --git a/InvenTree/static/script/inventree/part.js b/InvenTree/static/script/inventree/part.js index 053355113b..7682d5a6c1 100644 --- a/InvenTree/static/script/inventree/part.js +++ b/InvenTree/static/script/inventree/part.js @@ -16,4 +16,60 @@ function getPartList(filters={}, options={}) { function getBomList(filters={}, options={}) { return inventreeGet('/api/bom/', filters, options); +} + +function toggleStar(options) { + /* Toggle the 'starred' status of a part. + * Performs AJAX queries and updates the display on the button. + * + * options: + * - button: ID of the button (default = '#part-star-icon') + * - part: pk of the part object + * - user: pk of the user + */ + + var url = '/api/part/star/'; + + inventreeGet( + url, + { + part: options.part, + user: options.user, + }, + { + success: function(response) { + if (response.length == 0) { + // Zero length response = star does not exist + // So let's add one! + inventreeUpdate( + url, + { + part: options.part, + user: options.user, + }, + { + method: 'POST', + success: function(response, status) { + $(options.button).removeClass('glyphicon-star-empty').addClass('glyphicon-star'); + }, + } + ); + } else { + var pk = response[0].pk; + // There IS a star (delete it!) + inventreeUpdate( + url + pk + "/", + { + }, + { + method: 'DELETE', + success: function(response, status) { + $(options.button).removeClass('glyphicon-star').addClass('glyphicon-star-empty'); + }, + } + ); + } + }, + } + ); } \ No newline at end of file