-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.html
182 lines (174 loc) · 5.29 KB
/
play.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="shortcut icon"
type="image/png"
href="http://jsonlogic.com/images/favicon.png?v=1445437254"
/>
<title>JsonLogic Browser Test</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css"
integrity="sha256-PI8n5gCcz9cQqQXm3PEtDuPG8qx9oFsFctPg0S5zb8g="
crossorigin="anonymous"
/>
<style>
:root {
--jl-blue: #337ab7;
--jl-blue-dim: #22699e;
}
.btn-primary {
--bs-btn-bg: var(--jl-blue);
--bs-btn-border-color: var(--jl-blue);
--bs-btn-hover-bg: var(--jl-blue-dim);
--bs-btn-hover-border-color: var(--jl-blue-dim);
}
header {
background-color: var(--jl-blue);
color: white;
padding: 1rem;
margin-bottom: 1rem;
}
.flex {
display: flex;
justify-content: space-between;
}
#rule,
#data,
pre {
height: 300px;
margin: 0 0 1rem;
border: 1px solid #dcdcdc;
border-radius: 0.25rem;
}
pre {
padding: 0.5rem;
background-color: #f1f1f1;
}
textarea {
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<header>
<div class="container flex">
<h2>JsonLogic Browser Test</h2>
<img
src="http://jsonlogic.com/images/jsonlogic-white.png"
style="height: 50px"
/>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="rule"><h6>Rule</h6></label>
<textarea required class="form-control" id="rule">{}</textarea>
<div class="alert" id="message-rule" style="display: none"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="rule"><h6>Data</h6></label>
<textarea class="form-control" id="data"></textarea>
<div class="alert" id="message-data" style="display: none"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="rule"><h6>Output</h6></label>
<pre id="output"><code></code></pre>
<div class="btn btn-primary" id="compute">Compute</div>
<div
class="alert alert-danger"
id="message-compute"
style="display: none"
></div>
</div>
</div>
</div>
</div>
<script type="module">
import $ from "https://cdn.jsdelivr.net/npm/jquery@3/+esm";
import jsonLogic from "./dist/json-logic.js";
/**
* Lints the text in the element with the given id.
* @param {string} elementId
* @returns {boolean}
*/
const lint = (elementId) => {
const $element = $(elementId);
const val = $element.val();
const $message = $("#message-" + $element.attr("id"));
if (val === "") {
if ($element.attr("required")) {
$message
.removeClass("alert-success")
.addClass("alert-danger")
.text("Can't be blank")
.fadeIn();
return false;
} else {
$message
.removeClass("alert-danger")
.addClass("alert-success")
.text("OK (null)")
.fadeIn();
return true;
}
}
try {
JSON.parse(val);
$message
.removeClass("alert-danger")
.addClass("alert-success")
.text("OK")
.fadeIn();
return true;
} catch (e) {
$message
.removeClass("alert-success")
.addClass("alert-danger")
.text(e.message)
.fadeIn();
return false;
}
};
/** Applies the rule if both the rule and data are valid. */
const compute = () => {
if (lint("#rule") && lint("#data")) {
const rule = JSON.parse($("#rule").val());
const data =
$("#data").val() === "" ? null : JSON.parse($("#data").val());
try {
const output = jsonLogic.apply(rule, data);
console.log(output);
$("#message-compute").fadeOut();
$("#output>code").text(JSON.stringify(output, null, 2));
} catch (e) {
$("#output>code").text("");
$("#message-compute").text(e.message).fadeIn();
}
}
// else: They make their own noise.
};
/** Lints the text in the rule textarea after a neglightible delay. */
const lintRule = () => setTimeout(() => lint("#rule"));
/** Lints the text in the data textarea after a neglightible delay. */
const lintData = () => setTimeout(() => lint("#data"));
// Add event listeners
$("#rule").keyup(lintRule);
$("#data").keyup(lintData);
$("#compute").click(compute);
// On page load
$(lintRule);
$(lintData);
</script>
</body>
</html>