{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ViewPatterns #-} module Handler.Utils.Form where import Import import qualified Data.Char as Char import Handler.Utils.DateTime import Data.String (IsString(..)) import qualified Data.Foldable as Foldable -- import Yesod.Core import qualified Data.Text as T -- import Yesod.Form.Types import Yesod.Form.Functions (parseHelper) import Yesod.Form.Bootstrap3 import qualified Text.Blaze.Internal as Blaze (null) import Web.PathPieces (showToPathPiece, readFromPathPiece) ------------------------------------------------ -- Unique Form Identifiers to avoid accidents -- ------------------------------------------------ data FormIdentifier = FIDcourse | FIDsheet deriving (Enum, Eq, Ord, Bounded, Read, Show) identForm :: FormIdentifier -> Form a -> Form a identForm fid = identifyForm (T.pack $ show fid) ------------------- -- Form Renderer -- ------------------- -- | Use this type to pass information to the form template data FormLayout = FormStandard renderAForm :: Monad m => FormLayout -> FormRender m a renderAForm formLayout aform fragment = do (res, (($ []) -> views)) <- aFormToForm aform let widget = $(widgetFile "form") return (res, widget) ---------------------------- -- Buttons (new version ) -- ---------------------------- data ButtonCssClass = BCDefault | BCPrimary | BCSuccess | BCInfo | BCWarning | BCDanger | BCLink deriving (Enum, Eq, Ord, Bounded, Read, Show) bcc2txt :: ButtonCssClass -> Text -- a Hack; maybe define Read/Show manually bcc2txt bcc = T.pack $ "btn-" ++ (Char.toLower <$> (drop 2 $ show bcc)) class (Enum a, Bounded a, Ord a, PathPiece a) => Button a where label :: a -> Widget label = toWidget . toPathPiece cssClass :: a -> ButtonCssClass cssClass _ = BCDefault {- Abort is not useful (press Back instead); Delete should be different: data StandardButton = BtnDelete | BtnAbort | BtnSave deriving (Enum, Eq, Ord, Bounded, Read, Show) instance PathPiece StandardButton where -- for displaying the button only, not really for paths toPathPiece = showToPathPiece fromPathPiece = readFromPathPiece instance Button StandardButton where label BtnDelete = "Löschen" label BtnAbort = "Abbrechen" label BtnSave = "Speichern" cssClass BtnDelete = BCWarning cssClass BtnAbort = BCDefault cssClass BtnSave = BCPrimary -} data SubmitButton = BtnSubmit deriving (Enum, Eq, Ord, Bounded, Read, Show) instance PathPiece SubmitButton where toPathPiece = showToPathPiece fromPathPiece = readFromPathPiece instance Button SubmitButton where label BtnSubmit = "Submit" cssClass BtnSubmit = BCPrimary -- -- Looks like a button, but is just a link (e.g. for create course, etc.) -- data LinkButton = LinkButton (Route UniWorX) -- deriving (Enum, Eq, Ord, Bounded, Read, Show) -- -- instance PathPiece LinkButton where -- LinkButton route = ??? linkButton :: Widget -> ButtonCssClass -> Route UniWorX -> Widget linkButton lbl cls url = [whamlet| ^{lbl} |] -- [whamlet| --
-- --