@@ -22,6 +22,9 @@ import Ocelot.Block.Icon as Icon
22
22
import Ocelot.Data.Tree (ItemPath , Node (..), IndexPath , _expanded , _selected , _children )
23
23
import Ocelot.HTML.Properties (css )
24
24
25
+ type ComponentM item m =
26
+ H.HalogenM (State item ) (Action item ) () (Message item ) m
27
+
25
28
type State item =
26
29
{ items :: Array (Node item )
27
30
, initial :: Array (Node item )
@@ -36,6 +39,7 @@ data Action item
36
39
data Query item a
37
40
= SetItems (Array (Node item )) a
38
41
| SetSelections (Array (ItemPath item )) a
42
+ | ToggleSingle (ItemPath item ) Boolean a
39
43
40
44
type Input item =
41
45
{ renderItem :: item -> HH.PlainHTML
@@ -146,11 +150,24 @@ handleQuery = case _ of
146
150
SetSelections itemPaths a -> do
147
151
{ items, initial } <- H .get
148
152
let paths = flip itemPathToIndexPath items <$> itemPaths
149
- updates = (\p r -> r { items = expandPath p r.items }) <$> paths
153
+ updates = (\p r -> r { items = expandPath true p r.items }) <$> paths
150
154
updater = A .foldl (>>>) (_ { items = initial }) updates
151
155
H .modify_ updater
152
156
pure $ Just a
153
157
158
+ ToggleSingle itemPath checked a -> toggleSingle itemPath checked $> Just a
159
+
160
+ toggleSingle ::
161
+ forall item m .
162
+ Eq item =>
163
+ ItemPath item ->
164
+ Boolean ->
165
+ ComponentM item m Unit
166
+ toggleSingle itemPath checked = do
167
+ H .modify_ \old ->
168
+ old { items = expandPath checked (itemPathToIndexPath itemPath old.items) old.items }
169
+
170
+
154
171
-- ---
155
172
-- Helper functions for expanding paths, toggling checkboxes, etc.
156
173
@@ -176,8 +193,8 @@ pathToLens path lastProp = (<<<) _items <$> pathToLens'
176
193
177
194
-- TODO : update this to use lenses, possibly using pathToLens on increasing subsections of array
178
195
-- e.g. [pathToLens [0] _expanded, pathToLens [0, 2] _expanded, pathToLens [0, 2, 1] _checked]
179
- expandPath :: ∀ a . IndexPath -> Array (Node a ) -> Array (Node a )
180
- expandPath path traits = do
196
+ expandPath :: ∀ a . Boolean -> IndexPath -> Array (Node a ) -> Array (Node a )
197
+ expandPath checked path traits = do
181
198
expandPath' (A .head path) (A .tail path) traits
182
199
where
183
200
expandPath' (Just ix) (Just p) ts | A .length p > 0 = fromMaybe [] $ A .modifyAt ix (expand p) ts
@@ -187,7 +204,7 @@ expandPath path traits = do
187
204
{ expanded = true
188
205
, children = expandPath' (A .head p) (A .tail p) t.children
189
206
}
190
- check (Node t) = Node $ t { selected = true }
207
+ check (Node t) = Node $ t { selected = checked }
191
208
192
209
itemPathToIndexPath :: ∀ a . Eq a => ItemPath a -> Array (Node a ) -> IndexPath
193
210
itemPathToIndexPath path ns =
0 commit comments