API Documentation

Util.Base

Public function interface

  • addScripts
  • addScript

addScripts

addScripts :: [String] -> IO ()

Used to dynamically add external JavaScript files to your document.

addScript

addScript :: String -> IO ()

Used to dynamically add an external JavaScript file to your document.

Example:
main :: IO ()
main = do
    addScript "../../js/handlebars.js"
    wrap $ do
        ...
    >>= onLoad  -- We must use onLoad instead of onReady to wait for
                -- the added scripts to load.

Util.DOM

Low-level DOM manipulation API

Public function interface

  • createElement
  • setInnerHtml
  • documentWrite
  • documentBody
  • window
  • appendChild
  • (~>)
  • setAttribute
  • removeAttribute
  • setValue
  • toString
  • innerHtml
  • createTextNode
  • parentElement
  • getElementById
  • getElementsByClassName
  • addEventListener
  • onClick
  • setStyleProperty

createElement

createElement :: String -> IO Element
createElement' :: PackedString -> IO Element

Create an element with the specified name.

setInnerHtml

setInnerHtml :: Element -> String -> IO Element
setInnerHtml' :: Element -> PackedString -> IO Element

Set the inner HTML of an element.

documentWrite

documentWrite :: String -> IO ()
documentWrite' :: PackedString -> IO ()

Writes HTML expressions or JavaScript code to a document.

documentBody

documentBody :: IO Element

Retrieve the document body element.

window

window :: IO Element

Retrieve the window element.

appendChild

appendChild :: Element   -- ^ Parent node
            -> Element   -- ^ The child element
            -> IO ()

Add an element after the last child node of the specified element.

(~>)

(~>) :: Element -> Element -> IO ()

The ~> operator is an infix version of appendChild with the arguments flipped.

setAttribute

setAttribute :: Element    -- ^ An element
             -> String     -- ^ Attribute name
             -> String     -- ^ Attribute value
             -> IO ()
setAttribute' :: Element          -- ^ An element
              -> PackedString     -- ^ Attribute name
              -> PackedString     -- ^ Attribute value
              -> IO ()

Add or set the attribute/value pair on the provided target element.

removeAttribute

removeAttribute :: Element       -- ^ An element
                -> String        -- ^ Attribute name
                -> IO ()
removeAttribute' :: Element -> PackedString -> IO ()

Remove an attribute from the element.

setValue

setValue :: Element -> String -> IO ()
setValue' :: Element -> PackedString -> IO ()

Set the value attribute of an element (typically an input field).

toString

toString :: Element -> IO String
toString' :: Element -> IO PackedString

Stringify an element.

innerHtml

innerHtml :: Element -> IO String
innerHtml' :: Element -> IO PackedString

Retrieve the HTML between the start and end tags of the object.

createTextNode

createTextNode :: String -> IO Element
createTextNode' :: PackedString -> IO Element

parentElement

parentElement :: Element -> IO (Maybe Element)

Return the parent node of an element. This function returns a Maybe value, since a parent element doesn't always exist.

getElementById

getElementById :: String -> IO (Maybe Element)
getElementById' :: PackedString -> IO (Maybe Element)

Return the element with the id attribute matching the specified value, if one exists. This function returns a Maybe Element to properly handle cases where no element is found.

getElementsByClassName

getElementsByClassName :: String -> IO [Element]
getElementsByClassName' :: PackedString -> IO [Element]

Return a list of elements which matches the provided class name(s).

addEventListener

addEventListener :: Element   -- ^ The target element
                 -> String    -- ^ Event type
                 -> IO ()     -- ^ Callback action
                 -> IO ()
addEventListener' :: Element -> PackedString -> IO () -> IO ()

Add an event listener to the provided element.

onClick

onClick :: Element     -- ^ The target element
        -> IO ()       -- ^ Callback action
        -> IO ()

Add an onClick event listener to the provided element.

setStyleProperty

setStyleProperty :: Element -> String -> String -> IO ()
setStyleProperty' :: Element -> PackedString -> 

Set a property on an element's style object.


Util.FRP

Functional reactive programming library

Public function interface

  • attach
  • _bindSignal
  • _bindSignalOn
  • _bindUnitSignal
  • onSignal
  • inputValue
  • clickSignal
  • mouseSignal
  • scrollXSignal
  • scrollYSignal

attach

attach :: Signal (IO a) -> IO ()

_bindSignal

_bindSignal :: String      -- ^ The name of the event, e.g., 'change'
            -> String      -- ^ Property to read from when the event is triggered.
            -> Element     -- ^ A DOM element
            -> Signal a

Create a signal by binding a specific event to a property on the given element. When implementing your own signals using _bindSignal you should always qualify the return type:

scrollY :: Element -> Signal Int
scrollY = _bindSignal "scroll" "scrollY" 

_bindSignalOn

_bindSignalOn :: String      -- ^ The name of the event, e.g., 'change'
              -> String      -- ^ Property to read from when the event is triggered.
              -> Element     -- ^ The event element 
              -> Element     -- ^ The property element
              -> Signal a

Create a signal by binding an event associated with one element to a property on another element.

_bindUnitSignal

_bindUnitSignal :: String      -- ^ The name of the event, e.g., 'click'
                -> Element     -- ^ A HTML DOM element
                -> Signal ()

Create a signal of unit type, i.e., one which is not associated with a property, but instead emits a simple () value.

onSignal

onSignal :: Signal a        -- ^ The "activating" signal
         -> IO b            -- ^ An action to trigger when the signal emits.
         -> Signal (IO b)

Promote an IO action to a signal with the help of another signal.

inputValue

inputValue :: Element -> Signal String
inputValue' :: Element -> Signal PackedString

Creates a signal for the value of an input field.

clickSignal

clickSignal :: Element -> Signal ()

A signal that reacts to click events on an element.

mouseSignal

mouseSignal :: Signal (Int, Int)

Mouse cursor position signal.

scrollXSignal

scrollXSignal :: Signal Int

Window scroll X position signal.

scrollYSignal

scrollYSignal :: Signal Int

Window scroll Y position signal.


Util.HTML

HTML combinator library

Public function interface

  • renderHtml
  • escapeHtmlEntities
  • attribute
  • empty
  • parent
  • leaf
  • toHtml
  • makeAttr
  • makePar
  • makeLeaf
  • ($<)

type Html = HtmlM ()

renderHtml

renderHtml :: HtmlM t -> String

escapeHtmlEntities

escapeHtmlEntities :: String -> String

attribute

attribute :: String -> String -> String -> Attribute

empty

empty :: Html

parent

parent :: String -> String -> String -> String -> Html -> Html

leaf

leaf :: String -> String -> String -> Html

toHtml

toHtml :: String -> HtmlM a

makeAttr

makeAttr :: String -> String -> Attribute

makePar

makePar :: String -> Html -> Html

makeLeaf

makeLeaf :: String -> Html

($<)

($<) :: (Html -> Html) -> String -> Html

Util.HTML.Handlebars

Public function interface

  • handlebar
  • each
  • ($?)
  • renderWithHtml

handlebar

handlebar :: String -> Html

each

each :: String -> Html -> Html

($?)

($?) :: (Html -> Html) -> String -> Html

renderWithHtml

renderWithHtml :: (ToJSON a) => Html -> a -> IO String

Util.HTML.PS

PackedString versions of HTML combinators. Has identical API to Util.HTML.


Util.Handlebars

Public function interface

  • compile
  • render

compile

compile :: forall a. String -> IO (Ptr a -> IO PackedString)

render

render :: (ToJSON a) => (Ptr a -> IO PackedString) -> a -> IO String
render' :: (ToJSON a) => (Ptr a -> IO PackedString) -> a -> IO PackedString

Collection

newtype Collection a = Collection [a]

Util.JSON

Public function interface

  • json
  • mapFrom
  • (.!)
  • mapTo
  • obj
  • (|=)
  • stringify
  • valueToStr
  • toStr
  • fromStr

newtype Object = Obj [(String, Value)]

A JSON object is implemented as an association list.

class FromJSON a where fromJSON :: Value -> Maybe a
class ToJSON a where toJSON :: a -> Value

json

json :: Parser Value

mapFrom

mapFrom :: (FromJSON a) => Value -> Maybe [a]

(.!)

(.!) :: (FromJSON a) => Object -> String -> Maybe a

mapTo

mapTo :: (ToJSON a) => [a] -> Value

obj

obj :: [(String, Value)] -> Value

(|=)

(|=) :: ToJSON a => String -> a -> (String, Value)

stringify

stringify :: Object -> String

valueToStr

valueToStr :: Value -> String

toStr

toStr :: (ToJSON a) => a -> String

fromStr

 fromStr :: (FromJSON a) => String -> Maybe a

Util.Router

Parser-monad for hashtag navigation

Public function interface

  • str
  • num
  • atom
  • components
  • locationHash
  • setMap

str

str :: RouteM String

Read a string segment.

num

num :: RouteM Int

Read a numeric segment.

atom

atom :: String -> RouteM ()

Match a string literal.

components

components :: String -> [String]

Split a uri string into its path components.

locationHash

locationHash :: IO [String]

Return a list of the slash-separated segments of the anchor portion of the current URL.

setMap

setMap :: [Route] -> IO ()

Set routing rules.


Util.Socket

WebSocket client API

Public function interface

  • newSocket
  • onMessage
  • onOpen
  • send
  • close
  • connectAnd

newSocket

newSocket :: String -> IO (Maybe Socket)
newSocket' :: PackedString -> IO (Maybe Socket)

Creates a web socket or returns Nothing if the connection attempt fails.

onMessage

onMessage :: Socket -> (String -> IO ()) -> IO ()

Event listener to handle incoming messages.

onOpen

onOpen :: Socket -> IO () -> IO ()

Event listener called after a connection is successfully opened.

send

send :: Socket -> String -> IO ()
send' :: Socket -> PackedString -> IO ()

Send a message on a socket.

close

close :: Socket -> IO ()

Close a socket.

connectAnd

connectAnd :: String -> (Socket -> IO ()) -> IO ()

Convenience function for opening a connection.

Example use:

connectAnd "ws://localhost:9160" $ \sock -> do
    onOpen sock $ do
        send sock $ "Hi!"
    onMessage sock $ \msg -> do
        -- Process incoming message here...

Util.State

Saves global application state in a hidden DOM property.

Public function interface

  • _readState
  • _setState

IsState

class IsState a where
    readState :: IO a
    setState  :: a -> IO ()
    initState :: a
    readState = _readState ""
    setState  = _setState  ""

Unless you need more than a single IsState instance, it is sufficient to implement initState to provide a default value (used when reading from an empty store).

_readState

_readState :: String -> IO a

_setState

_setState :: String -> a -> IO ()

Use these functions to reimplement setState with a unique namespace identifier if you need multiple IsState instances within a single application.

instance IsState AppState where
    initState = AppState "" []
    readState = _readState "app_state"
    setState  = _setState  "app_state"

Util.Storage

Key-value based local storage interface

Public function interface

  • set
  • get
  • deleteKey
  • index
  • flush
  • setTTL
  • getTTL
  • storageSize
  • currentBackend
  • reInit
  • storageAvailable

set

set :: (ToJSON a) => String -> a -> IO ()
set' :: (ToJSON a) => PackedString -> a -> IO ()

Saves a value to local storage.

get

get :: (FromJSON a) => String -> IO (Maybe a)
get' :: (FromJSON a) => PackedString -> IO (Maybe a)

Retrieves the stored value matching the given key, if one exists.

deleteKey

deleteKey :: String -> IO ()
deleteKey' :: PackedString -> IO ()

Removes a key from the storage.

index

index :: IO [String]
index' :: IO [PackedString]

Returns all keys currently in use as a list.

flush

flush :: IO ()

Clears the cache.

setTTL

setTTL :: String -> Int -> IO ()
setTTL' :: PackedString -> Int -> IO ()

Sets a TTL (in milliseconds) for an existing key. Use 0 or negative value to clear TTL.

getTTL

getTTL :: String -> IO Int
getTTL' :: PackedString -> IO Int

Gets remaining TTL (in milliseconds) for a key or 0 if not TTL has been set.

storageSize

storageSize :: IO Int

Returns the size of the stored data in bytes.

currentBackend

currentBackend :: IO String

Returns the storage engine currently in use or "false" if none.

reInit

reInit :: IO ()

Reloads the data from browser storage.

storageAvailable

storageAvailable :: IO Bool

Returns True if storage is available.


Util.String

Public function interface

  • pack
  • unpack

pack

pack :: String -> PackedString

Convert a Haskell String to JavaScript string format.

unpack

unpack :: PackedString -> String

Convert a JavaScript string to Haskell String format.


IRC: Join #liquidepsilon on FreeNode

© 2014 Johannes Hildén. Code licensed under BSD.