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 (EventData (Just x:Just y:_)) = (read x, read y)
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 read1 . domEvent "keydown"
keyup :: Element -> Event KeyCode
keyup = fmap read1 . domEvent "keyup"
read1 (EventData (Just s:_)) = read s