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

118 lines
2.6 KiB
Elm
Raw Normal View History

module Shared exposing (Data, Model, Msg, template)
2021-04-14 19:26:58 +03:00
import Browser.Navigation
2021-04-20 17:31:19 +03:00
import DataSource
import DocsSection
2020-05-24 01:31:56 +03:00
import Html exposing (Html)
import Html.Styled
2021-05-17 22:51:04 +03:00
import Pages.Flags
2021-05-24 21:17:34 +03:00
import Pages.PageUrl exposing (PageUrl)
import Path exposing (Path)
import Route exposing (Route)
import SharedTemplate exposing (SharedTemplate)
import TableOfContents
2021-05-23 19:00:20 +03:00
import View exposing (View)
2021-05-10 07:36:10 +03:00
import View.Header
2020-05-24 01:31:56 +03:00
template : SharedTemplate Msg Model Data 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
}
2020-05-24 01:31:56 +03:00
type Msg
= OnPageChange
{ path : Path
2020-05-24 01:31:56 +03:00
, query : Maybe String
, fragment : Maybe String
}
| ToggleMobileMenu
| IncrementFromChild
2020-05-24 01:31:56 +03:00
2021-04-24 02:22:23 +03:00
type alias Data =
TableOfContents.TableOfContents TableOfContents.Data
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 : Path
2021-04-14 19:26:58 +03:00
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
2021-05-24 21:17:34 +03:00
, pageUrl : Maybe PageUrl
}
-> ( Model, Cmd Msg )
2021-05-17 22:51:04 +03:00
init navigationKey flags maybePagePath =
( { 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
IncrementFromChild ->
( { model | counter = model.counter + 1 }, Cmd.none )
2020-09-06 07:31:14 +03:00
2020-05-24 01:31:56 +03:00
subscriptions : Path -> Model -> Sub Msg
2021-04-10 23:26:19 +03:00
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-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
->
{ path : Path
, route : Maybe Route
}
2020-05-24 01:31:56 +03:00
-> Model
-> (Msg -> msg)
2021-05-23 19:00:20 +03:00
-> View 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 =
2021-05-23 19:00:20 +03:00
((View.Header.view ToggleMobileMenu 123 page.path
|> Html.Styled.map toMsg
)
:: TableOfContents.view model.showMobileMenu False Nothing tableOfContents
:: pageView.body
)
|> 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
}