{-# LANGUAGE OverloadedStrings #-}
module Miso.Native.X.Element.Input.Event
(
onBlur
, onBlurWith
, onBlurMain
, onBlurMainWith
, onConfirm
, onConfirmWith
, onConfirmMain
, onConfirmMainWith
, onFocus
, onFocusWith
, onFocusMain
, onFocusMainWith
, onInput
, onInputWith
, onInputMain
, onInputMainWith
, onSelection
, onSelectionWith
, onSelectionMain
, onSelectionMainWith
, InputEvent (..)
, SelectionEvent (..)
, inputValueDecoder
, inputDecoder
, selectionDecoder
, inputEvents
) where
import qualified Data.Map as M
import Miso.Event
import Miso.JSON
import Miso.String (MisoString)
import Miso.Types (Attribute, EventHandler, DOMRef)
inputEvents :: Events
inputEvents :: Events
inputEvents
= [(MisoString, Phase)] -> Events
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
[ (MisoString
"blur", Phase
BUBBLE)
, (MisoString
"confirm", Phase
BUBBLE)
, (MisoString
"focus", Phase
BUBBLE)
, (MisoString
"input", Phase
BUBBLE)
, (MisoString
"selection", Phase
BUBBLE)
]
data InputEvent
= InputEvent
{ InputEvent -> MisoString
inputValue :: MisoString
, InputEvent -> Int
inputSelectionStart :: Int
, InputEvent -> Int
inputSelectionEnd :: Int
, InputEvent -> Bool
inputIsComposing :: Bool
} deriving (Int -> InputEvent -> ShowS
[InputEvent] -> ShowS
InputEvent -> String
(Int -> InputEvent -> ShowS)
-> (InputEvent -> String)
-> ([InputEvent] -> ShowS)
-> Show InputEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputEvent -> ShowS
showsPrec :: Int -> InputEvent -> ShowS
$cshow :: InputEvent -> String
show :: InputEvent -> String
$cshowList :: [InputEvent] -> ShowS
showList :: [InputEvent] -> ShowS
Show, InputEvent -> InputEvent -> Bool
(InputEvent -> InputEvent -> Bool)
-> (InputEvent -> InputEvent -> Bool) -> Eq InputEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputEvent -> InputEvent -> Bool
== :: InputEvent -> InputEvent -> Bool
$c/= :: InputEvent -> InputEvent -> Bool
/= :: InputEvent -> InputEvent -> Bool
Eq)
data SelectionEvent
= SelectionEvent
{ SelectionEvent -> Int
selStart :: Int
, SelectionEvent -> Int
selEnd :: Int
} deriving (Int -> SelectionEvent -> ShowS
[SelectionEvent] -> ShowS
SelectionEvent -> String
(Int -> SelectionEvent -> ShowS)
-> (SelectionEvent -> String)
-> ([SelectionEvent] -> ShowS)
-> Show SelectionEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SelectionEvent -> ShowS
showsPrec :: Int -> SelectionEvent -> ShowS
$cshow :: SelectionEvent -> String
show :: SelectionEvent -> String
$cshowList :: [SelectionEvent] -> ShowS
showList :: [SelectionEvent] -> ShowS
Show, SelectionEvent -> SelectionEvent -> Bool
(SelectionEvent -> SelectionEvent -> Bool)
-> (SelectionEvent -> SelectionEvent -> Bool) -> Eq SelectionEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SelectionEvent -> SelectionEvent -> Bool
== :: SelectionEvent -> SelectionEvent -> Bool
$c/= :: SelectionEvent -> SelectionEvent -> Bool
/= :: SelectionEvent -> SelectionEvent -> Bool
Eq)
inputValueDecoder :: Decoder MisoString
inputValueDecoder :: Decoder MisoString
inputValueDecoder = [MisoString
"detail"] [MisoString] -> (Value -> Parser MisoString) -> Decoder MisoString
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser MisoString
details
where
details :: Value -> Parser MisoString
details = MisoString
-> (Object -> Parser MisoString) -> Value -> Parser MisoString
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser MisoString) -> Value -> Parser MisoString)
-> (Object -> Parser MisoString) -> Value -> Parser MisoString
forall a b. (a -> b) -> a -> b
$ \Object
o -> Object
o Object -> MisoString -> Parser MisoString
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"value"
inputDecoder :: Decoder InputEvent
inputDecoder :: Decoder InputEvent
inputDecoder = [MisoString
"detail"] [MisoString] -> (Value -> Parser InputEvent) -> Decoder InputEvent
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser InputEvent
details
where
details :: Value -> Parser InputEvent
details = MisoString
-> (Object -> Parser InputEvent) -> Value -> Parser InputEvent
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser InputEvent) -> Value -> Parser InputEvent)
-> (Object -> Parser InputEvent) -> Value -> Parser InputEvent
forall a b. (a -> b) -> a -> b
$ \Object
o ->
MisoString -> Int -> Int -> Bool -> InputEvent
InputEvent
(MisoString -> Int -> Int -> Bool -> InputEvent)
-> Parser MisoString -> Parser (Int -> Int -> Bool -> InputEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> MisoString -> Parser MisoString
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"value"
Parser (Int -> Int -> Bool -> InputEvent)
-> Parser Int -> Parser (Int -> Bool -> InputEvent)
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 (Maybe Int)
forall a. FromJSON a => Object -> MisoString -> Parser (Maybe a)
.:? MisoString
"selectionStart" Parser (Maybe Int) -> Int -> Parser Int
forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
0
Parser (Int -> Bool -> InputEvent)
-> Parser Int -> Parser (Bool -> InputEvent)
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 (Maybe Int)
forall a. FromJSON a => Object -> MisoString -> Parser (Maybe a)
.:? MisoString
"selectionEnd" Parser (Maybe Int) -> Int -> Parser Int
forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
0
Parser (Bool -> InputEvent) -> Parser Bool -> Parser InputEvent
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Bool -> (Int -> Bool) -> Maybe Int -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= (Int
0 :: Int)) (Maybe Int -> Bool) -> Parser (Maybe Int) -> Parser Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> MisoString -> Parser (Maybe Int)
forall a. FromJSON a => Object -> MisoString -> Parser (Maybe a)
.:? MisoString
"isComposing")
selectionDecoder :: Decoder SelectionEvent
selectionDecoder :: Decoder SelectionEvent
selectionDecoder = [MisoString
"detail"] [MisoString]
-> (Value -> Parser SelectionEvent) -> Decoder SelectionEvent
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser SelectionEvent
details
where
details :: Value -> Parser SelectionEvent
details = MisoString
-> (Object -> Parser SelectionEvent)
-> Value
-> Parser SelectionEvent
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser SelectionEvent)
-> Value -> Parser SelectionEvent)
-> (Object -> Parser SelectionEvent)
-> Value
-> Parser SelectionEvent
forall a b. (a -> b) -> a -> b
$ \Object
o ->
Int -> Int -> SelectionEvent
SelectionEvent
(Int -> Int -> SelectionEvent)
-> Parser Int -> Parser (Int -> SelectionEvent)
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
"selectionStart"
Parser (Int -> SelectionEvent)
-> Parser Int -> Parser SelectionEvent
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 Int
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"selectionEnd"
onBlur :: (MisoString -> action) -> Attribute model action
onBlur :: forall action model.
(MisoString -> action) -> Attribute model action
onBlur MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"blur" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onBlurMain :: (MisoString -> action) -> EventHandler model action
onBlurMain :: forall action model.
(MisoString -> action) -> EventHandler model action
onBlurMain MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"blur" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onBlurMainWith :: (MisoString -> model -> DOMRef -> action) -> EventHandler model action
onBlurMainWith :: forall model action.
(MisoString -> model -> DOMRef -> action)
-> EventHandler model action
onBlurMainWith MisoString -> model -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"blur" Decoder MisoString
inputValueDecoder MisoString -> model -> DOMRef -> action
action
onConfirm :: (MisoString -> action) -> Attribute model action
onConfirm :: forall action model.
(MisoString -> action) -> Attribute model action
onConfirm MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"confirm" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onConfirmMain :: (MisoString -> action) -> EventHandler model action
onConfirmMain :: forall action model.
(MisoString -> action) -> EventHandler model action
onConfirmMain MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"confirm" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onConfirmMainWith :: (MisoString -> model -> DOMRef -> action) -> EventHandler model action
onConfirmMainWith :: forall model action.
(MisoString -> model -> DOMRef -> action)
-> EventHandler model action
onConfirmMainWith MisoString -> model -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"confirm" Decoder MisoString
inputValueDecoder MisoString -> model -> DOMRef -> action
action
onFocus :: (MisoString -> action) -> Attribute model action
onFocus :: forall action model.
(MisoString -> action) -> Attribute model action
onFocus MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"focus" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onFocusMain :: (MisoString -> action) -> EventHandler model action
onFocusMain :: forall action model.
(MisoString -> action) -> EventHandler model action
onFocusMain MisoString -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"focus" Decoder MisoString
inputValueDecoder (\MisoString
e model
_ DOMRef
_ -> MisoString -> action
action MisoString
e)
onFocusMainWith :: (MisoString -> model -> DOMRef -> action) -> EventHandler model action
onFocusMainWith :: forall model action.
(MisoString -> model -> DOMRef -> action)
-> EventHandler model action
onFocusMainWith MisoString -> model -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"focus" Decoder MisoString
inputValueDecoder MisoString -> model -> DOMRef -> action
action
onInput :: (InputEvent -> action) -> Attribute model action
onInput :: forall action model.
(InputEvent -> action) -> Attribute model action
onInput InputEvent -> action
action = MisoString
-> Decoder InputEvent
-> (InputEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"input" Decoder InputEvent
inputDecoder (\InputEvent
e model
_ DOMRef
_ -> InputEvent -> action
action InputEvent
e)
onInputMain :: (InputEvent -> action) -> EventHandler model action
onInputMain :: forall action model.
(InputEvent -> action) -> EventHandler model action
onInputMain InputEvent -> action
action = MisoString
-> Decoder InputEvent
-> (InputEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"input" Decoder InputEvent
inputDecoder (\InputEvent
e model
_ DOMRef
_ -> InputEvent -> action
action InputEvent
e)
onInputMainWith :: (InputEvent -> model -> DOMRef -> action) -> EventHandler model action
onInputMainWith :: forall model action.
(InputEvent -> model -> DOMRef -> action)
-> EventHandler model action
onInputMainWith InputEvent -> model -> DOMRef -> action
action = MisoString
-> Decoder InputEvent
-> (InputEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"input" Decoder InputEvent
inputDecoder InputEvent -> model -> DOMRef -> action
action
onSelection :: (SelectionEvent -> action) -> Attribute model action
onSelection :: forall action model.
(SelectionEvent -> action) -> Attribute model action
onSelection SelectionEvent -> action
action = MisoString
-> Decoder SelectionEvent
-> (SelectionEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"selection" Decoder SelectionEvent
selectionDecoder (\SelectionEvent
e model
_ DOMRef
_ -> SelectionEvent -> action
action SelectionEvent
e)
onSelectionMain :: (SelectionEvent -> action) -> EventHandler model action
onSelectionMain :: forall action model.
(SelectionEvent -> action) -> EventHandler model action
onSelectionMain SelectionEvent -> action
action = MisoString
-> Decoder SelectionEvent
-> (SelectionEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"selection" Decoder SelectionEvent
selectionDecoder (\SelectionEvent
e model
_ DOMRef
_ -> SelectionEvent -> action
action SelectionEvent
e)
onSelectionMainWith :: (SelectionEvent -> model -> DOMRef -> action) -> EventHandler model action
onSelectionMainWith :: forall model action.
(SelectionEvent -> model -> DOMRef -> action)
-> EventHandler model action
onSelectionMainWith SelectionEvent -> model -> DOMRef -> action
action = MisoString
-> Decoder SelectionEvent
-> (SelectionEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"selection" Decoder SelectionEvent
selectionDecoder SelectionEvent -> model -> DOMRef -> action
action
onBlurWith :: (MisoString -> DOMRef -> action) -> Attribute model action
onBlurWith :: forall action model.
(MisoString -> DOMRef -> action) -> Attribute model action
onBlurWith MisoString -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"blur" Decoder MisoString
inputValueDecoder ((MisoString -> model -> DOMRef -> action)
-> Attribute model action)
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \MisoString
v model
_ DOMRef
domRef -> MisoString -> DOMRef -> action
action MisoString
v DOMRef
domRef
onConfirmWith :: (MisoString -> DOMRef -> action) -> Attribute model action
onConfirmWith :: forall action model.
(MisoString -> DOMRef -> action) -> Attribute model action
onConfirmWith MisoString -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"confirm" Decoder MisoString
inputValueDecoder ((MisoString -> model -> DOMRef -> action)
-> Attribute model action)
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \MisoString
v model
_ DOMRef
domRef -> MisoString -> DOMRef -> action
action MisoString
v DOMRef
domRef
onFocusWith :: (MisoString -> DOMRef -> action) -> Attribute model action
onFocusWith :: forall action model.
(MisoString -> DOMRef -> action) -> Attribute model action
onFocusWith MisoString -> DOMRef -> action
action = MisoString
-> Decoder MisoString
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"focus" Decoder MisoString
inputValueDecoder ((MisoString -> model -> DOMRef -> action)
-> Attribute model action)
-> (MisoString -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \MisoString
v model
_ DOMRef
domRef -> MisoString -> DOMRef -> action
action MisoString
v DOMRef
domRef
onInputWith :: (InputEvent -> DOMRef -> action) -> Attribute model action
onInputWith :: forall action model.
(InputEvent -> DOMRef -> action) -> Attribute model action
onInputWith InputEvent -> DOMRef -> action
action = MisoString
-> Decoder InputEvent
-> (InputEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"input" Decoder InputEvent
inputDecoder ((InputEvent -> model -> DOMRef -> action)
-> Attribute model action)
-> (InputEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \InputEvent
v model
_ DOMRef
domRef -> InputEvent -> DOMRef -> action
action InputEvent
v DOMRef
domRef
onSelectionWith :: (SelectionEvent -> DOMRef -> action) -> Attribute model action
onSelectionWith :: forall action model.
(SelectionEvent -> DOMRef -> action) -> Attribute model action
onSelectionWith SelectionEvent -> DOMRef -> action
action = MisoString
-> Decoder SelectionEvent
-> (SelectionEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"selection" Decoder SelectionEvent
selectionDecoder ((SelectionEvent -> model -> DOMRef -> action)
-> Attribute model action)
-> (SelectionEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \SelectionEvent
v model
_ DOMRef
domRef -> SelectionEvent -> DOMRef -> action
action SelectionEvent
v DOMRef
domRef