Skip to content

Commit 4c1650f

Browse files
committed
maint(lib dependshandler): Modernize code.
1 parent 9abf534 commit 4c1650f

File tree

2 files changed

+335
-167
lines changed

2 files changed

+335
-167
lines changed

src/lib/dependshandler.js

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
1-
import $ from "jquery";
21
import parser from "./depends_parse";
32

4-
function DependsHandler(el, expression) {
5-
const $el = $(el);
6-
var $context = $el.closest("form");
7-
if (!$context.length) $context = $(document);
8-
this.$el = $el;
9-
this.$context = $context;
10-
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
11-
}
3+
class DependsHandler {
4+
constructor(el, expression) {
5+
this.el = el;
6+
this.context = el.closest("form") || document;
7+
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
8+
}
9+
10+
_findInput(name) {
11+
const input = this.context.querySelector(`
12+
input[name=${name}],
13+
select[name=${name}],
14+
textarea[name=${name}],
15+
button[name=${name}]
16+
`);
17+
return input || document.querySelector(`#${name}`) || null;
18+
}
1219

13-
DependsHandler.prototype = {
14-
_findInputs: function (name) {
15-
var $input = this.$context.find(":input[name='" + name + "']");
16-
if (!$input.length) $input = $("#" + name);
17-
return $input;
18-
},
20+
_getValue(name) {
21+
const input = this._findInput(name);
22+
if (!input) {
23+
return null;
24+
}
1925

20-
_getValue: function (name) {
21-
var $input = this._findInputs(name);
22-
if (!$input.length) return null;
26+
if (
27+
(input.type === "radio" || input.type === "checkbox") &&
28+
input.checked === false
29+
) {
30+
return null;
31+
}
2332

24-
if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox")
25-
return $input.filter(":checked").val() || null;
26-
else return $input.val();
27-
},
33+
return input.value;
34+
}
2835

29-
getAllInputs: function () {
30-
var todo = [this.ast],
31-
$inputs = $(),
32-
node;
36+
getAllInputs() {
37+
const todo = [this.ast];
38+
const inputs = new Set();
3339

3440
while (todo.length) {
35-
node = todo.shift();
36-
if (node.input) $inputs = $inputs.add(this._findInputs(node.input));
37-
if (node.children && node.children.length)
41+
const node = todo.shift();
42+
if (node.input) {
43+
const input = this._findInput(node.input);
44+
if (input) {
45+
inputs.add(input);
46+
}
47+
}
48+
if (node.children && node.children.length) {
3849
todo.push.apply(todo, node.children);
50+
}
3951
}
40-
return $inputs;
41-
},
52+
return [...inputs];
53+
}
4254

43-
_evaluate: function (node) {
44-
var value = node.input ? this._getValue(node.input) : null,
45-
i;
55+
_evaluate(node) {
56+
const value = node.input ? this._getValue(node.input) : null;
4657

4758
switch (node.type) {
4859
case "NOT":
4960
return !this._evaluate(node.children[0]);
50-
case "AND":
51-
for (i = 0; i < node.children.length; i++)
52-
if (!this._evaluate(node.children[i])) return false;
53-
return true;
54-
case "OR":
55-
for (i = 0; i < node.children.length; i++)
56-
if (this._evaluate(node.children[i])) return true;
57-
return false;
61+
case "AND": {
62+
// As soon as one child evaluates to false, the AND expression is false.
63+
const is_false = node.children.some((child) => !this._evaluate(child));
64+
return !is_false;
65+
}
66+
case "OR": {
67+
// As soon as one child evaluates to true, the OR expression is true.
68+
const is_true = node.children.some((child) => this._evaluate(child));
69+
return is_true;
70+
}
5871
case "comparison":
5972
switch (node.operator) {
6073
case "=":
@@ -70,21 +83,25 @@ DependsHandler.prototype = {
7083
case ">=":
7184
return value >= node.value;
7285
case "~=":
73-
if (value === null) return false;
86+
if (value === null) {
87+
return false;
88+
}
7489
return value.indexOf(node.value) != -1;
7590
case "=~":
76-
if (value === null || !node.value) return false;
91+
if (value === null || !node.value) {
92+
return false;
93+
}
7794
return node.value.indexOf(value) != -1;
7895
}
7996
break;
8097
case "truthy":
8198
return !!value;
8299
}
83-
},
100+
}
84101

85-
evaluate: function () {
102+
evaluate() {
86103
return this._evaluate(this.ast);
87-
},
88-
};
104+
}
105+
}
89106

90107
export default DependsHandler;

0 commit comments

Comments
 (0)