{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}
module IHP.ViewSupport
( HtmlWithContext
, Layout
, Html
, View (..)
, currentViewId
, forEach
, isActivePath
, isActivePathOrSub
, css
, onClick
, onLoad
, theRequest
, ViewFetchHelpMessage
, param
, fetch
, query
, isActiveController
, isActiveAction
, nl2br
, stripTags
, theCSSFramework
, fromCSSFramework
, liveReloadWebsocketUrl
, assetPath
, assetVersion
) where
import IHP.Prelude
import qualified Text.Blaze.Html5 as Html5
import IHP.ControllerSupport
import IHP.ModelSupport
import qualified Data.Aeson as JSON
import qualified Data.Text as Text
import qualified Data.Typeable as Typeable
import qualified Text.Inflections as Inflector
import qualified Data.Either as Either
import GHC.TypeLits as T
import qualified Data.ByteString as ByteString
import IHP.Router.UrlGenerator (HasPath(..))
import Network.Wai
import Text.Blaze.Html5.Attributes as A
import IHP.HSX.QQ (hsx)
import IHP.HSX.ToHtml
import qualified Data.Sequences as Sequences
import qualified IHP.View.CSSFramework as CSSFramework ()
import IHP.View.Types
import qualified IHP.FrameworkConfig as FrameworkConfig
import qualified IHP.HSX.Attribute as HSX
import qualified Network.Wai.Middleware.AssetPath as AssetPath
import IHP.ActionType (isActiveController)
class View theView where
beforeRender :: (?context :: ControllerContext, ?request :: Request) => theView -> IO ()
beforeRender theView
view = () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
html :: (?context :: ControllerContext, ?view :: theView, ?request :: Request) => theView -> Html5.Html
json :: theView -> JSON.Value
json = Text -> theView -> Value
forall a. Text -> a
error Text
"Json View for this route is not implemented"
{-# INLINE currentViewId #-}
currentViewId :: (?view :: view, Typeable view) => Text
currentViewId :: forall view. (?view::view, Typeable view) => Text
currentViewId =
case [Text]
moduleParts of
[Text
_, Text
"View", Text
controllerName, Text
viewName] -> Text -> Either (ParseErrorBundle Text Void) Text -> Text
forall b a. b -> Either a b -> b
Either.fromRight (Text -> Text
forall a. Text -> a
error Text
"currentViewId: Failed to parse controller name") (Text -> Either (ParseErrorBundle Text Void) Text
Inflector.toDashed Text
controllerName) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Either (ParseErrorBundle Text Void) Text -> Text
forall b a. b -> Either a b -> b
Either.fromRight (Text -> Text
forall a. Text -> a
error Text
"currentViewId: Failed to parse view name") (Text -> Either (ParseErrorBundle Text Void) Text
Inflector.toDashed Text
viewName)
[Text]
_ -> Text -> Text
forall a. Text -> a
error (Text
"currentViewId: Failed to read view id for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Text
moduleName)
where
constructor :: TyCon
constructor = TypeRep -> TyCon
Typeable.typeRepTyCon (view -> TypeRep
forall a. Typeable a => a -> TypeRep
Typeable.typeOf view
?view::view
?view)
moduleName :: Text
moduleName :: Text
moduleName = String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (TyCon -> String
Typeable.tyConModule TyCon
constructor)
moduleParts :: [Text]
moduleParts :: [Text]
moduleParts = HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
"." Text
moduleName
isActivePath :: (?request :: Request, PathString controller) => controller -> Bool
isActivePath :: forall controller.
(?request::Request, PathString controller) =>
controller -> Bool
isActivePath controller
route =
let
currentPath :: ByteString
currentPath = Request
(?request::Request) => Request
theRequest.rawPathInfo ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Request
(?request::Request) => Request
theRequest.rawQueryString
in
ByteString
currentPath ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs (controller -> Text
forall a. PathString a => a -> Text
pathToString controller
route)
isActivePathOrSub :: (?request :: Request, PathString controller) => controller -> Bool
isActivePathOrSub :: forall controller.
(?request::Request, PathString controller) =>
controller -> Bool
isActivePathOrSub controller
route =
let
currentPath :: ByteString
currentPath = Request
(?request::Request) => Request
theRequest.rawPathInfo
in
Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs (controller -> Text
forall a. PathString a => a -> Text
pathToString controller
route) ByteString -> ByteString -> Bool
`ByteString.isPrefixOf` ByteString
currentPath
isActiveAction :: forall controllerAction. (?request :: Request, HasPath controllerAction) => controllerAction -> Bool
isActiveAction :: forall controllerAction.
(?request::Request, HasPath controllerAction) =>
controllerAction -> Bool
isActiveAction controllerAction
controllerAction =
Text -> Bool
forall controller.
(?request::Request, PathString controller) =>
controller -> Bool
isActivePath (controllerAction -> Text
forall controller. HasPath controller => controller -> Text
pathTo controllerAction
controllerAction)
css :: QuasiQuoter
css = QuasiQuoter
plain
onClick :: AttributeValue -> Attribute
onClick = AttributeValue -> Attribute
A.onclick
onLoad :: AttributeValue -> Attribute
onLoad = AttributeValue -> Attribute
A.onload
theRequest :: (?request :: Request) => Request
theRequest :: (?request::Request) => Request
theRequest = ?request::Request
Request
?request
{-# INLINE theRequest #-}
class PathString a where
pathToString :: a -> Text
instance PathString Text where
pathToString :: Text -> Text
pathToString Text
path = Text
path
instance {-# OVERLAPPABLE #-} HasPath action => PathString action where
pathToString :: action -> Text
pathToString = action -> Text
forall controller. HasPath controller => controller -> Text
pathTo
class ViewParamHelpMessage where
param :: a
instance (T.TypeError (T.Text "‘param‘ can only be used inside your controller actions.\nYou have to run the ‘param \"my_param\"‘ call inside your controller and then pass the resulting value to your view.\n\nController Example:\n\n module Web.Controller.Projects\n\n instance Controller ProjectsController where\n action ProjectsAction = do\n let showDetails = param \"showDetails\"\n render ProjectsView { showDetails }\n\nView Example:\n\n module Web.View.Projects.Index\n\n data ProjectsView = ProjectsView { showDetails :: Bool }\n instance View ProjectsView where\n html ProjectsView { .. } = [hsx|Show details: {showDetails}|]\n\n")) => ViewParamHelpMessage where
param :: forall a. a
param = Text -> a
forall a. Text -> a
error Text
"unreachable"
class ViewFetchHelpMessage where
fetch :: a
query :: a
instance (T.TypeError (T.Text "‘fetch‘ or ‘query‘ can only be used inside your controller actions. You have to call it from your controller action and then pass the result to the view.")) => ViewFetchHelpMessage where
fetch :: forall a. a
fetch = Text -> a
forall a. Text -> a
error Text
"unreachable"
query :: forall a. a
query = Text -> a
forall a. Text -> a
error Text
"unreachable"
instance (T.TypeError (T.Text "Looks like you forgot to pass a " :<>: (T.ShowType (GetModelByTableName record)) :<>: T.Text " id to this data constructor.")) => Eq (Id' (record :: T.Symbol) -> controller) where
Id' record -> controller
a == :: (Id' record -> controller) -> (Id' record -> controller) -> Bool
== Id' record -> controller
b = Text -> Bool
forall a. Text -> a
error Text
"unreachable"
fromCSSFramework :: (?request :: Request, KnownSymbol field, HasField field CSSFramework (CSSFramework -> appliedFunction)) => Proxy field -> appliedFunction
fromCSSFramework :: forall (field :: Symbol) appliedFunction.
(?request::Request, KnownSymbol field,
HasField field CSSFramework (CSSFramework -> appliedFunction)) =>
Proxy field -> appliedFunction
fromCSSFramework Proxy field
field = let cssFramework :: CSSFramework
cssFramework = CSSFramework
(?request::Request) => CSSFramework
theCSSFramework in (Proxy field -> CSSFramework -> CSSFramework -> appliedFunction
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get Proxy field
field CSSFramework
cssFramework) CSSFramework
cssFramework
theCSSFramework :: (?request :: Request) => CSSFramework
theCSSFramework :: (?request::Request) => CSSFramework
theCSSFramework = ?request::Request
Request
?request.frameworkConfig.cssFramework
nl2br :: (Sequences.Textual text, ToHtml text) => text -> Html5.Html
nl2br :: forall text. (Textual text, ToHtml text) => text -> Html
nl2br text
content = text
content
text -> (text -> [text]) -> [text]
forall a b. a -> (a -> b) -> b
|> text -> [text]
forall t. Textual t => t -> [t]
Sequences.lines
[text] -> ([text] -> [Html]) -> [Html]
forall a b. a -> (a -> b) -> b
|> (text -> Html) -> [text] -> [Html]
forall a b. (a -> b) -> [a] -> [b]
map (\text
line -> [hsx|{line}<br/>|])
[Html] -> ([Html] -> Html) -> Html
forall a b. a -> (a -> b) -> b
|> [Html] -> Html
forall a. Monoid a => [a] -> a
mconcat
type Html = HtmlWithContext ControllerContext
liveReloadWebsocketUrl :: (?request :: Request) => Text
liveReloadWebsocketUrl :: (?request::Request) => Text
liveReloadWebsocketUrl = ?request::Request
Request
?request.frameworkConfig.ideBaseUrl
Text -> (Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
|> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
Text.replace Text
"http://" Text
"ws://"
Text -> (Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
|> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
Text.replace Text
"https://" Text
"wss://"
instance InputValue (PrimaryKey table) => HSX.ApplyAttribute (Id' table) where
applyAttribute :: Text -> Text -> Id' table -> Html -> Html
applyAttribute Text
attr Text
attr' Id' table
value Html
h = Text -> Text -> Text -> Html -> Html
forall value.
ApplyAttribute value =>
Text -> Text -> value -> Html -> Html
HSX.applyAttribute Text
attr Text
attr' (Id' table -> Text
forall a. InputValue a => a -> Text
inputValue Id' table
value) Html
h
assetPath :: (?request :: Request) => Text -> Text
assetPath :: (?request::Request) => Text -> Text
assetPath Text
assetPath = Request -> Text -> Text
AssetPath.assetPath Request
(?request::Request) => Request
theRequest Text
assetPath
assetVersion :: (?request :: Request) => Text
assetVersion :: (?request::Request) => Text
assetVersion = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Text -> Text
forall a. Text -> a
error Text
"assetPath middleware missing") (Request -> Maybe Text
AssetPath.assetVersion Request
(?request::Request) => Request
theRequest)