module Graphics.UI.Threepenny.DragNDrop (
draggable, droppable, dragData,
DragData,
drag, dragStart, dragEnd, drop, dragEnter, dragLeave, dragOver,
) where
import Prelude hiding (drop)
import Control.Monad
import Graphics.UI.Threepenny.Core
draggable :: WriteAttr Element Bool
draggable :: WriteAttr Element Bool
draggable = forall i x. (i -> x -> UI ()) -> WriteAttr x i
mkWriteAttr Bool -> Element -> UI ()
set
where
set :: Bool -> Element -> UI ()
set Bool
v = forall x i o. ReadWriteAttr x i o -> i -> x -> UI ()
set' (String -> WriteAttr Element String
attr String
"draggable") forall a b. (a -> b) -> a -> b
$ if Bool
v then String
"true" else String
"false"
dragData :: WriteAttr Element DragData
dragData :: WriteAttr Element String
dragData = forall i x. (i -> x -> UI ()) -> WriteAttr x i
mkWriteAttr String -> Element -> UI ()
set
where
set :: String -> Element -> UI ()
set String
v = forall x i o. ReadWriteAttr x i o -> i -> x -> UI ()
set' (String -> WriteAttr Element String
attr String
"ondragstart") forall a b. (a -> b) -> a -> b
$
String
"event.dataTransfer.setData('dragData', '" forall a. [a] -> [a] -> [a]
++ String
v forall a. [a] -> [a] -> [a]
++ String
"')"
droppable :: WriteAttr Element Bool
droppable :: WriteAttr Element Bool
droppable = forall i x. (i -> x -> UI ()) -> WriteAttr x i
mkWriteAttr forall {a}. Widget a => Bool -> a -> UI ()
enable
where
enable :: Bool -> a -> UI ()
enable Bool
v = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. if Bool
v then forall {w}. Widget w => w -> UI Element
allowDrop else forall {w}. Widget w => w -> UI Element
blockDrop
allowDrop :: w -> UI Element
allowDrop w
el =
forall (m :: * -> *) w. (MonadIO m, Widget w) => w -> m Element
element w
el
# set (attr "ondragover") "event.preventDefault()"
# set (attr "ondrop" ) "event.preventDefault()"
blockDrop :: w -> UI Element
blockDrop w
el =
forall (m :: * -> *) w. (MonadIO m, Widget w) => w -> m Element
element w
el
# set (attr "ondragover") ""
# set (attr "ondrop" ) ""
type DragData = String
withDragData :: Event EventData -> Event String
withDragData = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([String] -> String
extract forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => EventData -> a
unsafeFromJSON)
where
extract :: [String] -> String
extract [String
s] = String
s
extract [String]
_ = String
""
drag :: Element -> Event DragData
drag :: Element -> Event String
drag = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"drag"
dragStart :: Element -> Event DragData
dragStart :: Element -> Event String
dragStart = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"dragstart"
dragEnd :: Element -> Event DragData
dragEnd :: Element -> Event String
dragEnd = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"dragend"
dragEnter :: Element -> Event DragData
dragEnter :: Element -> Event String
dragEnter = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"dragenter"
dragOver :: Element -> Event DragData
dragOver :: Element -> Event String
dragOver = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"dragover"
dragLeave :: Element -> Event DragData
dragLeave :: Element -> Event String
dragLeave = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"dragleave"
drop :: Element -> Event DragData
drop :: Element -> Event String
drop = Event EventData -> Event String
withDragData forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"drop"