web: make view data a little easier to construct and customise
This commit is contained in:
parent
dca66a63a7
commit
95f461fc94
@ -825,52 +825,61 @@ nulltemplate = [$hamlet||]
|
|||||||
|
|
||||||
-- | A bundle of data useful for hledger-web request handlers and templates.
|
-- | A bundle of data useful for hledger-web request handlers and templates.
|
||||||
data ViewData = VD {
|
data ViewData = VD {
|
||||||
opts :: [Opt] -- ^ command-line options at startup
|
opts :: [Opt] -- ^ the command-line options at startup
|
||||||
,q :: String -- ^ current q parameter, the query expression
|
|
||||||
,p :: Bool -- ^ current p parameter, 1 or 0 shows/hides all postings, default is based on query
|
|
||||||
,m :: Matcher -- ^ a matcher parsed from the main query expr ("q" parameter)
|
|
||||||
,qopts :: [QueryOpt] -- ^ query options parsed from the main query expr
|
|
||||||
,am :: Matcher -- ^ a matcher parsed from the accounts sidebar query expr ("a" parameter)
|
|
||||||
,aopts :: [QueryOpt] -- ^ query options parsed from the accounts sidebar query expr
|
|
||||||
,j :: Journal -- ^ the up-to-date parsed unfiltered journal
|
|
||||||
,today :: Day -- ^ the current day
|
|
||||||
,here :: AppRoute -- ^ the current route
|
,here :: AppRoute -- ^ the current route
|
||||||
,msg :: Maybe Html -- ^ the current UI message if any, possibly from the current request
|
,msg :: Maybe Html -- ^ the current UI message if any, possibly from the current request
|
||||||
|
,today :: Day -- ^ today's date (for queries containing relative dates)
|
||||||
|
,j :: Journal -- ^ the up-to-date parsed unfiltered journal
|
||||||
|
,q :: String -- ^ the current q parameter, the main query expression
|
||||||
|
,m :: Matcher -- ^ a matcher parsed from the q parameter
|
||||||
|
,qopts :: [QueryOpt] -- ^ query options parsed from the q parameter
|
||||||
|
,am :: Matcher -- ^ a matcher parsed from the accounts sidebar query expr ("a" parameter)
|
||||||
|
,aopts :: [QueryOpt] -- ^ query options parsed from the accounts sidebar query expr
|
||||||
|
,showpostings :: Bool -- ^ current p parameter, 1 or 0 shows/hides all postings where applicable
|
||||||
}
|
}
|
||||||
|
|
||||||
mkvd :: ViewData
|
-- | Make a default ViewData, using day 0 as today's date.
|
||||||
mkvd = VD {
|
nullviewdata :: ViewData
|
||||||
opts = []
|
nullviewdata = viewdataWithDateAndParams nulldate "" "" ""
|
||||||
,q = ""
|
|
||||||
,p = False
|
-- | Make a ViewData using the given date and request parameters, and defaults elsewhere.
|
||||||
,m = MatchAny
|
viewdataWithDateAndParams :: Day -> String -> String -> String -> ViewData
|
||||||
,qopts = []
|
viewdataWithDateAndParams d q a p =
|
||||||
,am = MatchAny
|
let (querymatcher,queryopts) = parseQuery d q
|
||||||
,aopts = []
|
(acctsmatcher,acctsopts) = parseQuery d a
|
||||||
|
in VD {
|
||||||
|
opts = [NoElide]
|
||||||
,j = nulljournal
|
,j = nulljournal
|
||||||
,today = ModifiedJulianDay 0
|
|
||||||
,here = RootR
|
,here = RootR
|
||||||
,msg = Nothing
|
,msg = Nothing
|
||||||
|
,today = d
|
||||||
|
,q = q
|
||||||
|
,m = querymatcher
|
||||||
|
,qopts = queryopts
|
||||||
|
,am = acctsmatcher
|
||||||
|
,aopts = acctsopts
|
||||||
|
,showpostings = p == "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | Gather useful data for handlers and templates.
|
-- | Gather data used by handlers and templates in the current request.
|
||||||
getViewData :: Handler ViewData
|
getViewData :: Handler ViewData
|
||||||
getViewData = do
|
getViewData = do
|
||||||
app <- getYesod
|
app <- getYesod
|
||||||
let opts = appOpts app
|
let opts = appOpts app
|
||||||
(j, err) <- getCurrentJournal opts
|
(j, err) <- getCurrentJournal opts
|
||||||
msg <- getMessageOr err
|
msg <- getMessageOr err
|
||||||
Just here' <- getCurrentRoute
|
Just here <- getCurrentRoute
|
||||||
today <- liftIO getCurrentDay
|
today <- liftIO getCurrentDay
|
||||||
q <- getParameter "q"
|
q <- getParameter "q"
|
||||||
let (querymatcher,queryopts) = parseQuery today q
|
|
||||||
a <- getParameter "a"
|
a <- getParameter "a"
|
||||||
let (acctsmatcher,acctsopts) = parseQuery today a
|
|
||||||
p <- getParameter "p"
|
p <- getParameter "p"
|
||||||
let p' | p == "1" = True
|
return (viewdataWithDateAndParams today q a p){
|
||||||
| p == "0" = False
|
opts=opts
|
||||||
| otherwise = isNothing $ inAccountMatcher queryopts
|
,msg=msg
|
||||||
return mkvd{opts=opts, q=q, p=p', m=querymatcher, qopts=queryopts, am=acctsmatcher, aopts=acctsopts, j=j, today=today, here=here', msg=msg}
|
,here=here
|
||||||
|
,today=today
|
||||||
|
,j=j
|
||||||
|
}
|
||||||
where
|
where
|
||||||
-- | Update our copy of the journal if the file changed. If there is an
|
-- | Update our copy of the journal if the file changed. If there is an
|
||||||
-- error while reloading, keep the old one and return the error, and set a
|
-- error while reloading, keep the old one and return the error, and set a
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user