-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
WorkaroundFastEvent.hs
39 lines (30 loc) · 1.19 KB
/
WorkaroundFastEvent.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
import Control.Monad
import Paths
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Main
------------------------------------------------------------------------------}
main :: IO ()
main = do
static <- getStaticDir
startGUI defaultConfig { jsStatic = Just static } setup
setup :: Window -> UI ()
setup window = do
return window # set title "Workaround for slow registering of event handlers"
button <- UI.button
# set UI.text "Click me"
# set UI.id_ "button"
msg <- UI.span # set UI.text "Some text"
getBody window #+ [ element button, element msg ]
onElementId "button" "click" $ do
element msg # set UI.text "I have been clicked!"
onElementId
:: String -- ID attribute of the element
-> String -- name of the DOM event to register the handler at
-> UI void -- handler to fire whenever the event happens
-> UI ()
onElementId elid event handler = do
window <- askWindow
exported <- ffiExport (runUI window handler >> return ())
runFunction $ ffi "$(%1).on(%2,%3)" ("#"++elid) event exported