forked from douglascrockford/parseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
143 lines (134 loc) · 4.04 KB
/
demo.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
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
// demo.js
// This is used by demo.html to demonstrate parseq.js. It includes a widget
// function that represents a service factory, a show callback that displays the
// final result, and a parseq routine written as an annotated nested array.
// This interacts with the browser using Plain Old DOM.
/*jslint
browser
*/
/*property
addEventListener, appendChild, backgroundColor, body, createElement,
createTextNode, fallback, getElementById, location, onclick, parallel,
race, reload, sequence, stringify, style, type, value
*/
import parseq from "./parseq.js";
document.getElementById("reset").onclick = function (ignore) {
window.location.reload(true);
};
function widget(name) {
return function widget_requestor(callback, value) {
let result = (
value !== undefined
? value + ">" + name
: name
);
let demo = document.getElementById("demo");
let fieldset = document.createElement("fieldset");
let legend = document.createElement("legend");
let success = document.createElement("input");
let failure = document.createElement("input");
fieldset.appendChild(legend);
fieldset.appendChild(success);
fieldset.appendChild(failure);
legend.appendChild(document.createTextNode(name));
success.type = "button";
success.value = "success";
success.addEventListener(
"click",
function success_handler() {
fieldset.style.backgroundColor = "lightgreen";
return callback(result);
},
false
);
failure.type = "button";
failure.value = "failure";
failure.addEventListener(
"click",
function failure_handler() {
fieldset.style.backgroundColor = "pink";
return callback(undefined, result);
},
false
);
demo.appendChild(fieldset);
return function widget_cancel() {
fieldset.style.backgroundColor = "darkgray";
};
};
}
function show(value, reason) {
let body;
let color;
let demo = document.getElementById("demo");
let fieldset = document.createElement("fieldset");
let legend = document.createElement("legend");
let result;
let title;
if (value !== undefined) {
result = JSON.stringify(value);
title = "success";
color = "lightgreen";
body = "mintcream";
} else {
result = JSON.stringify(reason);
title = "failure";
color = "pink";
body = "mistyrose";
}
fieldset.appendChild(legend);
legend.appendChild(document.createTextNode(title));
fieldset.appendChild(document.createTextNode(result));
fieldset.style.backgroundColor = color;
legend.style.backgroundColor = color;
document.body.style.backgroundColor = body;
demo.appendChild(fieldset);
}
parseq.parallel(
[
widget("A"),
parseq.race([
widget("Race B0"),
widget("Race B1"),
widget("Race B2")
]),
parseq.sequence([
widget("Seq C0"),
widget("Seq C1"),
widget("Seq C2")
]),
parseq.sequence([
widget("Seq D0"),
widget("Seq D1"),
widget("Seq D2")
]),
parseq.fallback([
widget("Fall F0"),
widget("Fall F1"),
widget("Fall F2")
])
],
[
widget("Opt Q"),
parseq.race([
widget("Opt Race R0"),
widget("Opt Race R1"),
widget("Opt Race R2")
]),
parseq.sequence([
widget("Opt Seq S0"),
widget("Opt Seq S1"),
widget("Opt Seq S2")
]),
parseq.sequence([
widget("Opt Seq T0"),
widget("Opt Seq T1"),
widget("Opt Seq T2")
]),
parseq.fallback([
widget("Opt Fall V0"),
widget("Opt Fall V1"),
widget("Opt Fall V2")
])
]
)(show);