{-# 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, isNothing) 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.XMLType x)] v String input = input' (\n v -> []) textarea :: (XMLGenerator x, Monad v) => (String -> Rect) -> Maybe String -> Form [XMLGenT x (HSX.XMLType x)] v String textarea calcRect = input' (\n v -> let rect = calcRect v in [ ]) file :: (XMLGenerator x, Monad v) => Form [XMLGenT x (HSX.XMLType x)] v File file = inputFile $ \name -> [] label :: (XMLGenerator x, Monad v) => String -> Form [XMLGenT x (HSX.XMLType x)] v () label str = xml $ [] hidden :: (XMLGenerator x, Monad v) => Maybe String -> Form [XMLGenT x (HSX.XMLType x)] v String hidden = input' (\n v -> []) submit :: (XMLGenerator x, Monad v) => String -> Form [XMLGenT x (HSX.XMLType 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.XMLType x)] v String password = input' (\n v -> []) checkboxes :: (XMLGenerator x, Monad v, Applicative v, Read a, Show a) => [(a, String)] -> [a] -> Form [XMLGenT x (HSX.XMLType x)] v [a] checkboxes items checked = (checkboxes' (map (first show) items) (map show checked)) `check` (\strs -> sequenceA $ map (flip maybeRead' "Could not parse checkbox data") strs) checkboxes' :: (XMLGenerator x, Monad v, Applicative v) => [(String, String)] -> [String] -> Form [XMLGenT x (HSX.XMLType x)] v [String] checkboxes' items checked = generalInputMulti $ \name selectedValues -> [