Safe Haskell | None |
---|
- data Config = Config {}
- startGUI :: Config -> (Window -> IO ()) -> IO ()
- loadFile :: Window -> String -> FilePath -> IO String
- loadDirectory :: Window -> FilePath -> IO String
- type Window = Session
- title :: WriteAttr Window String
- cookies :: ReadAttr Window [(String, String)]
- getRequestLocation :: Window -> IO URI
- data Element
- mkElement :: String -> IO Element
- getWindow :: Element -> IO (Maybe Window)
- delete :: Element -> IO ()
- (#+) :: IO Element -> [IO Element] -> IO Element
- string :: String -> IO Element
- getHead :: Window -> IO Element
- getBody :: Window -> IO Element
- children :: WriteAttr Element [Element]
- text :: WriteAttr Element String
- html :: WriteAttr Element String
- attr :: String -> WriteAttr Element String
- style :: WriteAttr Element [(String, String)]
- value :: Attr Element String
- getValuesList :: [Element] -> IO [String]
- getElementsByTagName :: Window -> String -> IO [Element]
- getElementByTagName :: Window -> String -> IO (Maybe Element)
- getElementsById :: Window -> [String] -> IO [Element]
- getElementById :: Window -> String -> IO (Maybe Element)
- grid :: [[IO Element]] -> IO Element
- row :: [IO Element] -> IO Element
- column :: [IO Element] -> IO Element
- data EventData = EventData [Maybe String]
- domEvent :: String -> Element -> Event EventData
- on :: (element -> Event a) -> element -> (a -> IO void) -> IO ()
- module Control.Event
- (#) :: a -> (a -> b) -> b
- (#.) :: IO Element -> String -> IO Element
- element :: Element -> IO Element
- type Attr x a = ReadWriteAttr x a a
- type WriteAttr x i = ReadWriteAttr x i ()
- type ReadAttr x o = ReadWriteAttr x () o
- data ReadWriteAttr x i o = ReadWriteAttr {}
- set :: MonadIO m => ReadWriteAttr x i o -> i -> m x -> m x
- get :: ReadWriteAttr x i o -> x -> IO o
- mkReadWriteAttr :: (x -> IO o) -> (i -> x -> IO ()) -> ReadWriteAttr x i o
- mkWriteAttr :: (i -> x -> IO ()) -> WriteAttr x i
- mkReadAttr :: (x -> IO o) -> ReadAttr x o
- debug :: Window -> String -> IO ()
- clear :: Window -> IO ()
- class ToJS a
- class FFI a
- ffi :: FFI a => String -> a
- data JSFunction a
- runFunction :: Window -> JSFunction () -> IO ()
- callFunction :: Window -> JSFunction a -> IO a
- callDeferredFunction :: Window -> String -> [String] -> ([Maybe String] -> IO ()) -> IO ()
- atomic :: Window -> IO a -> IO a
- updateElement :: (Element -> IO ()) -> Element -> IO ()
- manifestElement :: Window -> Element -> IO Element
- audioPlay :: Element -> IO ()
- fromProp :: String -> (JSValue -> a) -> (a -> JSValue) -> Attr Element a
Guide
Threepenny runs a small web server that displays the user interface
as a web page to any browser that connects to it.
To start the web server, use the startGUI
function.
Creating of DOM elements is easy, the '(#+)' combinator allows a style similar to HTML combinator libraries.
Existing DOM elements can be accessed much in the same way they are accessed from JavaScript; they can be searched, updated, moved and inspected. Events can be bound to DOM elements and handled.
Applications written in Threepenny are multithreaded. Each client (user) has a separate thread which runs with no awareness of the asynchronous protocol below. Each session should only be accessed from one thread. There is not yet any clever architecture for accessing the (single threaded) web browser from multi-threaded Haskell. That's my recommendation. You can choose to ignore it, but don't blame me when you run an element search and you get a click event as a result.
This project was originally called Ji.
Server
To display the user interface, you have to start a server using startGUI
.
Then, visit the URL http://localhost:10000/ in your browser
(assuming that you have set the port number to tpPort=10000
in the server configuration).
Record for configuring the Threepenny GUI server.
:: Config | Server configuration. |
-> (Window -> IO ()) | Action to run whenever a client browser connects. |
-> IO () |
Start server for GUI sessions.
:: Window | Browser window |
-> String | MIME type |
-> FilePath | Local path to the file |
-> IO String | Generated URI |
Make a local file available as a relative URI.
loadDirectory :: Window -> FilePath -> IO StringSource
Make a local directory available as a relative URI.
Browser Window
getRequestLocation :: Window -> IO URISource
Get the request location.
DOM elements
Create and manipulate DOM elements.
getWindow :: Element -> IO (Maybe Window)Source
Retreive the browser Window
in which the element resides.
Note that elements do not reside in any browser window when they are first created. To move the element to a particular browser window, you have to append it to a parent, for instance with the `(#+)` operator.
WARNING: The ability to move elements from one browser window to another is currently not implemented yet.
(#+) :: IO Element -> [IO Element] -> IO ElementSource
Append DOM elements as children to a given element.
value :: Attr Element StringSource
Value attribute of an element.
Particularly relevant for control widgets like input
.
Get values from inputs. Blocks. This is faster than many getValue
invocations.
Get all elements of the given tag name. Blocks.
:: Window | Browser window |
-> String | The tag name. |
-> IO (Maybe Element) | An element (if any) with that tag name. |
Get an element by its tag name. Blocks.
Get a list of elements by particular IDs. Blocks.
:: Window | Browser window |
-> String | The ID string. |
-> IO (Maybe Element) | Element (if any) with given ID. |
Get an element by a particular ID. Blocks.
Layout
Combinators for quickly creating layouts. They can be adjusted with CSS later on.
grid :: [[IO Element]] -> IO ElementSource
Align given elements in a rectangular grid.
Layout is achieved by using the CSS display:table
property.
The following element tree will be generated
<div class="table"> <div class="table-row"> <div class="table-cell"> ... </div> <div class="table-cell"> ... </div> </div> <div class="table-row"> ... </div> ... </div>
You can customatize the actual layout by assigning an id
to the element
and changing the .table
, .table-row
and table-column
classes in a custom CSS file.
Events
For a list of predefined events, see Graphics.UI.Threepenny.Events.
:: String | Event name. A full list can be found at
http://www.w3schools.com/jsref/dom_obj_event.asp.
Note that the |
-> Element | Element where the event is to occur. |
-> Event EventData |
Obtain DOM event for a given element.
module Control.Event
Attributes
For a list of predefined attributes, see Graphics.UI.Threepenny.Attributes.
(#) :: a -> (a -> b) -> bSource
Reverse function application. Allows convenient notation for setting properties.
Example usage.
mkElement "div" # set style [("color","#CCAABB")] # set draggable True # set children otherElements
(#.) :: IO Element -> String -> IO ElementSource
Convenient combinator for setting the CSS class on element creation.
type Attr x a = ReadWriteAttr x a aSource
type WriteAttr x i = ReadWriteAttr x i ()Source
Attribute that only supports the set
operation.
type ReadAttr x o = ReadWriteAttr x () oSource
Attribute that only supports the get
operation.
data ReadWriteAttr x i o Source
Generalized attribute with different types for getting and setting.
set :: MonadIO m => ReadWriteAttr x i o -> i -> m x -> m xSource
Set value of an attribute in the IO
monad.
Best used in conjunction with '#'.
get :: ReadWriteAttr x i o -> x -> IO oSource
Get attribute value.
:: (x -> IO o) | Getter. |
-> (i -> x -> IO ()) | Setter. |
-> ReadWriteAttr x i o |
Build an attribute from a getter and a setter.
mkWriteAttr :: (i -> x -> IO ()) -> WriteAttr x iSource
Build attribute from a setter.
mkReadAttr :: (x -> IO o) -> ReadAttr x oSource
Build attribute from a getter.
JavaScript FFI
Direct interface to JavaScript in the browser window.
Send a debug message to the client. The behaviour of the client is unspecified.
Class for rendering Haskell types as JavaScript code.
Helper class for making a simple JavaScript FFI
FFI (JSFunction String) | |
FFI (JSFunction ()) | |
FFI (JSFunction JSValue) | |
(ToJS a, FFI b) => FFI (a -> b) |
ffi :: FFI a => String -> aSource
Simple JavaScript FFI with string substitution.
Inspired by the Fay language. http://fay-lang.org/
example :: String -> Int -> JSFunction String example = ffi "$(%1).prop('checked',%2)"
The ffi
function takes a string argument representing the JavaScript
code to be executed on the client.
Occurrences of the substrings %1
to %9
will be replaced by
subequent arguments.
Note: Always specify a type signature! The types automate
how values are marshalled between Haskell and JavaScript.
The class instances for the FFI
class show which conversions are supported.
data JSFunction a Source
Representation of a JavaScript expression with a girven output type.
Functor JSFunction | |
FFI (JSFunction String) | |
FFI (JSFunction ()) | |
FFI (JSFunction JSValue) |
runFunction :: Window -> JSFunction () -> IO ()Source
Run the given JavaScript function and carry on. Doesn't block.
The client window uses JavaScript's eval()
function to run the code.
callFunction :: Window -> JSFunction a -> IO aSource
Run the given JavaScript function and wait for results. Blocks.
The client window uses JavaScript's eval()
function to run the code.
:: Window | Browser window |
-> String | The function name. |
-> [String] | Parameters. |
-> ([Maybe String] -> IO ()) | The continuation to call if/when the function completes. |
-> IO () |
Call the given function with the given continuation. Doesn't block.
atomic :: Window -> IO a -> IO aSource
Atomically execute the given computation in the context of a browser window
Internal and oddball functions
updateElement :: (Element -> IO ()) -> Element -> IO ()Source
manifestElement :: Window -> Element -> IO ElementSource