text-and-plots-0.2.1.0: EDSL to create HTML documents with plots based on the C3.js library.

Safe HaskellNone
LanguageHaskell2010

Text.DocL.Javascript

Contents

Synopsis

Rationale

This is basically a JSON encoding library, however it goes beyond JSON by allowing raw javascript to be used as a value. This allows, for instance, function calls and anonymous functions to be placed in the data when used in a javascript context. This is needed to expose some options in C3.js, e.g. customizing tick formatting.

The Obj type

data Obj Source

Represents a javascript object or scalar value.

Instances

Show Obj

show x returns x encoded.

IsString Obj 
ToObj Obj

ToObj is the identity for an Obj value.

class ToObj a where Source

Class of types that can be converted to an Obj.

Minimal complete definition

toObj

Methods

toObj :: a -> Obj Source

Converts to an Obj.

toObjList :: [a] -> Obj Source

Only for writing instances. This function serves the same role as showList

Instances

ToObj Bool 
ToObj Char

A String becomes a javascript string literal.

ToObj Double 
ToObj Int 
ToObj Integer 
ToObj ()

Becomes a javascript null object

ToObj Text

Becomes a javascript string literal.

ToObj Obj

ToObj is the identity for an Obj value.

ToObj Prop

A [Prop] becomes a javascript object.

ToObj a => ToObj [a]

Becomes a javascript array.

Constructing javascript types

data Prop Source

A key/value pair created with the /:-operator.

A list of type [Prop] can be converted to an object using toObj. If the list contains duplicate keys, then merge is used to combine the values.

>>> toObj ["legs" /: 4, "ears" /: "floppy"]
{"ears":"floppy","legs":4}

Instances

ToObj Prop

A [Prop] becomes a javascript object.

(/:) :: ToObj a => String -> a -> Prop Source

Constructs a Prop.

>>> toObj ["tail" /: True, "nose" /: "wet"]
{"nose":"wet","tail":true}

javascript :: String -> Obj Source

Raw unescaped javascript.

>>> toObj ["speak" /: javascript "function(){alert('Woof!');}"]
{"speak":function(){alert('Woof!');}}

Encoding

encode :: Obj -> Text Source

Encode an Obj to a lazy Text.

Miscellanea

merge :: Obj -> Obj -> Obj Source

merge x y returns y unless both x and y are javascript objects, in which case the properties of the two are merged using merge recursively for duplicate keys.