Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish event type definitions #2

Open
joelburget opened this issue Dec 3, 2014 · 1 comment
Open

finish event type definitions #2

joelburget opened this issue Dec 3, 2014 · 1 comment

Comments

@joelburget
Copy link
Owner

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?

@commandodev
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants