2012-07-31 05:11:32 +00:00
|
|
|
{- git-annex assistant webapp sidebar
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-31 05:11:32 +00:00
|
|
|
-}
|
|
|
|
|
2013-06-05 01:02:09 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.SideBar where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.WebApp
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp.Notifications
|
2013-04-04 05:48:26 +00:00
|
|
|
import Assistant.Alert.Utility
|
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 Data.Text (Text)
|
2013-04-24 15:45:41 +00:00
|
|
|
import qualified Data.Text as T
|
2012-07-31 05:11:32 +00:00
|
|
|
import qualified Data.Map as M
|
2012-08-02 17:55:38 +00:00
|
|
|
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. -}
|
2013-06-03 17:51:54 +00:00
|
|
|
alertpairs <- liftH $ M.toList . alertMap
|
2012-10-30 21:14:26 +00:00
|
|
|
<$> 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"
|
2014-04-20 10:42:31 +00:00
|
|
|
bootstrapclass Error = "alert-danger"
|
2012-10-31 06:34:03 +00:00
|
|
|
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
|
2013-04-24 15:45:41 +00:00
|
|
|
let message = renderAlertMessage alert
|
|
|
|
let messagelines = T.lines message
|
|
|
|
let multiline = length messagelines > 1
|
2013-11-23 04:54:08 +00:00
|
|
|
let buttons = zip (alertButtons alert) [1..]
|
2012-10-31 06:34:03 +00:00
|
|
|
$(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.
|
|
|
|
-}
|
2013-06-27 05:15:28 +00:00
|
|
|
getSideBarR :: NotificationId -> Handler Html
|
2012-07-31 05:11:32 +00:00
|
|
|
getSideBarR nid = do
|
2012-11-03 01:13:06 +00:00
|
|
|
waitNotifier getAlertBroadcaster nid
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-08-02 17:55:38 +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
|
2013-06-27 05:15:28 +00:00
|
|
|
giveUrlRenderer $ [hamlet|^{pageBody page}|]
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
{- Called by the client to close an alert. -}
|
|
|
|
getCloseAlert :: AlertId -> Handler ()
|
2012-10-30 21:14:26 +00:00
|
|
|
getCloseAlert = liftAssistant . removeAlert
|
2012-09-09 05:02:44 +00:00
|
|
|
|
|
|
|
{- When an alert with a button is clicked on, the button takes us here. -}
|
2013-11-23 04:54:08 +00:00
|
|
|
getClickAlert :: AlertId -> Int -> Handler ()
|
|
|
|
getClickAlert i bnum = do
|
2012-10-30 21:14:26 +00:00
|
|
|
m <- alertMap <$> liftAssistant getDaemonStatus
|
2012-09-09 05:02:44 +00:00
|
|
|
case M.lookup i m of
|
2013-11-23 04:54:08 +00:00
|
|
|
Just (Alert { alertButtons = bs })
|
|
|
|
| length bs >= bnum -> do
|
|
|
|
let b = bs !! (bnum - 1)
|
|
|
|
{- 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
|
|
|
|
| otherwise -> redirectBack
|
2012-09-09 05:02:44 +00:00
|
|
|
_ -> redirectBack
|
|
|
|
|
2013-06-02 19:57:22 +00:00
|
|
|
htmlIcon :: AlertIcon -> Widget
|
2013-04-09 02:54:02 +00:00
|
|
|
htmlIcon ActivityIcon = [whamlet|<img src="@{StaticR activityicon_gif}" alt="">|]
|
|
|
|
htmlIcon SyncIcon = [whamlet|<img src="@{StaticR syncicon_gif}" alt="">|]
|
2012-11-25 04:38:11 +00:00
|
|
|
htmlIcon InfoIcon = bootstrapIcon "info-sign"
|
|
|
|
htmlIcon SuccessIcon = bootstrapIcon "ok"
|
|
|
|
htmlIcon ErrorIcon = bootstrapIcon "exclamation-sign"
|
2013-11-21 21:49:56 +00:00
|
|
|
htmlIcon UpgradeIcon = bootstrapIcon "arrow-up"
|
2014-04-09 20:27:24 +00:00
|
|
|
htmlIcon ConnectionIcon = bootstrapIcon "signal"
|
2012-11-25 04:38:11 +00:00
|
|
|
|
2013-06-02 19:57:22 +00:00
|
|
|
bootstrapIcon :: Text -> Widget
|
2014-04-21 11:05:52 +00:00
|
|
|
bootstrapIcon name = [whamlet|<span .glyphicon .glyphicon-#{name}>|]
|