.
[clean-tests.git] / exeditor / test.icl
1 module test
2
3 import StdEnv
4 import Data.Maybe
5 import Data.Functor
6 import qualified Data.Map as DM
7 import iTasks
8 import iTasks.Internal.Serialization
9
10 :: Box = E.t: Box t & iTask t
11 unBox (Box b) :== b
12
13 gEq{|Box|} (Box l) (Box r) = dynamicJSONEncode l == dynamicJSONEncode r
14 JSONEncode{|Box|} _ c = [dynamicJSONEncode c]
15 JSONDecode{|Box|} _ [c:r] = (dynamicJSONDecode c, r)
16 JSONDecode{|Box|} _ r = (Nothing, r)
17 gText{|Box|} tv ma = maybe [] (gText{|*|} tv o Just) ma
18 gEditor{|Box|} = bijectEditorValue fromBox toBox gEditor{|*|}
19
20 fromBox :: Box -> Type
21 fromBox (Box t) = fromBoxd (dynamic t)
22
23 fromBoxd :: Dynamic -> Type
24 fromBoxd (a :: ()) = Unit
25 fromBoxd (a :: Int) = Int a
26 fromBoxd (a :: Bool) = Bool a
27 fromBoxd (a :: Real) = Real a
28 fromBoxd (a :: String) = String a
29 fromBoxd ((a, b) :: (a, b)) = Tuple (fromBoxd (dynamic a)) (fromBoxd (dynamic b))
30 fromBoxd ((a, b, c) :: (a, b, c)) = Tuple3 (fromBoxd (dynamic a)) (fromBoxd (dynamic b)) (fromBoxd (dynamic b))
31 fromBoxd (a :: Person) = Person a
32
33 toBox :: Type -> Box
34 toBox (Int a) = Box a
35 toBox (Bool a) = Box a
36 toBox (Real a) = Box a
37 toBox (String a) = Box a
38 toBox Unit = Box ()
39 toBox (Tuple l r) = case (toBox l, toBox r) of
40 (Box l, Box r) = Box (l, r)
41 toBox (Tuple3 l m r) = case (toBox l, toBox m, toBox r) of
42 (Box l, Box m, Box r) = Box (l, m, r)
43 toBox (Person p) = Box p
44 /*
45 gEditor{|Box|} =
46 { Editor
47 | genUI = \uia dp em vst->case em of
48 Enter = (Error "enterInformation not possible for existentials (genUI)", vst)
49 (View b=:(Box a)) = case (castEditor a).Editor.genUI uia dp (View a) vst of
50 (Error e, vst) = (Error e, vst)
51 (Ok (ui, es), vst) = (Ok (ui, AnnotatedState (dynamicJSONEncode (View (), uia, b)) es), vst)
52 (Update b=:(Box a)) = case (castEditor a).Editor.genUI uia dp (Update a) vst of
53 (Error e, vst) = (Error e, vst)
54 (Ok (ui, es), vst) = (Ok (ui, AnnotatedState (dynamicJSONEncode (Update (), uia, b)) es), vst)
55 , onEdit = \dp dpn es vst->case es of
56 AnnotatedState ebox es = case dynamicJSONDecode ebox of
57 Just (Enter, _, Box b) = case (castEditor b).Editor.onEdit dp dpn es vst of
58 (Error e, vst) = (Error e, vst)
59 (Ok (ui, es), vst) = (Ok (ui, AnnotatedState ebox es), vst)
60 Just (View (), _, Box b) = case (castEditor b).Editor.onEdit dp dpn es vst of
61 (Error e, vst) = (Error e, vst)
62 (Ok (ui, es), vst) = (Ok (ui, AnnotatedState ebox es), vst)
63 Just (Update (), _, Box b) = case (castEditor b).Editor.onEdit dp dpn es vst of
64 (Error e, vst) = (Error e, vst)
65 (Ok (ui, es), vst) = (Ok (ui, AnnotatedState ebox es), vst)
66 Nothing = (Error "corrupt editor state in Box", vst)
67 _ = (Error "corrupt editor state in Box", vst)
68 , onRefresh = \dp b=:(Box nb) es vst->case es of
69 AnnotatedState box es = case dynamicJSONDecode box of
70 Just (View (), uia, Box _) = case (castEditor nb).Editor.genUI uia dp (View nb) vst of
71 (Ok (ui, es), vst) = (Ok (ReplaceUI ui, AnnotatedState (dynamicJSONEncode (View (), uia, b)) es), vst)
72 (Error e, vst) = (Error e, vst)
73 Just (Update (), uia, Box _) = case (castEditor nb).Editor.genUI uia dp (Update nb) vst of
74 (Ok (ui, es), vst) = (Ok (ReplaceUI ui, AnnotatedState (dynamicJSONEncode (Update (), uia, b)) es), vst)
75 (Error e, vst) = (Error e, vst)
76 Just (Enter, uia, Box _) = case (castEditor nb).Editor.genUI uia dp (Update nb) vst of
77 (Ok (ui, es), vst) = (Ok (ReplaceUI ui, AnnotatedState (dynamicJSONEncode (Update (), uia, b)) es), vst)
78 (Error e, vst) = (Error e, vst)
79 Nothing = (Error "corrupt editor state in Box", vst)
80 _ = (Error "corrupt editor state in Box", vst)
81 , valueFromState = \es->case es of
82 AnnotatedState box es = case dynamicJSONDecode box of
83 Just (_, _, Box a) = case (castEditor a).Editor.valueFromState es of
84 Just a = Just (Box a)
85 Nothing = Nothing
86 Nothing = Nothing
87 }
88 where
89 castEditor :: t -> Editor t | gEditor{|*|} t
90 castEditor _ = gEditor{|*|}
91 */
92
93 Start w = doTasks t w
94
95 //t = updateSharedInformation [] (Box 42)
96 t = withShared (Box 42) \bs->
97 updateSharedInformation [] bs
98 -|| updateSharedInformation [] bs
99
100 :: Type
101 = Int Int
102 | Bool Bool
103 | String String
104 | Real Real
105 | Unit
106 | List [Type]
107 | Tuple Type Type
108 | Tuple3 Type Type Type
109 | Person Person
110 :: Person = {firstName :: String, lastName :: String}
111
112 derive class iTask Type, Person