module Graphics.UI.Threepenny.JQuery where

import Data.Char
import Data.Default

import Graphics.UI.Threepenny.Core

{-----------------------------------------------------------------------------
    jQuery utilities
------------------------------------------------------------------------------}
data Easing = Swing | Linear
  deriving (Easing -> Easing -> Bool
(Easing -> Easing -> Bool)
-> (Easing -> Easing -> Bool) -> Eq Easing
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Easing -> Easing -> Bool
== :: Easing -> Easing -> Bool
$c/= :: Easing -> Easing -> Bool
/= :: Easing -> Easing -> Bool
Eq,Int -> Easing
Easing -> Int
Easing -> [Easing]
Easing -> Easing
Easing -> Easing -> [Easing]
Easing -> Easing -> Easing -> [Easing]
(Easing -> Easing)
-> (Easing -> Easing)
-> (Int -> Easing)
-> (Easing -> Int)
-> (Easing -> [Easing])
-> (Easing -> Easing -> [Easing])
-> (Easing -> Easing -> [Easing])
-> (Easing -> Easing -> Easing -> [Easing])
-> Enum Easing
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Easing -> Easing
succ :: Easing -> Easing
$cpred :: Easing -> Easing
pred :: Easing -> Easing
$ctoEnum :: Int -> Easing
toEnum :: Int -> Easing
$cfromEnum :: Easing -> Int
fromEnum :: Easing -> Int
$cenumFrom :: Easing -> [Easing]
enumFrom :: Easing -> [Easing]
$cenumFromThen :: Easing -> Easing -> [Easing]
enumFromThen :: Easing -> Easing -> [Easing]
$cenumFromTo :: Easing -> Easing -> [Easing]
enumFromTo :: Easing -> Easing -> [Easing]
$cenumFromThenTo :: Easing -> Easing -> Easing -> [Easing]
enumFromThenTo :: Easing -> Easing -> Easing -> [Easing]
Enum,Int -> Easing -> ShowS
[Easing] -> ShowS
Easing -> String
(Int -> Easing -> ShowS)
-> (Easing -> String) -> ([Easing] -> ShowS) -> Show Easing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Easing -> ShowS
showsPrec :: Int -> Easing -> ShowS
$cshow :: Easing -> String
show :: Easing -> String
$cshowList :: [Easing] -> ShowS
showList :: [Easing] -> ShowS
Show)

instance Default Easing where def :: Easing
def = Easing
Linear

-- | Fade in an element.
fadeIn :: Element -> Int -> Easing -> IO () -> UI ()
fadeIn :: Element -> Int -> Easing -> IO () -> UI ()
fadeIn Element
el Int
duration Easing
easing IO ()
complete = do
    JSObject
callback <- IO () -> UI JSObject
forall a. IsHandler a => a -> UI JSObject
ffiExport IO ()
complete
    JSFunction () -> UI ()
runFunction (JSFunction () -> UI ()) -> JSFunction () -> UI ()
forall a b. (a -> b) -> a -> b
$ String -> Element -> Int -> String -> JSObject -> JSFunction ()
forall a. FFI a => String -> a
ffi String
"$(%1).animate({opacity: 1}, %2 * 1, %3, %4)"
        Element
el Int
duration ((Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (Easing -> String
forall a. Show a => a -> String
show Easing
easing)) JSObject
callback

-- | Fade out an element.
fadeOut :: Element -> Int -> Easing -> IO () -> UI ()
fadeOut :: Element -> Int -> Easing -> IO () -> UI ()
fadeOut Element
el Int
duration Easing
easing IO ()
complete = do
    JSObject
callback <- IO () -> UI JSObject
forall a. IsHandler a => a -> UI JSObject
ffiExport IO ()
complete
    JSFunction () -> UI ()
runFunction (JSFunction () -> UI ()) -> JSFunction () -> UI ()
forall a b. (a -> b) -> a -> b
$ String -> Element -> Int -> String -> JSObject -> JSFunction ()
forall a. FFI a => String -> a
ffi String
"$(%1).animate({opacity: 0}, %2 * 1, %3, %4)"
        Element
el Int
duration ((Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (Easing -> String
forall a. Show a => a -> String
show Easing
easing)) JSObject
callback

-- | The 'sendValue' event happens whenever the return key is pressed
-- while the element has focus. Its data is the event value.
sendValue :: Element -> Event String
sendValue :: Element -> Event String
sendValue = (EventData -> String) -> Event EventData -> Event String
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> String
forall a. FromJSON a => EventData -> a
unsafeFromJSON (Event EventData -> Event String)
-> (Element -> Event EventData) -> Element -> Event String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"sendvalue"

-- | Focus an element.
setFocus :: Element -> UI ()
setFocus :: Element -> UI ()
setFocus = JSFunction () -> UI ()
runFunction (JSFunction () -> UI ())
-> (Element -> JSFunction ()) -> Element -> UI ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> JSFunction ()
forall a. FFI a => String -> a
ffi String
"$(%1).focus()"

-- | Scroll to the bottom of an element.
scrollToBottom :: Element -> UI ()
scrollToBottom :: Element -> UI ()
scrollToBottom = JSFunction () -> UI ()
runFunction (JSFunction () -> UI ())
-> (Element -> JSFunction ()) -> Element -> UI ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> JSFunction ()
forall a. FFI a => String -> a
ffi String
"jquery_scrollToBottom(%1)"