2012-09-02 04:27:48 +00:00
|
|
|
{- git-annex assistant webapp core
|
2012-07-31 05:11:32 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012, 2013 Joey Hess <id@joeyh.name>
|
2012-07-31 05:11:32 +00:00
|
|
|
-
|
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-03-16 04:12:28 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp where
|
|
|
|
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2013-03-16 04:12:28 +00:00
|
|
|
import Assistant.Common
|
2012-07-31 05:11:32 +00:00
|
|
|
import Utility.NotificationBroadcaster
|
|
|
|
import Utility.Yesod
|
2014-03-13 01:21:10 +00:00
|
|
|
import Utility.WebApp
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-09-02 04:27:48 +00:00
|
|
|
import Data.Text (Text)
|
2012-09-08 23:57:15 +00:00
|
|
|
import Control.Concurrent
|
2013-03-16 23:00:11 +00:00
|
|
|
import qualified Network.Wai as W
|
|
|
|
import qualified Data.ByteString.Char8 as S8
|
|
|
|
import qualified Data.Text as T
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2013-06-02 19:57:22 +00:00
|
|
|
waitNotifier :: Assistant NotificationBroadcaster -> NotificationId -> Handler ()
|
2012-11-03 01:13:06 +00:00
|
|
|
waitNotifier getbroadcaster nid = liftAssistant $ do
|
|
|
|
b <- getbroadcaster
|
|
|
|
liftIO $ waitNotification $ notificationHandleFromId b nid
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2013-06-02 19:57:22 +00:00
|
|
|
newNotifier :: Assistant NotificationBroadcaster -> Handler NotificationId
|
2012-11-03 01:13:06 +00:00
|
|
|
newNotifier getbroadcaster = liftAssistant $ do
|
|
|
|
b <- getbroadcaster
|
2013-03-27 18:56:15 +00:00
|
|
|
liftIO $ notificationHandleToId <$> newNotificationHandle True b
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-07-31 21:57:08 +00:00
|
|
|
{- Adds the auth parameter as a hidden field on a form. Must be put into
|
|
|
|
- every form. -}
|
|
|
|
webAppFormAuthToken :: Widget
|
|
|
|
webAppFormAuthToken = do
|
2013-06-03 17:51:54 +00:00
|
|
|
webapp <- liftH getYesod
|
2014-03-13 01:21:10 +00:00
|
|
|
[whamlet|<input type="hidden" name="auth" value="#{fromAuthToken (authToken webapp)}">|]
|
2012-08-08 21:07:38 +00:00
|
|
|
|
2012-09-27 16:55:00 +00:00
|
|
|
{- A button with an icon, and maybe label or tooltip, that can be
|
|
|
|
- clicked to perform some action.
|
2012-08-08 21:07:38 +00:00
|
|
|
- With javascript, clicking it POSTs the Route, and remains on the same
|
|
|
|
- page.
|
|
|
|
- With noscript, clicking it GETs the Route. -}
|
2012-09-27 16:55:00 +00:00
|
|
|
actionButton :: Route WebApp -> (Maybe String) -> (Maybe String) -> String -> String -> Widget
|
|
|
|
actionButton route label tooltip buttonclass iconclass = $(widgetFile "actionbutton")
|
2012-09-08 23:57:15 +00:00
|
|
|
|
|
|
|
type UrlRenderFunc = Route WebApp -> [(Text, Text)] -> Text
|
|
|
|
type UrlRenderer = MVar (UrlRenderFunc)
|
|
|
|
|
|
|
|
newUrlRenderer :: IO UrlRenderer
|
|
|
|
newUrlRenderer = newEmptyMVar
|
|
|
|
|
|
|
|
setUrlRenderer :: UrlRenderer -> (UrlRenderFunc) -> IO ()
|
|
|
|
setUrlRenderer = putMVar
|
|
|
|
|
2013-03-16 04:12:28 +00:00
|
|
|
inFirstRun :: Handler Bool
|
|
|
|
inFirstRun = isNothing . relDir <$> getYesod
|
|
|
|
|
2012-09-08 23:57:15 +00:00
|
|
|
{- Blocks until the webapp is running and has called setUrlRenderer. -}
|
|
|
|
renderUrl :: UrlRenderer -> Route WebApp -> [(Text, Text)] -> IO Text
|
|
|
|
renderUrl urlrenderer route params = do
|
|
|
|
r <- readMVar urlrenderer
|
|
|
|
return $ r route params
|
2012-09-09 05:02:44 +00:00
|
|
|
|
2013-03-13 02:18:36 +00:00
|
|
|
{- Redirects back to the referring page, or if there's none, DashboardR -}
|
2012-09-09 05:02:44 +00:00
|
|
|
redirectBack :: Handler ()
|
|
|
|
redirectBack = do
|
2013-03-16 23:00:11 +00:00
|
|
|
mr <- lookup "referer" . W.requestHeaders <$> waiRequest
|
|
|
|
case mr of
|
|
|
|
Nothing -> redirect DashboardR
|
|
|
|
Just r -> redirect $ T.pack $ S8.unpack r
|