github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/web/elm/src/Dashboard/Drag.elm (about) 1 module Dashboard.Drag exposing (drag, dragPipeline, insertAt) 2 3 import Dashboard.Group.Models exposing (Pipeline) 4 import List.Extra 5 import Message.Message exposing (DropTarget(..)) 6 7 8 insertAt : Int -> a -> List a -> List a 9 insertAt idx x xs = 10 case ( idx > 0, xs ) of 11 ( True, head :: tail ) -> 12 head :: insertAt (idx - 1) x tail 13 14 _ -> 15 x :: xs 16 17 18 dragPipeline : String -> DropTarget -> List Pipeline -> List Pipeline 19 dragPipeline pipeline target pipelines = 20 let 21 pipelineIndex name = 22 pipelines |> List.Extra.findIndex (.name >> (==) name) 23 24 fromIndex = 25 pipelineIndex pipeline 26 27 toIndex = 28 case target of 29 Before name -> 30 pipelineIndex name 31 32 After name -> 33 pipelineIndex name |> Maybe.map ((+) 1) 34 in 35 case ( fromIndex, toIndex ) of 36 ( Just from, Just to ) -> 37 drag from (to + 1) pipelines 38 39 _ -> 40 pipelines 41 42 43 drag : Int -> Int -> List a -> List a 44 drag from to xs = 45 if from >= to then 46 let 47 n = 48 List.length xs 49 in 50 List.reverse (drag (n - from - 1) (n - to + 1) (List.reverse xs)) 51 52 else 53 case xs of 54 [] -> 55 [] 56 57 head :: tail -> 58 if from == 0 then 59 insertAt (to - 1) head tail 60 61 else 62 head :: drag (from - 1) (to - 1) tail