-
Notifications
You must be signed in to change notification settings - Fork 0
/
Record.purs
178 lines (161 loc) · 4.36 KB
/
Record.purs
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
module InteractiveData.DataUIs.Record
( CfgRecord
, RecordMode(..)
, mkSurface
, module Export
, record
, record_
) where
import InteractiveData.Core.Prelude
import Chameleon as C
import Data.Array (mapWithIndex)
import Data.Array as Array
import DataMVC.Record.DataUI (class DataUiRecord)
import DataMVC.Record.DataUI as R
import DataMVC.Types.DataUI (refineDataUi)
import InteractiveData.App.FastForward.Inline as FastForwardInline
import InteractiveData.Core.Types.DataTree as DT
import InteractiveData.Core.Types.IDSurface (runIdSurface)
import MVC.Record (RecordMsg, RecordState) as Export
import MVC.Record (RecordMsg, RecordState, ViewResult)
view
:: forall html msg
. IDHtml html
=> (Array (DataPathSegmentField /\ DataTree html msg))
-> html msg
view fields =
withCtx \ctx ->
let
el =
{ fieldsCountInfo: styleNode C.div
[ "font-style: italic"
, "font-size: 10px"
, "color: #999"
]
, root: styleNode C.div
[ "display: flex"
, "gap: 20px"
, "flex-direction: column"
]
}
countFieldsText :: String
countFieldsText = case Array.length fields of
0 -> "no fields"
1 -> "1 field"
n -> show n <> " fields"
in
el.root []
if Array.null fields then
[ el.fieldsCountInfo []
[ C.text countFieldsText ]
]
else
case ctx.viewMode of
Inline ->
[ el.fieldsCountInfo []
[ C.text countFieldsText ]
]
Standalone | not ctx.fastForward -> []
Standalone ->
(fields # mapWithIndex viewRow)
viewRow
:: forall html msg
. IDHtml html
=> Int
-> (DataPathSegmentField /\ DataTree html msg)
-> html msg
viewRow _ (seg /\ tree) = withCtx \ctx ->
let
newPath = ctx.path <> [ SegField seg ]
el =
{ root: C.div
}
trivialTrees :: Array (Array DataPathSegment /\ DataTree html msg)
trivialTrees = DT.digTrivialTrees
newPath
tree
in
el.root []
[ C.noHtml
, putCtx ctx { path = newPath, viewMode = Inline } $
FastForwardInline.view trivialTrees
]
mkSurface
:: forall html msg
. IDHtml html
=> { text :: Maybe String
, mkSegment :: Int -> String -> DataPathSegmentField
}
-> Array (ViewResult (IDSurface html) msg)
-> IDSurface html msg
mkSurface { mkSegment, text } opts = IDSurface \ctx ->
let
fields :: Array (DataPathSegmentField /\ DataTree html msg)
fields = opts # mapWithIndex
( \ix x ->
let
newCtx = ctx { path = ctx.path <> [ SegField $ SegStaticKey x.key ] }
in
mkSegment ix x.key /\
runIdSurface newCtx x.viewValue
)
children :: DataTreeChildren html msg
children = Fields fields
in
DataTree
{ view: view fields
, children
, actions: []
, meta: Nothing
, text
}
data RecordMode = Keys | Indices
type CfgRecord =
{ text :: Maybe String
, mode :: RecordMode
}
defaultCfgRecord :: CfgRecord
defaultCfgRecord =
{ text: Nothing
, mode: Keys
}
record
:: forall opt datauis html fm fs rmsg rsta r
. DataUiRecord datauis fm fs (IDSurface html) rmsg rsta r
=> IDHtml html
=> OptArgs CfgRecord opt
=> opt
-> Record datauis
-> DataUI (IDSurface html) fm fs (RecordMsg rmsg) (RecordState rsta) (Record r)
record opt dataUis =
let
cfg :: CfgRecord
cfg = getAllArgs defaultCfgRecord opt
in
R.dataUiRecord
{ viewEntries: mkSurface
{ mkSegment: case cfg.mode of
Keys -> mkSegmentKey
Indices -> mkSegmentIndex
, text: cfg.text
}
}
dataUis
# refineDataUi
{ typeName: case cfg.mode of
Keys -> "Record"
Indices -> "Fields"
, refine: Right
, unrefine: identity
}
record_
:: forall datauis html fm fs rmsg rsta r
. DataUiRecord datauis fm fs (IDSurface html) rmsg rsta r
=> IDHtml html
=> Record datauis
-> DataUI (IDSurface html) fm fs (RecordMsg rmsg) (RecordState rsta) (Record r)
record_ = record {}
mkSegmentKey :: Int -> String -> DataPathSegmentField
mkSegmentKey _ = SegStaticKey
mkSegmentIndex :: Int -> String -> DataPathSegmentField
mkSegmentIndex ix _ = SegStaticIndex (ix + 1)