-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-editor.dylan
537 lines (511 loc) · 18.9 KB
/
class-editor.dylan
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
module: web-framework
author: Hannes Mehnert <hannes@mehnert.org>
define open generic respond-to-get
(page, request :: <request>, response :: <response>, #key errors);
define open generic respond-to-post
(page, request :: <request>, response :: <response>);
define method check (object :: <object>, #key test-result = 0)
=> (res :: <boolean>)
#t;
end;
define method edit-form (object :: <object>, #key refer, xml) => (res)
with-xml()
form(action => "/edit", \method => "post")
{ div(class => "edit")
{ do(for (slot in data-slots(object.object-class))
let object = slot.slot-getter-method(object);
collect(with-xml() text(concatenate(slot.slot-name, ": ")) end);
//XXX check if slot is initialized?
collect(edit-slot(object, slot.slot-name));
collect(with-xml() br end);
end;
for (slot in reference-slots(object.object-class))
collect(with-xml() text(concatenate(slot.slot-name, ": ")) end);
//get slot, generate select, option field for each element
//of global list of elements...
collect(with-xml()
\select(name => slot.slot-name)
{ do(let chosen = slot.slot-getter-method(object);
let sel = #f;
for (ele in storage(slot.slot-type))
if (visible?(ele))
if (ele = chosen)
sel := #t;
collect(with-xml()
option(as(<string>, ele),
value => get-reference(ele),
selected => "selected")
end);
else
collect(with-xml()
option(as(<string>, ele),
value => get-reference(ele))
end)
end;
end if;
end;
unless(sel)
collect(with-xml()
option(as(<string>, chosen),
value => get-reference(chosen),
selected => "selected")
end);
end)
}
end);
collect(with-xml() br end);
end),
input(type => "hidden",
name => "parent-object",
value => get-reference(object)),
input(type => "hidden",
name => "action",
value => "save-object"),
input(type => "hidden",
name => "refer-to",
value => if (refer)
refer
else
get-url-from-type(object.object-class)
end),
do(if(xml) xml end),
input(type => "submit",
name => "save-button",
value => "Save")
}
}
end;
end;
//simple case for lists of strings....
define method add-form (type == <string>,
name :: <string>,
parent :: <object>,
#key fill-from-request,
refer,
xml) => (foo)
with-xml()
form(action => "/edit", \method => "post")
{ div(class => "edit")
{
input(type => "text",
name => "string"),
input(type => "hidden",
name => "parent-object",
value => get-reference(parent)),
input(type => "hidden",
name => "object-type",
value => get-reference(type)),
input(type => "hidden",
name => "action",
value => "add-object"),
do(if(xml) xml end),
do(if(refer)
with-xml()
input(type => "hidden",
name => "refer-to",
value => refer)
end
end),
input(type => "submit",
name => "add-button",
value => concatenate("Add to ", name))
}
}
end;
end;
define method add-form (object-type :: subclass(<object>),
name :: false-or(<string>),
parent :: <object>,
#key fill-from-request,
refer,
xml) => (foo) // :: <list> ?
with-xml()
form(action => "/edit", \method => "post")
{ div(class => "edit")
{ do(for (slot in data-slots(object-type))
collect(with-xml() text(concatenate(slot.slot-name, ": ")) end);
//here we should have at least a seperation between integer,
//strings and lists... or should we implement all lists with
//has-many?
let value = default(slot);
let query-value = get-query-value(slot.slot-name);
if (fill-from-request & (query-value & query-value ~= ""))
value := query-value;
end;
if (slot.slot-type = <boolean>)
collect(edit-slot(value, slot.slot-name));
else
if (value)
collect(edit-slot(value, slot.slot-name));
else
collect(with-xml() input(type => "text",
name => slot.slot-name)
end);
end if;
end;
if (slot.default-help-text)
collect(with-xml()
text(concatenate(" defaults to: ", slot.default-help-text))
end);
end;
collect(with-xml() br end);
end;
for (slot in reference-slots(object-type))
collect(with-xml() text(concatenate(slot.slot-name, ": ")) end);
//get slot, generate select, option field for each element
//of global list of elements...
let value = get-object(get-query-value(slot.slot-name));
collect(with-xml()
\select(name => slot.slot-name)
{ do(let sel = #t;
for (ele in storage(slot.slot-type))
if (visible?(ele))
if (fill-from-request & (ele = value))
sel := #f;
collect(with-xml()
option(as(<string>, ele),
value => get-reference(ele),
selected => "selected")
end);
else
collect(with-xml()
option(as(<string>, ele),
value => get-reference(ele))
end);
end;
end;
end;
if (slot.default & sel)
collect(with-xml()
option(as(<string>, slot.default),
value => get-reference(slot.default),
selected => "selected")
end);
end)
}
end);
collect(with-xml() br end);
end),
input(type => "hidden",
name => "parent-object",
value => get-reference(parent)),
input(type => "hidden",
name => "object-type",
value => get-reference(object-type)),
input(type => "hidden",
name => "refer-to",
value => if (refer) refer else get-url-from-type(object-type) end),
input(type => "hidden",
name => "action",
value => "add-object"),
do(if(xml) xml end),
input(type => "submit",
name => "add-button",
value => if (name) concatenate("Add to ", name) else "Add" end)
}
}
end;
end;
define method remove-form (object :: <object>, parent :: <object>,
#key url :: <string> = "edit",
xml)
with-xml()
form(action => "/edit", \method => "post")
{ div(class => "edit")
{
input(type => "hidden",
name => "refer-to",
value => url),
input(type => "hidden",
name => "parent-object",
value => get-reference(parent)),
input(type => "hidden",
name => "remove-this",
value => get-reference(object)),
input(type => "hidden",
name => "action",
value => "remove-object"),
do(if(xml) xml end),
input(type => "submit",
name => "remove-button",
value => "Remove")
}
}
end;
end;
define method list-forms (obj :: <object>) => (res)
let res = make(<stretchy-vector>);
for (slot in list-reference-slots(obj.object-class))
let object = slot.slot-getter-method(obj);
res := add!(res, with-xml()
text(concatenate(slot.slot-name, ": "))
end);
res := add!(res, with-xml() br end);
for (ele in object)
res := add!(res, with-xml()
a(as(<string>, ele),
href => concatenate("/edit?obj=",
get-reference(ele)))
end);
res := add!(res, remove-form(ele, object));
res := add!(res, with-xml() br end);
end;
res := add!(res, add-form(slot.slot-type, slot.slot-name, object));
end;
res;
end;
define generic edit-slot (object :: <object>, slot-name :: <string>);
define method edit-slot (object :: <object>, slot-name :: <string>)
with-xml()
input(type => "text",
size => "60",
name => slot-name,
value => as(<string>, object))
end;
end;
define method edit-slot (object :: <string>, slot-name :: <string>)
with-xml()
input(type => "text",
name => slot-name,
value => object)
end;
end;
define method edit-slot (object :: <integer>, slot-name :: <string>)
with-xml()
input(type => "text",
name => slot-name,
value => integer-to-string(object))
end;
end;
define method edit-slot (object :: <boolean>, slot-name :: <string>)
if (object)
with-xml()
input(type => "checkbox",
name => slot-name,
value => slot-name,
checked => "checked")
end;
else
with-xml()
input(type => "checkbox",
name => slot-name,
value => slot-name)
end;
end;
end;
define constant *debug* = #t;
define method respond-to-post
(page == #"edit",
request :: <request>,
response :: <response>)
let errors = #();
let action = as(<symbol>, get-query-value("action"));
let object-string = get-query-value("parent-object");
let object = get-object(object-string);
let handler <web-form-warning>
= method(e :: <web-form-warning>, next-handler :: <function>)
errors := add!(errors, e);
end;
block(return)
//add, save, remove... we may not need this here...
unless (object)
signal(make(<web-error>,
error: concatenate("Unknown object: ", object-string)));
end;
select (action)
#"add-object" => add-object(object, request);
#"remove-object" => remove-object(object, request);
#"save-object" => save-object(object, request);
otherwise => signal(make(<web-error>,
error: concatenate("Unknown action: ",
as(<string>, action))));
end select;
dump-data(); //yes, this is a bit ugly :(
exception (e :: <web-error>)
errors := add!(errors, e);
if (*debug*)
break()
end;
return();
exception (e :: <error>)
errors := add!(errors, make(<web-error>,
error: format-to-string("%=", e)));
if (*debug*)
break()
end;
return();
end;
let referer = get-query-value("refer-to");
let elements = split(referer, '-');
block(return)
unless ((elements.size = 2) & elements[1] = "detail")
if ((action = #"add-object")
& (any?(rcurry(instance?, <web-error>), errors)))
respond-to-get(#"add", request, response, errors: errors);
return();
end;
end;
referer := if (referer)
as(<symbol>, referer);
else
#"edit";
end;
respond-to-get(referer, request, response, errors: if (errors.size > 0) errors else #f end);
end;
end;
define method add-object (parent-object :: <object>, request :: <request>)
//look what type of object needs to be generated
let object-type = get-object(get-query-value("object-type"));
//XXX: hmm, make should probably only be done when all slots
//are successfully parsed and then use init-keywords...
//let object = make(object-type);
if (object-type = <string>)
let value = get-query-value("string");
parent-object := add!(parent-object, value);
else
let init = make(<stretchy-vector>);
//more complex objects:
//data-slots ref-slots needs to be read and sanity checked
let deferred-slot-setter = make(<stretchy-vector>);
for (slot in data-slots(object-type))
let value = parse(slot.slot-name, slot.slot-type);
//then set slots of object
if ((slot.slot-type = <boolean>) | value)
add!(init, as(<symbol>, slot.slot-name));
add!(init, value);
else
add!(deferred-slot-setter, slot);
end;
//slot.slot-setter-method(value, object);
end;
for (slot in reference-slots(object-type))
let value = get-object(get-query-value(slot.slot-name));
add!(init, as(<symbol>, slot.slot-name));
add!(init, value);
//slot.slot-setter-method(value, object);
end;
let object = apply(make, object-type, as(<vector>, init));
for (slot in deferred-slot-setter)
let value = slot.default-function(object);
unless (value)
signal(make(<web-error>,
error: concatenate("Please specify ",
slot.slot-name,
" correctly!")));
end unless;
slot.slot-setter-method(value, object);
end;
unless(object.object-class = object-type)
for (slot in data-slots(object.object-class))
unless(member?(as(<symbol>, slot.slot-name), init))
let value = slot.default | slot.default-function(object);
unless (slot.slot-type = <boolean> | value)
signal(make(<web-error>,
error: concatenate("Please specify ",
slot.slot-name,
" correctly!")));
end unless;
slot.slot-setter-method(value, object);
end;
end;
end;
let command = make(<add-command>,
arguments: list(object, parent-object));
redo(command);
let change = make(<change>,
command: command);
save(change);
signal(make(<web-success>,
warning: concatenate("Added ",
get-url-from-type(object-type),
": ", show(object))));
end;
end;
define method remove-object (parent-object :: <object>, request :: <request>)
//read object value, get it from $obj-table
let object = get-object(get-query-value("remove-this"));
//sanity type-check
let command = make(<remove-command>,
arguments: list(object, parent-object));
let change = make(<change>,
command: command);
save(change);
redo(command);
signal(make(<web-success>,
warning: concatenate("Removed ",
get-url-from-type(object.object-class),
": ", show(object))));
end;
define method parse (name, type)
let value = get-query-value(name);
if (type = <boolean>)
if (value = name)
#t;
else
#f;
end;
else
if (value & (value ~= ""))
if (type = <string>)
value;
elseif (type = <integer>)
string-to-integer(value)
else
as(type, value);
end;
else
#f;
end;
end;
end;
define method save-object (object :: <object>, request :: <request>)
//data-slots and ref-slots may have changed...
let slots = #();
for (slot in data-slots(object.object-class))
//maybe use another generic function, not as in parse?
let value = parse(slot.slot-name, slot.slot-type);
//can't check for if(value) here, because value can be #f and valid...
if (value ~= slot.slot-getter-method(object))
slots := add!(slots,
make(<triple>,
slot-name: slot.slot-name,
new-value: value,
old-value: slot.slot-getter-method(object)));
end;
end;
//now something completely different
for (slot in reference-slots(object.object-class))
//get new value via reference
let value = get-object(get-query-value(slot.slot-name));
//error check it!
let current-object = slot.slot-getter-method(object);
if (value & (value ~= current-object))
slots := add!(slots,
make(<triple>,
slot-name: slot.slot-name,
new-value: value,
old-value: slot.slot-getter-method(object)));
end;
end;
if (slots.size > 0)
let command = make(<edit-command>,
arguments: list(object, slots));
redo(command);
//check world, if broken, do a rollback!
let change = make(<change>,
command: command);
save(change);
let slot-names = apply(concatenate,
map(method(x)
concatenate(x.slot-name, " to ",
show(x.new-value), " ")
end, slots));
signal(make(<web-success>,
warning: concatenate("Saved \"",
get-url-from-type(object.object-class),
"\", ",
show(object),
" changed slots: ",
slot-names)));
end;
end;