Skip to content

Commit

Permalink
Rename 'Property' to 'Attr' to bring this in line with Gtk2Hs and wxH…
Browse files Browse the repository at this point in the history
…askell. Clean up read-only and write-only attributes to be more type safe. #24

http://hackage.haskell.org/packages/archive/glib/0.12.4/doc/html/System-Glib-Attributes.html
  • Loading branch information
HeinrichApfelmus committed May 13, 2013
1 parent 437ddf5 commit 8831818
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/Graphics/UI/Threepenny.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Graphics.UI.Threepenny (
module Graphics.UI.Threepenny.Attributes,
module Graphics.UI.Threepenny.Core,
module Graphics.UI.Threepenny.Elements,
module Graphics.UI.Threepenny.Events,
module Graphics.UI.Threepenny.Properties,
module Graphics.UI.Threepenny.JQuery,
) where

import Graphics.UI.Threepenny.Attributes
import Graphics.UI.Threepenny.Core
import Graphics.UI.Threepenny.Elements
import Graphics.UI.Threepenny.Events
import Graphics.UI.Threepenny.Properties
import Graphics.UI.Threepenny.JQuery
94 changes: 53 additions & 41 deletions src/Graphics/UI/Threepenny/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ module Graphics.UI.Threepenny.Core (
EventData(..), domEvent, on,
module Control.Event,

-- * Properties
-- | For a list of predefined properties, see "Graphics.UI.Threepenny.Properties".
-- * Attributes
-- | For a list of predefined attributes, see "Graphics.UI.Threepenny.Attributes".
(#), element,
Property(..), WriteOnlyProperty, ReadOnlyProperty,
mkProperty, set, get,
Attr, WriteAttr, ReadAttr, ReadWriteAttr(..),
set, get, mkReadWriteAttr, mkWriteAttr, mkReadAttr,

-- * Utilities
-- | A few raw JavaScript utilities.
Expand Down Expand Up @@ -105,16 +105,16 @@ startGUI config handler =
Manipulate DOM
------------------------------------------------------------------------------}
-- | Title of the client window.
title :: WriteOnlyProperty Window String
title = mkProperty Core.setTitle undefined
title :: WriteAttr Window String
title = mkWriteAttr Core.setTitle

-- | Cookies on the client.
cookies :: ReadOnlyProperty Window [(String,String)]
cookies = mkProperty undefined Core.getRequestCookies
cookies :: ReadAttr Window [(String,String)]
cookies = mkReadAttr Core.getRequestCookies

-- | Child elements of a given element.
children :: WriteOnlyProperty Element [Element]
children = mkProperty set undefined
children :: WriteAttr Element [Element]
children = mkWriteAttr set
where
set xs x = do
Core.emptyEl x
Expand All @@ -127,21 +127,21 @@ appendTo :: Element -- ^ Parent.
appendTo x my = do { y <- my; Core.appendElementTo x y; }

-- | Child elements of a given element as a HTML string.
html :: WriteOnlyProperty Element String
html = mkProperty (\v a -> Core.setHtml v a # void) undefined
html :: WriteAttr Element String
html = mkWriteAttr (\i x -> Core.setHtml i x # void)

-- | Attributes of an element.
attr :: String -> WriteOnlyProperty Element String
attr name = mkProperty (\v a -> Core.setAttr name v a # void) undefined
-- | HTML attributes of an element.
attr :: String -> WriteAttr Element String
attr name = mkWriteAttr (\i x -> Core.setAttr name i x # void)

-- | Value attribute of an element.
-- Particularly relevant for control widgets like 'input'.
value :: Property Element String
value = mkProperty (set' $ attr "value") getValue
value :: Attr Element String
value = mkReadWriteAttr getValue (set' $ attr "value")

-- | Text content of an element.
text :: WriteOnlyProperty Element String
text = mkProperty (\v a -> Core.setText v a # void) undefined
text :: WriteAttr Element String
text = mkWriteAttr (\i x -> Core.setText i x # void)

{-----------------------------------------------------------------------------
Create DOM
Expand Down Expand Up @@ -238,7 +238,7 @@ on f x h = void $ register (f x) (void . h)


{-----------------------------------------------------------------------------
Properties
Attributes
------------------------------------------------------------------------------}
infixl 8 #

Expand All @@ -254,7 +254,7 @@ infixl 8 #
(#) :: a -> (a -> b) -> b
(#) = flip ($)

-- | Convience synonym for 'return' to make elements work well with 'set'.
-- | Convience synonym for 'return' to make elements work well with 'set'
-- and with the 'Dom' monad.
--
-- Example usage.
Expand All @@ -264,31 +264,43 @@ infixl 8 #
element :: Monad m => Element -> m Element
element = return

-- | Properties are things that you can set and get.
data Property a value = Property
{ set' :: value -> a -> IO ()
, get' :: a -> IO value
}

-- | Properties that only support the 'get' operation.
type ReadOnlyProperty = Property
-- | Attributes can be 'set' and 'get'.
type Attr x a = ReadWriteAttr x a a

-- | Attribute that only supports the 'get' operation.
type ReadAttr x o = ReadWriteAttr x () o

-- | Attribute that only supports the 'set' operation.
type WriteAttr x i = ReadWriteAttr x i ()

-- | Properties that only support the 'set' operation.
type WriteOnlyProperty = Property
-- | Generalized attribute with different types for getting and setting.
data ReadWriteAttr x i o = ReadWriteAttr
{ get' :: x -> IO o
, set' :: i -> x -> IO ()
}

-- | Set value of a property in the 'IO' monad.
-- | Set value of an attribute in the 'IO' monad.
-- Best used in conjunction with '#'.
set :: Property a value -> value -> IO a -> IO a
set prop value ma = do { a <- ma; set' prop value a; return a; }
set :: ReadWriteAttr x i o -> i -> IO x -> IO x
set attr i mx = do { x <- mx; set' attr i x; return x; }

-- | Get property value.
get :: Property a value -> a -> IO value
-- | Get attribute value.
get :: ReadWriteAttr x i o -> x -> IO o
get = get'

-- | Build a property from a getter and a setter.
mkProperty
:: (value -> a -> IO ()) -- ^ Setter.
-> (a -> IO value) -- ^ Getter.
-> Property a value
mkProperty = Property
-- | Build an attribute from a getter and a setter.
mkReadWriteAttr
:: (x -> IO o) -- ^ Getter.
-> (i -> x -> IO ()) -- ^ Setter.
-> ReadWriteAttr x i o
mkReadWriteAttr get set = ReadWriteAttr { get' = get, set' = set }

-- | Build attribute from a getter.
mkReadAttr :: (x -> IO o) -> ReadAttr x o
mkReadAttr get = mkReadWriteAttr get (\_ _ -> return ())

-- | Build attribute from a setter.
mkWriteAttr :: (i -> x -> IO ()) -> WriteAttr x i
mkWriteAttr set = mkReadWriteAttr (\_ -> return ()) set

2 changes: 1 addition & 1 deletion threepenny-gui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ Library
Exposed-modules:
Control.Event
,Graphics.UI.Threepenny
,Graphics.UI.Threepenny.Attributes
,Graphics.UI.Threepenny.Core
,Graphics.UI.Threepenny.Elements
,Graphics.UI.Threepenny.Events
,Graphics.UI.Threepenny.JQuery
,Graphics.UI.Threepenny.Properties
Other-modules:
Control.Concurrent.Chan.Extra
,Control.Monad.Extra
Expand Down

0 comments on commit 8831818

Please sign in to comment.