From 3cd4169e33c07b71129aafcecfb81a3b5007fa39 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 15 Mar 2017 18:11:47 +0100 Subject: Autocomplete types --- Handler/Common.hs | 126 ++++++++++++++++++++++++++++++++---------------------- Handler/Types.hs | 15 +++++++ 2 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 Handler/Types.hs (limited to 'Handler') 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 -