Improve resilience and handling of client conditional match case helper

This commit is contained in:
guerler
2021-10-02 12:49:02 -04:00
parent 0bfb624ea9
commit d209b8a87a
2 changed files with 18 additions and 11 deletions
+16 -8
View File
@@ -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;
};
}
+2 -3
View File
@@ -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]);
}
});
},