WebFudgets-0.1.0.1: Web Fudgets is a prototype library for writing web application in a functional style in Haskell.

MaintainerThomas Hallgren
StabilityExperimental
Safe HaskellNone
LanguageHaskell2010

WebFudgets

Contents

Description

Web Fudgets is a prototype library for writing web applications in a functional style in Haskell.

Web Fudgets is based on Fudgets (a Graphical User Interface Libary for Haskell developed in the early 1990s) but has a completely separate implementation on top of Haste.

Synopsis

The Fudget type

data F hi ho

F hi ho is the type of a fudget that consumes an high-level input stream of values of type hi and produces an high-level output stream of values of type ho. It can also generate a number of user interface elements, and can read input from and send output to those user interface elements.

Instances

Functor (F hi) 
Applicative (F hi) 
Monoid (F hi ho) 

Other types

type (+) a b = Either a b

We use the notation a+b for Either a b, the standard disjoint union type in Haskell.

fromLeft :: Either a b -> Maybe a

fromRight :: Either b a -> Maybe a

Running a fudget

runF :: F t t1 -> IO ()

runF is typically used only once in the main function of a program. It runs the fudget and adds any user interface elements it generates to the documentBody of the web page.

User interface elements

buttonF :: AttrValue -> F ButtonClick ButtonClick

Creates a button, like <input type="button" value="label">. It outputs BtnClick when pressed. (BtnClick received on the input is propagated directly to the output, allowing button clicks to be simulated programmatically.)

buttonGroupF :: F hi ho -> F (ButtonClick + hi) (ButtonClick + ho)

Creates a button with another fudget inside (which should be something simple, e.g. an imgF or a stringDisplayF...), <button>...</button>.

toggleButtonF :: String -> F Bool Bool

A checkboxF with a label

checkboxF :: F Bool Bool

A checkbox, <input type=checkbox>.

radioGroupF :: Eq alt => Options alt -> F alt alt

A group of checkboxes allowing you to select one of several alternatives

radioGroupF' :: Eq alt => Options alt -> F alt alt

A radioGroupF without the built-in vertical layout

selectF :: Eq alt => Options alt -> F alt alt

A menu of options, <select><option>...</option>...</select>

data Options alt

Constructors

Options [(alt, String)] alt 

dynSelectF :: Eq alt => Options alt -> SelectF alt

A menu of options that can be modified dynamically

type SelectF alt = F (ListRequest alt) alt

data ListRequest alt

Constructors

New (Options alt) 
Pick alt 

sliderF :: Enum a => (a, a) -> F a a

A slider which lets you choose a value from an enumeration, <input type="range">

progressF :: (Num i, Ord i, Show i) => i -> F i o

A progress meter <progress max=...></meter>

meterF :: (Num i, Ord i, Show i) => (i, i) -> F i o

A meter for scalar value between given minimum and maximum. <meter min=... max=...></meter>

stringDisplayF :: F String ho

An output-only element displaying text, <span>...</span>

htmlDisplayF :: F String ho

An output-only element displaying HTML content

showF :: Show i => F i o

stringF combined with show, marked read-only

numberF :: (Show a, Read a, Num a) => F a a

readShowF restricted to numbers, <input type="number">

readShowF :: (Show a, Read a) => F a a

stringF combined with show and read

stringF :: F String String

A string input/output field, <input type="text">

passwordF :: F String String

A string input/output field that shows **** instead of the actual input, <input type="password">

canvasF :: (Int, Int) -> F (Picture ()) (MouseEvent, MouseData)

Creates a canvas of given width and height. Use the Picture type from Haste.Graphics.Canvas to draw things. The canvas outputs MouseUp events (becase MouseUp seems to work for both the left and the right mouse button, while Click only works for the left mouse button).

canvasF' :: Int -> (Int, Int) -> F (Bool, Picture ()) (MouseEvent, MouseData)

canvasF with a custom scaling factor and option to render on top of or replace the current picture

imgF :: URL -> F URL o

An image, <img src="url" alt=""> You can change the image dynamically by sending in the URL of another image.

imgF' :: [Attribute] -> URL -> F URL o

An image with extra attributes, <img src="url" ...>. You can change the image dynamically by sending in the URL of another image.

alertF :: F String ho

Pops up a message that the user must acknowledge before continuing

Interaction control

focusF :: F hi ho -> F (Bool + hi) (Bool + ho)

Allows you to observe and control the focus of a fudget. (Focus determines where keyboard input goes.)

disableF :: F hi ho -> F (Bool + hi) ho

Disable and enable buttons and other input elements.

eventF :: Event a => [a] -> ((a, EventData a) -> ho) -> F t ho -> F t ho

Add event handlers to the elements generated by a fudget. MouseEvent and MouseData are re-exported from this module, other event types can be imported from Haste.Events.

Static content

textF :: String -> F hi ho

Plain text

htmlF :: String -> F hi ho

Text with HTML markup

ahrefF :: URL -> F i o -> F i o

A hyperlink <a href="url">...</a>

Layout

h1F :: F t ho -> F t ho

Level 1 header <h1>...</h1>

h2F :: F t ho -> F t ho

Level 2 header <h2>...</h2>

h3F :: F t ho -> F t ho

Level 3 header <h3>...</h3>

h4F :: F t ho -> F t ho

Level 4 header <h4>...</h4>

pF :: F t ho -> F t ho

Paragraph <p>...</p>

tableF :: Int -> F t ho -> F t ho

A table with n columns

divF :: F t ho -> F t ho

A div element <div>...</div>

boxF :: F t ho -> F t ho

A div element with a black border and some padding

ulF :: F t ho -> F t ho

Unordered list <ul>...</ul>

olF :: F t ho -> F t ho

Ordered list <ol>...</ol>

liF :: F t ho -> F t ho

List item <li>...</li>

preF :: F t ho -> F t ho

A pre element <pre>...</pre>

permuteF :: ([Elem] -> [Elem]) -> F t ho -> F t ho

Rearrange the elements generated by a fudget. Note that Elems can not be duplicated.

Traditional Fudgets compatibility

shellF :: String -> F t ho -> F t ho

With traditional Fudgets, shellF creates top-level application windows. With WebFudgets, using shellF is entierly optional. It just puts a title above another fudget and adds a couple of <div> elements that can be styled to look like a traditional application window with a title bar, if you wish. <div class="shellF"><h4>title</h4><div>...</div></div>

vBoxF :: F t ho -> F t ho

Place elements vertically

hBoxF :: F t ho -> F t ho

Place elements horizontally

Changing style and other properties

classF :: F t ho -> AttrValue -> F t ho

Set the class attribute of the elements generated by a fudget

withF :: F t ho -> [Attribute] -> F t ho

Apply attributes to the elements generated by a fudget

dynWithF :: F hi ho -> F ([Attribute] + hi) ho

Dynamically change element attributes

dynF :: F i o -> F (F i o + i) o

A fudget that can be replaced dynamically

Fudget plumbing

Parallel composition

(>+<) :: F i1 o1 -> F i2 o2 -> F (i1 + i2) (o1 + o2) infixl 5

Tagged parallel composition. Messages to/from the left fudget are tagged Left. Messages to/from the right fudget are tagged Right.

(>+) :: F t ho -> F t1 t2 -> F t ho infixl 5

Parallel composition where only the left fudget is connected. The right fudget is typically static content.

(+<) :: F t t2 -> F t1 ho -> F t1 ho infixl 5

Parallel composition where only the right fudget is connected. The left fudget is typically static content.

listF :: Eq a => [(a, F t b)] -> F (a, t) (a, b)

Tagged parallel composition of a list of fudgets

Serial composition

(=<=) :: F t ho -> F t1 t -> F t1 ho infixr 3

Right-to-left serial composition. The output stream of the right fudget is connected to the input stream of the left fudget. This was originally called >==< in Fudgets.

(=>=) :: F t t1 -> F t1 ho -> F t ho infixr 3

Left-to-right serial composition. The output stream of the left fudget is connected to the input stream of the right fudget. (This was not included in the original implementation of Fudgets. Using both =>= and =<= in the same expression might lead to confusion...)

Loops

loopLeftF :: F (loop + hi) (loop + ho) -> F hi ho

Creates a feedback loop. loopLeftF fud behaves as follows: output from fud tagged Left will be sent back to the input of fud. Output from fud tagged Right will be sent to the output of loopLeftF fud. Input to loopLeftF fud will be tagged Right and delivered to fud.

loopF :: F t t -> F t t

Copy output back to the input. The fudget needs to send on average strictly less than one output message per input message, otherwise it will become busy reacting to its own messages.

loopThroughRightF :: F (ro + hi) (ri + ho) -> F ri ro -> F hi ho

loopThroughRightF master slave is similar to loopLeftF master, but the loop goes through the slave fudget. (A better name might be encapsulateF since all communication with the slave has to go via the master, so the slave is encapsulated in this sense.)

Adding application specific functionality

Stateless

mapF :: (hi -> ho) -> F hi ho

Like map for lists. mapF f outputs f x for every x in the input stream

filterF :: (ho -> Bool) -> F ho ho

Like filter for lists. Propagates values from the input stream to the output stream if they pass a test.

mapMaybeF :: (hi -> Maybe ho) -> F hi ho

Like mapMaybe for lists. A combination of mapF and filterF.

concatMapF :: (i -> [o]) -> F i o

Like concatMap for lists. A combination of concatF and mapF.

Stateful

stateF :: (s -> hi -> (s, [ho])) -> s -> F hi ho

stateF is used to maintain an internal state. Given a state transition function f and an initial state s, stateF f s responds to input by applying f to it to update the internal state and generate zero or more output messages.

persistentStateF :: (Read s, Show s) => String -> (s -> i -> (s, [o])) -> s -> F i o

Like stateF, but also uses LocalStorage to retain the state between activations of the web application. The first argument is a key that should be unique among all web applications on the same server.

localStorageF :: (Read a, Show a) => String -> a -> F a a

Outputs one message read from LocalStorage on startup. Writes any input to LocalStorage.

Stream manipulation

putF :: ho -> F hi ho -> F hi ho

putF x fud outputs x, then behaves like fud

putsF :: [ho] -> F hi ho -> F hi ho

putF xs fud outputs xs, then behaves like fud

nullF :: F hi ho

Ignores all input. Doesn't produce any output.

idF :: F hi hi

mapF id, propagates all input directly to the output

concatF :: F [i] i

Like concat for lists, flattens a stream of lists.

toBothF :: F a (a + a)

throughF :: F ho ho -> F ho ho

splitF :: F (a, b) (a + b)

gatherF :: F (a + b) (a, b)

After the first Left a and Right b has arrived on the input, gatherF output pairs (a,b) with the most recent a and b values received.

gatherF' :: (a, b) -> F (a + b) (a, b)

gatherF with initial values.

Timing

timerF :: F (Maybe Interval) Tick

timerF outputs Tick after a specified delay or at regular intervals. The timer starts when it receives Just interval on its input. The timer stops when it receives Nothing in its input.

data Tick

Constructors

Tick 

Instances

Debugging

writeLogF :: (ho -> String) -> F ho ho

Like idF but also writes messages to the console.log

Low-level access

This reveals implementation details that might change.

initF :: CIO t -> (t -> F hi ho) -> F hi ho

ioF :: (hi -> CIO ho) -> F hi ho

modifyF :: (H ho -> [Elem] -> CIO [Elem]) -> F t ho -> F t ho

type H a = a -> CIO ()

The type of an event handler

Re-exported from Haste

type URL = String

data Attribute :: *

A key/value pair representing the value of an attribute. May represent a property, an HTML attribute, a style attribute or a list of child elements.

attr :: String -> AttrName

Create an HTML attribute name.

style :: String -> AttrName

Create a style attribute name.

prop :: String -> AttrName

Create a DOM property name. See http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html for more information about the difference between attributes and properties.

data MouseData :: *

Event data for mouse events.

Constructors

MouseData 

Fields

mouseCoords :: !(Int, Int)

Mouse coordinates.

mouseButton :: !(Maybe MouseButton)

Pressed mouse button, if any.

mouseWheelDeltas :: !(Double, Double, Double)

(x, y, z) mouse wheel delta. Always all zeroes except for Wheel.

data Interval :: *

Interval and repeat for timers.

Constructors

Once !Int

Fire once, in n milliseconds.

Repeat !Int

Fire every n milliseconds.

Other utilities

chop :: Int -> [t] -> [[t]]

readM :: Read a => String -> Maybe a