{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} module Ditto.Lucid.Named where import Data.Foldable (traverse_) import Data.Monoid ((<>), mconcat, mempty) import Data.Text (Text) import Lucid import Ditto.Backend import Ditto.Core import Ditto.Generalized.Named as G import Ditto.Result (FormId, Result (Ok), unitRange) import Web.PathPieces import qualified Data.Text as T import qualified Text.Read instance PathPiece FormId where toPathPiece fid = T.pack (show fid) fromPathPiece fidT = Nothing inputText :: (Monad m, FormError error, PathPiece text, Applicative f) => (input -> Either error text) -> String -> text -> Form m input error (HtmlT f ()) text inputText getInput name initialValue = G.input getInput inputField initialValue name where inputField i a = input_ [type_ "text", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] inputPassword :: (Monad m, FormError error, PathPiece text, Applicative f) => (input -> Either error text) -> String -> text -> Form m input error (HtmlT f ()) text inputPassword getInput name initialValue = G.input getInput inputField initialValue name where inputField i a = input_ [type_ "password", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] inputSubmit :: (Monad m, FormError error, PathPiece text, Applicative f) => (input -> Either error text) -> String -> text -> Form m input error (HtmlT f ()) (Maybe text) inputSubmit getInput name initialValue = G.inputMaybe getInput inputField initialValue name where inputField i a = input_ [type_ "submit", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] inputReset :: (Monad m, FormError error, PathPiece text, Applicative f) => String -> text -> Form m input error (HtmlT f ()) () inputReset name lbl = G.inputNoData inputField lbl name where inputField i a = input_ [type_ "submit", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] inputHidden :: (Monad m, FormError error, PathPiece text, Applicative f) => (input -> Either error text) -> String -> text -> Form m input error (HtmlT f ()) text inputHidden getInput name initialValue = G.input getInput inputField initialValue name where inputField i a = input_ [type_ "hidden", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] inputButton :: (Monad m, FormError error, PathPiece text, Applicative f) => String -> text -> Form m input error (HtmlT f ()) () inputButton name label = G.inputNoData inputField label name where inputField i a = input_ [type_ "button", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)] textarea :: (Monad m, FormError error, ToHtml text, Monad f) => (input -> Either error text) -> Int -- ^ cols -> Int -- ^ rows -> String -> text -- ^ initial text -> Form m input error (HtmlT f ()) text textarea getInput cols rows name initialValue = G.input getInput textareaView initialValue name where textareaView i txt = textarea_ [ rows_ (toPathPiece rows) , cols_ (toPathPiece cols) , id_ (toPathPiece i) , name_ (toPathPiece i) ] $ toHtml txt -- | Create an @\@ element -- -- This control may succeed even if the user does not actually select a file to upload. In that case the uploaded name will likely be \"\" and the file contents will be empty as well. inputFile :: (Monad m, FormError error, FormInput input, ErrorInputType error ~ input, Applicative f) => String -> Form m input error (HtmlT f ()) (FileType input) inputFile name = G.inputFile fileView name where fileView i = input_ [type_ "file", id_ (toPathPiece i), name_ (toPathPiece i)] -- | Create a @\