elm-pages-v3-beta/examples/docs/src/Shared.elm

138 lines
3.3 KiB
Elm
Raw Normal View History

2021-04-24 02:52:35 +03:00
module Shared exposing (Data, Model, Msg, SharedMsg(..), template)
2021-04-14 19:26:58 +03:00
import Browser.Navigation
2021-04-20 17:31:19 +03:00
import DataSource
import DocsSection
2021-04-13 02:54:08 +03:00
import Document exposing (Document)
2020-05-24 01:31:56 +03:00
import Html exposing (Html)
import Html.Styled
2021-05-17 22:51:04 +03:00
import Json.Decode
import Pages.Flags
2021-05-14 19:46:02 +03:00
import Pages.PagePath exposing (PagePath)
import SharedTemplate exposing (SharedTemplate)
import TableOfContents
2021-05-10 07:36:10 +03:00
import View.Header
2020-05-24 01:31:56 +03:00
2021-04-24 02:52:35 +03:00
template : SharedTemplate Msg Model Data SharedMsg msg
template =
{ init = init
, update = update
, view = view
2021-04-24 02:22:23 +03:00
, data = data
, subscriptions = subscriptions
2020-10-25 22:06:08 +03:00
, onPageChange = Just OnPageChange
2021-04-24 02:52:35 +03:00
, sharedMsg = SharedMsg
}
2020-05-24 01:31:56 +03:00
type Msg
= OnPageChange
2021-04-10 20:33:26 +03:00
{ path : PagePath
2020-05-24 01:31:56 +03:00
, query : Maybe String
, fragment : Maybe String
}
| ToggleMobileMenu
2020-09-12 19:16:56 +03:00
| SharedMsg SharedMsg
2020-05-24 01:31:56 +03:00
2021-04-24 02:22:23 +03:00
type alias Data =
TableOfContents.TableOfContents TableOfContents.Data
type SharedMsg
= IncrementFromChild
type alias Model =
{ showMobileMenu : Bool
, counter : Int
2021-04-14 19:26:58 +03:00
, navigationKey : Maybe Browser.Navigation.Key
}
init :
2021-04-14 19:26:58 +03:00
Maybe Browser.Navigation.Key
2021-05-17 22:51:04 +03:00
-> Pages.Flags.Flags
2021-04-14 19:26:58 +03:00
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
-> ( Model, Cmd Msg )
2021-05-17 22:51:04 +03:00
init navigationKey flags maybePagePath =
let
_ =
case flags of
Pages.Flags.PreRenderFlags ->
Nothing
Pages.Flags.BrowserFlags browserFlags ->
browserFlags
|> Json.Decode.decodeValue Json.Decode.string
|> Result.toMaybe
in
( { showMobileMenu = False
, counter = 0
2021-04-14 19:26:58 +03:00
, navigationKey = navigationKey
}
, Cmd.none
)
2020-05-24 03:03:28 +03:00
2020-05-24 04:59:07 +03:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
2021-04-06 00:25:37 +03:00
OnPageChange _ ->
2020-05-24 04:59:07 +03:00
( { model | showMobileMenu = False }, Cmd.none )
ToggleMobileMenu ->
( { model | showMobileMenu = not model.showMobileMenu }, Cmd.none )
2020-05-24 01:31:56 +03:00
2020-09-06 07:02:05 +03:00
SharedMsg globalMsg ->
case globalMsg of
2020-09-06 07:31:14 +03:00
IncrementFromChild ->
( { model | counter = model.counter + 1 }, Cmd.none )
2020-05-24 01:31:56 +03:00
2021-04-10 23:26:19 +03:00
subscriptions : PagePath -> Model -> Sub Msg
subscriptions _ _ =
2020-09-30 23:05:35 +03:00
Sub.none
2021-04-24 02:22:23 +03:00
data : DataSource.DataSource Data
data =
2021-05-07 20:04:14 +03:00
--DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
-- (D.field "stargazers_count" D.int)
2021-05-15 04:56:02 +03:00
TableOfContents.dataSource DocsSection.all
2020-05-24 01:31:56 +03:00
2020-08-27 08:05:12 +03:00
view :
2021-04-24 02:22:23 +03:00
Data
->
2021-04-10 20:33:26 +03:00
{ path : PagePath
2021-04-02 07:23:55 +03:00
, frontmatter : route
}
2020-05-24 01:31:56 +03:00
-> Model
-> (Msg -> msg)
2021-04-13 02:54:08 +03:00
-> Document msg
2020-05-24 01:31:56 +03:00
-> { body : Html msg, title : String }
view tableOfContents page model toMsg pageView =
2020-05-24 01:31:56 +03:00
{ body =
case pageView.body of
Document.ElmCssView elements ->
((View.Header.view ToggleMobileMenu 123 page.path
|> Html.Styled.map toMsg
)
:: TableOfContents.view model.showMobileMenu False Nothing tableOfContents
:: elements
)
|> Html.Styled.div []
|> Html.Styled.toUnstyled
2020-08-27 06:07:50 +03:00
, title = pageView.title
2020-05-24 01:31:56 +03:00
}