From 3cd4169e33c07b71129aafcecfb81a3b5007fa39 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 15 Mar 2017 18:11:47 +0100 Subject: Autocomplete types --- Application.hs | 1 + Handler/Common.hs | 126 ++++++++++++++++++++++++++++++++---------------------- Handler/Types.hs | 15 +++++++ Settings.hs | 2 +- bar.cabal | 1 + config/routes | 3 ++ 6 files changed, 97 insertions(+), 51 deletions(-) create mode 100644 Handler/Types.hs diff --git a/Application.hs b/Application.hs index 85ceb2f..3a16e88 100644 --- a/Application.hs +++ b/Application.hs @@ -41,6 +41,7 @@ import Handler.ReferenceListing import Handler.ReferenceItem import Handler.DeleteRefItem import Handler.Kinds +import Handler.Types import Handler.List -- This line actually creates our YesodDispatch instance. It is the second half diff --git a/Handler/Common.hs b/Handler/Common.hs index 65988da..a1ae34b 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs @@ -26,6 +26,8 @@ import Control.Lens import Handler.Common.Types +import Text.Julius (RawJS(..)) + dayFormat :: Day -> String dayFormat = formatTime defaultTimeLocale "%e. %b %y" @@ -36,8 +38,12 @@ itemForm proto identView = do t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto - (kindRes, kindWidget) <- kindField $ itemKind <$> proto - (typeRes, typeWidget) <- typeField $ t + let kt kWidget tWidget = + [whamlet| +
^{kWidget} +
^{tWidget} + |] + ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t) (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" @@ -54,8 +60,7 @@ itemForm proto identView = do [whamlet| $newline never #{identView} -
^{kindWidget} -
^{typeWidget} + ^{typedKindWidget}
^{boughtWidget}
^{expiresWidget}
^{openedWidget} @@ -93,9 +98,14 @@ referenceForm :: Maybe Reference -- ^ Update existing item or insert new? -> Html -> MForm Handler (FormResult (WithType Reference), Widget) referenceForm proto identView = do t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto + + let kt kWidget tWidget = + [whamlet| +
^{kWidget} +
^{tWidget} + |] - (kindRes, kindWidget) <- kindField $ referenceKind <$> proto - (typeRes, typeWidget) <- typeField $ t + ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((referenceKind <$> proto), t) let referenceRes = do referenceKind <- kindRes @@ -106,63 +116,79 @@ referenceForm proto identView = do [whamlet| $newline never #{identView} -
^{kindWidget} -
^{typeWidget} + ^{typedKindWidget} |] referenceListing :: ReferenceState -> Widget referenceListing ReferenceState{ refFormState = formState, ..} = $(widgetFile "referenceListing") -kindField :: Maybe Text -> MForm Handler (FormResult Text, Widget) -kindField proto = do - optionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique - +typedKindField :: (Widget -> Widget -> Widget) -- ^ `\kindWidget typeWidget -> _` + -> (Maybe Text, Maybe Text) -- ^ `(kindProto, typeProto)` + -> MForm Handler ((FormResult Text, FormResult Text), Widget) -- ^ `((kindRes, typeRes), typedKindWidget)` +typedKindField collate (kindProto, typeProto) = do + tOptionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique let - attrs = [ ("list", optionId) - , ("autocomplete", "off") - ] - - (kindRes, kindView) <- mreq textField ("" { fsAttrs = attrs }) proto - - options <- lift kinds - - return . (kindRes, ) $ - [whamlet| - $newline never - ^{fvInput kindView} - - $forall opt <- options -