git-annex/Assistant/WebApp/Form.hs

90 lines
3 KiB
Haskell
Raw Normal View History

{- git-annex assistant webapp form utilities
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
2013-03-12 12:15:41 +00:00
{-# LANGUAGE FlexibleContexts, TypeFamilies, QuasiQuotes #-}
{-# LANGUAGE MultiParamTypeClasses, TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings, RankNTypes #-}
2013-06-03 20:33:05 +00:00
{-# LANGUAGE CPP #-}
module Assistant.WebApp.Form where
2013-06-03 20:33:05 +00:00
import Assistant.WebApp.Types
import Assistant.Gpg
import Yesod hiding (textField, passwordField)
import Yesod.Form.Fields as F
import Data.Text (Text)
{- Yesod's textField sets the required attribute for required fields.
- We don't want this, because many of the forms used in this webapp
- display a modal dialog when submitted, which interacts badly with
- required field handling by the browser.
-
- Required fields are still checked by Yesod.
-}
2013-06-03 20:33:05 +00:00
textField :: MkField Text
textField = F.textField
{ fieldView = \theId name attrs val _isReq -> [whamlet|
<input id="#{theId}" name="#{name}" *{attrs} type="text" value="#{either id id val}">
|]
}
readonlyTextField :: MkField Text
readonlyTextField = F.textField
{ fieldView = \theId name attrs val _isReq -> [whamlet|
<input id="#{theId}" name="#{name}" *{attrs} type="text" value="#{either id id val}" readonly="true">
|]
}
{- Also without required attribute. -}
2013-06-03 20:33:05 +00:00
passwordField :: MkField Text
passwordField = F.passwordField
{ fieldView = \theId name attrs val _isReq -> toWidget [hamlet|
<input id="#{theId}" name="#{name}" *{attrs} type="password" value="#{either id id val}">
|]
}
{- Makes a note widget be displayed after a field. -}
2013-06-03 20:33:05 +00:00
#if MIN_VERSION_yesod(1,2,0)
2013-06-02 19:57:22 +00:00
withNote :: (Monad m, ToWidget (HandlerSite m) a) => Field m v -> a -> Field m v
2013-06-03 20:33:05 +00:00
#else
withNote :: Field sub master v -> GWidget sub master () -> Field sub master v
#endif
withNote field note = field { fieldView = newview }
where
newview theId name attrs val isReq =
let fieldwidget = (fieldView field) theId name attrs val isReq
in [whamlet|^{fieldwidget}&nbsp;&nbsp;<span>^{note}</span>|]
2013-04-26 03:44:55 +00:00
{- Note that the toggle string must be unique on the form. -}
2013-06-03 20:33:05 +00:00
#if MIN_VERSION_yesod(1,2,0)
2013-06-02 19:57:22 +00:00
withExpandableNote :: (Monad m, ToWidget (HandlerSite m) w) => Field m v -> (String, w) -> Field m v
2013-06-03 20:33:05 +00:00
#else
withExpandableNote :: Field sub master v -> (String, GWidget sub master ()) -> Field sub master v
#endif
withExpandableNote field (toggle, note) = withNote field $ [whamlet|
2013-11-14 17:49:11 +00:00
<a .btn data-toggle="collapse" data-target="##{ident}">#{toggle}</a>
2013-04-26 03:44:55 +00:00
<div ##{ident} .collapse>
^{note}
|]
where
ident = "toggle_" ++ toggle
2013-04-26 03:44:55 +00:00
{- Adds a check box to an AForm to control encryption. -}
2013-06-03 20:33:05 +00:00
#if MIN_VERSION_yesod(1,2,0)
2013-06-02 19:57:22 +00:00
enableEncryptionField :: (RenderMessage site FormMessage) => AForm (HandlerT site IO) EnableEncryption
2013-06-03 20:33:05 +00:00
#else
enableEncryptionField :: RenderMessage master FormMessage => AForm sub master EnableEncryption
#endif
enableEncryptionField = areq (selectFieldList choices) "Encryption" (Just SharedEncryption)
where
choices :: [(Text, EnableEncryption)]
choices =
[ ("Encrypt all data", SharedEncryption)
, ("Disable encryption", NoEncryption)
]