From 54429264576c89058356de6e2d5da83c319dfa73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 20 Nov 2023 21:20:10 +0100 Subject: [PATCH] Allow DLC to have date_finished set --- games/static/js/add_purchase.js | 8 ++++++-- games/static/js/utils.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/games/static/js/add_purchase.js b/games/static/js/add_purchase.js index e27e88c..fbaa58e 100644 --- a/games/static/js/add_purchase.js +++ b/games/static/js/add_purchase.js @@ -2,7 +2,7 @@ import { syncSelectInputUntilChanged, getEl, disableElementsWhenTrue, - disableElementsWhenFalse, + disableElementsWhenValueNotEqual, } from "./utils.js"; let syncData = [ @@ -21,7 +21,11 @@ function setupElementHandlers() { "#id_name", "#id_related_purchase", ]); - disableElementsWhenFalse("#id_type", "game", ["#id_date_finished"]); + disableElementsWhenValueNotEqual( + "#id_type", + ["game", "dlc"], + ["#id_date_finished"] + ); } document.addEventListener("DOMContentLoaded", setupElementHandlers); diff --git a/games/static/js/utils.js b/games/static/js/utils.js index 3a5ee1b..a3ede13 100644 --- a/games/static/js/utils.js +++ b/games/static/js/utils.js @@ -138,16 +138,44 @@ function conditionalElementHandler(...configs) { }); } -function disableElementsWhenFalse(targetSelect, targetValue, elementList) { +function disableElementsWhenValueNotEqual( + targetSelect, + targetValue, + elementList +) { return conditionalElementHandler([ () => { - return getEl(targetSelect).value != targetValue; + let target = getEl(targetSelect); + console.debug( + `${disableElementsWhenTrue.name}: triggered on ${target.id}` + ); + console.debug(` + ${disableElementsWhenTrue.name}: matching against value(s): ${targetValue}`); + if (targetValue instanceof Array) { + if (targetValue.every((value) => target.value != value)) { + console.debug( + `${disableElementsWhenTrue.name}: none of the values is equal to ${target.value}, returning true.` + ); + return true; + } + } else { + console.debug( + `${disableElementsWhenTrue.name}: none of the values is equal to ${target.value}, returning true.` + ); + return target.value != targetValue; + } }, elementList, (el) => { + console.debug( + `${disableElementsWhenTrue.name}: evaluated true, disabling ${el.id}.` + ); el.disabled = "disabled"; }, (el) => { + console.debug( + `${disableElementsWhenTrue.name}: evaluated false, NOT disabling ${el.id}.` + ); el.disabled = ""; }, ]);