From 5e2c4526651bbd7669a7358f5239dd82731e77d1 Mon Sep 17 00:00:00 2001 From: Avinal Kumar Date: Thu, 8 Sep 2022 00:16:39 +0530 Subject: [PATCH] add basic project Signed-off-by: Avinal Kumar --- elm.json | 24 +++++ src/Blog.elm | 71 ++++++++++++ src/Main.elm | 274 +++++++++++++++++++++++++++++++++++++++++++++++ src/Splash.elm | 50 +++++++++ src/Static.elm | 36 +++++++ src/Terminal.elm | 41 +++++++ 6 files changed, 496 insertions(+) create mode 100644 elm.json create mode 100644 src/Blog.elm create mode 100644 src/Main.elm create mode 100644 src/Splash.elm create mode 100644 src/Static.elm create mode 100644 src/Terminal.elm diff --git a/elm.json b/elm.json new file mode 100644 index 0000000..f135688 --- /dev/null +++ b/elm.json @@ -0,0 +1,24 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/browser": "1.0.2", + "elm/core": "1.0.5", + "elm/html": "1.0.0", + "elm/url": "1.0.0" + }, + "indirect": { + "elm/json": "1.1.3", + "elm/time": "1.0.0", + "elm/virtual-dom": "1.0.3" + } + }, + "test-dependencies": { + "direct": {}, + "indirect": {} + } +} diff --git a/src/Blog.elm b/src/Blog.elm new file mode 100644 index 0000000..8b1ddc6 --- /dev/null +++ b/src/Blog.elm @@ -0,0 +1,71 @@ +module Blog exposing (..) + +import Html exposing (..) +import Html.Attributes exposing (class, datetime) + + + +-- MODEL + + +type alias Model = + { blog : Blog + } + + + +-- Blog Post + + +type alias Blog = + { title : String + , url : String + , description : String + , content : String + , category : String + , tags : List String + , date : String + } + + +view : Model -> Html msg +view model = + div [ class "max-width mx-auto px3 ltr" ] + [ div [ class "content index py4" ] + [ article [ class "post" ] + [ h1 [ class "posttitle" ] [ text model.blog.title ] + , div [ class "meta" ] + [ div [ class "article-tag" ] + [ a [ class "tag-link" ] [ text "tag" ] ] + ] + , div [ class "content" ] [ text model.blog.content ] + ] + ] + ] + + +type Msg + = Nothing + + +init : () -> ( Model, Cmd Msg ) +init _ = + ( { blog = + { title = "My First Blog Post" + , url = "my-first-blog-post" + , description = "This is my first blog post" + , content = "This is the content of my first blog post" + , category = "blog" + , tags = [ "elm", "blog" ] + , date = "2018-01-01" + } + } + , Cmd.none + ) + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + Nothing -> + ( model, Cmd.none ) diff --git a/src/Main.elm b/src/Main.elm new file mode 100644 index 0000000..2f2e3b6 --- /dev/null +++ b/src/Main.elm @@ -0,0 +1,274 @@ +module Main exposing (main) + +import Blog as Blog +import Browser exposing (Document) +import Browser.Navigation as Nav +import Html exposing (Html, a, div, footer, header, img, li, text, ul) +import Html.Attributes exposing (class, href, src) +import Html.Lazy exposing (lazy) +import Splash as Splash +import Static as Static +import Terminal as Terminal +import Url exposing (Url) +import Url.Parser as Parser exposing ((), Parser, s) + + + +--MODEL + + +{-| Model design +Model +Page: Page that currently is active +-} +type alias Model = + { page : Page + , title : String + , key : Nav.Key + , url : Url + } + + + +-- PAGE + + +{-| Page designs + + BlogPage: Page design for blogs + TerminalPage: Page design for terminal + StaticPage: Page design for static pages + NotFound: Page design for 404 page + +-} +type Page + = SplashPage Splash.Model + | BlogPage Blog.Model + | TerminalPage Terminal.Model + | StaticPage Static.Model + | NotFound + + + +-- VIEW + + +view : Model -> Document Msg +view model = + let + content = + case model.page of + SplashPage splashModel -> + Splash.view splashModel + |> Html.map GotSplashMsg + + BlogPage blogs -> + Blog.view blogs + |> Html.map GotBlogMsg + + TerminalPage terminal -> + Terminal.view terminal + |> Html.map GotTerminalMsg + + StaticPage static -> + Static.view static + |> Html.map GotStaticMsg + + NotFound -> + text "404" + in + { title = model.title + , body = + [ lazy viewHeader model.page + , content + , lazy viewFooter model.page + ] + } + + +type Msg + = GotSplashMsg Splash.Msg + | GotBlogMsg Blog.Msg + | GotTerminalMsg Terminal.Msg + | GotStaticMsg Static.Msg + | ChangeUrl Url + | ClickedLink Browser.UrlRequest + + +viewHeader : Page -> Html msg +viewHeader page = + let + headerContent = + case page of + SplashPage _ -> + div [] [] + + _ -> + header [ class "foo-logo" ] + [ img [ src "logo-static.svg" ] + [] + ] + in + headerContent + + +viewFooter : Page -> Html msg +viewFooter page = + let + footerContent = + case page of + SplashPage _ -> + div [] [] + + _ -> + footer [ class "foo-footer" ] + [ ul [] + [ li [] + [ a [ href urlPrefix ] [ text "Home" ] + ] + ] + ] + in + footerContent + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + GotSplashMsg splashMsg -> + case model.page of + SplashPage splash -> + toSplash model (Splash.update splashMsg splash) + + _ -> + ( model, Cmd.none ) + + GotBlogMsg blogMsg -> + case model.page of + BlogPage blog -> + toBlog model (Blog.update blogMsg blog) + + _ -> + ( model, Cmd.none ) + + GotTerminalMsg terminalMsg -> + case model.page of + TerminalPage terminal -> + toTerminal model (Terminal.update terminalMsg terminal) + + _ -> + ( model, Cmd.none ) + + GotStaticMsg staticMsg -> + case model.page of + StaticPage static -> + toStatic model (Static.update staticMsg static) + + _ -> + ( model, Cmd.none ) + + ChangeUrl url -> + updateUrl { model | url = url } + + ClickedLink urlRequest -> + case urlRequest of + Browser.Internal href -> + ( model, Nav.pushUrl model.key (Url.toString href) ) + + Browser.External url -> + ( model, Nav.load url ) + + +toSplash : Model -> ( Splash.Model, Cmd Splash.Msg ) -> ( Model, Cmd Msg ) +toSplash model ( splashModel, cmd ) = + ( { model | page = SplashPage splashModel }, Cmd.map GotSplashMsg cmd ) + + +toBlog : Model -> ( Blog.Model, Cmd Blog.Msg ) -> ( Model, Cmd Msg ) +toBlog model ( blogModel, cmd ) = + ( { model | page = BlogPage blogModel }, Cmd.map GotBlogMsg cmd ) + + +toTerminal : Model -> ( Terminal.Model, Cmd Terminal.Msg ) -> ( Model, Cmd Msg ) +toTerminal model ( terminalModel, cmd ) = + ( { model | page = TerminalPage terminalModel }, Cmd.map GotTerminalMsg cmd ) + + +toStatic : Model -> ( Static.Model, Cmd Static.Msg ) -> ( Model, Cmd Msg ) +toStatic model ( staticModel, cmd ) = + ( { model | page = StaticPage staticModel }, Cmd.map GotStaticMsg cmd ) + + +init : () -> Url -> Nav.Key -> ( Model, Cmd Msg ) +init _ url key = + updateUrl { url = url, page = NotFound, title = "404", key = key } + + + +-- PARSER + + +type Route + = Splash + | Blog + | Terminal + | Static + + +parser : Parser (Route -> a) a +parser = + Parser.oneOf + [ Parser.map Splash Parser.top + , Parser.map Splash (s urlPrefix) + , Parser.map Blog (s urlPrefix s "posts") + , Parser.map Static (s "pages") + , Parser.map Terminal (s "terminal") + ] + + +updateUrl : Model -> ( Model, Cmd Msg ) +updateUrl model = + case Parser.parse parser model.url of + Just Splash -> + Splash.init () + |> toSplash model + + Just Blog -> + Blog.init () + |> toBlog model + + Just Terminal -> + Terminal.init () + |> toTerminal model + + Just Static -> + Static.init () + |> toStatic model + + Nothing -> + ( { model | page = NotFound }, Cmd.none ) + + + +-- ENTRYPOINT + + +main : Program () Model Msg +main = + Browser.application + { init = init + , view = view + , update = update + , subscriptions = \_ -> Sub.none + , onUrlChange = ChangeUrl + , onUrlRequest = ClickedLink + } + + + +-- URLPREFIX + + +urlPrefix : String +urlPrefix = + "website" diff --git a/src/Splash.elm b/src/Splash.elm new file mode 100644 index 0000000..98c670b --- /dev/null +++ b/src/Splash.elm @@ -0,0 +1,50 @@ +module Splash exposing (..) + +import Html exposing (Html, div, img, p, text) +import Html.Attributes exposing (alt, class, height, src, width) + + +type alias Model = + { urls : List String + , support_message : String + , error_message : String + } + + +view : Model -> Html msg +view model = + div [ class "foo-content" ] + [ div [ class "foo-error" ] + [ img + [ class "foo-error__logo" + , src "public/logo-loading.svg" + , alt "Finding the SpaceTime" + , width 130 + , height 130 + ] + [] + , p [ class "foo-support__message" ] [ text model.support_message ] + , p [ class "foo-error__message" ] [ text model.error_message ] + ] + ] + + +type Msg + = Nothing + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + Nothing -> + ( model, Cmd.none ) + + +init : () -> ( Model, Cmd Msg ) +init _ = + ( { urls = [] + , support_message = "We are looking for the SpaceTime" + , error_message = "We are sorry, but we can't find the SpaceTime" + } + , Cmd.none + ) diff --git a/src/Static.elm b/src/Static.elm new file mode 100644 index 0000000..800c781 --- /dev/null +++ b/src/Static.elm @@ -0,0 +1,36 @@ +module Static exposing (..) + +import Html exposing (..) +import Html.Attributes exposing (..) + + +type alias Model = + { title : String + , url : String + } + + +view : Model -> Html Msg +view model = + div [] + [ h1 [] [ text model.title ] + , a [ href model.url ] [ text model.url ] + ] + + +type Msg + = NoOp + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + NoOp -> + ( model, Cmd.none ) + + +init : () -> ( Model, Cmd Msg ) +init _ = + ( Model "Hello World" "http://elm-lang.org" + , Cmd.none + ) diff --git a/src/Terminal.elm b/src/Terminal.elm new file mode 100644 index 0000000..e1d9e9a --- /dev/null +++ b/src/Terminal.elm @@ -0,0 +1,41 @@ +module Terminal exposing (..) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) + + +type alias Model = + { title : String + , url : String + } + + +view : Model -> Html Msg +view model = + div [] + [ h1 [] [ text model.title ] + , input [ placeholder "Enter URL", onInput UrlEntered ] [] + , button [ onClick Go ] [ text "Go" ] + , a [ href model.url ] [ text model.url ] + ] + + +type Msg + = UrlEntered String + | Go + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + UrlEntered url -> + ( { model | url = url }, Cmd.none ) + + Go -> + ( model, Cmd.none ) + + +init : () -> ( Model, Cmd Msg ) +init _ = + ( Model "Terminal" "", Cmd.none )