haste-perch 0.1.0.3 → 0.1.0.4
raw patch · 4 files changed
+139/−105 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Haste.Perch: instance [overlap ok] Typeable1 PerchM
+ Haste.Perch: forElems :: String -> Perch -> Perch
+ Haste.Perch: forElems' :: String -> Perch -> IO ()
+ Haste.Perch: instance [overlap ok] Typeable PerchM
Files
- Haste/Perch.hs +86/−55
- Main.hs +20/−22
- README.md +5/−1
- haste-perch.cabal +28/−27
Haste/Perch.hs view
@@ -16,18 +16,18 @@ , OverloadedStrings, DeriveDataTypeable, UndecidableInstances , OverlappingInstances #-} module Haste.Perch where -import Data.Typeable +import Data.Typeable import Haste -import Haste.DOM+import Haste.DOM import Haste.Foreign import Data.Maybe import Data.Monoid import Unsafe.Coerce import Data.String import Control.Monad.IO.Class - + newtype PerchM a= Perch{build :: Elem -> IO Elem} deriving Typeable type Perch = PerchM () @@ -43,10 +43,10 @@ (>>) x y= mappend (unsafeCoerce x) y (>>=) = error "bind (>>=) invocation creating DOM elements" return = mempty --instance MonadIO PerchM where- liftIO mx= Perch $ \e -> mx >> return e- + +instance MonadIO PerchM where + liftIO mx= Perch $ \e -> mx >> return e + instance IsString Perch where fromString= toElem @@ -54,7 +54,7 @@ toElem :: a -> Perch instance ToElem String where - toElem s= Perch $ \e ->do + toElem s= Perch $ \e -> do e' <- newTextElem s addChild e' e return e' @@ -80,17 +80,17 @@ let t = toElem ch r <- build t e return e --setHtml :: Perch -> String -> Perch+ +setHtml :: Perch -> String -> Perch setHtml me text= Perch $ \e' -> do - e <- build me e'- inner e text- return e'- where- inner :: Elem -> String -> IO ()- inner e txt = setProp e "innerHTML" txt-+ e <- build me e' + inner e text + return e' + where + inner :: Elem -> String -> IO () + inner e txt = setProp e "innerHTML" txt + addEvent :: Perch -> Event IO a -> a -> Perch addEvent be event action= Perch $ \e -> do e' <- build be e @@ -99,7 +99,7 @@ case has of "true" -> return e' _ -> do - onEvent e' event action + onEvent e' event action setAttr e' atr "true" return e' @@ -125,7 +125,7 @@ wbr = nelem "wbr" -- Parent DOM nodes ---+-- a cont = nelem "a" `child` cont abbr cont = nelem "abbr" `child` cont @@ -251,39 +251,70 @@ href= atr "href" src= atr "src" ------------------- DOM Tree navigation---- | return the current node-this :: Perch-this= Perch $ \e -> return e---- | goes to the parent node of the first and execute the second-goParent :: Perch -> Perch -> Perch-goParent pe pe'= Perch $ \e' -> do- e <- build pe e'- p <- parent e- e2 <- build pe' p- return e2---- | delete the current node. Return the parent-delete :: Perch-delete= Perch $ \e -> do- p <- parent e- removeChild e p- return p---- | delete the children of the current node.-clear :: Perch-clear= Perch $ \e -> clearChildren e >> return e---parent :: Elem -> IO Elem-parent= ffi "(function(e){return e.parentNode;})"---getBody :: IO Elem-getBody= ffi "(function(){return document.body;})"--+ + +---------------- DOM Tree navigation + +-- | return the current node +this :: Perch +this= Perch $ \e -> return e + +-- | goes to the parent node of the first and execute the second +goParent :: Perch -> Perch -> Perch +goParent pe pe'= Perch $ \e' -> do + e <- build pe e' + p <- parent e + e2 <- build pe' p + return e2 + +-- | delete the current node. Return the parent +delete :: Perch +delete= Perch $ \e -> do + p <- parent e + removeChild e p + return p + +-- | delete the children of the current node. +clear :: Perch +clear= Perch $ \e -> clearChildren e >> return e + + +parent :: Elem -> IO Elem +parent= ffi "(function(e){return e.parentNode;})" + + +getBody :: IO Elem +getBody= ffi "(function(){return document.body;})" + + + +-- ! JQuery-like DOM manipulation: using a selector for querySelectorAll, +-- it apply the Perch DOM manipulation of the second parameter for each of the matches +-- +-- Example +-- +-- > main= do +-- > body <- getBody +-- > (flip build) body $ pre $ do +-- > div ! atr "class" "modify" $ "click" +-- > div $ "not changed" +-- > div ! atr "class" "modify" $ "here" +-- > +-- > addEvent this OnClick $ \_ _ -> do +-- > forElems' ".modify" $ this ! style "color:red" +forElems' :: String -> Perch -> IO () +forElems' for doit= do + (flip build) undefined (forElems for doit) + return () + +-- ! JQuery-like DOM manipulation: using a selector for querySelectorAll, +-- it apply the Perch DOM manipulation of the second parameter for each of the matches +forElems :: String -> Perch -> Perch +forElems selectors dosomething= Perch $ \e -> do + es <- queryAll selectors + mapM (build dosomething) es + return e + where + queryAll :: String -> IO [Elem] + queryAll = ffi "(function(sel){return document.querySelectorAll(sel);})" +
Main.hs view
@@ -1,22 +1,20 @@-module Main where-import Haste.DOM-import Haste.Perch-import Haste.Perch.Literate-import Data.Monoid-import Prelude hiding (div)----main= do- lit "hello <b>world<b>"- body <- getBody- build' body $ do- div $ do- div $ do- p "hello"- p ! atr "style" "color:red" $ "world"-- return ()-- where- build'= flip build+module Main where +import Haste.DOM +import Haste.Perch +import Data.Monoid +import Prelude hiding (div) + + + +main= do + body <- getBody + build' body $ do + div $ do + div $ do + p "hello" + p ! atr "style" "color:red" $ "world" + + return () + + where + build'= flip build
README.md view
@@ -86,9 +86,13 @@ How it works ------------ - The basic element is a "builder" that has a "hole" parameter and a IO action about what element will be created. The hole will receive the parent (Elem) of the element/s that will be created by the builder. So a builder can be considered like a perch that has other perchs that hang from it. either a single element or an entire tree. the call `nelem` (new element) is a perch that creates a single tag element. Upon created, it is added to the parent and return itself as parent of the next elements that can be hooked. To append two elements, both are added to the parent. The Monad instance is there in order to use the do notation, that add a new level of syntax, in the style of the package blaze-html. This monad invokes the same appending mechanism. ++A perch is a generalization of a list and it is handled in the same way;+while a list is an unary tree, perch create n-ary trees. While in a list the monoid instance add child nodes+down in the only direction that it can grow, the perch monoid add childs at the same level, horizontally.+Is the `child` primitive the one that creates branches down.
haste-perch.cabal view
@@ -1,42 +1,43 @@ name: haste-perch -version: 0.1.0.3 -synopsis: Create, navigate and modify the DOM tree with composable syntax, with the haste compiler -description: see the description at the homepage.- +version: 0.1.0.4 +synopsis: Create, navigate and modify the DOM tree with composable syntax, with the haste compiler +description: see the description at the homepage. + This version add JQuery-like DOM manipulation + homepage: https://github.com/agocorona/haste-perch license: GPL-3 license-file: LICENSE -author: Alberto G. Corona +author: Alberto G. Corona maintainer: agocorona@gmail.com bug-reports: https://github.com/agocorona/haste-perch/issues category: Web build-type: Simple extra-source-files: README.md, Main.hs -cabal-version: >=1.10+cabal-version: >=1.10 extra-source-files: Main.hs, Main.html --source-repository head- type: git- location: http://github.com/agocorona/haste-perch--Flag Haste-inst- Description: either if it is being compiled with haste-inst or with cabal- Default: False--library - exposed-modules: Haste.Perch-+source-repository head + type: git + location: http://github.com/agocorona/haste-perch + +Flag Haste-inst + Description: either if it is being compiled with haste-inst or with cabal + Default: False + +library + + exposed-modules: Haste.Perch + if flag(haste-inst) - build-depends: base >4.0 && <5,- transformers,- haste-lib- else- build-depends: base >4.0 && <5,- transformers,- haste-compiler- - -- hs-source-dirs: + build-depends: base >4.0 && <5, + transformers, + haste-lib + else + build-depends: base >4.0 && <5, + transformers, + haste-compiler + + -- hs-source-dirs: default-language: Haskell2010