arrowcounter/counter/static/js/count_edit.js

45 lines
1.1 KiB
JavaScript

'use strict';
// vim: set ts=2 sw=2 et tw=80:
var acForm = $('#edit-arrowcount-form'),
input = acForm.find('#id_count'),
csrf = acForm.find('[name="csrfmiddlewaretoken"]');
function arrowCountUpdateAjax(mode, n) {
$.ajax({
url: acForm.attr('data-update-ajax'),
data: {
'mode': mode,
'value': n,
'csrfmiddlewaretoken': csrf.val()
},
method: 'POST',
success: function(e) {
if(!e.success) {
M.toast({html: gettext('Error while updating') + ': ' + e.error})
} else if (mode === 'relative') {
input.val(e.count);
}
}
});
}
$('#id_count').keypress(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count) && count >= 0) {
arrowCountUpdateAjax('absolute', count);
}
});
$('.count-up').click(function(e) {
var increment = parseInt(e.currentTarget.getAttribute("data-increment"));
arrowCountUpdateAjax('relative', increment);
});
$('.count-down').click(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count) && count > 0) {
arrowCountUpdateAjax('relative', -1);
}
});