mpizenberg/elm-pointer-events/2.0.1

NOTE: this package is not compatible with Elm 0.19.1

Drag

Handling drag events. Due to the limitation of not being able to call JavaScript functions directly in elm, full drag and drop API cannot be supported.

However, most of the time we just need to be able to drop some files from the file system to the web page. By providing the dragover, and drop events, this module enables such use case. Of course, the file retrieved in the form of a Json.Decode.Value would still have to be sent through ports if further processing is needed that cannot be done directly using the Value.

type alias Event =
{ dataTransfer : DataTransfer
, mouseEvent : Event
}

Type that get returned by a browser drag event. It corresponds to a JavaScript DragEvent.

Since a DragEvent inherits from MouseEvent, all mouse event related properties are provided in the mouseEvent attribute of type Mouse.Event. Please refer to the Mouse module for more information on this value.

type alias DataTransfer =
{ files : List File
, types : List String
, dropEffect : String
}

Hold the data being dragged during a drag and drop operation. This corresponds to JavaScript DataTransfer.

The files attribute holds the list of files being dragged.

The types attribute contains a list of strings providing the different formats of objects being dragged.

The dropEffect attribute provides feedback on the selected effect for the current drag and drop operation. It can be one of:

  • "none": the item may not be dropped
  • "copy": a copy of the source item is made at the new location
  • "move": the item is moved to a new location
  • "link": a link to the source is somehow established at the new location

Beware that contrary to JavaScript, you have no way of modifying dropEffect in elm. This is provided purely for information as read only, like any other elm value.

The effectAllowed property is not provided since it has no use in the context of elm.

The items property is not provided by lack of compatibility.

type alias File =
{ name : String
, typeMIME : String
, size : Int
, data : Value
}

A file object is a specific kind of blob. Its raw JavaScript value is hold in the data attribute in the form of a Json.Decode.Value. This corresponds to a JavaScript File

  • name: name of the file, without the path for security reasons
  • typeMIME: MIME type of the file
  • size: size of the file in bytes

Remark: providing these properties as attributes in an elm record is the easiest way of bringing this File API to elm. Of course if at some point the File API is supported in elm, this would be changed to the supported version.

Drag Events

onOver : (Event -> msg) -> Attribute msg

Listen to dragover events.

onDrop : (Event -> msg) -> Attribute msg

Listen to drop events.

onLeave : (Event -> msg) -> Attribute msg

Listen to dragleave events.

Advanced Usage

onWithOptions : String -> Options -> (Event -> msg) -> Attribute msg

Personalize your drag events with chosen html options.

eventDecoder : Decoder Event

Drag.Event default decoder. It is provided in case you would like to extend it.

dataTransferDecoder : Decoder DataTransfer

DataTransfer decoder. It is provided in case you would like to extend it.

fileListDecoder : Decoder a -> Decoder (List a)

Turn a personalized file decoder into a List decoder.

fileDecoder : Decoder File

File decoder. It is provided in case you would like to extend it.