react-haskell icon indicating copy to clipboard operation
react-haskell copied to clipboard

finish event type definitions

Open joelburget opened this issue 10 years ago • 1 comments

What types of events are there? Currently mouse, keyboard, change, focus.

What properties do they all hold? How do properties map from js to haskell?

joelburget avatar Dec 03 '14 23:12 joelburget

You could have a look at @ocharles bindings to virtual-dom for some inspiration here. If a callback is JSRef Event -> IO () you can have a function like this one from virtual dom:

on :: MonadState HTMLElement m => JSString -> (JSRef Event -> IO ()) -> m ()

Giving you the following on* functions:

https://github.com/boothead/oHm/blob/master/src/Ohm/HTML.hs#L82

onInput :: MonadState HTMLElement m => DOMEvent String -> m ()
onInput chan = on "input" f
  where
  f evt = do
    t <- fromJSRef evt
    for_ t (\t' -> do
      t'' <- eventGetTarget t'
      for_ t''
        (htmlInputElementGetValue .
         castToHTMLInputElement >=> (channel chan)))

onKeyPress :: MonadState HTMLElement m => DOMEvent Int -> m ()
onKeyPress (DOMEvent chan) = on "keypress" f
  where
  f evt = do
    t <- fromJSRef evt
    for_ t
         (uiEventGetKeyCode .
          -- A little messy, but we're working with a dom-delegator 'KeyEvent' here.
          (unsafeCastGObject :: GObject -> UIEvent) .
           toGObject >=> chan)

DOMEvent is from here: https://github.com/boothead/oHm/blob/master/src/Ohm/DOMEvent.hs

I use DOMEvent as the entry point to stuff messages into a channel, with Contravariant instance use to convert the information coming out of the handler to an Event type.

Note that all the above requires dom-delegator.

commandodev avatar Mar 28 '15 11:03 commandodev