{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, MultiParamTypeClasses, FlexibleContexts, TypeFamilies, UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-orphans -F -pgmF trhsx #-} module HSP.Formlets ( input , textarea , file , label , hidden , submit , password , checkboxes , select , selectMulti , radio , div , span , withAttrs , fset ) where import Control.Arrow (first) import Control.Applicative (Applicative(pure),(<$>)) import Control.Applicative.Error (Failing(Success), maybeRead') import Data.Maybe (catMaybes) import Data.Monoid (Monoid) import Data.Traversable (sequenceA) import HSP import qualified HSX.XMLGenerator as HSX import Text.Formlets (Form, File, Rect(rectRows, rectCols), check, generalInput, generalInputMulti, input', inputFile, plug, xml) import Prelude hiding (div, span) input :: (XMLGenerator x, Monad v) => Maybe String -> Form [XMLGenT x (HSX.XML x)] v String input = input' (\n v -> []) textarea :: (XMLGenerator x, Monad v) => (String -> Rect) -> Maybe String -> Form [XMLGenT x (HSX.XML x)] v String textarea calcRect = input' (\n v -> let rect = calcRect v in [ ]) file :: (XMLGenerator x, Monad v) => Form [XMLGenT x (HSX.XML x)] v File file = inputFile $ \name -> [] label :: (XMLGenerator x, Monad v) => String -> Form [XMLGenT x (HSX.XML x)] v () label str = xml $ [] hidden :: (XMLGenerator x, Monad v) => Maybe String -> Form [XMLGenT x (HSX.XML x)] v String hidden = input' (\n v -> []) submit :: (XMLGenerator x, Monad v) => String -> Form [XMLGenT x (HSX.XML x)] v Bool submit val = (generalInput (\n _ -> [
])) `check` (\res -> case res of Nothing -> pure False (Just "") -> pure False _ -> pure True ) -- |A password field with an optional value password :: (Monad v, HSX.XMLGenerator x) => Maybe String -> Form [XMLGenT x (HSX.XML x)] v String password = input' (\n v -> []) checkboxes :: (XMLGenerator x, Monad v, Applicative v, Read a, Show a) => [(a, String)] -> Form [XMLGenT x (HSX.XML x)] v [a] checkboxes items = checkboxes' (map (first show) items) `check` (\strs -> sequenceA $ map (flip maybeRead' "Could not parse checkbox data") strs) {- Normally checkboxes all have the same 'name' element, and you get an array of results back. But we don't want to parse the array, so we use a trick and just give each checkbox a unique name. -} checkboxes' :: (XMLGenerator x, Monad v, Applicative v) => [(String, String)] -> Form [XMLGenT x (HSX.XML x)] v [String] checkboxes' items = plug (\listItems -> [<% labelText %>
<% labelText %>