Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lue-bird/elm-rosetree-path

navigate rosetrees and forests
https://github.com/lue-bird/elm-rosetree-path

elm forest multiway-tree navigation path rosetree tree tree-path

Last synced: 26 days ago
JSON representation

navigate rosetrees and forests

Awesome Lists containing this project

README

        

# rosetree-path

A path is the location of a branch in a tree (or forest).
With this package you can use paths to navigate and alter
a [zwilias/elm-rosetree](https://package.elm-lang.org/packages/zwilias/elm-rosetree/latest/) or a list of them, also called a "forest".

An alternative way of
keeping track of one focus is a [`Tree.Zipper`](https://package.elm-lang.org/packages/zwilias/elm-rosetree/latest/Tree-Zipper).
However! Paths are nice if you want to
- keep track of many different locations
- easily deal with _potentially_ focussed nodes etc.
- don't want to pollute `Msg`es with potentially large, changing tree content

Example: A big tree on the screen where subtrees can be moved and deleted.

```elm
import Tree.Path exposing (TreePath)
import Tree.Navigate

type alias Model =
{ tree : Tree { translate : ( Float, Float ) }
, dragged : Maybe TreePath
}

type Msg =
= RightClickedOn ForestPath
| MouseMoved ( Float, Float )
--| ...

viewTree =
Tree.Navigate.restructure
(\sub ->
(...
|> onMouseDown (PressedOn sub.path)
|> (case sub.path |> Tree.Path.step of
Just childPath ->
onRightClick (RightClickedOn childPath)
Nothing ->
-- top level node should not be removable
identity
)
)
:: sub.children
|> group
|> shift sub.label.translate
)

update msg model =
case msg of
RightClickedOn path ->
{ model
| tree =
model.tree
|> Tree.mapChildren
(Forest.Navigate.remove path)
}

MouseMoved mousePosition ->
case model.dragged of
Just path ->
{ model
| tree =
model.tree
|> Tree.Navigate.alter path
(Tree.mapLabel
(\l -> { l | translate = ... })
)
}

Nothing ->
model

--...
```

Complete implementation in [example/](https://github.com/lue-bird/rosetree-path/tree/master/example/).