-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Event
-- Copyright   :  (C) 2016-2026 David M. Johnson
-- License     :  BSD3-style (see the file LICENSE)
-- Maintainer  :  David M. Johnson <code@dmj.io>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- DOM event handlers and component lifecycle hooks for 'Miso.Types.View'.
--
-- There are two axes of event handling:
--
-- * __DOM events__ — 'on', 'onCapture', 'onWithOptions': attach JavaScript
--   event listeners to VDOM nodes. Decoded event payloads are dispatched as
--   @action@ values through the MVU loop.
--
-- * __Lifecycle hooks__ — 'onCreated', 'onDestroyed', etc.: fire Haskell
--   callbacks at specific points in a DOM element's mount\/unmount lifecycle.
--
-- See "Miso.Event.Decoder" for building custom 'Decoder' values and
-- "Miso.Event.Types" for structured payload types ('KeyboardEvent',
-- 'PointerEvent', etc.).
--
----------------------------------------------------------------------------
module Miso.Event
   ( -- *** Smart constructors
     on
   , onCapture
   , onWithOptions
   , Phase (..)
   -- *** Lifecycle events
   , onCreated
   , onCreatedWith
   , onBeforeCreated
   , onDestroyed
   , onBeforeDestroyed
   , onBeforeDestroyedWith
    -- *** Exports
   , module Miso.Event.Decoder
   , module Miso.Event.Types
   ) where
-----------------------------------------------------------------------------
import           Control.Monad (when)
import qualified Data.Map.Strict as M
import           Miso.JSON (parseEither)
-----------------------------------------------------------------------------
import           Miso.DSL
import           Miso.Event.Decoder
import           Miso.Event.Types
import qualified Miso.FFI.Internal as FFI
import           Miso.Types (Attribute (On), LogLevel(..), DOMRef, VTree(..))
import           Miso.String (MisoString, ms)
-----------------------------------------------------------------------------
-- | Attach a bubble-phase event handler to a VDOM node.
-- Convenience wrapper for @'onWithOptions' 'BUBBLE' 'defaultOptions'@.
--
-- The decoded event payload is converted to an @action@ by @toAction@ and
-- dispatched into the component's @update@ function.
--
-- @
-- let clickHandler = on \"click\" emptyDecoder $ \\() _ -> MyAction
-- in button_ [ clickHandler, class_ \"add\" ] [ text_ \"+\" ]
-- @
--
on :: MisoString
   -- ^ DOM event name (e.g. @\"click\"@, @\"input\"@)
   -> Decoder r
   -- ^ How to extract a Haskell value from the browser event object
   -> (r -> DOMRef -> action)
   -- ^ Converts the decoded payload and the element's DOM reference to an @action@
   -> Attribute action
on :: forall r action.
MisoString
-> Decoder r -> (r -> DOMRef -> action) -> Attribute action
on = Phase
-> Options
-> MisoString
-> Decoder r
-> (r -> DOMRef -> action)
-> Attribute action
forall r action.
Phase
-> Options
-> MisoString
-> Decoder r
-> (r -> DOMRef -> action)
-> Attribute action
onWithOptions Phase
BUBBLE Options
defaultOptions
-----------------------------------------------------------------------------
-- | Attach a capture-phase event handler to a VDOM node.
-- Convenience wrapper for @'onWithOptions' 'CAPTURE' 'defaultOptions'@.
--
-- Events in the capture phase propagate from the document root down to the
-- target element, before any bubble-phase handlers run.
--
-- @
-- let captureClick = onCapture \"click\" emptyDecoder $ \\() _ -> MyAction
-- in button_ [ captureClick ] [ text_ \"capture me\" ]
-- @
--
onCapture
   :: MisoString
   -- ^ DOM event name (e.g. @\"click\"@)
   -> Decoder r
   -- ^ How to extract a Haskell value from the browser event object
   -> (r -> DOMRef -> action)
   -- ^ Converts the decoded payload and the element's DOM reference to an @action@
   -> Attribute action
onCapture :: forall r action.
MisoString
-> Decoder r -> (r -> DOMRef -> action) -> Attribute action
onCapture = Phase
-> Options
-> MisoString
-> Decoder r
-> (r -> DOMRef -> action)
-> Attribute action
forall r action.
Phase
-> Options
-> MisoString
-> Decoder r
-> (r -> DOMRef -> action)
-> Attribute action
onWithOptions Phase
CAPTURE Options
defaultOptions
-----------------------------------------------------------------------------
-- | Attach an event handler with explicit phase and propagation options.
--
-- * @phase@    — 'BUBBLE' (default) or 'CAPTURE': which DOM propagation phase
--   the listener is registered on.
-- * @options@  — 'defaultOptions' or a custom 'Options' value: controls
--   @preventDefault@ and @stopPropagation@ behaviour.
-- * @eventName@ — the DOM event name, e.g. @\"click\"@, @\"keydown\"@.
-- * @decoder@  — a 'Decoder' that extracts relevant fields from the JS event object.
-- * @toAction@ — maps the decoded payload and the element's 'DOMRef' to an @action@.
--
-- @
-- let clickHandler = onWithOptions BUBBLE defaultOptions \"click\" emptyDecoder $ \\() _ -> Action
-- in button_ [ clickHandler, class_ \"add\" ] [ text_ \"+\" ]
-- @
--
onWithOptions
  :: Phase
  -- ^ Event propagation phase: 'BUBBLE' (default) or 'CAPTURE'
  -> Options
  -- ^ Propagation options (@preventDefault@, @stopPropagation@)
  -> MisoString
  -- ^ DOM event name (e.g. @\"click\"@, @\"keydown\"@)
  -> Decoder r
  -- ^ How to extract a Haskell value from the browser event object
  -> (r -> DOMRef -> action)
  -- ^ Converts the decoded payload and the element's DOM reference to an @action@
  -> Attribute action
onWithOptions :: forall r action.
Phase
-> Options
-> MisoString
-> Decoder r
-> (r -> DOMRef -> action)
-> Attribute action
onWithOptions Phase
phase Options
options MisoString
eventName Decoder{DecodeTarget
Value -> Parser r
decoder :: Value -> Parser r
decodeAt :: DecodeTarget
decodeAt :: forall a. Decoder a -> DecodeTarget
decoder :: forall a. Decoder a -> Value -> Parser a
..} r -> DOMRef -> action
toAction =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
n) LogLevel
logLevel Events
events -> do
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (LogLevel
logLevel LogLevel -> LogLevel -> Bool
forall a. Eq a => a -> a -> Bool
== LogLevel
DebugAll Bool -> Bool -> Bool
|| LogLevel
logLevel LogLevel -> LogLevel -> Bool
forall a. Eq a => a -> a -> Bool
== LogLevel
DebugEvents) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
      case MisoString -> Events -> Maybe Phase
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup MisoString
eventName Events
events of
        Maybe Phase
Nothing ->
            MisoString -> IO ()
FFI.consoleError (MisoString -> IO ()) -> MisoString -> IO ()
forall a b. (a -> b) -> a -> b
$ [MisoString] -> MisoString
forall a. Monoid a => [a] -> a
mconcat
              [ MisoString
"Event \""
              , MisoString
eventName
              , MisoString
"\" is not being listened on. To use this event, "
              , MisoString
"add to the 'events' Map in Component"
              ]
        Maybe Phase
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    eventsVal <-
      MisoString -> Object -> IO DOMRef
forall o. ToObject o => MisoString -> o -> IO DOMRef
getProp MisoString
"events" Object
n
    eventObj <-
      case phase of
        Phase
CAPTURE -> MisoString -> Object -> IO DOMRef
forall o. ToObject o => MisoString -> o -> IO DOMRef
getProp MisoString
"captures" (DOMRef -> Object
Object DOMRef
eventsVal)
        Phase
BUBBLE -> MisoString -> Object -> IO DOMRef
forall o. ToObject o => MisoString -> o -> IO DOMRef
getProp MisoString
"bubbles" (DOMRef -> Object
Object DOMRef
eventsVal)
    eventHandlerObject@(Object eo) <- create
    jsOptions <- toJSVal options
    decodeAtVal <- toJSVal decodeAt
    cb <- FFI.asyncCallback2 $ \DOMRef
e DOMRef
domRef -> do
        Just v <- DOMRef -> IO (Maybe Value)
forall a. FromJSVal a => DOMRef -> IO (Maybe a)
fromJSVal (DOMRef -> IO (Maybe Value)) -> IO DOMRef -> IO (Maybe Value)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< DOMRef -> DOMRef -> IO DOMRef
FFI.eventJSON DOMRef
decodeAtVal DOMRef
e
        case parseEither decoder v of
          Left MisoString
msg -> MisoString -> IO ()
FFI.consoleError (MisoString
"[EVENT DECODE ERROR]: " MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString -> MisoString
forall str. ToMisoString str => str -> MisoString
ms MisoString
msg)
          Right r
event -> Sink action
sink (r -> DOMRef -> action
toAction r
event DOMRef
domRef)
    FFI.set "runEvent" cb eventHandlerObject
    FFI.set "options" jsOptions eventHandlerObject
    FFI.set eventName eo (Object eventObj)
-----------------------------------------------------------------------------
-- | Fire an action immediately after the DOM element is inserted into the document.
--
-- Use this to trigger imperative setup (focus, measurements, third-party widget
-- initialisation) that requires the element to be live in the page.
--
-- @since 1.9.0.0
--
onCreated
  :: action
  -- ^ Action to dispatch after the element is inserted into the DOM
  -> Attribute action
onCreated :: forall action. action -> Attribute action
onCreated action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- IO () -> IO DOMRef
FFI.syncCallback (Sink action
sink action
action)
    FFI.set "onCreated" callback object
-----------------------------------------------------------------------------
-- | Like 'onCreated' but also receives the element's 'DOMRef'.
--
-- Useful when you need to store or forward the raw DOM node to a JS library.
--
-- @since 1.9.0.0
--
onCreatedWith
  :: (DOMRef -> action)
  -- ^ Callback receiving the element's 'DOMRef' after it is inserted into the DOM
  -> Attribute action
onCreatedWith :: forall action. (DOMRef -> action) -> Attribute action
onCreatedWith DOMRef -> action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- (DOMRef -> IO ()) -> IO DOMRef
FFI.syncCallback1 (Sink action
sink Sink action -> (DOMRef -> action) -> DOMRef -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DOMRef -> action
action)
    FFI.set "onCreated" callback object
-----------------------------------------------------------------------------
-- | Fire an action immediately after the DOM element is removed from the document.
--
-- The element has already been detached from the DOM when this fires.
--
-- @since 1.9.0.0
--
onDestroyed
  :: action
  -- ^ Action to dispatch after the element is removed from the DOM
  -> Attribute action
onDestroyed :: forall action. action -> Attribute action
onDestroyed action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- IO () -> IO DOMRef
FFI.syncCallback (Sink action
sink action
action)
    FFI.set "onDestroyed" callback object
-----------------------------------------------------------------------------
-- | Fire an action just before the DOM element is removed from the document.
--
-- The element is still present in the DOM when this fires, making it suitable
-- for teardown logic (cancel animations, disconnect observers, etc.).
--
-- @since 1.9.0.0
--
onBeforeDestroyed
  :: action
  -- ^ Action to dispatch just before the element is removed from the DOM
  -> Attribute action
onBeforeDestroyed :: forall action. action -> Attribute action
onBeforeDestroyed action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- IO () -> IO DOMRef
FFI.syncCallback (Sink action
sink action
action)
    FFI.set "onBeforeDestroyed" callback object
-----------------------------------------------------------------------------
-- | Like 'onBeforeDestroyed' but also receives the element's 'DOMRef'.
--
-- @since 1.9.0.0
--
onBeforeDestroyedWith
  :: (DOMRef -> action)
  -- ^ Callback receiving the element's 'DOMRef' just before it is removed from the DOM
  -> Attribute action
onBeforeDestroyedWith :: forall action. (DOMRef -> action) -> Attribute action
onBeforeDestroyedWith DOMRef -> action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- (DOMRef -> IO ()) -> IO DOMRef
FFI.syncCallback1 (Sink action
sink Sink action -> (DOMRef -> action) -> DOMRef -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DOMRef -> action
action)
    FFI.set "onBeforeDestroyed" callback object
-----------------------------------------------------------------------------
-- | Fire an action just before the DOM element is inserted into the document.
--
-- The element has been constructed but is not yet attached to the live DOM when
-- this fires.
--
-- @since 1.9.0.0
--
onBeforeCreated
  :: action
  -- ^ Action to dispatch just before the element is inserted into the DOM
  -> Attribute action
onBeforeCreated :: forall action. action -> Attribute action
onBeforeCreated action
action =
  (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall action.
(Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
On ((Sink action -> VTree -> LogLevel -> Events -> IO ())
 -> Attribute action)
-> (Sink action -> VTree -> LogLevel -> Events -> IO ())
-> Attribute action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink (VTree Object
object) LogLevel
_ Events
_ -> do
    callback <- IO () -> IO DOMRef
FFI.syncCallback (Sink action
sink action
action)
    FFI.set "onBeforeCreated" callback object
-----------------------------------------------------------------------------