-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathGetElementsBy.hs
47 lines (37 loc) · 1.5 KB
/
GetElementsBy.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
import Control.Monad
import Data.IORef
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
-- | Main entry point.
main :: IO ()
main = startGUI defaultConfig setup
setup :: Window -> UI ()
setup w = do
return w # set title "Element Test"
button1 <- UI.button # set UI.text "by tag"
button2 <- UI.button # set UI.text "by class"
button3 <- UI.button # set UI.text "by id 'me'" # set UI.id_ "me"
button4 <- UI.button # set UI.text "by id 'ma'"
let mkString s = UI.string s # set UI.class_ "string"
getBody w #+ [column $
row [element button1, element button2, element button3, element button4]
: map mkString (words "Many different elements")]
on UI.click button1 $ const $ do
xs <- getElementsByTagName w "span"
forM_ xs $ \x -> element x # set text "tag"
on UI.click button2 $ const $ do
xs <- getElementsByClassName w "string"
forM_ xs $ \x -> element x # set text "class"
Just button3 <- getElementById w "me"
ref <- liftIO $ newIORef True
on UI.click button3 $ const $ void $ do
Just x <- getElementById w "me"
b <- liftIO $ readIORef ref
let s = if b then "yay" else "wow"
element x # set UI.text s
liftIO $ writeIORef ref (not b)
on UI.click button4 $ const $ void $ do
m <- getElementById w "ma"
case m of
Nothing -> element button4 # set UI.text "not found"
Just el -> element el # set UI.text "found"