packages feed

haste-perch (empty) → 0.1.0.0

raw patch · 7 files changed

+272/−0 lines, 7 filesdep +basedep +haste-compilerdep +mtlsetup-changed

Dependencies added: base, haste-compiler, mtl

Files

+ Haste/Perch.hs view
@@ -0,0 +1,105 @@+-----------------------------------------------------------------------------+--+-- Module      :  Builder+-- Copyright   :+-- License     :  BSD3+--+-- Maintainer  :  agocorona@gmail.com+-- Stability   :  experimental+-- Portability :+--+-- | Monad and Monoid instances for a builder that hang DOM elements from the+-- current parent element. It uses Haste.DOM from the haste-compiler+--+-----------------------------------------------------------------------------+{-#LANGUAGE TypeSynonymInstances, FlexibleInstances+            , OverloadedStrings, DeriveDataTypeable, UndecidableInstances+            , OverlappingInstances #-}+module Haste.Perch where+import Data.Typeable+import Haste+import Haste.DOM+import Haste.Foreign(ffi)+import Data.Maybe+import Data.Monoid+import Unsafe.Coerce++newtype PerchM a= Perch{build :: Elem -> IO Elem} deriving Typeable++type Perch = PerchM ()++instance Monoid (PerchM a) where+    mappend mx my= Perch $ \e -> do+         build mx e+         build my e+         return e+    mempty  = Perch return++instance Monad PerchM where+   (>>) x y= mappend (unsafeCoerce x) y+   (>>=) = error "bind (>>=) invocation creating DOM elements"+   return  = mempty++++class ToElem a where+  toElem :: a -> Perch++instance ToElem String where+   toElem s= Perch $ \e ->do+        e' <- newTextElem s+        addChild e' e+        return e'++instance Show a => ToElem a where toElem = toElem . show++instance ToElem (PerchM a) where toElem e = unsafeCoerce e++attr tag (n, v)=Perch $ \e -> do+        tag' <- build tag e+        setAttr tag' n v+        return tag'++nelem s= Perch $ \e ->do+        e' <- newElem s+        addChild e' e+        return e'++child :: ToElem a => Perch -> a -> Perch+child me ch= Perch $ \e' -> do+        e <- build me e'+        let t = toElem ch+        r <- build t e+        return e++addEvent :: Perch -> Event IO b -> IO () -> Perch+addEvent be event action= Perch $ \e -> do+     e' <- build be e+     has <- getAttr e' "hasevent"+     case has of+       "true" -> return e'+       _ -> do+        onEvent e' event $ unsafeCoerce $ action -- >> focus e+        setAttr e' "hasevent" "true"+        return e'++elemsByTagName :: String -> IO [Elem]+elemsByTagName = ffi "(function(s){document.getElementsByTagName(s)})"++parent :: Elem -> IO Elem+parent= ffi "(function(e){return e.parentNode;})"++br= nelem "br"+++div cont=  nelem "div" `child`  cont++p cont = nelem "p" `child` cont++b cont = nelem "b" `child` cont+++(!) pe atrib = \e ->  pe e `attr` atrib++atr n v= (n,v)+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Alberto G. Corona
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Alberto G. Corona nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Main.hs view
@@ -0,0 +1,16 @@+module Main where+import Haste.DOM+import Haste.Perch+import Control.Monad.State+import Data.Monoid+import Prelude hiding (div)++main= do+  withElem "idelem" $   build $ do+    div $ do+         div $ do+               p "hello"+               p ! atr "style" "color:red" $   "world"++  return ()+
+ Main.html view
@@ -0,0 +1,8 @@+<!DOCTYPE HTML>
+<html>
+
+<head><title>Main.html</title><meta charset="UTF-8">
+
+<script type="text/javascript" src="Main.js" ></script>
+
+</head><body><div id="idelem"></div></body></html>
+ README.md view
@@ -0,0 +1,73 @@+haskell-js-html-builder
+=================
+
+A monad and monoid instance for Haste.DOM elements. It can be ported to other haskell-js compilers
+
+Haste is a compiler that generates Javascript code from Haskell.
+
+https://github.com/valderman/haste-compiler
+
+The Haste.DOM module define a thin layer over the JavaScript DOM. That makes the creation and manipulation of DOM elements  as painful as in JavaScript.
+
+This package makes the creation of DOM elements easy with a syntax  similar to other haskell HTML generators, using monoids and monads, such is the case of the package blaze-html.
+
+This is an example. `withElem`  is a Haste.DOM call that give the DOM object whose id is "idelem", that has been created "by hand" in Main.hs. The builder takes this element and add content to it:
+
+      main= do
+       withElem "idelem" $   build $ do
+       div $ do
+         div $ do
+               p "hello"
+               p ! atr "style" "color:red" $   "world"
+
+       return ()
+
+       
+
+The equivalent monoid expression can also be used, by concatenating elements with the operator <>
+
+How to run
+----------
+
+install the ghc compiler
+
+install Haste:
+
+    >cabal install haste-compiler
+
+clone haskell-js-html-builder
+  
+    >git clone http://github.com/agocorona/haskell-js-html-builder.git
+    
+compile
+
+    >hastec Main.hs
+    
+browse the Main.html file. In windows simply execute it in the command line:
+
+    >Main.html
+
+Execute it in the same directory where Main.js is, since it references it assuming that it is in the current folder
+
+
+Status
+---------
+
+Standard tags and attributes are not defined except div, p and b. Define your owns. For example:
+
+div cont=  nelem "div" `child`  cont
+
+p cont = nelem "p" `child` cont
+
+b cont = nelem "b" `child` cont
+
+onclick= atr "onclick"
+ 
+
+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. Upon created, an elem  is added to the parent and return itself as parent of the next elements. 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple
+main = defaultMain
+ haste-perch.cabal view
@@ -0,0 +1,38 @@+name:                haste-perch
+version:             0.1.0.0
+synopsis:            Create dynamic HTML in the browser using blaze-html-style notation with Haste 
+description:         Perch defines builder elements (perchs) for Haste.DOM elements that are appendable, so that dynamic HTML can be created in the client in a natural way, like textual HTML, but programmatically and with the advantage of static type checking. It can be ported to other haskell-js compilers+                     .+                     Haste is a compiler that generates Javascript code from Haskell.+                     .+                     https://github.com/valderman/haste-compiler+                     .+                     The Haste.DOM module define a thin layer over the JavaScript DOM. The DOM is a low level HTML tree manipulation API. That makes the creation and manipulation of DOM elements almost as painful as in JavaScript.+                     .+                     This package makes the creation of DOM elements easy with a syntax similar to other haskell HTML generators, using monoids and monads, such is the case of the package blaze-html.+                     .+                     http://hackage.haskell.org/package/blaze-html+                     .+                     Since the creation is in the browser, that permit quite dynamic pages for data+                     presentation, and interctive textual (a.k.a "serious") applications and in general+                     the development of client-side web frameworks using haskell with the haste compiler.+ 
+homepage:            https://github.com/agocorona/haste-perch
+license:             BSD3
+license-file:        LICENSE
+author:              Alberto G. Corona
+maintainer:          agocorona@gmail.com
+-- copyright:           
+category:            Web
+build-type:          Simple
+extra-source-files:  README.md
+cabal-version:       >=1.10
+extra-source-files:  Main.html+
+library
+  exposed-modules:     Haste.Perch
+  other-modules:       Main
+  -- other-extensions:    
+  build-depends:       base >=4.6 && <4.7, haste-compiler, mtl
+  -- hs-source-dirs:      
+  default-language:    Haskell2010