From d209b8a87a00ceb65a67af1180d56086feba0cb1 Mon Sep 17 00:00:00 2001 From: guerler Date: Sat, 2 Oct 2021 11:40:03 -0400 Subject: [PATCH] Improve resilience and handling of client conditional match case helper --- client/src/components/Form/utilities.js | 24 ++++++++++++++++-------- client/src/mvc/form/form-data.js | 5 ++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/client/src/components/Form/utilities.js b/client/src/components/Form/utilities.js index cac83e9ff82..2548b450a6a 100644 --- a/client/src/components/Form/utilities.js +++ b/client/src/components/Form/utilities.js @@ -4,7 +4,7 @@ import _ from "underscore"; * @param{dict} inputs - Nested dictionary of input elements * @param{dict} callback - Called with the mapped dictionary object and corresponding model node */ -export var visitInputs = (inputs, callback, prefix, context) => { +export function visitInputs(inputs, callback, prefix, context) { context = Object.assign({}, context); _.each(inputs, (input) => { if (input && input.type && input.name) { @@ -41,24 +41,32 @@ export var visitInputs = (inputs, callback, prefix, context) => { callback(node, name, context); } } -}; +} /** Match conditional values to selected cases * @param{dict} input - Definition of conditional input parameter * @param{dict} value - Current value */ -export var matchCase = (input, value) => { +export function matchCase(input, value) { if (input.test_param.type == "boolean") { - if (value == "true") { - value = input.test_param.truevalue || "true"; + if (["true", true].includes(value)) { + if (input.test_param.truevalue !== undefined) { + value = input.test_param.truevalue; + } else { + value = "true"; + } } else { - value = input.test_param.falsevalue || "false"; + if (input.test_param.falsevalue !== undefined) { + value = input.test_param.falsevalue; + } else { + value = "false"; + } } } - for (var i in input.cases) { + for (let i = 0; i < input.cases.length; i++) { if (input.cases[i].value == value) { return i; } } return -1; -}; +} diff --git a/client/src/mvc/form/form-data.js b/client/src/mvc/form/form-data.js index b8cd86dfe64..ddf83bc01f4 100644 --- a/client/src/mvc/form/form-data.js +++ b/client/src/mvc/form/form-data.js @@ -125,10 +125,9 @@ export var Manager = Backbone.Model.extend({ /** Matches a new tool model to the current input elements e.g. used to update dynamic options */ matchModel: function (inputs, callback) { - var self = this; visitInputs(inputs, (input, name) => { - if (self.flat_dict[name]) { - callback(input, self.flat_dict[name]); + if (this.flat_dict[name]) { + callback(input, this.flat_dict[name]); } }); },