-
Notifications
You must be signed in to change notification settings - Fork 141
/
play.html
116 lines (105 loc) · 3.54 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
<!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>Test JsonLogic in the Browser</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<style>
#rule, #data, #output{
height:300px;
margin:0 0 10px;
}
</style>
</head>
<body>
<div style="background-color:#337ab7;color:white;">
<div class="container">
<img src="http://jsonlogic.com/images/jsonlogic-white.png" style="height:70px;float:right;"/>
<h1>Test JsonLogic in your Browser</h1>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="rule">Rule:</label>
<textarea required class="form-control" id="rule"></textarea>
<div class="btn btn-default" id="lint-rule">Lint</div>
<div class="alert" id="message-rule" style="display:none;"></div>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="rule">Data:</label>
<textarea class="form-control" id="data"></textarea>
<div class="btn btn-default" id="lint-data">Lint</div>
<div class="alert" id="message-data" style="display:none;"></div>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="rule">Output:</label>
<pre id="output"></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>
<!-- Latest compiled and minified JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="logic.js"></script>
<script>
function lint(element){
var $element = $(element),
text = $element.val(),
output,
$message = $("#message-" + $element.attr("id") );
if(text === ""){
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{
output = JSON.parse(text);
$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;
}
}
$("#compute").click(function(){
if(lint("#rule") && lint("#data")){
var rule = JSON.parse($("#rule").val()),
data = $("#data").val() === "" ? null : JSON.parse($("#data").val()),
output;
try{
output = jsonLogic.apply(rule, data);
console.log(output);
$("#message-compute").fadeOut();
$("#output").text( JSON.stringify(output, null, 4) );
}catch(e){
$("#output").text("");
$("#message-compute").text(e.message).fadeIn();
}
}
//else: They make their own noise.
});
$("#lint-rule").click(function(){ lint("#rule"); });
$("#lint-data").click(function(){ lint("#data"); });
</script>
</body>
</html>