While dragging a list item, the mouse events and the list reordering are handled internally by this module.
First you need to create a System object which holds the information and functions related to the drag operation.
Using this object you can wire up the internal model, subscriptions, commands, and update into your model, subscriptions, commands, and update respectively.
Also you can get access to the drag and drop events as well as the dragged position styles in your view functions.
Creates a System object according to your configuration.
Having a list of fruits:
type alias Fruit =
String
data : List Fruit
data =
[ "Apples", "Bananas", "Cherries", "Dates" ]The System is a wrapper type around your message and list item types:
system : DnDList.System Msg Fruit
system =
DnDList.create configRepresents the System configuration.
message: your message wrapper
movement: the kind of the Movement. It can be Free, Horizontal, or Vertical.
Example configuration:
config : DnDList.Config Msg
config =
{ message = MyMsg
, movement = DnDList.Free
}Internal message type. You should wrap it within your message constructor.
type Msg
= MyMsg DnDList.MsgRepresents the mouse dragging movement. Dragging can be restricted to vertical or horizontal axis only, or it can be free.
A Draggable represents the information about the current drag operation.
It should be set in your model and can be initialized through the System's draggable field.
type alias Model =
{ draggable : DnDList.Draggable
, items : List Fruit
}
initialModel : Model
initialModel =
{ draggable = system.draggable
, items = data
}subscriptions is a function to access browser events during the drag.
subscriptions : Model -> Sub Msg
subscriptions model =
system.subscriptions model.draggablecommands is a function to access the DOM for the dragged element x, y, width and height information.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MyMsg message ->
let
updatedModel = ...
in
( updatedModel
, system.commands model.draggable
)update is a function which returns an updated Draggable and the reordered list for your model.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MyMsg message ->
let
( draggable, items ) =
system.update message model.draggable model.items
in
( { model | draggable = draggable, items = items }
, system.commands model.draggable
)dragEvents is a function which wraps all the events for draggable elements.
model.items
|> List.indexedMap
(\index item ->
let
itemId : String
itemId =
"id-" ++ item
in
Html.div
(Html.Attributes.id itemId :: system.dragEvents index itemId)
[ Html.text item ]
)
|> Html.div []dropEvents is a function which wraps all the events for droppable elements.
model.items
|> List.indexedMap
(\index item ->
Html.div
(system.dropEvents index)
[ Html.text item ]
)
|> Html.div []draggedIndex is a helper which returns the index of the dragged element.
maybeDraggedIndex : Maybe Int
maybeDraggedIndex =
system.draggedIndex model.draggabledraggedStyles is a helper to set the current position of the dragged element.
Html.div
(system.draggedStyles model.draggable)
[ Html.text item ]
A
Systemencapsulates aDraggablewhich represents the information about the drag operation and the drag related functions.For the details, see System Fields