-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.purs
297 lines (260 loc) · 8.18 KB
/
Array.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
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
module InteractiveData.DataUIs.Array
( ArrayMsg
, ArrayState
, CfgArray
, array
, array_
, defaultCfgArray
) where
import InteractiveData.Core.Prelude
import Chameleon as C
import Control.Alt ((<|>))
import Data.Array as Array
import Data.FunctorWithIndex (mapWithIndex)
import Data.Traversable (traverse)
import DataMVC.Types.DataUI (applyWrap, runDataUi)
import InteractiveData.App.FastForward.Inline as FastForwardInline
import InteractiveData.App.UI.ActionButton as UIActionButton
import InteractiveData.Core.Types.DataTree as DT
import InteractiveData.Core.Types.IDSurface (runIdSurface)
-------------------------------------------------------------------------------
--- Types
-------------------------------------------------------------------------------
data ArrayMsg msg
= EntryMsg Int msg
| AddAtEnd
| AddAtStart
| Delete Int
| DeleteAll
newtype ArrayState sta = ArrayState (Array sta)
derive newtype instance Show sta => Show (ArrayState sta)
-------------------------------------------------------------------------------
--- Extract
-------------------------------------------------------------------------------
extract :: forall sta a. { extract :: sta -> DataResult a } -> ArrayState sta -> DataResult (Array a)
extract item (ArrayState s) =
traverse item.extract s
-------------------------------------------------------------------------------
--- Init
-------------------------------------------------------------------------------
init
:: forall sta a
. { init :: Maybe a -> sta
}
-> Maybe (Array a)
-> ArrayState sta
init item = case _ of
Nothing -> ArrayState []
Just a -> ArrayState $ map (Just >>> item.init) a
-------------------------------------------------------------------------------
--- Update
-------------------------------------------------------------------------------
update
:: forall msg sta a
. { update :: msg -> sta -> sta
, init :: Maybe a -> sta
}
-> ArrayMsg msg
-> ArrayState sta
-> ArrayState sta
update item msg (ArrayState state) =
case msg of
EntryMsg index childMsg -> ArrayState
let
result :: Maybe (Array sta)
result = Array.modifyAt index (item.update childMsg) state
in
fromMaybe state result
AddAtStart -> ArrayState
(Array.cons (item.init Nothing) state)
AddAtEnd -> ArrayState
(Array.snoc state (item.init Nothing))
Delete index -> ArrayState
let
result :: Maybe (Array sta)
result = Array.deleteAt index state
in
fromMaybe state result
DeleteAll -> ArrayState []
-------------------------------------------------------------------------------
--- View
-------------------------------------------------------------------------------
view
:: forall html msg sta
. IDHtml html
=> { view :: sta -> html msg
, childDataTrees :: Array (DataTree html msg)
}
-> ArrayState sta
-> html (ArrayMsg msg)
view cfg (ArrayState items) =
withCtx \ctx ->
let
el =
{ root: styleNode C.div
[ "display: flex"
, "flex-direction: column"
, "gap: 20px"
]
, item: C.div
}
countItems :: Int
countItems = Array.length items
in
case ctx.viewMode of
Standalone ->
el.root []
( Array.zip items cfg.childDataTrees
#
mapWithIndex \index (item /\ childDataTree) ->
el.item []
[ viewItem (pick cfg) index childDataTree item
]
)
Inline ->
el.root []
[ C.text (printItemsString countItems)
]
viewItem
:: forall html msg sta
. IDHtml html
=> { view :: sta -> html msg }
-> Int
-> DataTree html msg
-> sta
-> html (ArrayMsg msg)
viewItem _ index childDataTree _ =
withCtx \ctx ->
let
el =
{ root: styleNode C.div
[ "display: flex"
, "align-items: flex-start"
, "justify-content: space-between"
]
, item: styleNode C.div
[ "width: 100%"
]
, actions: C.div
}
newPath = ctx.path <> [ SegField $ SegDynamicIndex index ]
trivialTrees :: Array (Array DataPathSegment /\ DataTree html msg)
trivialTrees = DT.digTrivialTrees
newPath
childDataTree
in
el.root []
[ el.item []
[ putCtx ctx { path = newPath, viewMode = Inline }
$ EntryMsg index
<$> FastForwardInline.view trivialTrees
]
, el.actions []
[ UIActionButton.view
{ dataAction: DataAction
{ label: "Delete"
, msg: This $ Delete index
, description: "Delete an item from the Array"
}
}
]
]
printItemsString :: Int -> String
printItemsString = case _ of
0 -> "No items"
1 -> "1 item"
n -> show n <> " items"
-------------------------------------------------------------------------------
--- DataActions
-------------------------------------------------------------------------------
actions :: forall msg. Array (DataAction (ArrayMsg msg))
actions =
[ DataAction
{ label: "Add at start"
, msg: This $ AddAtStart
, description: "Add an item at the start of the Array"
}
, DataAction
{ label: "Add at end"
, msg: This $ AddAtEnd
, description: "Add an item at the end of the Array"
}
, DataAction
{ label: "Delete all"
, msg: This $ DeleteAll
, description: "Delete all items from the Array"
}
]
-------------------------------------------------------------------------------
--- DataUI
-------------------------------------------------------------------------------
type CfgArray a =
{ text :: Maybe String
, init :: Maybe (Array a)
}
defaultCfgArray :: forall a. CfgArray a
defaultCfgArray =
{ text: Nothing
, init: Nothing
}
array
:: forall opt html fm fs msg sta a
. OptArgs (CfgArray a) opt
=> IDHtml html
=> opt
-> DataUI (IDSurface html) fm fs msg sta a
-> DataUI (IDSurface html) fm fs (ArrayMsg (fm msg)) (ArrayState (fs sta)) (Array a)
array opt dataUi =
let
cfg :: CfgArray a
cfg = getAllArgs defaultCfgArray opt
dataUi' :: DataUI (IDSurface html) fm fs (fm msg) (fs sta) a
dataUi' = applyWrap dataUi
in
DataUI \ctx ->
let
DataUiInterface itf = runDataUi dataUi' ctx
in
DataUiInterface
{ name: "Array"
, view: \state@(ArrayState items) -> IDSurface \srfCtx ->
let
view' :: fs sta -> html (fm msg)
view' state' =
let
dataTree :: DataTree html (fm msg)
dataTree = runIdSurface srfCtx $ itf.view state'
DataTree { view } = dataTree
in
view
mkChildren :: Int -> fs sta -> DataPathSegmentField /\ (DataTree html (ArrayMsg (fm msg)))
mkChildren index state' =
let
dataTree :: DataTree html (fm msg)
dataTree = runIdSurface srfCtx $ itf.view state'
in
SegDynamicIndex index /\ map (EntryMsg index) dataTree
childDataTrees :: Array (DataTree html (fm msg))
childDataTrees = items
#
map
(\item -> runIdSurface srfCtx $ itf.view item)
fields = mapWithIndex mkChildren items
in
DataTree
{ view: view { view: view', childDataTrees } state
, actions
, children: Fields fields
, meta: Nothing
, text: cfg.text
}
, extract: extract (pick itf)
, update: update (pick itf)
, init: \initGlobal -> init (pick itf) (initGlobal <|> cfg.init)
}
array_
:: forall html fm fs msg sta a
. IDHtml html
=> DataUI (IDSurface html) fm fs msg sta a
-> DataUI (IDSurface html) fm fs (ArrayMsg (fm msg)) (ArrayState (fs sta)) (Array a)
array_ = array {}