module Graphics.UI.Threepenny.Events (
valueChange, selectionChange, checkedChange,
click, mousemove, mousedown, mouseup, hover, leave,
focus, blur,
KeyCode, keyup, keydown,
) where
import Graphics.UI.Threepenny.Attributes
import Graphics.UI.Threepenny.Core
silence = fmap (const ())
valueChange :: Element -> Event String
valueChange el = unsafeMapUI el (const $ get value el) (domEvent "keydown" el)
unsafeMapUI el f = unsafeMapIO (\a -> getWindow el >>= \w -> runUI w (f a))
selectionChange :: Element -> Event (Maybe Int)
selectionChange el = unsafeMapUI el (const $ get selection el) (click el)
checkedChange :: Element -> Event Bool
checkedChange el = unsafeMapUI el (const $ get checked el) (click el)
click :: Element -> Event ()
click = silence . domEvent "click"
hover :: Element -> Event ()
hover = silence . domEvent "mouseenter"
mousemove :: Element -> Event (Int,Int)
mousemove = fmap readCoordinates . domEvent "mousemove"
readCoordinates :: EventData -> (Int,Int)
readCoordinates json = (x,y)
where [x,y] = unsafeFromJSON json
mousedown :: Element -> Event (Int,Int)
mousedown = fmap readCoordinates . domEvent "mousedown"
mouseup :: Element -> Event (Int,Int)
mouseup = fmap readCoordinates . domEvent "mouseup"
leave :: Element -> Event ()
leave = silence . domEvent "mouseleave"
focus :: Element -> Event ()
focus = silence . domEvent "focus"
blur :: Element -> Event ()
blur = silence . domEvent "blur"
type KeyCode = Int
keydown :: Element -> Event KeyCode
keydown = fmap unsafeFromJSON . domEvent "keydown"
keyup :: Element -> Event KeyCode
keyup = fmap unsafeFromJSON . domEvent "keyup"