2022-05-20 22:20:12 +03:00
|
|
|
module Routes exposing (Route(..), fromLocation, toString, viewBreadCrumbs)
|
2018-02-13 00:32:38 +03:00
|
|
|
|
2022-05-20 22:20:12 +03:00
|
|
|
import Accessibility.Styled as Html exposing (Html)
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category
|
2022-05-20 22:48:54 +03:00
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Example exposing (Example)
|
2022-05-20 22:20:12 +03:00
|
|
|
import Html.Styled.Attributes as Attributes
|
|
|
|
import Nri.Ui.BreadCrumbs.V1 as BreadCrumbs exposing (BreadCrumbs)
|
2022-05-20 22:48:54 +03:00
|
|
|
import Nri.Ui.Util exposing (dashify)
|
2018-12-05 21:56:04 +03:00
|
|
|
import Parser exposing ((|.), (|=), Parser)
|
2018-12-05 01:36:15 +03:00
|
|
|
import Url exposing (Url)
|
2018-02-13 00:32:38 +03:00
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
type Route state msg
|
|
|
|
= Doodad (Example state msg)
|
2020-03-24 03:33:42 +03:00
|
|
|
| Category Category.Category
|
2018-02-13 00:32:38 +03:00
|
|
|
| All
|
2022-05-20 22:48:54 +03:00
|
|
|
| NotFound String
|
2018-02-13 00:32:38 +03:00
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
toString : Route state msg -> String
|
2021-11-06 01:04:14 +03:00
|
|
|
toString route_ =
|
|
|
|
case route_ of
|
2022-05-20 22:48:54 +03:00
|
|
|
NotFound name ->
|
|
|
|
"#/doodad/" ++ name
|
|
|
|
|
|
|
|
Doodad example ->
|
|
|
|
"#/doodad/" ++ example.name
|
2021-11-06 01:04:14 +03:00
|
|
|
|
|
|
|
Category c ->
|
|
|
|
"#/category/" ++ Debug.toString c
|
|
|
|
|
|
|
|
All ->
|
2021-11-06 01:16:54 +03:00
|
|
|
"#/"
|
2021-11-06 01:04:14 +03:00
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
route : Dict String (Example state msg) -> Parser (Route state msg)
|
|
|
|
route examples =
|
|
|
|
let
|
|
|
|
findExample : String -> Route state msg
|
|
|
|
findExample name =
|
|
|
|
Dict.get name examples
|
|
|
|
|> Maybe.map Doodad
|
|
|
|
|> Maybe.withDefault (NotFound name)
|
|
|
|
in
|
2018-12-05 21:56:04 +03:00
|
|
|
Parser.oneOf
|
|
|
|
[ Parser.succeed Category
|
2020-03-24 23:05:30 +03:00
|
|
|
|. Parser.token "/category/"
|
2018-12-08 01:16:00 +03:00
|
|
|
|= (restOfPath |> Parser.andThen category)
|
2022-05-20 22:48:54 +03:00
|
|
|
, Parser.succeed findExample
|
2020-03-24 23:05:30 +03:00
|
|
|
|. Parser.token "/doodad/"
|
2018-12-08 01:16:00 +03:00
|
|
|
|= restOfPath
|
2018-12-05 21:56:04 +03:00
|
|
|
, Parser.succeed All
|
2018-02-13 00:32:38 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-12-08 01:16:00 +03:00
|
|
|
restOfPath : Parser String
|
|
|
|
restOfPath =
|
|
|
|
Parser.getChompedString (Parser.chompWhile (always True))
|
2018-12-05 21:56:04 +03:00
|
|
|
|
|
|
|
|
2020-03-24 03:33:42 +03:00
|
|
|
category : String -> Parser Category.Category
|
2018-12-05 21:56:04 +03:00
|
|
|
category string =
|
2020-03-24 03:33:42 +03:00
|
|
|
case Category.fromString string of
|
2018-12-05 21:56:04 +03:00
|
|
|
Ok c ->
|
|
|
|
Parser.succeed c
|
|
|
|
|
|
|
|
Err e ->
|
|
|
|
Parser.problem e
|
2018-02-13 00:32:38 +03:00
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
fromLocation : Dict String (Example state msg) -> Url -> Route state msg
|
|
|
|
fromLocation examples location =
|
2020-03-24 23:05:30 +03:00
|
|
|
location.fragment
|
|
|
|
|> Maybe.withDefault ""
|
2022-05-20 22:48:54 +03:00
|
|
|
|> Parser.run (route examples)
|
2018-12-05 21:56:04 +03:00
|
|
|
|> Result.withDefault All
|
2022-05-20 22:20:12 +03:00
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
viewBreadCrumbs : Route state msg -> Html msg2
|
2022-05-20 22:20:12 +03:00
|
|
|
viewBreadCrumbs currentRoute =
|
|
|
|
breadCrumbs currentRoute
|
|
|
|
|> Maybe.map
|
|
|
|
(BreadCrumbs.view
|
|
|
|
{ aTagAttributes = \r -> [ Attributes.href ("/" ++ toString r) ]
|
|
|
|
, isCurrentRoute = (==) currentRoute
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Maybe.withDefault (Html.text "")
|
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
breadCrumbs : Route state msg -> Maybe (BreadCrumbs (Route state msg))
|
2022-05-20 22:20:12 +03:00
|
|
|
breadCrumbs route_ =
|
|
|
|
case route_ of
|
|
|
|
All ->
|
|
|
|
Just allBreadCrumb
|
|
|
|
|
|
|
|
Category category_ ->
|
|
|
|
Just (categoryCrumb category_)
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
Doodad example ->
|
|
|
|
Just (doodadCrumb example)
|
|
|
|
|
|
|
|
NotFound _ ->
|
2022-05-20 22:20:12 +03:00
|
|
|
Nothing
|
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
allBreadCrumb : BreadCrumbs (Route state msg)
|
2022-05-20 22:20:12 +03:00
|
|
|
allBreadCrumb =
|
|
|
|
BreadCrumbs.init
|
|
|
|
{ icon = Nothing
|
|
|
|
, iconStyle = BreadCrumbs.Default
|
|
|
|
, id = "breadcrumbs__all"
|
|
|
|
, text = "All"
|
|
|
|
, route = All
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-20 22:48:54 +03:00
|
|
|
categoryCrumb : Category.Category -> BreadCrumbs (Route state msg)
|
2022-05-20 22:20:12 +03:00
|
|
|
categoryCrumb category_ =
|
|
|
|
BreadCrumbs.after allBreadCrumb
|
|
|
|
{ icon = Nothing
|
|
|
|
, iconStyle = BreadCrumbs.Default
|
|
|
|
, id = "breadcrumbs__" ++ Category.forId category_
|
|
|
|
, text = Category.forDisplay category_
|
|
|
|
, route = Category category_
|
|
|
|
}
|
2022-05-20 22:48:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
doodadCrumb : Example state msg -> BreadCrumbs (Route state msg)
|
|
|
|
doodadCrumb example =
|
|
|
|
BreadCrumbs.after allBreadCrumb
|
|
|
|
{ icon = Nothing
|
|
|
|
, iconStyle = BreadCrumbs.Default
|
|
|
|
, id = "breadcrumbs__" ++ dashify example.name
|
|
|
|
, text = Example.fullName example
|
|
|
|
, route = Doodad example
|
|
|
|
}
|