forked from HeinrichApfelmus/threepenny-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragNDropExample.hs
66 lines (52 loc) · 1.9 KB
/
DragNDropExample.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Control.Applicative
import Control.Monad
import Data.IORef
import Data.Maybe
import Paths
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Drag'N'Drop example
------------------------------------------------------------------------------}
main :: IO ()
main = do
static <- getStaticDir
startGUI defaultConfig
{ tpPort = 10000
, tpStatic = Just static
} setup
setup :: Window -> UI ()
setup w = void $ do
return w # set title "Drag 'N' Drop Example"
UI.addStyleSheet w "DragNDropExample.css"
pairs <- sequence $
zipWith mkDragPair (words "red green blue") (map (150*) [0..2])
getBody w #+ concat [[element i, element o] | (i,o) <- pairs]
type Color = String
mkDragPair :: Color -> Int -> UI (Element, Element)
mkDragPair color position = do
elDrag <- UI.new #. "box-drag"
# set UI.style [("left", show position ++ "px"), ("color",color)]
# set text "Drag me!"
# set UI.draggable True
# set UI.dragData color
elDrop <- UI.new #. "box-drop"
# set UI.style [("border","2px solid " ++ color), ("left", show position ++ "px")]
dropSuccess <- liftIO $ newIORef False
on UI.dragStart elDrag $ \_ -> void $
element elDrop
# set text "Drop here!"
# set UI.droppable True
on UI.dragEnd elDrag $ \_ -> void $ do
dropped <- liftIO $ readIORef dropSuccess
when (not dropped) $ void $
element elDrop
# set text ""
# set UI.droppable False
on UI.drop elDrop $ \color' -> when (color == color') $ void $ do
liftIO $ writeIORef dropSuccess True
delete elDrag
element elDrop
# set text "Dropped!"
# set UI.droppable False
return (elDrag, elDrop)