-
Notifications
You must be signed in to change notification settings - Fork 88
/
candid-ui.ts
284 lines (273 loc) · 9.04 KB
/
candid-ui.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as IDL from './idl';
import { Principal } from '@dfinity/principal';
import * as UI from './candid-core';
// tslint:disable:max-classes-per-file
type InputBox = UI.InputBox;
const InputConfig: UI.UIConfig = { parse: parsePrimitive };
const FormConfig: UI.FormConfig = { render: renderInput };
export const inputBox = (t: IDL.Type, config: Partial<UI.UIConfig>) => {
return new UI.InputBox(t, { ...InputConfig, ...config });
};
export const recordForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
return new UI.RecordForm(fields, { ...FormConfig, ...config });
};
export const tupleForm = (components: IDL.Type[], config: Partial<UI.FormConfig>) => {
return new UI.TupleForm(components, { ...FormConfig, ...config });
};
export const variantForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
return new UI.VariantForm(fields, { ...FormConfig, ...config });
};
export const optForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
return new UI.OptionForm(ty, { ...FormConfig, ...config });
};
export const vecForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
return new UI.VecForm(ty, { ...FormConfig, ...config });
};
export class Render extends IDL.Visitor<null, InputBox> {
public visitType<T>(t: IDL.Type<T>, d: null): InputBox {
const input = document.createElement('input');
input.classList.add('argument');
input.placeholder = t.display();
return inputBox(t, { input });
}
public visitNull(t: IDL.NullClass, d: null): InputBox {
return inputBox(t, {});
}
public visitRecord(t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, d: null): InputBox {
let config = {};
if (fields.length > 1) {
const container = document.createElement('div');
container.classList.add('popup-form');
config = { container };
}
const form = recordForm(fields, config);
return inputBox(t, { form });
}
public visitTuple<T extends any[]>(
t: IDL.TupleClass<T>,
components: IDL.Type[],
d: null,
): InputBox {
let config = {};
if (components.length > 1) {
const container = document.createElement('div');
container.classList.add('popup-form');
config = { container };
}
const form = tupleForm(components, config);
return inputBox(t, { form });
}
public visitVariant(t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, d: null): InputBox {
const select = document.createElement('select');
for (const [key, type] of fields) {
const option = new Option(key);
select.add(option);
}
select.selectedIndex = -1;
select.classList.add('open');
const config: Partial<UI.FormConfig> = { open: select, event: 'change' };
const form = variantForm(fields, config);
return inputBox(t, { form });
}
public visitOpt<T>(t: IDL.OptClass<T>, ty: IDL.Type<T>, d: null): InputBox {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.classList.add('open');
const form = optForm(ty, { open: checkbox, event: 'change' });
return inputBox(t, { form });
}
public visitVec<T>(t: IDL.VecClass<T>, ty: IDL.Type<T>, d: null): InputBox {
const len = document.createElement('input');
len.type = 'number';
len.min = '0';
len.max = '100';
len.style.width = '8rem';
len.placeholder = 'len';
len.classList.add('open');
const container = document.createElement('div');
container.classList.add('popup-form');
const form = vecForm(ty, { open: len, event: 'change', container });
return inputBox(t, { form });
}
public visitRec<T>(t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, d: null): InputBox {
return renderInput(ty);
}
}
class Parse extends IDL.Visitor<string, any> {
public visitNull(t: IDL.NullClass, v: string): null {
return null;
}
public visitBool(t: IDL.BoolClass, v: string): boolean {
if (v === 'true') {
return true;
}
if (v === 'false') {
return false;
}
throw new Error(`Cannot parse ${v} as boolean`);
}
public visitText(t: IDL.TextClass, v: string): string {
return v;
}
public visitFloat(t: IDL.FloatClass, v: string): number {
return parseFloat(v);
}
public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
if (t._bits <= 32) {
return parseInt(v, 10);
} else {
return BigInt(v);
}
}
public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
if (t._bits <= 32) {
return parseInt(v, 10);
} else {
return BigInt(v);
}
}
public visitNumber(t: IDL.PrimitiveType, v: string): bigint {
return BigInt(v);
}
public visitPrincipal(t: IDL.PrincipalClass, v: string): Principal {
return Principal.fromText(v);
}
public visitService(t: IDL.ServiceClass, v: string): Principal {
return Principal.fromText(v);
}
public visitFunc(t: IDL.FuncClass, v: string): [Principal, string] {
const x = v.split('.', 2);
return [Principal.fromText(x[0]), x[1]];
}
}
class Random extends IDL.Visitor<string, any> {
public visitNull(t: IDL.NullClass, v: string): null {
return null;
}
public visitBool(t: IDL.BoolClass, v: string): boolean {
return Math.random() < 0.5;
}
public visitText(t: IDL.TextClass, v: string): string {
return Math.random().toString(36).substring(6);
}
public visitFloat(t: IDL.FloatClass, v: string): number {
return Math.random();
}
public visitInt(t: IDL.IntClass, v: string): bigint {
return BigInt(this.generateNumber(true));
}
public visitNat(t: IDL.NatClass, v: string): bigint {
return BigInt(this.generateNumber(false));
}
public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
const x = this.generateNumber(true);
if (t._bits <= 32) {
return x;
} else {
return BigInt(v);
}
}
public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
const x = this.generateNumber(false);
if (t._bits <= 32) {
return x;
} else {
return BigInt(v);
}
}
private generateNumber(signed: boolean): number {
const num = Math.floor(Math.random() * 100);
if (signed && Math.random() < 0.5) {
return -num;
} else {
return num;
}
}
}
function parsePrimitive(t: IDL.Type, config: UI.ParseConfig, d: string) {
if (config.random && d === '') {
return t.accept(new Random(), d);
} else {
return t.accept(new Parse(), d);
}
}
/**
*
* @param t an IDL type
* @returns an input for that type
*/
export function renderInput(t: IDL.Type): InputBox {
return t.accept(new Render(), null);
}
interface ValueConfig {
input: InputBox;
value: any;
}
/**
*
* @param t an IDL Type
* @param input an InputBox
* @param value any
* @returns rendering that value to the provided input
*/
export function renderValue(t: IDL.Type, input: InputBox, value: any) {
return t.accept(new RenderValue(), { input, value });
}
class RenderValue extends IDL.Visitor<ValueConfig, void> {
public visitType<T>(t: IDL.Type<T>, d: ValueConfig) {
(d.input.ui.input as HTMLInputElement).value = t.valueToString(d.value);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
public visitNull(t: IDL.NullClass, d: ValueConfig) {}
public visitText(t: IDL.TextClass, d: ValueConfig) {
(d.input.ui.input as HTMLInputElement).value = d.value;
}
public visitRec<T>(t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, d: ValueConfig) {
renderValue(ty, d.input, d.value);
}
public visitOpt<T>(t: IDL.OptClass<T>, ty: IDL.Type<T>, d: ValueConfig) {
if (d.value.length === 0) {
return;
} else {
const form = d.input.ui.form!;
const open = form.ui.open as HTMLInputElement;
open.checked = true;
open.dispatchEvent(new Event(form.ui.event!));
renderValue(ty, form.form[0], d.value[0]);
}
}
public visitRecord(t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
const form = d.input.ui.form!;
fields.forEach(([key, type], i) => {
renderValue(type, form.form[i], d.value[key]);
});
}
public visitTuple<T extends any[]>(t: IDL.TupleClass<T>, components: IDL.Type[], d: ValueConfig) {
const form = d.input.ui.form!;
components.forEach((type, i) => {
renderValue(type, form.form[i], d.value[i]);
});
}
public visitVariant(t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
const form = d.input.ui.form!;
const selected = Object.entries(d.value)[0];
fields.forEach(([key, type], i) => {
if (key === selected[0]) {
const open = form.ui.open as HTMLSelectElement;
open.selectedIndex = i;
open.dispatchEvent(new Event(form.ui.event!));
renderValue(type, form.form[0], selected[1]);
}
});
}
public visitVec<T>(t: IDL.VecClass<T>, ty: IDL.Type<T>, d: ValueConfig) {
const form = d.input.ui.form!;
const len = d.value.length;
const open = form.ui.open as HTMLInputElement;
open.value = len;
open.dispatchEvent(new Event(form.ui.event!));
d.value.forEach((v: T, i: number) => {
renderValue(ty, form.form[i], v);
});
}
}