Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more elaborate example with changing cursors #25

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
elm-stuff
.DS_Store

# Jet Brains

.idea/.DS_Store

## User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

## Generated files
.idea/**/contentModel.xml

## Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/elm-html5-drag-drop.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ You can try out the sample code right here: https://ellie-app.com/7TTxFRNPDGJa1
## Example

https://github.com/norpan/elm-html5-drag-drop/blob/master/example/Example.elm

### Running the example locally

```bash
cd example
npm install
npx elm-tooling install
npx npx elm-watch hot
open index.html
```
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
elm-stuff
node_modules
main.js
78 changes: 55 additions & 23 deletions example/Example.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
port module Example exposing (Model, Msg(..), Position(..), divStyle, init, isNothing, main, update, view, viewDiv)
port module Example exposing (Model, Msg(..), Position(..), divStyle, init, main, update, view, viewDiv)

import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Attributes.Extra
import Html5.DragDrop as DragDrop
import Json.Decode exposing (Value)

Expand All @@ -26,6 +27,7 @@ type Msg
= DragDropMsg (DragDrop.Msg Int Position)


init : () -> ( Model, Cmd Msg )
init () =
( { data = { count = 0, position = Up }
, dragDrop = DragDrop.init
Expand All @@ -34,6 +36,7 @@ init () =
)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
DragDropMsg msg_ ->
Expand All @@ -57,7 +60,7 @@ update msg model =
)


subscriptions model =
subscriptions _ =
Sub.none


Expand All @@ -68,35 +71,43 @@ divStyle =
]


view : Model -> Html Msg
view model =
let
dropId =
DragDrop.getDropId model.dragDrop
widthAttributes =
[ style "min-width" "30vw", style "max-width" "90vw", style "display" "flex", style "flex-direction" "column" ]

position =
DragDrop.getDroppablePosition model.dragDrop
in
div []
[ viewDiv Up model.data dropId position
, viewDiv Middle model.data dropId position
, viewDiv Down model.data dropId position
]
attributes =
-- While we are dragging we can set the cursor on the parent div.
if DragDrop.isDraggedOver model.dragDrop then
style "cursor" "move" :: widthAttributes

else if DragDrop.isDragging model.dragDrop then
style "cursor" "grabbing" :: widthAttributes

isNothing maybe =
case maybe of
Just _ ->
False

Nothing ->
True
else
widthAttributes
in
div [ style "display" "flex", style "justify-content" "center" ]
[ div attributes
[ viewDiv Up model.data model.dragDrop
, viewDiv Middle model.data model.dragDrop
, viewDiv Down model.data model.dragDrop
]
]


viewDiv position data dropId droppablePosition =
viewDiv : Position -> { count : Int, position : Position } -> DragDrop.Model Int Position -> Html Msg
viewDiv position data dragDrop =
let
maybeDropId : Maybe Position
maybeDropId =
DragDrop.getDropId dragDrop

highlight : List (Html.Attribute Msg)
highlight =
if dropId |> Maybe.map ((==) position) |> Maybe.withDefault False then
case droppablePosition of
if maybeDropId |> Maybe.map ((==) position) |> Maybe.withDefault False then
case maybeDroppablePosition of
Nothing ->
[]

Expand All @@ -109,6 +120,21 @@ viewDiv position data dropId droppablePosition =

else
[]

maybeDroppablePosition : Maybe DragDrop.Position
maybeDroppablePosition =
DragDrop.getDroppablePosition dragDrop

cursorStyle =
-- While we are dragging the cursor is set on the parent div.
if DragDrop.isDraggedOver dragDrop then
Html.Attributes.Extra.empty

else if DragDrop.isDragging dragDrop then
Html.Attributes.Extra.empty

else
style "cursor" "grab"
in
div
(divStyle
Expand All @@ -121,7 +147,13 @@ viewDiv position data dropId droppablePosition =
)
)
(if data.position == position then
[ img (src "https://upload.wikimedia.org/wikipedia/commons/f/f3/Elm_logo.svg" :: width 100 :: DragDrop.draggable DragDropMsg data.count) []
[ img
(src "https://upload.wikimedia.org/wikipedia/commons/f/f3/Elm_logo.svg"
:: width 100
:: cursorStyle
:: DragDrop.draggable DragDropMsg data.count
)
[]
, text (String.fromInt data.count)
]

Expand Down
7 changes: 7 additions & 0 deletions example/elm-tooling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tools": {
"elm": "0.19.1",
"elm-format": "0.8.7",
"elm-json": "0.2.13"
}
}
10 changes: 10 additions & 0 deletions example/elm-watch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targets": {
"My target name": {
"inputs": [
"Example.elm"
],
"output": "main.js"
}
}
}
12 changes: 7 additions & 5 deletions example/elm.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"type": "application",
"source-directories": [
".", "../src"
".",
"../src"
],
"elm-version": "0.19.0",
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.2"
"elm/json": "1.1.3",
"elm-community/html-extra": "3.4.0"
},
"indirect": {
"elm/time": "1.0.0",
Expand Down
3 changes: 1 addition & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Main</title>
<link rel="stylesheet" href="whatever-you-want.css">
<title>Drag and Drop Example</title>
<script src="main.js"></script>
</head>

Expand Down
Loading