@@ -22,6 +22,9 @@ import Ocelot.Block.Icon as Icon
2222import Ocelot.Data.Tree (ItemPath , Node (..), IndexPath , _expanded , _selected , _children )
2323import Ocelot.HTML.Properties (css )
2424
25+ type ComponentM item m =
26+ H.HalogenM (State item ) (Action item ) () (Message item ) m
27+
2528type State item =
2629 { items :: Array (Node item )
2730 , initial :: Array (Node item )
@@ -36,6 +39,7 @@ data Action item
3639data Query item a
3740 = SetItems (Array (Node item )) a
3841 | SetSelections (Array (ItemPath item )) a
42+ | ToggleSingle (ItemPath item ) Boolean a
3943
4044type Input item =
4145 { renderItem :: item -> HH.PlainHTML
@@ -146,11 +150,24 @@ handleQuery = case _ of
146150 SetSelections itemPaths a -> do
147151 { items, initial } <- H .get
148152 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
150154 updater = A .foldl (>>>) (_ { items = initial }) updates
151155 H .modify_ updater
152156 pure $ Just a
153157
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+
154171-- ---
155172-- Helper functions for expanding paths, toggling checkboxes, etc.
156173
@@ -176,8 +193,8 @@ pathToLens path lastProp = (<<<) _items <$> pathToLens'
176193
177194-- TODO : update this to use lenses, possibly using pathToLens on increasing subsections of array
178195-- 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
181198 expandPath' (A .head path) (A .tail path) traits
182199 where
183200 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
187204 { expanded = true
188205 , children = expandPath' (A .head p) (A .tail p) t.children
189206 }
190- check (Node t) = Node $ t { selected = true }
207+ check (Node t) = Node $ t { selected = checked }
191208
192209itemPathToIndexPath :: ∀ a . Eq a => ItemPath a -> Array (Node a ) -> IndexPath
193210itemPathToIndexPath path ns =
0 commit comments