Skip to content

Commit

Permalink
maint(lib dependshandler): Modernize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Nov 5, 2023
1 parent 8bba671 commit 295e83d
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions src/lib/dependshandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 295e83d

Please sign in to comment.