Toggle part star status using AJAX

This commit is contained in:
Oliver Walters 2019-05-05 11:18:38 +10:00
parent d2d248c72e
commit fc5fd5e477
4 changed files with 72 additions and 5 deletions

View File

@ -1,5 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %}
{% block sidenav %} {% block sidenav %}
<div id='part-tree'></div> <div id='part-tree'></div>
{% endblock %} {% endblock %}
@ -14,6 +16,11 @@
{% endblock %} {% endblock %}
{% block js_load %}
{{ block.super }}
<script type='text/javascript' src="{% static 'script/inventree/part.js' %}"></script>
{% endblock %}
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
loadTree("{% url 'api-part-tree' %}", loadTree("{% url 'api-part-tree' %}",

View File

@ -105,6 +105,14 @@
); );
}); });
$("#toggle-starred").click(function() {
toggleStar({
part: {{ part.id }},
user: {{ user.id }},
button: '#part-star-icon'
});
});
$('#toggle-starred').click(function() { $('#toggle-starred').click(function() {
}); });

View File

@ -43,9 +43,6 @@ function inventreeGet(url, filters={}, options={}) {
} }
function inventreeUpdate(url, data={}, options={}) { function inventreeUpdate(url, data={}, options={}) {
if ('final' in options && options.final) {
data["_is_final"] = true;
}
var method = options.method || 'PUT'; var method = options.method || 'PUT';
@ -63,8 +60,7 @@ function inventreeUpdate(url, data={}, options={}) {
dataType: 'json', dataType: 'json',
contentType: 'application/json', contentType: 'application/json',
success: function(response, status) { success: function(response, status) {
response['_status_code'] = status; console.log(method + ' - ' + url + ' : result = ' + status);
console.log('UPDATE object to ' + url + ' - result = ' + status);
if (options.success) { if (options.success) {
options.success(response, status); options.success(response, status);
} }

View File

@ -16,4 +16,60 @@ function getPartList(filters={}, options={}) {
function getBomList(filters={}, options={}) { function getBomList(filters={}, options={}) {
return inventreeGet('/api/bom/', 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');
},
}
);
}
},
}
);
} }