From 295e83dc888724ab275bd6ba0a5542e2b4f9d04f Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sun, 5 Nov 2023 15:28:50 +0100 Subject: [PATCH] maint(lib dependshandler): Modernize code. --- src/lib/dependshandler.js | 56 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/lib/dependshandler.js b/src/lib/dependshandler.js index 770ad487d..a67044adb 100644 --- a/src/lib/dependshandler.js +++ b/src/lib/dependshandler.js @@ -11,48 +11,60 @@ function DependsHandler($el, expression) { DependsHandler.prototype = { _findInputs: function (name) { - var $input = this.$context.find(":input[name='" + name + "']"); - if (!$input.length) $input = $("#" + name); + let $input = this.$context.find(":input[name='" + name + "']"); // TODO input outside form + if (!$input.length) { + $input = $("#" + name); + } return $input; }, _getValue: function (name) { - var $input = this._findInputs(name); - if (!$input.length) return null; - - if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox") + const $input = this._findInputs(name); + if (!$input.length) { + return null; + } + if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox") { return $input.filter(":checked").val() || null; - else return $input.val(); + } + return $input.val(); }, getAllInputs: function () { - var todo = [this.ast], - $inputs = $(), - node; + const todo = [this.ast]; + let $inputs = $(); + let node; while (todo.length) { node = todo.shift(); - if (node.input) $inputs = $inputs.add(this._findInputs(node.input)); - if (node.children && node.children.length) + if (node.input) { + $inputs = $inputs.add(this._findInputs(node.input)); + } + if (node.children && node.children.length) { todo.push.apply(todo, node.children); + } } return $inputs; }, _evaluate: function (node) { - var value = node.input ? this._getValue(node.input) : null, - i; + const value = node.input ? this._getValue(node.input) : null; switch (node.type) { case "NOT": return !this._evaluate(node.children[0]); case "AND": - for (i = 0; i < node.children.length; i++) - if (!this._evaluate(node.children[i])) return false; + for (const child of node.children.length) { + if (!this._evaluate(child)) { + return false; + } + } return true; case "OR": - for (i = 0; i < node.children.length; i++) - if (this._evaluate(node.children[i])) return true; + for (const child of node.children) { + if (this._evaluate(child)) { + return true; + } + } return false; case "comparison": switch (node.operator) { @@ -69,10 +81,14 @@ DependsHandler.prototype = { case ">=": return value >= node.value; case "~=": - if (value === null) return false; + if (value === null) { + return false; + } return value.indexOf(node.value) != -1; case "=~": - if (value === null || !node.value) return false; + if (value === null || !node.value) { + return false; + } return node.value.indexOf(value) != -1; } break;