From 329efa7944e0cf84ad5a7cabfce723bb0fd57047 Mon Sep 17 00:00:00 2001 From: Ryan Haskell-Glatz Date: Sun, 20 Oct 2019 16:01:03 -0500 Subject: [PATCH] its happening... its HAPPENING --- example/src/Components/Navbar.elm | 3 +- example/src/Generated/Pages.elm | 2 +- example/src/Generated/Pages/Settings.elm | 2 +- src/Application.elm | 81 ++++++++++++------------ src/Internals/Page.elm | 51 ++++++++++++--- src/Internals/Transition.elm | 40 +++++++++--- 6 files changed, 119 insertions(+), 60 deletions(-) diff --git a/example/src/Components/Navbar.elm b/example/src/Components/Navbar.elm index 867e2a7..990fdec 100644 --- a/example/src/Components/Navbar.elm +++ b/example/src/Components/Navbar.elm @@ -20,8 +20,7 @@ view = , ( "Counter", Route.Counter () ) , ( "Cats", Route.Random () ) , ( "User", Route.Users_Slug "alice" ) - , ( "Account settings", Route.Settings (Settings.Account ()) ) - , ( "User settings", Route.Settings (Settings.User ()) ) + , ( "Settings", Route.Settings (Settings.Account ()) ) ] diff --git a/example/src/Generated/Pages.elm b/example/src/Generated/Pages.elm index 7ce9289..6258816 100644 --- a/example/src/Generated/Pages.elm +++ b/example/src/Generated/Pages.elm @@ -88,7 +88,7 @@ users_slug_posts_slug = } -init : Route -> ( Model, Cmd Msg ) +init : Route -> Application.Init Model Msg init route = case route of Route.Homepage params -> diff --git a/example/src/Generated/Pages/Settings.elm b/example/src/Generated/Pages/Settings.elm index a4be0b4..e741eab 100644 --- a/example/src/Generated/Pages/Settings.elm +++ b/example/src/Generated/Pages/Settings.elm @@ -69,7 +69,7 @@ user = } -init : Route -> ( Model, Cmd Msg ) +init : Route -> Application.Init Model Msg init route = case route of Route.Account params -> diff --git a/src/Application.elm b/src/Application.elm index ec75673..f3a2d6f 100644 --- a/src/Application.elm +++ b/src/Application.elm @@ -2,7 +2,7 @@ module Application exposing ( Application, create , Layout , Page, Recipe - , Bundle, keep + , Init, Bundle, keep , Static, static , Sandbox, sandbox , Element, element @@ -27,7 +27,7 @@ module Application exposing @docs Page, Recipe -@docs Bundle, keep +@docs Init, Bundle, keep @docs Static, static @@ -75,30 +75,26 @@ create : , transition : Transition (Html msg) } , pages : - { init : route -> ( model, Cmd msg ) + { init : route -> Init model msg , update : msg -> model -> ( model, Cmd msg ) - , bundle : model -> Page.Bundle msg + , bundle : model -> Bundle msg } } -> Application flags model msg create config = - let - (Transition.Transition transition) = - config.layout.transition - in Browser.application { init = init { init = config.pages.init , fromUrl = config.routing.fromUrl - , speed = transition.speed + , speed = Transition.speed config.layout.transition } , update = update { fromUrl = config.routing.fromUrl , init = config.pages.init , update = config.pages.update - , speed = transition.speed + , speed = Transition.speed config.layout.transition } , subscriptions = subscriptions @@ -112,7 +108,7 @@ create config = \model -> config.pages.bundle model Page.Initial |> .view , layout = config.layout - , transition = transition.strategy + , transition = Transition.strategy config.layout.transition } , onUrlChange = Url , onUrlRequest = Link @@ -128,12 +124,13 @@ type alias Model flags model = , flags : flags , key : Nav.Key , page : Transitionable model + , speed : Int } init : { fromUrl : Url -> route - , init : route -> ( model, Cmd msg ) + , init : route -> Init model msg , speed : Int } -> flags @@ -143,21 +140,20 @@ init : init config flags url key = url |> config.fromUrl - |> config.init - |> Tuple.mapBoth - (\page -> - { flags = flags - , url = url - , key = key - , page = Transitionable.Ready page - } - ) - (\cmd -> - Cmd.batch - [ handleJumpLinks url cmd - , Utils.delay config.speed TransitionComplete + |> (\route -> config.init route { parentSpeed = config.speed }) + |> (\page -> + ( { flags = flags + , url = url + , key = key + , page = Transitionable.Ready page.model + , speed = page.speed + } + , Cmd.batch + [ handleJumpLinks url page.cmd + , Utils.delay page.speed TransitionComplete ] - ) + ) + ) handleJumpLinks : Url -> Cmd msg -> Cmd (Msg msg) @@ -197,7 +193,7 @@ type Msg msg update : { fromUrl : Url -> route - , init : route -> ( model, Cmd msg ) + , init : route -> Init model msg , update : msg -> model -> ( model, Cmd msg ) , speed : Int } @@ -217,15 +213,12 @@ update config msg model = ( model, Nav.load (Url.toString url) ) else - ( { model - | page = - if navigatingToNewLayout { old = model.url, new = url } then - Transitionable.begin model.page + ( if navigatingToNewLayout { old = model.url, new = url } then + { model | page = Transitionable.begin model.page } - else - Transitionable.complete model.page - } - , Utils.delay config.speed (TransitionTo url) + else + { model | page = Transitionable.complete model.page } + , Utils.delay model.speed (TransitionTo url) ) Link (Browser.External url) -> @@ -246,10 +239,16 @@ update config msg model = Url url -> url |> config.fromUrl - |> config.init - |> Tuple.mapBoth - (\page -> { model | url = url, page = Transitionable.Complete page }) - (handleJumpLinks url) + |> (\route -> config.init route { parentSpeed = config.speed }) + |> (\page -> + ( { model + | url = url + , page = Transitionable.Complete page.model + , speed = page.speed + } + , handleJumpLinks url page.cmd + ) + ) Page pageMsg -> Tuple.mapBoth @@ -342,6 +341,10 @@ type alias Recipe params pageModel pageMsg model msg = Page.Recipe params pageModel pageMsg model msg +type alias Init model msg = + Page.Init model msg + + type alias Bundle msg = Page.Bundle msg diff --git a/src/Internals/Page.elm b/src/Internals/Page.elm index f6ea3aa..ea5ca88 100644 --- a/src/Internals/Page.elm +++ b/src/Internals/Page.elm @@ -1,6 +1,6 @@ module Internals.Page exposing ( Page, Recipe - , Bundle + , Init, Bundle , Static, static , Sandbox, sandbox , Element, element @@ -12,7 +12,7 @@ module Internals.Page exposing @docs Page, Recipe -@docs Bundle +@docs Init, Bundle @docs Static, static @@ -26,6 +26,7 @@ module Internals.Page exposing import Html exposing (Html, div, text) import Internals.Layout exposing (Layout) +import Internals.Transition as Transition type alias Page params pageModel pageMsg model msg = @@ -36,7 +37,7 @@ type alias Page params pageModel pageMsg model msg = type alias Recipe params pageModel pageMsg model msg = - { init : params -> ( model, Cmd msg ) + { init : params -> Init model msg , update : pageMsg -> pageModel -> ( model, Cmd msg ) , bundle : pageModel -> Bundle msg } @@ -48,6 +49,16 @@ type TransitionStatus | Entering +type alias Init model msg = + { parentSpeed : Int + } + -> + { model : model + , cmd : Cmd msg + , speed : Int + } + + type alias Bundle msg = TransitionStatus -> @@ -69,7 +80,12 @@ static : Static -> Page params () Never model msg static page { toModel, toMsg } = - { init = \_ -> ( toModel (), Cmd.none ) + { init = + \_ { parentSpeed } -> + { model = toModel () + , cmd = Cmd.none + , speed = parentSpeed + } , update = \_ model -> ( toModel model, Cmd.none ) , bundle = \_ _ -> @@ -94,7 +110,12 @@ sandbox : Sandbox params pageModel pageMsg -> Page params pageModel pageMsg model msg sandbox page { toModel, toMsg } = - { init = \params -> ( toModel (page.init params), Cmd.none ) + { init = + \params { parentSpeed } -> + { model = toModel (page.init params) + , cmd = Cmd.none + , speed = parentSpeed + } , update = \msg model -> ( page.update msg model |> toModel @@ -125,7 +146,14 @@ element : -> Page params pageModel pageMsg model msg element page { toModel, toMsg } = { init = - page.init >> Tuple.mapBoth toModel (Cmd.map toMsg) + \params { parentSpeed } -> + page.init params + |> (\( model, cmd ) -> + { model = toModel model + , cmd = Cmd.map toMsg cmd + , speed = parentSpeed + } + ) , update = \msg model -> page.update msg model @@ -149,7 +177,7 @@ type alias Glue params layoutModel layoutMsg = type alias Pages params layoutModel layoutMsg = - { init : params -> ( layoutModel, Cmd layoutMsg ) + { init : params -> Init layoutModel layoutMsg , update : layoutMsg -> layoutModel -> ( layoutModel, Cmd layoutMsg ) , bundle : layoutModel -> Bundle layoutMsg } @@ -160,7 +188,14 @@ glue : -> Page params layoutModel layoutMsg model msg glue options { toModel, toMsg } = { init = - options.pages.init >> Tuple.mapBoth toModel (Cmd.map toMsg) + \params _ -> + options.pages.init params { parentSpeed = Transition.speed options.layout.transition } + |> (\page -> + { model = toModel page.model + , cmd = Cmd.map toMsg page.cmd + , speed = page.speed + } + ) , update = \msg model -> options.pages.update msg model diff --git a/src/Internals/Transition.elm b/src/Internals/Transition.elm index a00aaac..b213c41 100644 --- a/src/Internals/Transition.elm +++ b/src/Internals/Transition.elm @@ -1,12 +1,19 @@ module Internals.Transition exposing - ( Options - , Strategy - , Transition(..) - , custom - , fade - , none + ( Transition, Options, Strategy + , fade, custom, none + , speed, strategy ) +{-| + +@docs Transition, Options, Strategy + +@docs fade, custom, none + +@docs speed strategy + +-} + import Html exposing (Html, div) import Html.Attributes as Attr @@ -15,6 +22,21 @@ type Transition view = Transition (Options view) +unwrap : Transition view -> Options view +unwrap (Transition options) = + options + + +speed : Transition view -> Int +speed = + unwrap >> .speed + + +strategy : Transition view -> Strategy view +strategy = + unwrap >> .strategy + + type alias Options view = { speed : Int , strategy : Strategy view @@ -35,10 +57,10 @@ type alias Views view = fade : Int -> Transition (Html msg) -fade speed = +fade speed_ = let transition = - "opacity " ++ String.fromInt speed ++ "ms" + "opacity " ++ String.fromInt speed_ ++ "ms" styles = { invisible = @@ -54,7 +76,7 @@ fade speed = } in Transition - { speed = speed + { speed = speed_ , strategy = { beforeLoad = \{ layout, page } ->