module IHP.Breadcrumb.ViewFunctions (
    module IHP.Breadcrumb.Types,
    renderBreadcrumb,
    breadcrumbLink,
    breadcrumbLinkExternal,
    breadcrumbText,
) where

import IHP.Prelude
import IHP.Breadcrumb.Types

import IHP.ControllerSupport

import Text.Blaze.Html (Html)

import IHP.View.Types (BreadcrumbsView(..), styledBreadcrumb, styledBreadcrumbItem)
import IHP.ViewSupport (theCSSFramework)
import IHP.ControllerPrelude

renderBreadcrumb :: (?context :: ControllerContext) => [BreadcrumbItem] -> Html
renderBreadcrumb :: (?context::ControllerContext) => [BreadcrumbItem] -> Html
renderBreadcrumb [BreadcrumbItem]
breadcrumbItems = CSSFramework
-> CSSFramework -> [BreadcrumbItem] -> BreadcrumbsView -> Html
styledBreadcrumb CSSFramework
(?context::ControllerContext) => CSSFramework
theCSSFramework CSSFramework
(?context::ControllerContext) => CSSFramework
theCSSFramework [BreadcrumbItem]
breadcrumbItems BreadcrumbsView
breadcrumbsView
        where
            breadcrumbsView :: BreadcrumbsView
breadcrumbsView = BreadcrumbsView
                { breadcrumbItems :: Html
breadcrumbItems = [BreadcrumbItem] -> ((Int, BreadcrumbItem) -> Html) -> Html
forall (m :: * -> *) a.
Applicative m =>
[a] -> ((Int, a) -> m ()) -> m ()
forEachWithIndex [BreadcrumbItem]
breadcrumbItems (\(Int
index, BreadcrumbItem
breadcrumbItem) ->
                    let
                        isLast :: Bool
isLast = Int
index Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [BreadcrumbItem] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BreadcrumbItem]
breadcrumbItems Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
                    in
                    CSSFramework
-> CSSFramework
-> [BreadcrumbItem]
-> BreadcrumbItem
-> Bool
-> Html
styledBreadcrumbItem CSSFramework
(?context::ControllerContext) => CSSFramework
theCSSFramework CSSFramework
(?context::ControllerContext) => CSSFramework
theCSSFramework [BreadcrumbItem]
breadcrumbItems BreadcrumbItem
breadcrumbItem Bool
isLast
                    )
                }


breadcrumbLink :: (HasPath controller) => Html -> controller -> BreadcrumbItem
breadcrumbLink :: forall controller.
HasPath controller =>
Html -> controller -> BreadcrumbItem
breadcrumbLink Html
label controller
route =
    Html -> Text -> BreadcrumbItem
breadcrumbLinkExternal Html
label (controller -> Text
forall controller. HasPath controller => controller -> Text
pathTo controller
route)

breadcrumbLinkExternal :: Html -> Text -> BreadcrumbItem
breadcrumbLinkExternal :: Html -> Text -> BreadcrumbItem
breadcrumbLinkExternal Html
label Text
url =
    BreadcrumbItem { breadcrumbLabel :: Html
breadcrumbLabel = Html
label, url :: Maybe Text
url = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
url }



breadcrumbText :: Html -> BreadcrumbItem
breadcrumbText :: Html -> BreadcrumbItem
breadcrumbText Html
label =
        BreadcrumbItem { breadcrumbLabel :: Html
breadcrumbLabel = Html
label, url :: Maybe Text
url = Maybe Text
forall a. Maybe a
Nothing }