2012-12-02 21:32:54 +00:00
|
|
|
{- git-annex assistant webapp form utilities
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-12-02 21:32:54 +00:00
|
|
|
-
|
|
|
|
- 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 #-}
|
2012-12-02 21:32:54 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Form where
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
import Assistant.WebApp.Types
|
2013-09-26 20:09:45 +00:00
|
|
|
import Assistant.Gpg
|
2012-12-04 17:28:22 +00:00
|
|
|
|
2014-04-25 10:50:02 +00:00
|
|
|
#if MIN_VERSION_yesod(1,2,0)
|
2012-12-02 21:32:54 +00:00
|
|
|
import Yesod hiding (textField, passwordField)
|
|
|
|
import Yesod.Form.Fields as F
|
2014-04-25 10:50:02 +00:00
|
|
|
#else
|
|
|
|
import Yesod hiding (textField, passwordField, selectField, selectFieldList)
|
|
|
|
import Yesod.Form.Fields as F hiding (selectField, selectFieldList)
|
2014-04-18 00:07:09 +00:00
|
|
|
import Data.String (IsString (..))
|
2014-04-25 10:50:02 +00:00
|
|
|
import Control.Monad (unless)
|
|
|
|
import Data.Maybe (listToMaybe)
|
2014-10-13 19:32:41 +00:00
|
|
|
#endif
|
2014-10-15 15:49:13 +00:00
|
|
|
#if MIN_VERSION_yesod_form(1,3,8)
|
|
|
|
import Yesod.Form.Bootstrap3 as Y hiding (bfs)
|
|
|
|
#else
|
|
|
|
import Assistant.WebApp.Bootstrap3 as Y hiding (bfs)
|
|
|
|
#endif
|
2014-05-14 19:02:18 +00:00
|
|
|
import Data.Text (Text)
|
2014-04-25 10:50:02 +00:00
|
|
|
|
2012-12-02 21:32:54 +00:00
|
|
|
{- 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
|
2012-12-02 21:32:54 +00:00
|
|
|
textField = F.textField
|
|
|
|
{ fieldView = \theId name attrs val _isReq -> [whamlet|
|
|
|
|
<input id="#{theId}" name="#{name}" *{attrs} type="text" value="#{either id id val}">
|
|
|
|
|]
|
|
|
|
}
|
|
|
|
|
2013-06-18 21:08:37 +00:00
|
|
|
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">
|
|
|
|
|]
|
|
|
|
}
|
|
|
|
|
2012-12-03 02:33:30 +00:00
|
|
|
{- Also without required attribute. -}
|
2013-06-03 20:33:05 +00:00
|
|
|
passwordField :: MkField Text
|
2012-12-02 21:32:54 +00:00
|
|
|
passwordField = F.passwordField
|
|
|
|
{ fieldView = \theId name attrs val _isReq -> toWidget [hamlet|
|
|
|
|
<input id="#{theId}" name="#{name}" *{attrs} type="password" value="#{either id id val}">
|
|
|
|
|]
|
|
|
|
}
|
2012-12-03 02:33:30 +00:00
|
|
|
|
2014-04-25 10:50:02 +00:00
|
|
|
{- In older Yesod versions attrs is written into the <option> tag instead of the
|
|
|
|
- surrounding <select>. This breaks the Bootstrap 3 layout of select fields as
|
|
|
|
- it requires the "form-control" class on the <select> tag.
|
|
|
|
- We need to change that to behave the same way as in newer versions.
|
|
|
|
-}
|
|
|
|
#if ! MIN_VERSION_yesod(1,2,0)
|
|
|
|
selectFieldList :: (Eq a, RenderMessage master FormMessage, RenderMessage master msg) => [(msg, a)] -> Field sub master a
|
|
|
|
selectFieldList = selectField . optionsPairs
|
|
|
|
|
|
|
|
selectField :: (Eq a, RenderMessage master FormMessage) => GHandler sub master (OptionList a) -> Field sub master a
|
|
|
|
selectField = selectFieldHelper
|
|
|
|
(\theId name attrs inside -> [whamlet|<select ##{theId} name=#{name} *{attrs}>^{inside}|]) -- outside
|
|
|
|
(\_theId _name isSel -> [whamlet|<option value=none :isSel:selected>_{MsgSelectNone}|]) -- onOpt
|
|
|
|
(\_theId _name _attrs value isSel text -> [whamlet|<option value=#{value} :isSel:selected>#{text}|]) -- inside
|
|
|
|
|
|
|
|
selectFieldHelper :: (Eq a, RenderMessage master FormMessage)
|
|
|
|
=> (Text -> Text -> [(Text, Text)] -> GWidget sub master () -> GWidget sub master ())
|
|
|
|
-> (Text -> Text -> Bool -> GWidget sub master ())
|
|
|
|
-> (Text -> Text -> [(Text, Text)] -> Text -> Bool -> Text -> GWidget sub master ())
|
|
|
|
-> GHandler sub master (OptionList a) -> Field sub master a
|
|
|
|
selectFieldHelper outside onOpt inside opts' = Field
|
|
|
|
{ fieldParse = \x -> do
|
|
|
|
opts <- opts'
|
|
|
|
return $ selectParser opts x
|
|
|
|
, fieldView = \theId name attrs val isReq -> do
|
|
|
|
opts <- fmap olOptions $ lift opts'
|
|
|
|
outside theId name attrs $ do
|
|
|
|
unless isReq $ onOpt theId name $ not $ render opts val `elem` map optionExternalValue opts
|
|
|
|
flip mapM_ opts $ \opt -> inside
|
|
|
|
theId
|
|
|
|
name
|
|
|
|
((if isReq then (("required", "required"):) else id) attrs)
|
|
|
|
(optionExternalValue opt)
|
|
|
|
((render opts val) == optionExternalValue opt)
|
|
|
|
(optionDisplay opt)
|
|
|
|
}
|
|
|
|
where
|
|
|
|
render _ (Left _) = ""
|
|
|
|
render opts (Right a) = maybe "" optionExternalValue $ listToMaybe $ filter ((== a) . optionInternalValue) opts
|
|
|
|
selectParser _ [] = Right Nothing
|
|
|
|
selectParser opts (s:_) = case s of
|
|
|
|
"" -> Right Nothing
|
|
|
|
"none" -> Right Nothing
|
|
|
|
x -> case olReadExternal opts x of
|
|
|
|
Nothing -> Left $ SomeMessage $ MsgInvalidEntry x
|
|
|
|
Just y -> Right $ Just y
|
|
|
|
#endif
|
|
|
|
|
2012-12-03 02:33:30 +00:00
|
|
|
{- 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
|
2012-12-03 02:33:30 +00:00
|
|
|
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} <span>^{note}</span>|]
|
2012-12-04 17:28:22 +00:00
|
|
|
|
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
|
2013-04-26 19:20:31 +00:00
|
|
|
withExpandableNote field (toggle, note) = withNote field $ [whamlet|
|
2014-04-20 10:42:31 +00:00
|
|
|
<a .btn .btn-default data-toggle="collapse" data-target="##{ident}">#{toggle}</a>
|
2013-04-26 03:44:55 +00:00
|
|
|
<div ##{ident} .collapse>
|
|
|
|
^{note}
|
|
|
|
|]
|
2013-04-26 19:20:31 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
ident = "toggle_" ++ toggle
|
2013-04-26 03:44:55 +00:00
|
|
|
|
2012-12-04 17:28:22 +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
|
2014-04-18 00:07:09 +00:00
|
|
|
enableEncryptionField = areq (selectFieldList choices) (bfs "Encryption") (Just SharedEncryption)
|
2012-12-04 17:28:22 +00:00
|
|
|
where
|
|
|
|
choices :: [(Text, EnableEncryption)]
|
|
|
|
choices =
|
|
|
|
[ ("Encrypt all data", SharedEncryption)
|
|
|
|
, ("Disable encryption", NoEncryption)
|
|
|
|
]
|
2014-04-18 00:07:09 +00:00
|
|
|
|
|
|
|
{- Defines the layout used by the Bootstrap3 form helper -}
|
|
|
|
bootstrapFormLayout :: BootstrapFormLayout
|
2014-04-30 14:38:37 +00:00
|
|
|
bootstrapFormLayout = BootstrapHorizontalForm (ColSm 0) (ColSm 2) (ColSm 0) (ColSm 10)
|
2014-04-18 00:07:09 +00:00
|
|
|
|
|
|
|
{- Adds the form-control class used by Bootstrap3 for layout to a field
|
|
|
|
- This is the same as Yesod.Form.Bootstrap3.bfs except it takes just a Text
|
|
|
|
- parameter as I couldn't get the original bfs to compile due to type ambiguities.
|
|
|
|
-}
|
|
|
|
bfs :: Text -> FieldSettings master
|
|
|
|
bfs msg = FieldSettings
|
|
|
|
{ fsLabel = SomeMessage msg
|
|
|
|
, fsName = Nothing
|
|
|
|
, fsId = Nothing
|
|
|
|
, fsAttrs = [("class", "form-control")]
|
|
|
|
, fsTooltip = Nothing
|
|
|
|
}
|