1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-29 20:51:11 +02:00

Replace arrays with objects

This commit is contained in:
Yohann D'ANELLO
2020-03-16 09:32:39 +01:00
committed by Bombar Maxime
parent c42a7745bc
commit da12733508
3 changed files with 128 additions and 100 deletions

View File

@ -22,6 +22,45 @@ $(document).ready(function() {
$(document.body).on("click", "a[data-toggle='tab']", function() {
location.hash = this.getAttribute("href");
});
// Switching in double consumptions mode should update the layout
$("#double_conso").click(function() {
$("#consos_list_div").show();
$("#infos_div").attr('class', 'col-sm-5 col-xl-6');
$("#note_infos_div").attr('class', 'col-xl-3');
$("#user_select_div").attr('class', 'col-xl-4');
$("#buttons_div").attr('class', 'col-sm-7 col-xl-6');
if (buttons.length > 0) {
let note_list_obj = $("#note_list");
$("#consos_list").html(note_list_obj.html());
note_list_obj.html("");
}
});
$("#single_conso").click(function() {
$("#consos_list_div").hide();
$("#infos_div").attr('class', 'col-sm-5 col-md-4');
$("#note_infos_div").attr('class', 'col-xl-5');
$("#user_select_div").attr('class', 'col-xl-7');
$("#buttons_div").attr('class', 'col-sm-7 col-md-8');
if (buttons.length > 0) {
if (notes_display.length === 0) {
let consos_list_obj = $("#consos_list");
$("#note_list").html(consos_list_obj.html());
consos_list_obj.html("");
}
else {
buttons.length = 0;
$("#consos_list").html("");
}
}
});
$("#consos_list_div").hide();
$("#consume_all").click(consumeAll);
});
notes = [];
@ -51,32 +90,42 @@ autoCompleteNote("note", "alias_matched", "note_list", notes, notes_display,
function addConso(dest, amount, type, category_id, category_name, template_id, template_name) {
var button = null;
buttons.forEach(function(b) {
if (b[6] === template_id) {
b[1] += 1;
if (b.id === template_id) {
b.quantity += 1;
button = b;
}
});
if (button == null)
buttons.push([dest, 1, amount, type, category_id, category_name, template_id, template_name]);
if (button == null) {
button = {
id: template_id,
name: template_name,
dest: dest,
quantity: 1,
amount: amount,
type: type,
category_id: category_id,
category_name: category_name
};
buttons.push(button);
}
if ($("#double_conso").is(":checked")) {
let dc_obj = $("#double_conso");
if (dc_obj.is(":checked") || notes_display.length === 0) {
let list = dc_obj.is(":checked") ? "consos_list" : "note_list";
let html = "";
buttons.forEach(function(button) {
html += li("conso_button_" + button[6], button[7]
+ "<span class=\"badge badge-dark badge-pill\">" + button[1] + "</span>");
html += li("conso_button_" + button.id, button.name
+ "<span class=\"badge badge-dark badge-pill\">" + button.quantity + "</span>");
});
$("#" + list).html(html);
buttons.forEach(function(button) {
$("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons, list));
});
$("#consos_list").html(html);
}
else if (notes_display.length > 0)
else
consumeAll();
else {
let html = "";
buttons.forEach(function(button) {
html += li("conso_button_" + button[6], button[7]
+ "<span class=\"badge badge-dark badge-pill\">" + button[1] + "</span>");
});
$("#note_list").html(html);
}
}
/**
@ -85,8 +134,8 @@ function addConso(dest, amount, type, category_id, category_name, template_id, t
function consumeAll() {
notes_display.forEach(function(note_display) {
buttons.forEach(function(button) {
consume(note_display[1], button[0], button[1] * note_display[3], button[2],
button[7] + " (" + button[5] + ")", button[3], button[4], button[6]);
consume(note_display.id, button.dest, button.quantity * note_display.quantity, button.amount,
button.name + " (" + button.category_name + ")", button.type, button.category_id, button.id);
});
});
}
@ -128,3 +177,31 @@ function consume(source, dest, quantity, amount, reason, type, category, templat
refreshBalance();
});
}
// When a validate button is clicked, we switch the validation status
function de_validate(id, validated) {
$("#validate_" + id).html("<strong style=\"font-size: 16pt;\">⟳ ...</strong>");
// Perform a PATCH request to the API in order to update the transaction
// If the user has insuffisent rights, an error message will appear
// TODO: Add this error message
$.ajax({
"url": "/api/note/transaction/transaction/" + id + "/",
type: "PATCH",
dataType: "json",
headers: {
"X-CSRFTOKEN": CSRF_TOKEN
},
data: {
"resourcetype": "TemplateTransaction",
valid: !validated
},
success: function () {
refreshHistory();
refreshBalance();
// Refresh jQuery objects
$(".validate").click(de_validate);
}
});
}