diff --git a/Makefile b/Makefile index f295d22..540d4b4 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) export PATH := $(NPM_PATH):$(PATH) all: $(ELM_FILES) - @elm-make --warn --yes src/Main.elm --output dist/main.js + @elm make src/Main.elm --output dist/main.js -analyse: deps - @elm-analyse --elm-format-path=./node_modules/elm-format/bin/elm-format src +# analyse: deps +# @elm-analyse --elm-format-path=./node_modules/elm-format/bin/elm-format src clean: @rm -Rf dist/* @@ -42,7 +42,7 @@ format-validate: help: @echo "Run: make where is one of the following:" @echo " all Compile all Elm files" - @echo " analyse Run Elm analyse" + # @echo " analyse Run Elm analyse" @echo " clean Remove 'dist' folder" @echo " cover Run Elm coverage" @echo " deps Install build dependencies" diff --git a/elm-package.json b/elm-package.json deleted file mode 100644 index d3a34af..0000000 --- a/elm-package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "version": "1.0.0", - "summary": "helpful summary of your project, less than 80 characters", - "repository": "https://github.com/user/project.git", - "license": "UNLICENSED", - "source-directories": [ - "src" - ], - "exposed-modules": [], - "dependencies": { - "NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0", - "elm-lang/core": "5.0.0 <= v < 6.0.0", - "elm-lang/html": "2.0.0 <= v < 3.0.0", - "elm-lang/http": "1.0.0 <= v < 2.0.0", - "elm-lang/navigation": "2.1.0 <= v < 3.0.0", - "evancz/url-parser": "2.0.1 <= v < 3.0.0" - }, - "elm-version": "0.18.0 <= v < 0.19.0" -} diff --git a/elm.json b/elm.json new file mode 100644 index 0000000..3e50460 --- /dev/null +++ b/elm.json @@ -0,0 +1,26 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.0", + "dependencies": { + "direct": { + "NoRedInk/elm-json-decode-pipeline": "1.0.0", + "elm/browser": "1.0.0", + "elm/core": "1.0.0", + "elm/html": "1.0.0", + "elm/http": "1.0.0", + "elm/json": "1.0.0", + "elm/url": "1.0.0" + }, + "indirect": { + "elm/time": "1.0.0", + "elm/virtual-dom": "1.0.0" + } + }, + "test-dependencies": { + "direct": {}, + "indirect": {} + } +} \ No newline at end of file diff --git a/src/Main.elm b/src/Main.elm index 4df31d9..42c0e2f 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -1,25 +1,27 @@ module Main exposing (main) -import Ginger.Media as Media -import Ginger.Resource exposing (Resource) -import Ginger.Rest +import Browser +import Browser.Navigation import Html exposing (..) import Html.Attributes exposing (..) import Http -import Navigation import Route exposing (Route) +import Url exposing (Url) + -- MAIN -main : Program Never Model Msg +main : Program () Model Msg main = - Navigation.program OnNavigation + Browser.application { init = init , view = view , update = update , subscriptions = \_ -> Sub.none + , onUrlChange = OnUrlChange + , onUrlRequest = OnUrlRequest } @@ -30,7 +32,7 @@ main = type alias Model = { route : Route , page : Page - , data : Maybe Resource + , key : Browser.Navigation.Key } @@ -41,12 +43,12 @@ type Page | Unknown -init : Navigation.Location -> ( Model, Cmd Msg ) -init location = +init : flags -> Url -> Browser.Navigation.Key -> ( Model, Cmd Msg ) +init _ location key = updatePage { route = Route.fromLocation location , page = Loading - , data = Nothing + , key = key } @@ -55,26 +57,23 @@ init location = type Msg - = OnNavigation Navigation.Location - | PushUrl Route - | GotResource (Result Http.Error Resource) + = OnUrlChange Url + | OnUrlRequest Browser.UrlRequest update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of - OnNavigation location -> + OnUrlChange location -> updatePage { model | route = Route.fromLocation location } - PushUrl route -> - ( model, Navigation.newUrl (Route.toUrl route) ) + OnUrlRequest urlRequest -> + case urlRequest of + Browser.Internal url -> + ( model, Browser.Navigation.pushUrl model.key (Url.toString url) ) - GotResource result -> - let - _ = - Debug.log "data" result - in - ( { model | data = Result.toMaybe result }, Cmd.none ) + Browser.External href -> + ( model, Browser.Navigation.load href ) updatePage : Model -> ( Model, Cmd Msg ) @@ -92,8 +91,7 @@ updatePage model = ( Unknown, Cmd.none ) in ( { model | page = page } - , Http.send GotResource <| - Ginger.Rest.requestResourceByPath "/" + , Cmd.none ) @@ -101,12 +99,8 @@ updatePage model = -- VIEW -view : Model -> Html Msg +view : Model -> Browser.Document Msg view model = - let - imageUrl = - Maybe.andThen (Media.depiction Media.Small) model.data - |> Maybe.withDefault "fallbackurl" - in - main_ [] - [ h1 [] [ text "hello world" ], img [ src imageUrl ] [] ] + { title = Route.toTitle model.route + , body = [ main_ [] [ h1 [] [ text "hello world" ] ] ] + } diff --git a/src/Route.elm b/src/Route.elm index 38d2d1c..a145a2c 100644 --- a/src/Route.elm +++ b/src/Route.elm @@ -2,10 +2,9 @@ module Route exposing (Route(..), fromLocation, link, toTitle, toUrl) import Html exposing (..) import Html.Attributes exposing (..) -import Html.Events exposing (defaultOptions, onWithOptions) import Json.Decode as Decode -import Navigation -import UrlParser as Url exposing (()) +import Url exposing (Url) +import Url.Parser exposing ((), Parser) type Route @@ -22,18 +21,18 @@ type alias Id = -- PARSE -fromLocation : Navigation.Location -> Route +fromLocation : Url -> Route fromLocation = Maybe.withDefault Root - << Url.parsePath parser + << Url.Parser.parse parser -parser : Url.Parser (Route -> a) a +parser : Parser (Route -> a) a parser = - Url.oneOf - [ Url.map Root Url.top - , Url.map Article (Url.s "page" Url.int) - , Url.map NotFound (Url.s "404") + Url.Parser.oneOf + [ Url.Parser.map Root Url.Parser.top + , Url.Parser.map Article (Url.Parser.s "page" Url.Parser.int) + , Url.Parser.map NotFound (Url.Parser.s "404") ] @@ -54,7 +53,7 @@ toRouteData route = RouteData "/" "Home" Article id -> - RouteData ("/page/" ++ toString id) ("Page " ++ toString id) + RouteData ("/page/" ++ String.fromInt id) ("Page " ++ String.fromInt id) NotFound -> RouteData "/404" "Not found" @@ -70,49 +69,20 @@ toUrl = .url << toRouteData +{-| link -{- - LINK +Type checked internal links. - Type checked internal links. + - Each path for each route is defined once + - Easy to change path + - You can't go to non-existing routes - - Each path for each route is defined once - - Easy to change path - - You can't go to non-existing routes - - Prevent page reload - - Enable ctrl-click -} - - -link : (Route -> msg) -> Route -> List (Attribute msg) -> List (Html msg) -> Html msg -link toMsg route attributes content = +link : Route -> List (Attribute msg) -> List (Html msg) -> Html msg +link route attributes content = a ([ href (toUrl route) - , onPreventDefaultClick (toMsg route) ] ++ attributes ) content - - -onPreventDefaultClick : msg -> Attribute msg -onPreventDefaultClick msg = - onWithOptions "click" - { defaultOptions | preventDefault = True } - (Decode.andThen (eventDecoder msg) eventKeyDecoder) - - -eventKeyDecoder : Decode.Decoder Bool -eventKeyDecoder = - Decode.map2 - (not >> xor) - (Decode.field "ctrlKey" Decode.bool) - (Decode.field "metaKey" Decode.bool) - - -eventDecoder : msg -> Bool -> Decode.Decoder msg -eventDecoder msg preventDefault = - if preventDefault then - Decode.succeed msg - else - Decode.fail ""