-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.hs
62 lines (54 loc) · 2.3 KB
/
hello.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE PartialTypeSignatures #-}
import Reflex.Dom
import Control.Monad
import qualified Data.Text as T
import Data.Text (Text)
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Monoid ((<>))
import GHCJS.DOM
import GHCJS.DOM.Document
import GHCJS.DOM.Node
import qualified Data.ByteString as BS
main :: IO ()
main = do
mainWidget $ do
elAttr "link" (Map.fromList [("rel", "stylesheet"), ("type", "text/css"), ("href", "main.css")]) blank
gateExample
return ()
gateExample :: forall t m. (MonadWidget t m) => m ()
gateExample = do
el "div" $ do
rec (inputContainer, marbleD) <- elClass' "div" "marbles-container" $ do
holdDyn 0 =<< inputMarble inputContainer
(_, (checkE, checkB)) <- elClass' "div" "gate-container" $ do
checkE' <- _checkbox_change <$> checkbox False def
checkB' <- hold False checkE'
return (checkE', checkB')
elClass' "div" "marbles-container" $ do
widgetHold blank $ fmap (\t -> case t of True -> outputMarble marbleD $ (gate checkB $ updated marbleD); False -> blank) checkE
return ()
return ()
intToAdjustedString :: Int -> Text
intToAdjustedString = T.pack . show . (+ (-25))
inputMarble :: forall t m. (MonadWidget t m) => Element EventResult GhcjsDomSpace t -> m (Event t Int)
inputMarble container = do
rec (div, _) <- elDynAttr' "div" attrsD $ blank
mousedownE <- hold False $ leftmost [True <$ domEvent Mousedown div, False <$ domEvent Mouseup container]
let moveE = gate mousedownE $ fmap fst $ leftmost [domEvent Mousemove div, domEvent Mousemove container]
attrsE = fmap coordToMarbleAttrs moveE
attrsD <- holdDyn (Map.singleton "class" "marble") attrsE
return moveE
outputMarble :: forall t m. (MonadWidget t m) => Dynamic t Int -> Event t Int -> m ()
outputMarble initialD moveE = do
rec _ <- elDynAttr' "div" attrsD $ blank
let attrsE = fmap coordToMarbleAttrs moveE
initialL <- sample . current $ initialD
attrsD <- holdDyn (coordToMarbleAttrs initialL) attrsE
return ()
coordToMarbleAttrs :: Int -> Map Text Text
coordToMarbleAttrs x = Map.fromList [("style", "left:" <> (T.pack . show . (+ (-25)) $ x) <> "px;"), ("class", "marble")]