module Main exposing (main) {-| The entry point for the application, responsible for mapping flags into an initial state of the app, connecting to those subscriptions that impact global state, and configuring and feeding the routing system. -} import Common exposing (..) import Html exposing (Html) import Html.Attributes as Attr import Navigation as Nav exposing (Location) import Routes as Route import Json.Decode as Json import Log import Ports import Dict exposing (Dict(..)) main : Program Flags Model Msg main = Nav.programWithFlags (NowAtLocation) { init = init , update = update , view = view , subscriptions = subscriptions } type alias Flags = Json.Value type alias Model = { route : Route.Model , global : GlobalModel } init : Flags -> Location -> ( Model, Cmd Msg ) init flags location = let ( assetsCmd, assets ) = case Json.decodeValue (Json.field "assets" <| Json.dict Json.string) flags of Err msg -> ( Log.warn ("Could not decode assets map: " ++ msg) flags , Dict.empty ) Ok decoded -> ( Log.debug "Assets" decoded , decoded ) initGlobal : GlobalModel initGlobal = { assets = assets } ( newGlobal, routeModel, routeCmd ) = Route.init initGlobal allCmds = Cmd.batch [ assetsCmd , Log.info "Initialized application" "" , Ports.initialized () , Cmd.map RouteMsg routeCmd ] newModel = { route = routeModel , global = newGlobal } in ( newModel, allCmds ) -- UPDATE type Msg = NowAtLocation Location | RouteMsg Route.Msg update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of NowAtLocation location -> let route = Route.parseLocation location cmdLoadRoute = Cmd.map RouteMsg <| Route.fetchRoute model.global model.route.pages route in ( model, cmdLoadRoute ) RouteMsg routeMsg -> let ( newGlobal, newRoute, routeCmd ) = Route.update model.global routeMsg model.route cmds = Cmd.batch [ Cmd.map RouteMsg routeCmd ] in ( { model | global = newGlobal, route = newRoute }, cmds ) -- VIEW view : Model -> Html Msg view model = Html.div [ Attr.id "elm-application" ] -- This id is important for swiping to work [ Html.map RouteMsg <| Route.view model.global model.route ] -- SUBSCRIPTIONS subscriptions : Model -> Sub Msg subscriptions model = let routeSubs = Sub.map RouteMsg <| Route.subscriptions model.route in Sub.batch -- TODO Add in any other subscriptions that you want to monitor [ routeSubs ]