addScripts :: [String] -> IO ()
Used to dynamically add external JavaScript files to your document.
addScript :: String -> IO ()
Used to dynamically add an external JavaScript file to your document.
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.
Low-level DOM manipulation API
createElement :: String -> IO Element
createElement' :: PackedString -> IO Element
Create an element with the specified name.
setInnerHtml :: Element -> String -> IO Element
setInnerHtml' :: Element -> PackedString -> IO Element
Set the inner HTML of an element.
documentWrite :: String -> IO ()
documentWrite' :: PackedString -> IO ()
Writes HTML expressions or JavaScript code to a document.
documentBody :: IO Element
Retrieve the document body element.
window :: IO Element
Retrieve the window element.
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 :: 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 :: Element -- ^ An element
-> String -- ^ Attribute name
-> IO ()
removeAttribute' :: Element -> PackedString -> IO ()
Remove an attribute from the element.
setValue :: Element -> String -> IO ()
setValue' :: Element -> PackedString -> IO ()
Set the value attribute of an element (typically an input field).
toString :: Element -> IO String
toString' :: Element -> IO PackedString
Stringify an element.
innerHtml :: Element -> IO String
innerHtml' :: Element -> IO PackedString
Retrieve the HTML between the start and end tags of the object.
createTextNode :: String -> IO Element
createTextNode' :: PackedString -> IO Element
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 :: 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 :: String -> IO [Element]
getElementsByClassName' :: PackedString -> IO [Element]
Return a list of elements which matches the provided class name(s).
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 :: Element -- ^ The target element
-> IO () -- ^ Callback action
-> IO ()
Add an onClick event listener to the provided element.
setStyleProperty :: Element -> String -> String -> IO ()
setStyleProperty' :: Element -> PackedString ->
Set a property on an element's style object.
Functional reactive programming library
attach :: Signal (IO a) -> IO ()
_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 :: 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 :: 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 :: 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 :: Element -> Signal String
inputValue' :: Element -> Signal PackedString
Creates a signal for the value of an input field.
clickSignal :: Element -> Signal ()
A signal that reacts to click events on an element.
mouseSignal :: Signal (Int, Int)
Mouse cursor position signal.
scrollXSignal :: Signal Int
Window scroll X position signal.
scrollYSignal :: Signal Int
Window scroll Y position signal.
HTML combinator library
type Html = HtmlM ()
renderHtml :: HtmlM t -> String
escapeHtmlEntities :: String -> String
attribute :: String -> String -> String -> Attribute
empty :: Html
parent :: String -> String -> String -> String -> Html -> Html
leaf :: String -> String -> String -> Html
toHtml :: String -> HtmlM a
makeAttr :: String -> String -> Attribute
makePar :: String -> Html -> Html
makeLeaf :: String -> Html
($<) :: (Html -> Html) -> String -> Html
handlebar :: String -> Html
each :: String -> Html -> Html
($?) :: (Html -> Html) -> String -> Html
renderWithHtml :: (ToJSON a) => Html -> a -> IO String
PackedString
versions of HTML combinators. Has identical API to Util.HTML.
compile :: forall a. String -> IO (Ptr a -> IO PackedString)
render :: (ToJSON a) => (Ptr a -> IO PackedString) -> a -> IO String
render' :: (ToJSON a) => (Ptr a -> IO PackedString) -> a -> IO PackedString
newtype Collection a = Collection [a]
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 :: Parser Value
mapFrom :: (FromJSON a) => Value -> Maybe [a]
(.!) :: (FromJSON a) => Object -> String -> Maybe a
mapTo :: (ToJSON a) => [a] -> Value
obj :: [(String, Value)] -> Value
(|=) :: ToJSON a => String -> a -> (String, Value)
stringify :: Object -> String
valueToStr :: Value -> String
toStr :: (ToJSON a) => a -> String
fromStr :: (FromJSON a) => String -> Maybe a
Parser-monad for hashtag navigation
str :: RouteM String
Read a string segment.
num :: RouteM Int
Read a numeric segment.
atom :: String -> RouteM ()
Match a string literal.
components :: String -> [String]
Split a uri string into its path components.
locationHash :: IO [String]
Return a list of the slash-separated segments of the anchor portion of the current URL.
setMap :: [Route] -> IO ()
Set routing rules.
WebSocket client API
newSocket :: String -> IO (Maybe Socket)
newSocket' :: PackedString -> IO (Maybe Socket)
Creates a web socket or returns Nothing if the connection attempt fails.
onMessage :: Socket -> (String -> IO ()) -> IO ()
Event listener to handle incoming messages.
onOpen :: Socket -> IO () -> IO ()
Event listener called after a connection is successfully opened.
send :: Socket -> String -> IO ()
send' :: Socket -> PackedString -> IO ()
Send a message on a socket.
close :: Socket -> IO ()
Close a socket.
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...
Saves global application state in a hidden DOM property.
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 :: String -> IO a
_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"
Key-value based local storage interface
set :: (ToJSON a) => String -> a -> IO ()
set' :: (ToJSON a) => PackedString -> a -> IO ()
Saves a value to local storage.
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 :: String -> IO ()
deleteKey' :: PackedString -> IO ()
Removes a key from the storage.
index :: IO [String]
index' :: IO [PackedString]
Returns all keys currently in use as a list.
flush :: IO ()
Clears the cache.
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 :: String -> IO Int
getTTL' :: PackedString -> IO Int
Gets remaining TTL (in milliseconds) for a key or 0 if not TTL has been set.
storageSize :: IO Int
Returns the size of the stored data in bytes.
currentBackend :: IO String
Returns the storage engine currently in use or
"false"
if none.
reInit :: IO ()
Reloads the data from browser storage.
storageAvailable :: IO Bool
Returns True
if storage is available.
pack :: String -> PackedString
Convert a Haskell String
to JavaScript string
format.
unpack :: PackedString -> String
Convert a JavaScript string to Haskell String
format.
© 2014 Johannes Hildén. Code licensed under BSD.