Git Product home page Git Product logo

elm-rosetree-path's Introduction

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 or a list of them, also called a "forest".

An alternative way of keeping track of one focus is a 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 Msges with potentially large, changing tree content

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

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/.

elm-rosetree-path's People

Contributors

indique avatar lue-bird avatar

Watchers

 avatar

Forkers

mackeyrms

elm-rosetree-path's Issues

In Tree.Navigate.restructure, state.path is reversed

It's taken me an hour to work out why this happens, but I don't think I'm able to come up with a way of fixing it.

In Tree.Navigate.restructure, the state object for a given tree node is built from the bottom up. This means that the path property is reversed, because the Tree.Path.toChild calls happen in reverse order.

I've made a small example on Ellie. Here's the code:

module Main exposing (main)

import Browser
import Html as H exposing (Html)
import Tree
import Tree.Path as TP
import Tree.Navigate as TN


test_tree = Tree.tree "top" 
    [ Tree.tree "0" 
        [ Tree.singleton "0, 0"
        , Tree.singleton "0, 1"
        ]
    , Tree.tree "1" 
        [ Tree.singleton "1, 0"
        , Tree.tree "1, 1" 
            [Tree.singleton "1, 1, 0"]
        , Tree.singleton "1, 2"
        ]
    ]

view_item sub = 
    H.div
        []
        [ H.text <| Debug.log "reduce" <| sub.label++" : "++(Debug.toString sub.path)
        , H.ul
            []
            (List.map (\c -> H.li [] [c]) sub.children)
        ]


view model =
    TN.restructure view_item test_tree


main : Program () () msg
main =
    Browser.sandbox
        { init = ()
        , view = view
        , update = \_ -> identity
        }

Is this intended behaviour?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.