-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
206 lines (176 loc) · 5.04 KB
/
ui.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
exports.stream = {
text,
form,
checkbox,
button,
label,
h1,
section,
textEdit,
textLive,
conditionalTextEdit
};
exports.action = { replace, handle };
exports.kbd = { onEnter, onEscape };
exports.withClass = withClass;
exports.withPlaceholder = withPlaceholder;
exports.withAutofocus = withAutofocus;
exports.withId = withId;
exports.withEvents = withEvents;
function label(s, labelFor) {
const props = { for: labelFor };
return { tag: "label", props, contents: { text: s.valueOf() } };
}
function h1(s) {
return { tag: "h1", props: {}, contents: { text: s.valueOf() } };
}
function section(contents) {
return { tag: "section", props: {}, contents };
}
function conditionalTextEdit(s, editing) {
if (!editing.valueOf()) return {};
const endEdit = replace(editing, null);
const e = textEdit(s, [replace(s), endEdit], endEdit, endEdit);
return withAutofocus(e);
}
function textLive(s, enter, blur, escape) {
const events = { keyup: [replace(s)] };
if (blur) events.blur = blur;
if (enter) events.keyup.push(onEnter(enter));
if (escape) events.keyup.push(onEscape(escape));
return text(s, events);
}
function textEdit(s, enter, blur, escape) {
const events = { keyup: [] };
if (blur) events.blur = blur;
if (enter) events.keyup.push(onEnter(enter));
if (escape) events.keyup.push(onEscape(escape));
return text(s, events);
}
function text(s, events) {
events = events || { keyup: replace(s) };
const props = { type: "text", value: s.valueOf() || "" };
return { tag: "input", props, events, contents: {} };
}
function form(action, contents) {
const props = { action: "#" };
const events = { submit: action };
return { tag: "form", props, events, contents };
}
function checkbox(checked, events) {
events = events || { change: { replace: checked.ref() } };
const props = { type: "checkbox" };
if (checked.valueOf()) props.checked = true;
return { tag: "input", props, events, contents: {} };
}
function button(label, action) {
const events = { click: action };
return { tag: "button", props: {}, events, contents: { text: label } };
}
function withClass(className, spec) {
if (!spec || !spec.props) return spec;
className = className.valueOf();
if (Array.isArray(className)) {
spec.props["class"] = className.join(" ");
} else if (typeof className == "object") {
const o = [];
for (let key in className) {
if ((className[key] || false).valueOf()) o.push(key);
}
spec.props["class"] = o.join(" ");
} else {
spec.props["class"] = className;
}
return spec;
}
function withPlaceholder(placeholder, spec) {
spec.props["placeholder"] = placeholder;
return spec;
}
function withAutofocus(spec) {
spec.props["autofocus"] = "";
return spec;
}
function withId(id, spec) {
spec.props.id = id;
return spec;
}
function withEvents(events, spec) {
spec.events = events || {};
for (let key in events) {
spec.events[key] = events[key];
}
return spec;
}
// Actions
function replace(stream, value) {
if (arguments.length === 1) return { replace: stream.ref() };
if (!value || typeof value !== "object") {
// TODO: fix this hacky way to check for basic type
return { replace: stream.ref(), value };
}
if (value.ref) {
return { replace: stream.ref(), valueref: value.ref() };
}
const action = { replace: stream.ref(), value: {} };
let streams = null;
for (let key in value) {
const v = value[key];
if (v && typeof v.ref === "function") {
streams = streams || {};
streams[key] = v.ref();
} else {
action.value[key] = v;
}
}
if (streams) action.streams = streams;
return action;
}
function onEnter(action) {
return { onKey: action, key: "Enter" };
}
function onEscape(action) {
return { onKey: action, key: "Escape" };
}
function handle(data, state, e, action) {
if (Array.isArray(action)) {
action.map(a => handle(data, state, e, a));
return;
}
if (e.type.startsWith("key") && action.hasOwnProperty("onKey")) {
if (action.key !== e.key) return;
return handle(data, state, e, action.onKey);
}
const resolve = path => {
if (path[0] === "state") {
return resolvePath(state.latest(), path.slice(1));
}
return resolvePath(data.latest(), path);
};
const value = actionValue(e, action, resolve);
const path = action.replace;
for (let key in action.streams || {}) {
value[key] = resolve(action.streams[key]);
}
replacePath(data, state, path, value);
}
function resolvePath(stream, path) {
for (let kk = 0; kk < path.length; kk++) {
stream = stream.get(path[kk]);
}
return stream.valueOf();
}
function actionValue(e, action) {
if (action.hasOwnProperty("value")) return action.value;
if (action.hasOwnProperty("valueref")) return resolve(action.valueref);
if (e.target.type === "checkbox") return e.target.checked;
return e.target.value;
}
function replacePath(data, state, path, value) {
if (path.length > 0 && path[0] === "state") {
state.latest().replacePath(path.slice(1), value);
} else {
data.latest().replacePath(path, value);
}
}