-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdependshandler.js
105 lines (96 loc) · 3.24 KB
/
dependshandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import $ from "jquery";
import parser from "./depends_parse";
import utils from "../core/utils";
function DependsHandler($el, expression) {
const el = utils.jqToNode($el);
this.$el = $el;
this.$context = $(el.form || el.closest("form") || document);
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
}
DependsHandler.prototype = {
_findInputs: function (name) {
let $input = this.$context.find(":input[name='" + name + "']"); // TODO input outside form
if (!$input.length) {
$input = $("#" + name);
}
return $input;
},
_getValue: function (name) {
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;
}
return $input.val();
},
getAllInputs: function () {
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) {
todo.push.apply(todo, node.children);
}
}
return $inputs;
},
_evaluate: function (node) {
const value = node.input ? this._getValue(node.input) : null;
switch (node.type) {
case "NOT":
return !this._evaluate(node.children[0]);
case "AND":
for (const child of node.children) {
if (!this._evaluate(child)) {
return false;
}
}
return true;
case "OR":
for (const child of node.children) {
if (this._evaluate(child)) {
return true;
}
}
return false;
case "comparison":
switch (node.operator) {
case "=":
return node.value == value;
case "!=":
return node.value != value;
case "<=":
return value <= node.value;
case "<":
return value < node.value;
case ">":
return value > node.value;
case ">=":
return value >= node.value;
case "~=":
if (value === null) {
return false;
}
return value.indexOf(node.value) != -1;
case "=~":
if (value === null || !node.value) {
return false;
}
return node.value.indexOf(value) != -1;
}
break;
case "truthy":
return !!value;
}
},
evaluate: function () {
return this._evaluate(this.ast);
},
};
export default DependsHandler;