mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Toggle part star status using AJAX
This commit is contained in:
parent
d2d248c72e
commit
fc5fd5e477
@ -1,5 +1,7 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% load static %}
|
||||
|
||||
{% block sidenav %}
|
||||
<div id='part-tree'></div>
|
||||
{% endblock %}
|
||||
@ -14,6 +16,11 @@
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block js_load %}
|
||||
{{ block.super }}
|
||||
<script type='text/javascript' src="{% static 'script/inventree/part.js' %}"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block js_ready %}
|
||||
{{ block.super }}
|
||||
loadTree("{% url 'api-part-tree' %}",
|
||||
|
@ -105,6 +105,14 @@
|
||||
);
|
||||
});
|
||||
|
||||
$("#toggle-starred").click(function() {
|
||||
toggleStar({
|
||||
part: {{ part.id }},
|
||||
user: {{ user.id }},
|
||||
button: '#part-star-icon'
|
||||
});
|
||||
});
|
||||
|
||||
$('#toggle-starred').click(function() {
|
||||
});
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -17,3 +17,59 @@ 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');
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user