-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Native.X.Element.Viewpager.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
----------------------------------------------------------------------------
module Miso.Native.X.Element.Viewpager.Event
  ( -- *** Events
    onChange
  , onChangeWith
  , onChangeMain
  , onChangeMainWith
  , onOffsetChange
  , onOffsetChangeWith
  , onOffsetChangeMain
  , onOffsetChangeMainWith
  , onWillChange
  , onWillChangeWith
  , onWillChangeMain
  , onWillChangeMainWith
    -- *** Types
  , ViewpagerChangeEvent (..)
    -- *** Decoders
  , viewpagerChangeDecoder
  , offsetChangeDecoder
    -- *** Event Map
  , viewpagerEvents
  ) where
-----------------------------------------------------------------------------
import qualified Data.Map as M
-----------------------------------------------------------------------------
import           Miso.Event
import           Miso.JSON
import           Miso.Types (DOMRef, Attribute, EventHandler)
-----------------------------------------------------------------------------
viewpagerEvents :: Events
viewpagerEvents :: Events
viewpagerEvents
  = [(MisoString, Phase)] -> Events
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
  [ (MisoString
"change", Phase
BUBBLE)
  , (MisoString
"offsetchange", Phase
BUBBLE)
  , (MisoString
"willchange", Phase
BUBBLE)
  ]
-----------------------------------------------------------------------------
-- | Payload of the @bindchange@ and @bindwillchange@ events.
data ViewpagerChangeEvent
  = ViewpagerChangeEvent
  { ViewpagerChangeEvent -> Int
index :: Int
    -- ^ The page index
  , ViewpagerChangeEvent -> Bool
isDragged :: Bool
    -- ^ Whether the change was user-initiated
  } deriving (Int -> ViewpagerChangeEvent -> ShowS
[ViewpagerChangeEvent] -> ShowS
ViewpagerChangeEvent -> String
(Int -> ViewpagerChangeEvent -> ShowS)
-> (ViewpagerChangeEvent -> String)
-> ([ViewpagerChangeEvent] -> ShowS)
-> Show ViewpagerChangeEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ViewpagerChangeEvent -> ShowS
showsPrec :: Int -> ViewpagerChangeEvent -> ShowS
$cshow :: ViewpagerChangeEvent -> String
show :: ViewpagerChangeEvent -> String
$cshowList :: [ViewpagerChangeEvent] -> ShowS
showList :: [ViewpagerChangeEvent] -> ShowS
Show, ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool
(ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool)
-> (ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool)
-> Eq ViewpagerChangeEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool
== :: ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool
$c/= :: ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool
/= :: ViewpagerChangeEvent -> ViewpagerChangeEvent -> Bool
Eq)
-----------------------------------------------------------------------------
viewpagerChangeDecoder :: Decoder ViewpagerChangeEvent
viewpagerChangeDecoder :: Decoder ViewpagerChangeEvent
viewpagerChangeDecoder = [MisoString
"detail"] [MisoString]
-> (Value -> Parser ViewpagerChangeEvent)
-> Decoder ViewpagerChangeEvent
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser ViewpagerChangeEvent
details
  where
    details :: Value -> Parser ViewpagerChangeEvent
details = MisoString
-> (Object -> Parser ViewpagerChangeEvent)
-> Value
-> Parser ViewpagerChangeEvent
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser ViewpagerChangeEvent)
 -> Value -> Parser ViewpagerChangeEvent)
-> (Object -> Parser ViewpagerChangeEvent)
-> Value
-> Parser ViewpagerChangeEvent
forall a b. (a -> b) -> a -> b
$ \Object
o ->
      Int -> Bool -> ViewpagerChangeEvent
ViewpagerChangeEvent
        (Int -> Bool -> ViewpagerChangeEvent)
-> Parser Int -> Parser (Bool -> ViewpagerChangeEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> MisoString -> Parser Int
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"index"
        Parser (Bool -> ViewpagerChangeEvent)
-> Parser Bool -> Parser ViewpagerChangeEvent
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> MisoString -> Parser Bool
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"isDragged"
-----------------------------------------------------------------------------
offsetChangeDecoder :: Decoder Double
offsetChangeDecoder :: Decoder Double
offsetChangeDecoder = [MisoString
"detail"] [MisoString] -> (Value -> Parser Double) -> Decoder Double
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser Double
details
  where
    details :: Value -> Parser Double
details = MisoString -> (Object -> Parser Double) -> Value -> Parser Double
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser Double) -> Value -> Parser Double)
-> (Object -> Parser Double) -> Value -> Parser Double
forall a b. (a -> b) -> a -> b
$ \Object
o -> Object
o Object -> MisoString -> Parser Double
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"offset"
-----------------------------------------------------------------------------
-- | https://lynxjs.org/api/elements/built-in/viewpager.html#bindchange
--
-- Triggered with the current page index after the transition completes.
--
onChange :: (ViewpagerChangeEvent -> action) -> Attribute model action
onChange :: forall action model.
(ViewpagerChangeEvent -> action) -> Attribute model action
onChange ViewpagerChangeEvent -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"change" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder (\ViewpagerChangeEvent
e model
_ DOMRef
_ -> ViewpagerChangeEvent -> action
action ViewpagerChangeEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onChange', but dispatched on the Lynx __main thread__ ('MTS').
--
-- Runs imperatively on the MTS (no VDOM diff). Meant to be used with
-- @-XStaticPointers@.
--
-- @
-- data Action = Changed ViewpagerChangeEvent
--
-- view_ [ event (static (onChangeMain Changed)) ] [ "some view" ]
-- @
--
onChangeMain :: (ViewpagerChangeEvent -> action) -> EventHandler model action
onChangeMain :: forall action model.
(ViewpagerChangeEvent -> action) -> EventHandler model action
onChangeMain ViewpagerChangeEvent -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"change" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder (\ViewpagerChangeEvent
e model
_ DOMRef
_ -> ViewpagerChangeEvent -> action
action ViewpagerChangeEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onChangeMain', but the handler also receives read-only access to the
-- @model@ and the target element's 'DOMRef' (for imperative MTS mutation).
--
-- @
-- data Action = Changed ViewpagerChangeEvent Model DOMRef
--
-- view_ [ event (static (onChangeMainWith Changed)) ] [ "some view" ]
-- @
--
onChangeMainWith :: (ViewpagerChangeEvent -> model -> DOMRef -> action) -> EventHandler model action
onChangeMainWith :: forall model action.
(ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
onChangeMainWith ViewpagerChangeEvent -> model -> DOMRef -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"change" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder ViewpagerChangeEvent -> model -> DOMRef -> action
action
-----------------------------------------------------------------------------
-- | https://lynxjs.org/api/elements/built-in/viewpager.html#bindoffsetchange
--
-- Triggered with the scrolling progress during a page transition.
--
onOffsetChange :: (Double -> action) -> Attribute model action
onOffsetChange :: forall action model. (Double -> action) -> Attribute model action
onOffsetChange Double -> action
action = MisoString
-> Decoder Double
-> (Double -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"offsetchange" Decoder Double
offsetChangeDecoder (\Double
e model
_ DOMRef
_ -> Double -> action
action Double
e)
-----------------------------------------------------------------------------
-- | Like 'onOffsetChange', but dispatched on the Lynx __main thread__ ('MTS').
--
-- Runs imperatively on the MTS (no VDOM diff). Meant to be used with
-- @-XStaticPointers@.
--
-- @
-- data Action = OffsetChanged Double
--
-- view_ [ event (static (onOffsetChangeMain OffsetChanged)) ] [ "some view" ]
-- @
--
onOffsetChangeMain :: (Double -> action) -> EventHandler model action
onOffsetChangeMain :: forall action model.
(Double -> action) -> EventHandler model action
onOffsetChangeMain Double -> action
action = MisoString
-> Decoder Double
-> (Double -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"offsetchange" Decoder Double
offsetChangeDecoder (\Double
e model
_ DOMRef
_ -> Double -> action
action Double
e)
-----------------------------------------------------------------------------
-- | Like 'onOffsetChangeMain', but the handler also receives read-only access to
-- the @model@ and the target element's 'DOMRef' (for imperative MTS mutation).
--
-- @
-- data Action = OffsetChanged Double Model DOMRef
--
-- view_ [ event (static (onOffsetChangeMainWith OffsetChanged)) ] [ "some view" ]
-- @
--
onOffsetChangeMainWith :: (Double -> model -> DOMRef -> action) -> EventHandler model action
onOffsetChangeMainWith :: forall model action.
(Double -> model -> DOMRef -> action) -> EventHandler model action
onOffsetChangeMainWith Double -> model -> DOMRef -> action
action = MisoString
-> Decoder Double
-> (Double -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"offsetchange" Decoder Double
offsetChangeDecoder Double -> model -> DOMRef -> action
action
-----------------------------------------------------------------------------
-- | https://lynxjs.org/api/elements/built-in/viewpager.html#bindwillchange
--
-- Triggered with the next page index before the transition starts.
--
onWillChange :: (ViewpagerChangeEvent -> action) -> Attribute model action
onWillChange :: forall action model.
(ViewpagerChangeEvent -> action) -> Attribute model action
onWillChange ViewpagerChangeEvent -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"willchange" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder (\ViewpagerChangeEvent
e model
_ DOMRef
_ -> ViewpagerChangeEvent -> action
action ViewpagerChangeEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onWillChange', but dispatched on the Lynx __main thread__ ('MTS').
--
-- Runs imperatively on the MTS (no VDOM diff). Meant to be used with
-- @-XStaticPointers@.
--
-- @
-- data Action = WillChange ViewpagerChangeEvent
--
-- view_ [ event (static (onWillChangeMain WillChange)) ] [ "some view" ]
-- @
--
onWillChangeMain :: (ViewpagerChangeEvent -> action) -> EventHandler model action
onWillChangeMain :: forall action model.
(ViewpagerChangeEvent -> action) -> EventHandler model action
onWillChangeMain ViewpagerChangeEvent -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"willchange" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder (\ViewpagerChangeEvent
e model
_ DOMRef
_ -> ViewpagerChangeEvent -> action
action ViewpagerChangeEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onWillChangeMain', but the handler also receives read-only access to
-- the @model@ and the target element's 'DOMRef' (for imperative MTS mutation).
--
-- @
-- data Action = WillChange ViewpagerChangeEvent Model DOMRef
--
-- view_ [ event (static (onWillChangeMainWith WillChange)) ] [ "some view" ]
-- @
--
onWillChangeMainWith :: (ViewpagerChangeEvent -> model -> DOMRef -> action) -> EventHandler model action
onWillChangeMainWith :: forall model action.
(ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
onWillChangeMainWith ViewpagerChangeEvent -> model -> DOMRef -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"willchange" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder ViewpagerChangeEvent -> model -> DOMRef -> action
action
-----------------------------------------------------------------------------
-- | Like 'onChange', but the handler also receives the target element's 'DOMRef'.
-- Use for main-thread ('MTS') handlers that imperatively mutate the element.
onChangeWith :: (ViewpagerChangeEvent -> DOMRef -> action) -> Attribute model action
onChangeWith :: forall action model.
(ViewpagerChangeEvent -> DOMRef -> action)
-> Attribute model action
onChangeWith ViewpagerChangeEvent -> DOMRef -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"change" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder ((ViewpagerChangeEvent -> model -> DOMRef -> action)
 -> Attribute model action)
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \ViewpagerChangeEvent
vpce model
_ DOMRef
domRef -> ViewpagerChangeEvent -> DOMRef -> action
action ViewpagerChangeEvent
vpce DOMRef
domRef
-----------------------------------------------------------------------------
-- | Like 'onOffsetChange', but the handler also receives the target element's 'DOMRef'.
-- Use for main-thread ('MTS') handlers that imperatively mutate the element.
onOffsetChangeWith :: (Double -> DOMRef -> action) -> Attribute model action
onOffsetChangeWith :: forall action model.
(Double -> DOMRef -> action) -> Attribute model action
onOffsetChangeWith Double -> DOMRef -> action
action = MisoString
-> Decoder Double
-> (Double -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"offsetchange" Decoder Double
offsetChangeDecoder ((Double -> model -> DOMRef -> action) -> Attribute model action)
-> (Double -> model -> DOMRef -> action) -> Attribute model action
forall a b. (a -> b) -> a -> b
$ \Double
vpce model
_ DOMRef
domRef -> Double -> DOMRef -> action
action Double
vpce DOMRef
domRef
-----------------------------------------------------------------------------
-- | Like 'onWillChange', but the handler also receives the target element's 'DOMRef'.
-- Use for main-thread ('MTS') handlers that imperatively mutate the element.
onWillChangeWith :: (ViewpagerChangeEvent -> DOMRef -> action) -> Attribute model action
onWillChangeWith :: forall action model.
(ViewpagerChangeEvent -> DOMRef -> action)
-> Attribute model action
onWillChangeWith ViewpagerChangeEvent -> DOMRef -> action
action = MisoString
-> Decoder ViewpagerChangeEvent
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"willchange" Decoder ViewpagerChangeEvent
viewpagerChangeDecoder ((ViewpagerChangeEvent -> model -> DOMRef -> action)
 -> Attribute model action)
-> (ViewpagerChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \ViewpagerChangeEvent
vpce model
_ DOMRef
domRef -> ViewpagerChangeEvent -> DOMRef -> action
action ViewpagerChangeEvent
vpce DOMRef
domRef
-----------------------------------------------------------------------------