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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
|
{-# OPTIONS_CYMAKE -Wno-incomplete-patterns #-}
module WUIjs(
Rendering,WuiSpec,
withRendering,withError,withCondition,withConditionJS,
adaptWSpec,transformWSpec,
wHidden,wConstant,wInt,
wString,wStringSize,wRequiredString,wRequiredStringSize,wTextArea,
wSelect,wSelectInt,wSelectBool,wRadioSelect,wRadioBool,wCheckBool,
wMultiCheckSelect,
wPair,wTriple,w4Tuple,w5Tuple,w6Tuple,w7Tuple,w8Tuple,
w9Tuple,w10Tuple,w11Tuple,w12Tuple,
wCons2,wCons3,wCons4,wCons5,wCons6,wCons7,
wCons8,wCons9,wCons10,wCons11,wCons12,wJoinTuple,
wMaybe,wCheckMaybe,wRadioMaybe,
wList,wListWithHeadings,wHList,wMatrix,wEither,
WTree(..),wTree,
WuiHandler,wuiHandler2button,
renderTuple,renderTaggedTuple,renderList,
mainWUI,wui2html,wuiInForm,wuiWithErrorForm,wuiHandler2button,
wCons2JS,wCons3JS,wCons4JS,wCons5JS,wCons6JS,wCons7JS,
wCons8JS,wCons9JS,wCons10JS,wCons11JS,wCons12JS,
withConditionJSName
)
where
import Char(isDigit,isSpace)
import FunctionInversion (invf1)
import HTML.Base
import List(elemIndex,intersperse)
import Maybe
import Read(readNat)
import ReadShowTerm
import JavaScript.Types
import JavaScript.Show
infixl 0 `withRendering`
infixl 0 `withError`
infixl 0 `withCondition`
infixl 0 `withConditionJS`
data WuiState =
Ref CgiRef (Maybe JSExp)
| Hidden String (Maybe JSExp)
| CompNode [WuiState] (Maybe JSExp)
| AltNode (Int,WuiState) (Maybe JSExp)
cgiRef2state :: CgiRef -> Maybe JSExp -> WuiState
cgiRef2state cr js = Ref cr js
state2cgiRef :: WuiState -> CgiRef
state2cgiRef (Ref cr _) = cr
value2state :: _ -> WuiState
value2state v = Hidden (showQTerm v) Nothing
state2value :: WuiState -> _
state2value (Hidden s _) = readQTerm s
states2state :: [WuiState] -> Maybe ([JSExp]->JSExp) -> WuiState
states2state sts Nothing = CompNode sts Nothing
states2state sts (Just jscomb) =
CompNode sts
(if Nothing `elem` jsOfElems
then Nothing
else Just (jscomb (map fromJust jsOfElems)))
where
jsOfElems = map jsAccessToState sts
state2states :: WuiState -> [WuiState]
state2states (CompNode sts _) = sts
altstate2state :: (Int,WuiState) -> WuiState
altstate2state alt@(_,st) = AltNode alt (jsAccessToState st)
state2altstate :: WuiState -> (Int,WuiState)
state2altstate (AltNode alt _) = alt
jsAccessToState :: WuiState -> Maybe JSExp
jsAccessToState (Ref _ js) = js
jsAccessToState (Hidden _ js) = js
jsAccessToState (CompNode _ js) = js
jsAccessToState (AltNode _ js) = js
setNoJSAccessInWuiState :: WuiState -> WuiState
setNoJSAccessInWuiState (Ref cref _) = Ref cref Nothing
setNoJSAccessInWuiState (Hidden s _) = Hidden s Nothing
setNoJSAccessInWuiState (CompNode sts _) = CompNode sts Nothing
setNoJSAccessInWuiState (AltNode alts _) = AltNode alts Nothing
state2refname :: WuiState -> String
state2refname (Ref cref _) = idOfCgiRef cref
state2refname (Hidden _ _) = ""
state2refname (CompNode ws _) =
"comp" ++ concatMap (("_"++) . state2refname) ws
state2refname (AltNode (i,s) _) =
"alt_" ++ show i ++ "_" ++ state2refname s
type Rendering = [HtmlExp] -> HtmlExp
type WuiParams a = (Rendering, String, a->Bool, Maybe String)
renderOf :: WuiParams a -> Rendering
renderOf (render,_,_,_) = render
errorOf :: WuiParams a -> String
errorOf (_,err,_,_) = err
conditionOf :: WuiParams a -> (a -> Bool)
conditionOf (_,_,c,_) = c
jsConditionOf :: WuiParams a -> Maybe String
jsConditionOf (_,_,_,jsc) = jsc
type HtmlState = (HtmlExp, Maybe JSExp, WuiState)
data WuiHandler = WHandler HtmlHandler (Maybe JSExp)
wuiHandler2button :: String -> WuiHandler -> HtmlExp
wuiHandler2button title (WHandler handler mbjs) =
let bt = button title handler in
maybe bt
(\jse->bt `addAttr` ("onclick","AllowSubmission = " ++ showJSExp jse))
mbjs
data WuiSpec a =
WuiSpec (WuiParams a)
(WuiParams a -> a -> HtmlState)
(WuiParams a -> CgiEnv -> WuiState -> (Maybe a,HtmlState))
withRendering :: WuiSpec a -> Rendering -> WuiSpec a
withRendering (WuiSpec (_,errmsg,legal,jsck) showhtml readvalue) render =
WuiSpec (render,errmsg,legal,jsck) showhtml readvalue
withError :: WuiSpec a -> String -> WuiSpec a
withError (WuiSpec (render,_,legal,jsck) showhtml readvalue) errmsg =
WuiSpec (render,errmsg,legal,jsck) showhtml readvalue
withCondition :: WuiSpec a -> (a -> Bool) -> WuiSpec a
withCondition (WuiSpec (render,errmsg,_,_) showhtml readvalue) legal =
WuiSpec (render,errmsg,legal,Nothing) showhtml readvalue
withConditionJS :: WuiSpec a -> (a->Bool) -> WuiSpec a
withConditionJS (WuiSpec (render,errmsg,_,_) showhtml readvalue) legal =
WuiSpec (render,errmsg,legal,Nothing) showhtml readvalue
withConditionJSName :: WuiSpec a -> (a->Bool,String) -> WuiSpec a
withConditionJSName (WuiSpec (render,errmsg,_,_) showhtml readvalue)
(legal,jsck) =
WuiSpec (render,errmsg,legal,if null jsck then Nothing else Just jsck)
showhtml readvalue
transformWSpec :: (a->b,b->a) -> WuiSpec a -> WuiSpec b
transformWSpec (a2b,b2a) (WuiSpec wparamsa showhtmla readvaluea) =
WuiSpec (transParamA2B wparamsa)
(\wparamsb b -> setNoJSAccessInHtmlState
(showhtmla (transParamB2A wparamsb) (b2a b)))
(\wparamsb env wst ->
let (mba,errstate) = readvaluea (transParamB2A wparamsb) env wst
in (maybe Nothing (Just . a2b) mba,
setNoJSAccessInHtmlState errstate))
where
transParamA2B :: WuiParams a -> WuiParams b
transParamA2B (render,errmsg,legal,_) =
(render, errmsg, legal . b2a, Nothing)
transParamB2A :: WuiParams b -> WuiParams a
transParamB2A (render,errmsg,legal,_) =
(render, errmsg, legal . a2b, jsConditionOf wparamsa)
setNoJSAccessInHtmlState (hexp,jsexp,ws) =
(hexp, jsexp, setNoJSAccessInWuiState ws)
adaptWSpec :: (a->b) -> WuiSpec a -> WuiSpec b
adaptWSpec a2b = transformWSpec (a2b, invf1 a2b)
wHidden :: WuiSpec a
wHidden =
WuiSpec (head,"?",const True,Nothing)
(\_ v -> (hempty, Nothing, value2state v))
(\_ _ s -> (Just (state2value s), (hempty,Nothing,s)))
wConstant :: (a->HtmlExp) -> WuiSpec a
wConstant showhtml =
WuiSpec (head,"?",const True,Nothing)
(\wparams v ->
((renderOf wparams) [showhtml v], Nothing, value2state v))
(\wparams _ s -> let v = state2value s in
(Just v, ((renderOf wparams) [showhtml v], Nothing, s)))
wInt :: WuiSpec Int
wInt = WuiSpec
(head,"Illegal integer:",const True,Nothing)
(\(render,errmsg,_,jsck) v -> intWidget errmsg jsck render (showInt v))
(\(render,errmsg,legal,jsck) env s ->
let input = env (state2cgiRef s)
renderr hexps = addErrMsg False False False errmsg "" (render hexps)
in maybe (Nothing, intWidget errmsg jsck renderr input)
(\v -> if legal v
then (Just v, intWidget errmsg jsck render input)
else (Nothing, intWidget errmsg jsck renderr input))
(readMaybeInt (stripSpaces input)))
where
showInt i = if i<0 then '-' : show (-i) else show i
intWidget errmsg mbjs render s =
(addErrMsg True True False errmsg refname
(render [textfield ref s `addAttr` ("size","6")
`addAttr` ("onblur",showJSExp jsCheckCall)]),
Just jsCheckCall,
cgiRef2state ref (Just jsaccess))
where
ref free
refname = idOfCgiRef ref
jsaccess = JSFCall "intValueOf" [JSString refname]
parseIntCheckCall = JSFCall "parseIntCheck" [JSString refname]
intCheckCall = maybe parseIntCheckCall
(\jsf->JSOp "&&" parseIntCheckCall
(JSFCall jsf [jsaccess]))
mbjs
jsCheckCall = JSFCall "setErrorClassName" [JSString refname,intCheckCall]
stripSpaces :: String -> String
stripSpaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
readMaybeInt :: String -> Maybe Int
readMaybeInt "" = Nothing
readMaybeInt (v:s) | v=='-' = maybe Nothing (\i->Just (-i)) (acc 0 s)
| isDigit v = acc 0 (v:s)
| otherwise = Nothing
where
acc n "" = Just n
acc n (c:cs) | isDigit c = acc (10*n + ord c - ord '0') cs
| otherwise = Nothing
checkLegalInput :: WuiParams a
-> (String -> Maybe String -> Rendering -> a -> HtmlState)
-> Bool
-> a
-> (Maybe a,HtmlState)
checkLegalInput (render,errmsg,legal,jsck) value2widget errorastable value =
if legal value
then (Just value, value2widget errmsg jsck render value)
else (Nothing,
value2widget errmsg jsck
(\hes -> addErrMsg False False errorastable errmsg ""
(render hes))
value)
filterStringInput :: String -> String
filterStringInput = removeCRs
removeCRs :: String -> String
removeCRs [] = []
removeCRs [c] = [c]
removeCRs (c1:c2:cs) =
if c1=='\r' && c2=='\n' then '\n' : removeCRs cs
else c1 : removeCRs (c2:cs)
wString :: WuiSpec String
wString = wStringAttrs []
wStringSize :: Int -> WuiSpec String
wStringSize size = wStringAttrs [("size",show size)]
wStringAttrs :: [(String,String)] -> WuiSpec String
wStringAttrs attrs =
WuiSpec (head, "?", const True, Nothing)
(\(render,errmsg,_,jsck) v -> stringWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams stringWidget False
(filterStringInput (env (state2cgiRef s))))
where
stringWidget errmsg mbjs render v =
(addErrMsg (isJust jsCheckCall) True False errmsg refname
(render [foldr (flip addAttr) (textfield ref v) (attrs++onblurAttr)]),
jsCheckCall,
cgiRef2state ref (Just jsaccess))
where
ref free
refname = idOfCgiRef ref
jsaccess = JSFCall "stringValueOf" [JSString refname]
jsCheckCall = maybeJSFun2checkCall refname jsaccess mbjs
onblurAttr = maybe [] (\jsc->[("onblur",showJSExp jsc)]) jsCheckCall
wRequiredString :: WuiSpec String
wRequiredString =
withConditionJSName (wString `withError` "Missing input:")
(not . null, "notEmpty")
wRequiredStringSize :: Int -> WuiSpec String
wRequiredStringSize size =
withConditionJSName (wStringSize size `withError` "Missing input:")
(not . null, "notEmpty")
wTextArea :: (Int,Int) -> WuiSpec String
wTextArea dims =
WuiSpec (head, "?", const True, Nothing)
(\ (render,errmsg,_,jsck) v -> textareaWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams textareaWidget False
(filterStringInput (env (state2cgiRef s))))
where
textareaWidget errmsg mbjs render v =
(addErrMsg (isJust jsCheckCall) True False errmsg refname
(render [textarea ref dims v `addAttrs` onblurAttr]),
jsCheckCall,
cgiRef2state ref (Just jsaccess))
where
ref free
refname = idOfCgiRef ref
jsaccess = JSFCall "stringValueOf" [JSString refname]
jsCheckCall = maybeJSFun2checkCall refname jsaccess mbjs
onblurAttr = maybe [] (\jsc->[("onblur",showJSExp jsc)]) jsCheckCall
wSelect :: (a->String) -> [a] -> WuiSpec a
wSelect showelem selset =
WuiSpec (head,"?",const True, Nothing)
(\ (render,errmsg,_,jsck) v -> selWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams selWidget False
(selset !! readNat (env (state2cgiRef s))))
where
selWidget _ _ render v =
(render [maybe (selection ref namevalues)
(\i -> selectionInitial ref namevalues i)
idx],
Nothing,
cgiRef2state ref Nothing)
where
ref free
idx = elemIndex v selset
namevalues = zip (map showelem selset) (map show [0..])
wSelectJS :: (a->String) -> (a->JSExp) -> [a] -> WuiSpec a
wSelectJS showelem showjselem selset =
WuiSpec (head,"?",const True, Nothing)
(\ (render,errmsg,_,jsck) v -> selWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams selWidget False
(selset !! readNat (env (state2cgiRef s))))
where
selWidget errmsg mbjs render v =
(addErrMsg (isJust jsCheckCall) True False errmsg refname
(render [maybe (selection ref namevalues)
(\i -> selectionInitial ref namevalues i)
idx]),
jsCheckCall,
cgiRef2state ref (Just jsaccess))
where
ref free
refname = idOfCgiRef ref
jsaccess = JSFCall "selectValueOf"
[JSString refname,
JSFCall "new Array" (map showjselem selset)]
jsCheckCall = maybeJSFun2checkCall refname jsaccess mbjs
idx = elemIndex v selset
namevalues = zip (map showelem selset) (map show [0..])
wSelectInt :: [Int] -> WuiSpec Int
wSelectInt = wSelectJS show JSInt
wSelectBool :: String -> String -> WuiSpec Bool
wSelectBool true false =
wSelectJS (\b->if b then true else false) JSBool [True,False]
wCheckBool :: [HtmlExp] -> WuiSpec Bool
wCheckBool hexps =
WuiSpec (head, "?", const True, Nothing)
(\ (render,errmsg,_,jsck) v -> checkWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams checkWidget False
(env (state2cgiRef s)=="True"))
where
checkWidget errmsg mbjs render v =
(addErrMsg (isJust jsCheckCall) True False errmsg refname
(render [inline ((if v then checkedbox else checkbox) ref "True"
: hexps)]),
jsCheckCall,
cgiRef2state ref (Just jsaccess))
where
ref free
refname = idOfCgiRef ref
jsaccess = JSFCall "checkBoxValueOf" [JSString refname]
jsCheckCall = maybeJSFun2checkCall refname jsaccess mbjs
wMultiCheckSelect :: (a->[HtmlExp]) -> [a] -> WuiSpec [a]
wMultiCheckSelect showelem selset =
WuiSpec (renderTuple, tupleError, const True, Nothing)
(\ (render,errmsg,_,jsck) vs -> checkWidget errmsg jsck render vs)
(\wparams env st ->
checkLegalInput wparams checkWidget True
(concatMap (\ (ref,s) -> if env ref=="True" then [s] else [])
(zip (map state2cgiRef (state2states st)) selset)))
where
checkWidget _ _ render vs =
let refs = take (length selset) newVars
numsetitems = zip refs selset
showItem (ref,s) =
inline ((if s `elem` vs then checkedbox else checkbox)
ref "True" : showelem s)
in (render (map showItem numsetitems), Nothing,
states2state (map (\cref->cgiRef2state cref Nothing) refs) Nothing)
newVars :: [_]
newVars = unknown : newVars
wRadioSelect :: (a->[HtmlExp]) -> [a] -> WuiSpec a
wRadioSelect showelem selset =
WuiSpec (renderTuple, tupleError, const True, Nothing)
(\ (render,errmsg,_,jsck) v -> radioWidget errmsg jsck render v)
(\wparams env s ->
checkLegalInput wparams radioWidget True
(selset !! readNat (env (state2cgiRef s))))
where
radioWidget _ _ render v =
(render (map showItem numhitems),
Nothing,
cgiRef2state ref Nothing)
where
ref free
idx = maybe 0 id (elemIndex v selset)
numhitems = zip [0..] (map showelem selset)
showItem (i,s) = table [[[(if i==idx then radio_main else radio_other)
ref (show i)],s]]
wRadioBool :: [HtmlExp] -> [HtmlExp] -> WuiSpec Bool
wRadioBool truehexps falsehexps =
wRadioSelect (\b->if b then truehexps else falsehexps) [True,False]
maybeJSFun2checkCall _ _ Nothing = Nothing
maybeJSFun2checkCall refname jsaccessvalue (Just jsf) =
Just (JSFCall "setErrorClassName" [JSString refname,
JSFCall jsf [jsaccessvalue]])
addErrMsg :: Bool -> Bool -> Bool -> String -> String -> HtmlExp -> HtmlExp
addErrMsg hasjsck hide astable errmsg refname hexp =
if not hide || hasjsck
then
if astable
then table [[[style errmsgclass [htxt errmsg] `addAttrs` msgidattr]],[[hexp]]]
`addAttrs` ([("class",tableclass)] ++ tblidattr)
else inline [style errmsgclass [htxt errmsg] `addAttrs` msgidattr, hexp]
else hexp
where
msgidattr = if null refname then [] else [("id","MSG_"++refname)]
tblidattr = if null refname then [] else [("id",refname)]
tableclass = if hide then "wuipassiveerrmsg" else "wuiactiveerrmsg"
errmsgclass = if hide then "wuihide" else "wuinohide"
jsTupleCons n = jsConsTerm ('(' : replicate (n - 1) ',' ++ ")")
joinSubFields :: WuiParams _ -> Bool -> [(HtmlExp,Maybe JSExp,WuiState)]
-> Maybe ([JSExp]->JSExp) -> (HtmlExp,Maybe JSExp,WuiState)
joinSubFields (render,errmsg,_,mbjsck) hide subfields jscomb =
let (subhexps,subjscks,substates) = unzip3 subfields
consstate = states2state substates jscomb
refname = state2refname consstate
jscall = jsCheckCallFromState consstate mbjsck
jscks = catMaybes subjscks
in (addErrMsg (isJust jscall) hide True errmsg refname (render subhexps),
if null jscks
then jscall
else Just
(maybe (parAndJS jscks)
(\jsc->JSOp "&&"
(JSOp "&&" (JSFCall "unsetErrorClassName"
[JSString refname])
(parAndJS jscks))
jsc)
jscall),
consstate)
where
parAndJS [j] = j
parAndJS (j1:j2:js) = JSFCall "And" [j1,parAndJS (j2:js)]
checkAndJoinSubFields wparams subfields jscons errorinsubfields joinvalue =
let combine hide = joinSubFields wparams hide subfields jscons
in if errorinsubfields
then (Nothing, combine True)
else if (conditionOf wparams) joinvalue
then (Just joinvalue, combine True)
else (Nothing, combine False)
altSubField :: WuiParams _ -> Bool -> HtmlExp -> Maybe JSExp -> (Int,WuiState)
-> (HtmlExp,Maybe JSExp,WuiState)
altSubField (render,errmsg,_,mbjsck) hide subhexp subjsck altstate =
let consstate = altstate2state altstate
refname = state2refname consstate
jscons = jsCheckCallFromState consstate mbjsck
in (addErrMsg (isJust jscons) hide False errmsg refname (render [subhexp]),
maybe jscons
(\subjs -> Just
(maybe subjs
(\jsc->JSOp "&&"
(JSOp "&&" (JSFCall "unsetErrorClassName"
[JSString refname])
subjs)
jsc)
jscons))
subjsck,
consstate)
jsCheckCallFromState _ Nothing = Nothing
jsCheckCallFromState wstate (Just jsck) =
maybe Nothing
(\jsstateaccess -> Just (JSFCall "setErrorClassName"
[JSString (state2refname wstate),
JSFCall jsck [jsstateaccess]]))
(jsAccessToState wstate)
wJoinTuple :: WuiSpec a -> WuiSpec b -> WuiSpec (a,b)
wJoinTuple (WuiSpec wparama showa reada) (WuiSpec wparamb showb readb) =
WuiSpec (renderTuple, tupleError, const True, Nothing) showc readc
where
jscons = Just (jsTupleCons 2)
render2joinrender render [h1,h2] =
let h1s = unRenderTuple h1
h2s = unRenderTuple h2
in render (h1s++h2s)
addJoinRender (render,errmsg,legal,jsck) =
(render2joinrender render,errmsg,legal,jsck)
showc wparams vc | (va,vb) =:<= vc =
joinSubFields (addJoinRender wparams) True
[showa wparama va, showb wparamb vb] jscons
where va,vb free
readc wparams env s =
let [ra,rb] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
in checkAndJoinSubFields (addJoinRender wparams) [fielda,fieldb] jscons
(rav==Nothing || rbv==Nothing)
(fromJust rav, fromJust rbv)
wPair :: WuiSpec a -> WuiSpec b -> WuiSpec (a,b)
wPair = wCons2JS (Just (jsTupleCons 2)) (\a b -> (a,b))
wCons2 :: (a->b->c) -> WuiSpec a -> WuiSpec b -> WuiSpec c
wCons2 = wCons2JS Nothing
wCons2JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb) =
WuiSpec (renderTuple, tupleError, const True, Nothing) showc readc
where
showc wparams vc | cons va vb =:<= vc =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb] jscons
where va,vb free
readc wparams env s =
let [ra,rb] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
in checkAndJoinSubFields wparams [fielda,fieldb] jscons
(rav==Nothing || rbv==Nothing)
(cons (fromJust rav) (fromJust rbv))
wTriple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec (a,b,c)
wTriple = wCons3JS (Just (jsTupleCons 3)) (\a b c -> (a,b,c))
wCons3 :: (a->b->c->d) -> WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d
wCons3 = wCons3JS Nothing
wCons3JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) =
WuiSpec (renderTuple, tupleError, const True, Nothing) showd readd
where
showd wparams vd | cons va vb vc =:<= vd =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc] jscons
where va,vb,vc free
readd wparams env s =
let [ra,rb,rc] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv))
w4Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec (a,b,c,d)
w4Tuple = wCons4JS (Just (jsTupleCons 4)) (\a b c d -> (a,b,c,d))
wCons4 :: (a->b->c->d->e) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e
wCons4 = wCons4JS Nothing
wCons4JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showe reade
where
showe wparams ve | cons va vb vc vd =:<= ve =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd] jscons
where va,vb,vc,vd free
reade wparams env s =
let [ra,rb,rc,rd] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
in checkAndJoinSubFields wparams
[fielda,fieldb,fieldc,fieldd] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv))
w5Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec (a,b,c,d,e)
w5Tuple = wCons5JS (Just (jsTupleCons 5)) (\a b c d e -> (a,b,c,d,e))
wCons5 :: (a->b->c->d->e->f) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f
wCons5 = wCons5JS Nothing
wCons5JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showf readf
where
showf wparams vl | cons va vb vc vd ve =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve] jscons
where va,vb,vc,vd,ve free
readf wparams env s =
let [ra,rb,rc,rd,re] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev))
w6Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec (a,b,c,d,e,f)
w6Tuple = wCons6JS (Just (jsTupleCons 6)) (\a b c d e f -> (a,b,c,d,e,f))
wCons6 :: (a->b->c->d->e->f->g) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g
wCons6 = wCons6JS Nothing
wCons6JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showg readg
where
showg wparams vl | cons va vb vc vd ve vf =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf] jscons
where va,vb,vc,vd,ve,vf free
readg wparams env s =
let [ra,rb,rc,rd,re,rf] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv))
w7Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec (a,b,c,d,e,f,g)
w7Tuple = wCons7JS (Just (jsTupleCons 7)) (\a b c d e f g -> (a,b,c,d,e,f,g))
wCons7 :: (a->b->c->d->e->f->g->h) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h
wCons7 = wCons7JS Nothing
wCons7JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showh readh
where
showh wparams vl | cons va vb vc vd ve vf vg =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg] jscons
where va,vb,vc,vd,ve,vf,vg free
readh wparams env s =
let [ra,rb,rc,rd,re,rf,rg] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv))
w8Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec (a,b,c,d,e,f,g,h)
w8Tuple = wCons8JS (Just (jsTupleCons 8))
(\a b c d e f g h -> (a,b,c,d,e,f,g,h))
wCons8 :: (a->b->c->d->e->f->g->h->i) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i
wCons8 = wCons8JS Nothing
wCons8JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) (WuiSpec wparamh showh readh) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showi readi
where
showi wparams vl | cons va vb vc vd ve vf vg vh =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg,
showh wparamh vh] jscons
where va,vb,vc,vd,ve,vf,vg,vh free
readi wparams env s =
let [ra,rb,rc,rd,re,rf,rg,rh] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
(rhv,fieldh) = readh wparamh env rh
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg,fieldh] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing || rhv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv) (fromJust rhv))
w9Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i ->
WuiSpec (a,b,c,d,e,f,g,h,i)
w9Tuple = wCons9JS (Just (jsTupleCons 9))
(\a b c d e f g h i -> (a,b,c,d,e,f,g,h,i))
wCons9 :: (a->b->c->d->e->f->g->h->i->j) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j
wCons9 = wCons9JS Nothing
wCons9JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) (WuiSpec wparamh showh readh)
(WuiSpec wparami showi readi) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showj readj
where
showj wparams vl | cons va vb vc vd ve vf vg vh vi =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg,
showh wparamh vh,
showi wparami vi] jscons
where va,vb,vc,vd,ve,vf,vg,vh,vi free
readj wparams env s =
let [ra,rb,rc,rd,re,rf,rg,rh,ri] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
(rhv,fieldh) = readh wparamh env rh
(riv,fieldi) = readi wparami env ri
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg,fieldh,
fieldi] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing || rhv==Nothing ||
riv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv) (fromJust rhv)
(fromJust riv))
w10Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec (a,b,c,d,e,f,g,h,i,j)
w10Tuple = wCons10JS (Just (jsTupleCons 10))
(\a b c d e f g h i j -> (a,b,c,d,e,f,g,h,i,j))
wCons10 :: (a->b->c->d->e->f->g->h->i->j->k) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec k
wCons10 = wCons10JS Nothing
wCons10JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) (WuiSpec wparamh showh readh)
(WuiSpec wparami showi readi) (WuiSpec wparamj showj readj) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showk readk
where
showk wparams vl | cons va vb vc vd ve vf vg vh vi vj =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg,
showh wparamh vh,
showi wparami vi,
showj wparamj vj] jscons
where va,vb,vc,vd,ve,vf,vg,vh,vi,vj free
readk wparams env s =
let [ra,rb,rc,rd,re,rf,rg,rh,ri,rj] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
(rhv,fieldh) = readh wparamh env rh
(riv,fieldi) = readi wparami env ri
(rjv,fieldj) = readj wparamj env rj
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg,fieldh,
fieldi,fieldj] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing || rhv==Nothing ||
riv==Nothing || rjv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv) (fromJust rhv)
(fromJust riv) (fromJust rjv))
w11Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec k -> WuiSpec (a,b,c,d,e,f,g,h,i,j,k)
w11Tuple = wCons11JS (Just (jsTupleCons 11))
(\a b c d e f g h i j k -> (a,b,c,d,e,f,g,h,i,j,k))
wCons11 :: (a->b->c->d->e->f->g->h->i->j->k->l) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec k -> WuiSpec l
wCons11 = wCons11JS Nothing
wCons11JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) (WuiSpec wparamh showh readh)
(WuiSpec wparami showi readi) (WuiSpec wparamj showj readj)
(WuiSpec wparamk showk readk) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showl readl
where
showl wparams vl | cons va vb vc vd ve vf vg vh vi vj vk =:<= vl =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg,
showh wparamh vh,
showi wparami vi,
showj wparamj vj,
showk wparamk vk] jscons
where va,vb,vc,vd,ve,vf,vg,vh,vi,vj,vk free
readl wparams env s =
let [ra,rb,rc,rd,re,rf,rg,rh,ri,rj,rk] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
(rhv,fieldh) = readh wparamh env rh
(riv,fieldi) = readi wparami env ri
(rjv,fieldj) = readj wparamj env rj
(rkv,fieldk) = readk wparamk env rk
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg,fieldh,
fieldi,fieldj,fieldk] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing || rhv==Nothing ||
riv==Nothing || rjv==Nothing || rkv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv) (fromJust rhv)
(fromJust riv) (fromJust rjv) (fromJust rkv))
w12Tuple :: WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec k -> WuiSpec l -> WuiSpec (a,b,c,d,e,f,g,h,i,j,k,l)
w12Tuple = wCons12JS (Just (jsTupleCons 12))
(\a b c d e f g h i j k l -> (a,b,c,d,e,f,g,h,i,j,k,l))
wCons12 :: (a->b->c->d->e->f->g->h->i->j->k->l->m) ->
WuiSpec a -> WuiSpec b -> WuiSpec c -> WuiSpec d -> WuiSpec e ->
WuiSpec f -> WuiSpec g -> WuiSpec h -> WuiSpec i -> WuiSpec j ->
WuiSpec k -> WuiSpec l -> WuiSpec m
wCons12 = wCons12JS Nothing
wCons12JS jscons cons
(WuiSpec wparama showa reada) (WuiSpec wparamb showb readb)
(WuiSpec wparamc showc readc) (WuiSpec wparamd showd readd)
(WuiSpec wparame showe reade) (WuiSpec wparamf showf readf)
(WuiSpec wparamg showg readg) (WuiSpec wparamh showh readh)
(WuiSpec wparami showi readi) (WuiSpec wparamj showj readj)
(WuiSpec wparamk showk readk) (WuiSpec wparaml showl readl) =
WuiSpec (renderTuple,tupleError, const True, Nothing) showm readm
where
showm wparams vm | cons va vb vc vd ve vf vg vh vi vj vk vl =:<= vm =
joinSubFields wparams True
[showa wparama va,
showb wparamb vb,
showc wparamc vc,
showd wparamd vd,
showe wparame ve,
showf wparamf vf,
showg wparamg vg,
showh wparamh vh,
showi wparami vi,
showj wparamj vj,
showk wparamk vk,
showl wparaml vl] jscons
where va,vb,vc,vd,ve,vf,vg,vh,vi,vj,vk,vl free
readm wparams env s =
let [ra,rb,rc,rd,re,rf,rg,rh,ri,rj,rk,rl] = state2states s
(rav,fielda) = reada wparama env ra
(rbv,fieldb) = readb wparamb env rb
(rcv,fieldc) = readc wparamc env rc
(rdv,fieldd) = readd wparamd env rd
(rev,fielde) = reade wparame env re
(rfv,fieldf) = readf wparamf env rf
(rgv,fieldg) = readg wparamg env rg
(rhv,fieldh) = readh wparamh env rh
(riv,fieldi) = readi wparami env ri
(rjv,fieldj) = readj wparamj env rj
(rkv,fieldk) = readk wparamk env rk
(rlv,fieldl) = readl wparaml env rl
in checkAndJoinSubFields wparams [fielda,fieldb,fieldc,fieldd,
fielde,fieldf,fieldg,fieldh,
fieldi,fieldj,fieldk,fieldl] jscons
(rav==Nothing || rbv==Nothing || rcv==Nothing || rdv==Nothing ||
rev==Nothing || rfv==Nothing || rgv==Nothing || rhv==Nothing ||
riv==Nothing || rjv==Nothing || rkv==Nothing || rlv==Nothing)
(cons (fromJust rav) (fromJust rbv) (fromJust rcv) (fromJust rdv)
(fromJust rev) (fromJust rfv) (fromJust rgv) (fromJust rhv)
(fromJust riv) (fromJust rjv) (fromJust rkv) (fromJust rlv))
wList :: WuiSpec a -> WuiSpec [a]
wList (WuiSpec rendera showa reada) = WuiSpec
(renderList,"Illegal list:",const True, Nothing)
(\wparams vas ->
joinSubFields wparams True (map (showa rendera) vas) (Just jsListComb))
(\wparams env s ->
let rvs = map (reada rendera env) (state2states s)
combine hide = joinSubFields wparams hide (map snd rvs)
(Just jsListComb)
in if Nothing `elem` (map fst rvs)
then (Nothing, combine True)
else let value = map (fromJust . fst) rvs in
if (conditionOf wparams) value
then (Just value, combine True)
else (Nothing, combine False) )
where
jsListComb args = JSFCall "array2list" [JSFCall "new Array" args]
wListWithHeadings :: [String] -> WuiSpec a -> WuiSpec [a]
wListWithHeadings headings wspec =
wList wspec `withRendering` renderHeadings
where
renderHeadings hs = addHeadings (renderList hs) (map (\s->[htxt s]) headings)
wHList :: WuiSpec a -> WuiSpec [a]
wHList wspec = wList wspec `withRendering` renderTuple
wMatrix :: WuiSpec a -> WuiSpec [[a]]
wMatrix wspec = wList (wHList wspec)
wMaybe :: WuiSpec Bool -> WuiSpec a -> a -> WuiSpec (Maybe a)
wMaybe (WuiSpec paramb showb readb) (WuiSpec parama showa reada) def =
WuiSpec
(renderTuple, tupleError, const True, Nothing)
(\wparams mbs ->
joinSubFields wparams True
[showb paramb (mbs/=Nothing),
showa parama (maybe def id mbs)] Nothing)
(\wparams env s ->
let [rb,ra] = state2states s
(rbv,fieldb) = readb paramb env rb
(rav,fielda) = reada parama env ra
combine hide = joinSubFields wparams hide [fieldb,fielda] Nothing
in if rbv==Nothing || rav==Nothing
then (Nothing, combine True)
else let value = if fromJust rbv
then Just (fromJust rav)
else Nothing in
if (conditionOf wparams) value
then (Just value, combine True)
else (Nothing, combine False))
wCheckMaybe :: WuiSpec a -> [HtmlExp] -> a -> WuiSpec (Maybe a)
wCheckMaybe wspec exps = wMaybe (wCheckBool exps) wspec
wRadioMaybe :: WuiSpec a -> [HtmlExp] -> [HtmlExp] -> a -> WuiSpec (Maybe a)
wRadioMaybe wspec hnothing hjust = wMaybe wBool wspec
where
wBool = wRadioSelect (\b->if b then hjust else hnothing) [False,True]
wEither :: WuiSpec a -> WuiSpec b -> WuiSpec (Either a b)
wEither (WuiSpec rendera showa reada) (WuiSpec renderb showb readb) =
WuiSpec (head, "?", const True, Nothing) showEither readEither
where
showEither wparams (Left va) =
let (hea,jsa,rta) = showa rendera va
in altSubField wparams True hea jsa (1,rta)
showEither wparams (Right vb) =
let (heb,jsb,rtb) = showb renderb vb
in altSubField wparams True heb jsb (2,rtb)
readEither wparams env s =
let (altindex,rab) = state2altstate s
in case altindex of
1 -> let (rv,(he,jsck,rst)) = reada rendera env rab
in checkValue (rv==Nothing) (Left (fromJust rv)) he jsck (1,rst)
2 -> let (rv,(he,jsck,rst)) = readb renderb env rab
in checkValue (rv==Nothing) (Right (fromJust rv)) he jsck (2,rst)
where
checkValue isnothing value hexp jsck altstate =
let combine hide = altSubField wparams hide hexp jsck altstate
in if isnothing
then (Nothing, combine True)
else if (conditionOf wparams) value
then (Just value, combine True)
else (Nothing, combine False)
data WTree a = WLeaf a | WNode [WTree a]
wTree :: WuiSpec a -> WuiSpec (WTree a)
wTree (WuiSpec rendera showa reada) =
WuiSpec (renderList, "Illegal tree:", const True, Nothing) showTree readTree
where
showTree _ (WLeaf va) =
let (hea,jsa,rta) = showa rendera va
in altSubField rendera True hea jsa (1,rta)
showTree wparams (WNode ns) =
let (hes,jscks,sts) = joinSubFields wparams True
(map (showTree wparams) ns) Nothing
in (hes,jscks,altstate2state (2,sts))
readTree wpar env s = let (altindex,rab) = state2altstate s in
case altindex of
1 -> let (rv,(he,jsck,rst)) = reada rendera env rab
combine hide = altSubField rendera hide he jsck (1,rst)
in checkValue combine (rv==Nothing) (WLeaf (fromJust rv))
2 -> let rvs = map (readTree wpar env) (state2states rab)
combine hide =
let (hes,jscks,sts) = joinSubFields wpar hide
(map snd rvs) Nothing
in (hes,jscks,altstate2state (2,sts))
in checkValue combine (Nothing `elem` (map fst rvs))
(WNode (map (fromJust . fst) rvs))
where
checkValue combine isnothing value =
if isnothing
then (Nothing, combine True)
else if conditionOf wpar value
then (Just value, combine True)
else (Nothing, combine False)
renderTuple :: Rendering
renderTuple hexps = table [map (\h->[h]) hexps]
unRenderTuple :: HtmlExp -> [HtmlExp]
unRenderTuple hexp =
if isTupleTable hexp
then getTupleTableElems hexp
else [hexp]
where
isTupleTable he = case he of
HtmlStruct "table" [] [HtmlStruct "tr" [] tds] -> all isSingleElem tds
_ -> False
isSingleElem he = case he of
HtmlStruct "td" _ [_] -> True
_ -> False
getTupleTableElems (HtmlStruct "table" [] [HtmlStruct "tr" [] tds]) =
map (\ (HtmlStruct "td" _ [e]) -> e) tds
tupleError :: String
tupleError = "Illegal combination:"
renderTaggedTuple :: [String] -> Rendering
renderTaggedTuple tags hexps =
table (map (\(t,h)->[[bold [htxt t]],[h]]) (zip tags hexps))
renderList :: Rendering
renderList hexps = mergeTableOfTable (table (map (\h->[[h]]) hexps))
`addAttr` ("border","1")
mergeTableOfTable :: HtmlExp -> HtmlExp
mergeTableOfTable (HtmlStruct "table" attrs rows) =
HtmlStruct "table" attrs
(if all isRowWithSingleTableData rows
then map mergeRowWithSingleTableData rows
else rows )
isRowWithSingleTableData :: HtmlExp -> Bool
isRowWithSingleTableData row = case row of
(HtmlStruct "tr" []
[HtmlStruct "td" []
[HtmlStruct "table" _ [HtmlStruct "tr" _ _]]]) -> True
_ -> False
mergeRowWithSingleTableData :: HtmlExp -> HtmlExp
mergeRowWithSingleTableData
(HtmlStruct "tr" [] [HtmlStruct "td" [] [HtmlStruct "table" _ [row]]]) = row
mainWUI :: WuiSpec a -> a -> (a -> IO HtmlForm) -> IO HtmlForm
mainWUI wuispec val store = do
let (hexp,whandler) = wui2html wuispec val store
return $ form "WUI" [hexp, breakline, wuiHandler2button "Submit" whandler]
wui2html :: WuiSpec a -> a -> (a -> IO HtmlForm) -> (HtmlExp,WuiHandler)
wui2html wspec val store = wuiWithErrorForm wspec val store standardErrorForm
standardErrorForm :: HtmlExp -> WuiHandler -> IO HtmlForm
standardErrorForm hexp whandler =
return $ standardForm "Input error"
[hexp, wuiHandler2button "Submit" whandler]
wuiInForm :: WuiSpec a -> a -> (a -> IO HtmlForm)
-> (HtmlExp -> WuiHandler -> IO HtmlForm) -> IO HtmlForm
wuiInForm wspec val store userform =
answerForm (wuiWithErrorForm wspec val store userform)
where
answerForm (hexp,whandler) = userform hexp whandler
wuiWithErrorForm :: WuiSpec a -> a -> (a -> IO HtmlForm)
-> (HtmlExp -> WuiHandler -> IO HtmlForm)
-> (HtmlExp,WuiHandler)
wuiWithErrorForm wspec val store errorform =
showAndReadWUI wspec store errorform (generateWUI wspec val)
generateWUI :: WuiSpec a -> a
-> (HtmlExp,Maybe JSExp, CgiEnv -> (Maybe a,HtmlState))
generateWUI (WuiSpec wparams showhtml readval) val =
hst2result (showhtml wparams val)
where
hst2result (htmledits,jsfs,wstate) =
(htmledits, jsfs, \env -> readval wparams env wstate)
showAndReadWUI :: WuiSpec a -> (a -> IO HtmlForm)
-> (HtmlExp -> WuiHandler -> IO HtmlForm)
-> (HtmlExp,Maybe JSExp,CgiEnv->(Maybe a,HtmlState))
-> (HtmlExp,WuiHandler)
showAndReadWUI wspec store errorform (htmledits,jsfs,readenv) =
(inline [wuiStyleSheet, htmledits], WHandler (htmlhandler wspec) jsfs)
where
htmlhandler wui@(WuiSpec wparams _ readval) env =
let (mbnewval, (htmlerrform,htmlerrjsfs,errwstate)) = readenv env
in maybe (let (errhexp,errwhdl) =
showAndReadWUI wui
store
errorform
(htmlerrform,htmlerrjsfs,
\errenv -> readval wparams errenv errwstate)
in errorform errhexp errwhdl)
(\newval -> seq (normalForm newval)
(store newval))
mbnewval
wuiStyleSheet :: HtmlExp
wuiStyleSheet = styleSheet $
"\n.wuihide { display: none; }\n" ++
".wuinohide { display: inline; color: red; font-weight: bold ; background-color: yellow; }\n" ++
".wuipassiveerrmsg { padding: 0px; border-spacing: 0px; }\n"++
".wuiactiveerrmsg { padding: 0px; border-spacing: 0px; background-color: yellow; border-width: 2px; border-style: solid; border-color: red; }\n"
|