From bb0fc8c2ccf8936385809e764ffa8a37ceb30af7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 13 Mar 2020 01:34:20 +0100 Subject: [PATCH] Consumptions with item selected before --- static/js/base.js | 11 ++++++++++- static/js/consos.js | 30 ++++++++++++++++++++++++++---- templates/note/conso_form.html | 4 +++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/static/js/base.js b/static/js/base.js index cfd8f70e..bc757d96 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -134,9 +134,12 @@ function removeNote(d, note_prefix="note", notes_display, note_list_id, user_not * consumptions, put null if not used) * @param profile_pic_field The identifier of the field that display the profile picture of the hovered note * (useful in consumptions, put null if not used) + * @param alias_click Function that is called when an alias is clicked. If this method exists and doesn't return true, + * the associated note is not displayed. + * Useful for a consumption if the item is selected before. */ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes_display, alias_prefix="alias", - note_prefix="note", user_note_field=null, profile_pic_field=null) { + note_prefix="note", user_note_field=null, profile_pic_field=null, alias_click=null) { let field = $("#" + field_id); // When the user clicks on the search field, it is immediately cleared field.click(function() { @@ -192,6 +195,12 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes // In the other case, we add a new emitter if (disp == null) notes_display.push([alias.name, note.id, note, 1]); + + // If the function alias_click exists, it is called. If it doesn't return true, then the notes are + // note displayed. Useful for a consumption when a button is already clicked + if (alias_click && !alias_click()) + return; + let note_list = $("#" + note_list_id); let html = ""; notes_display.forEach(function(disp) { diff --git a/static/js/consos.js b/static/js/consos.js index 9d56672a..7a38614c 100644 --- a/static/js/consos.js +++ b/static/js/consos.js @@ -29,7 +29,13 @@ let buttons = []; // When the user searches an alias, we update the auto-completion autoCompleteNote("note", "alias_matched", "note_list", notes, notes_display, - "alias", "note", "user_note", "profile_pic"); + "alias", "note", "user_note", "profile_pic", function() { + if (buttons.length > 0) { + consumeAll(); + return false; + } + return true; + }); /** * Add a transaction from a button. @@ -44,7 +50,7 @@ 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[5] === template_id) { + if (b[6] === template_id) { b[1] += 1; button = b; } @@ -52,9 +58,24 @@ function addConso(dest, amount, type, category_id, category_name, template_id, t if (button == null) buttons.push([dest, 1, amount, type, category_id, category_name, template_id, template_name]); - // TODO Only in simple consumption mode - if (notes.length > 0) + if ($("#double_conso:checked").length > 0) { + let html = ""; + buttons.forEach(function(button) { + html += li("conso_button_" + button[6], button[7] + + "" + button[1] + ""); + }); + $("#consos_list").html(html); + } + else if (notes_display.length > 0) consumeAll(); + else { + let html = ""; + buttons.forEach(function(button) { + html += li("conso_button_" + button[6], button[7] + + "" + button[1] + ""); + }); + $("#note_list").html(html); + } } /** @@ -100,6 +121,7 @@ function consume(source, dest, quantity, amount, reason, type, category, templat buttons.length = 0; $("#note_list").html(""); $("#alias_matched").html(""); + $("#consos_list").html(""); displayNote(null, ""); refreshHistory(); refreshBalance(); diff --git a/templates/note/conso_form.html b/templates/note/conso_form.html index f5cc82f9..6ae643b3 100644 --- a/templates/note/conso_form.html +++ b/templates/note/conso_form.html @@ -47,7 +47,7 @@ - + @@ -163,6 +163,8 @@ $("#consos_list_div").hide(); + $("#consume_all").click(consumeAll); + {% for button in transaction_templates %} {% if button.display %} $("#button{{ button.id }}").click(function() {