git-annex/Assistant/WebApp/SideBar.hs

91 lines
2.7 KiB
Haskell
Raw Normal View History

2012-07-31 05:11:32 +00:00
{- git-annex assistant webapp sidebar
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU AGPL version 3 or higher.
2012-07-31 05:11:32 +00:00
-}
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
module Assistant.WebApp.SideBar where
import Assistant.Common
import Assistant.WebApp
import Assistant.WebApp.Types
2012-07-31 05:11:32 +00:00
import Assistant.WebApp.Notifications
import Assistant.Alert
2012-10-30 18:34:48 +00:00
import Assistant.DaemonStatus
2012-07-31 05:11:32 +00:00
import Utility.NotificationBroadcaster
import Utility.Yesod
import Yesod
import Data.Text (Text)
import qualified Data.Map as M
import Control.Concurrent
2012-07-31 05:11:32 +00:00
2012-08-04 00:40:34 +00:00
sideBarDisplay :: Widget
sideBarDisplay = do
2012-07-31 05:11:32 +00:00
let content = do
{- Add newest alerts to the sidebar. -}
2012-10-30 18:44:18 +00:00
alertpairs <- lift $ M.toList . alertMap
<$> liftAssistant getDaemonStatus
2012-07-31 05:11:32 +00:00
mapM_ renderalert $
take displayAlerts $ reverse $ sortAlertPairs alertpairs
let ident = "sidebar"
2012-07-31 05:24:49 +00:00
$(widgetFile "sidebar/main")
2012-07-31 05:11:32 +00:00
autoUpdate ident NotifierSideBarR (10 :: Int) (10 :: Int)
2012-10-31 06:34:03 +00:00
where
bootstrapclass :: AlertClass -> Text
bootstrapclass Activity = "alert-info"
bootstrapclass Warning = "alert"
bootstrapclass Error = "alert-error"
bootstrapclass Success = "alert-success"
bootstrapclass Message = "alert-info"
2012-07-31 05:11:32 +00:00
2012-10-31 06:34:03 +00:00
renderalert (aid, alert) = do
let alertid = show aid
let closable = alertClosable alert
let block = alertBlockDisplay alert
let divclass = bootstrapclass $ alertClass alert
$(widgetFile "sidebar/alert")
2012-07-31 05:11:32 +00:00
{- Called by client to get a sidebar display.
-
- Returns a div, which will be inserted into the calling page.
-
- Note that the head of the widget is not included, only its
- body is. To get the widget head content, the widget is also
- inserted onto all pages.
-}
getSideBarR :: NotificationId -> Handler RepHtml
getSideBarR nid = do
2012-11-03 01:13:06 +00:00
waitNotifier getAlertBroadcaster nid
2012-07-31 05:11:32 +00:00
{- This 0.1 second delay avoids very transient notifications from
- being displayed and churning the sidebar unnecesarily.
-
- This needs to be below the level perceptable by the user,
- to avoid slowing down user actions like closing alerts. -}
liftIO $ threadDelay 100000
2012-08-04 00:40:34 +00:00
page <- widgetToPageContent sideBarDisplay
2012-07-31 05:11:32 +00:00
hamletToRepHtml $ [hamlet|^{pageBody page}|]
{- Called by the client to close an alert. -}
getCloseAlert :: AlertId -> Handler ()
getCloseAlert = liftAssistant . removeAlert
{- When an alert with a button is clicked on, the button takes us here. -}
getClickAlert :: AlertId -> Handler ()
getClickAlert i = do
m <- alertMap <$> liftAssistant getDaemonStatus
case M.lookup i m of
Just (Alert { alertButton = Just b }) -> do
{- Spawn a thread to run the action while redirecting. -}
case buttonAction b of
Nothing -> noop
Just a -> liftIO $ void $ forkIO $ a i
redirect $ buttonUrl b
_ -> redirectBack