elm-mdl/examples/Demo.elm

354 lines
7.3 KiB
Elm
Raw Normal View History

2016-04-05 04:26:22 +00:00
module Main (..) where
2016-03-14 10:00:58 +00:00
import StartApp
import Html exposing (..)
import Html.Attributes exposing (href, class, style)
import Signal exposing (Signal)
import Effects exposing (..)
import Task
import Signal
import Task exposing (Task)
import Array exposing (Array)
2016-04-05 04:26:22 +00:00
import Routing
import Hop
import Hop.Navigate exposing (navigateTo)
2016-03-17 12:31:43 +00:00
import Material.Color as Color
2016-04-05 04:26:22 +00:00
import Material.Layout
2016-03-14 10:00:58 +00:00
import Material.Layout as Layout exposing (defaultLayoutModel)
2016-03-17 12:31:43 +00:00
import Material exposing (lift, lift')
import Material.Style as Style
2016-03-14 10:00:58 +00:00
import Demo.Buttons
import Demo.Grid
import Demo.Textfields
2016-03-17 12:31:43 +00:00
import Demo.Snackbar
2016-03-17 21:30:49 +00:00
import Demo.Badges
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
--import Demo.Template
2016-03-14 10:00:58 +00:00
-- MODEL
layoutModel : Layout.Model
layoutModel =
{ defaultLayoutModel
2016-04-05 04:26:22 +00:00
| state = Layout.initState (List.length tabs)
}
2016-03-14 10:00:58 +00:00
type alias Model =
{ layout : Layout.Model
2016-04-05 04:26:22 +00:00
, routing : Routing.Model
2016-03-14 10:00:58 +00:00
, buttons : Demo.Buttons.Model
, textfields : Demo.Textfields.Model
2016-04-05 04:26:22 +00:00
, snackbar :
Demo.Snackbar.Model
--, template : Demo.Template.Model
2016-03-14 10:00:58 +00:00
}
model : Model
model =
{ layout = layoutModel
2016-04-05 04:26:22 +00:00
, routing = Routing.initialModel
2016-03-14 10:00:58 +00:00
, buttons = Demo.Buttons.model
, textfields = Demo.Textfields.model
2016-04-05 04:26:22 +00:00
, snackbar =
Demo.Snackbar.model
--, template = Demo.Template.model
2016-03-14 10:00:58 +00:00
}
2016-04-05 04:26:22 +00:00
2016-03-14 10:00:58 +00:00
-- ACTION, UPDATE
type Action
= LayoutAction Layout.Action
| ButtonsAction Demo.Buttons.Action
| TextfieldAction Demo.Textfields.Action
2016-03-17 12:31:43 +00:00
| SnackbarAction Demo.Snackbar.Action
2016-04-05 04:26:22 +00:00
| RoutingAction Routing.Action
| HopAction ()
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
--| TemplateAction Demo.Template.Action
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
changeTab : Layout.Action -> Effects Action
changeTab action =
let
navTo path =
Effects.map HopAction (navigateTo path)
in
case action of
Layout.SwitchTab n ->
case n of
0 ->
navTo "/snackbar"
1 ->
navTo "/textfields"
2 ->
navTo "/buttons"
3 ->
navTo "/grid"
4 ->
navTo "/badges"
_ ->
navTo "/404"
_ ->
Effects.none
update : Action -> Model -> ( Model, Effects.Effects Action )
2016-03-14 10:00:58 +00:00
update action model =
2016-03-17 12:31:43 +00:00
case Debug.log "Action: " action of
2016-04-05 04:26:22 +00:00
LayoutAction a ->
let
( lifted, layoutFx ) =
lift .layout (\m x -> { m | layout = x }) LayoutAction Layout.update a model
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
routeFx =
changeTab a
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
fx =
Effects.batch [ layoutFx, routeFx ]
in
( lifted, fx )
ButtonsAction a ->
lift .buttons (\m x -> { m | buttons = x }) ButtonsAction Demo.Buttons.update a model
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
TextfieldAction a ->
lift' .textfields (\m x -> { m | textfields = x }) Demo.Textfields.update a model
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
SnackbarAction a ->
lift .snackbar (\m x -> { m | snackbar = x }) SnackbarAction Demo.Snackbar.update a model
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
RoutingAction a ->
let
( routing', fx ) =
Routing.update a model.routing
model' =
{ model | routing = routing' }
in
( model'
, Effects.map RoutingAction fx
)
HopAction _ ->
( model, Effects.none )
--TemplateAction a -> lift .template (\m x->{m|template =x}) TemplateAction Demo.Template.update a model
-- VIEW
type alias Addr =
Signal.Address Action
2016-03-14 10:00:58 +00:00
drawer : List Html
drawer =
[ Layout.title "Example drawer"
, Layout.navigation
2016-04-05 04:26:22 +00:00
[ Layout.link
[ href "https://github.com/debois/elm-mdl" ]
[ text "github" ]
, Layout.link
[ href "http://package.elm-lang.org/packages/debois/elm-mdl/latest/" ]
[ text "elm-package" ]
]
2016-03-14 10:00:58 +00:00
]
header : List Html
header =
[ Layout.title "elm-mdl"
, Layout.spacer
, Layout.navigation
2016-04-05 04:26:22 +00:00
[ Layout.link
[ href "https://www.getmdl.io/components/index.html" ]
[ text "MDL" ]
, Layout.link
[ href "https://www.google.com/design/spec/material-design/introduction.html" ]
[ text "Material Design" ]
]
2016-03-14 10:00:58 +00:00
]
2016-04-05 04:26:22 +00:00
tabs : List ( String, Addr -> Model -> List Html )
2016-03-14 10:00:58 +00:00
tabs =
2016-04-05 04:26:22 +00:00
[ ( "Snackbar"
, \addr model ->
[ Demo.Snackbar.view (Signal.forwardTo addr SnackbarAction) model.snackbar ]
)
, ( "Textfields"
, \addr model ->
[ Demo.Textfields.view (Signal.forwardTo addr TextfieldAction) model.textfields ]
)
, ( "Buttons"
, \addr model ->
[ Demo.Buttons.view (Signal.forwardTo addr ButtonsAction) model.buttons ]
)
, ( "Grid", \addr model -> Demo.Grid.view )
, ( "Badges", \addr model -> Demo.Badges.view )
{-
, ("Template", \addr model ->
[Demo.Template.view (Signal.forwardTo addr TemplateAction) model.template])
-}
2016-03-14 10:00:58 +00:00
]
2016-04-05 04:26:22 +00:00
2016-03-14 10:00:58 +00:00
tabViews : Array (Addr -> Model -> List Html)
2016-04-05 04:26:22 +00:00
tabViews =
List.map snd tabs |> Array.fromList
2016-03-14 10:00:58 +00:00
tabTitles : List Html
2016-04-05 04:26:22 +00:00
tabTitles =
List.map (fst >> text) tabs
2016-03-14 10:00:58 +00:00
stylesheet : Html
2016-04-05 04:26:22 +00:00
stylesheet =
Style.stylesheet """
blockquote:before { content: none; }
blockquote:after { content: none; }
blockquote {
border-left-style: solid;
border-width: 1px;
padding-left: 1.3ex;
border-color: rgb(255,82,82);
2016-03-21 23:01:46 +00:00
font-style: normal;
/* TODO: Really need a way to specify "secondary color" in
inline css.
*/
}
2016-04-05 04:26:22 +00:00
p, blockquote {
2016-03-21 23:01:46 +00:00
max-width: 33em;
2016-03-26 11:13:15 +00:00
font-size: 13px;
2016-03-21 23:01:46 +00:00
}
"""
2016-03-14 10:00:58 +00:00
view : Signal.Address Action -> Model -> Html
view addr model =
2016-04-05 04:26:22 +00:00
routingView addr model
routingView : Signal.Address Action -> Model -> Html
routingView addr model =
case (Debug.log "Route " model.routing.route) of
Routing.Home ->
let
model' =
{ model | layout = setTab model.layout 0 }
in
appView addr model'
Routing.TabRoute tabNumber ->
let
model' =
{ model | layout = setTab model.layout tabNumber }
in
appView addr model'
Routing.NotFoundRoute ->
div [] [ h2 [] [ text "Not found" ] ]
setTab layout tabNumber =
{ layout | selectedTab = tabNumber }
appView : Signal.Address Action -> Model -> Html
appView addr model =
let
top =
div
[ style
[ ( "margin", "auto" )
, ( "padding-left", "5%" )
, ( "padding-right", "5%" )
2016-03-14 10:00:58 +00:00
]
2016-04-05 04:26:22 +00:00
]
((Array.get model.layout.selectedTab tabViews
|> Maybe.withDefault
(\addr model ->
[ div [] [ text "This can't happen." ] ]
)
)
addr
model
)
2016-03-14 10:00:58 +00:00
in
2016-04-05 04:26:22 +00:00
Layout.view
(Signal.forwardTo addr LayoutAction)
model.layout
2016-03-14 10:00:58 +00:00
{ header = Just header
, drawer = Just drawer
, tabs = Just tabTitles
, main = [ stylesheet, top ]
2016-03-14 10:00:58 +00:00
}
2016-04-05 04:26:22 +00:00
{- The following line is not needed when you manually set up
your html, as done with page.html. Removing it will then
fix the flicker you see on load.
-}
|>
Material.topWithScheme Color.Teal Color.Red
routerSignal : Signal Action
routerSignal =
Signal.map RoutingAction Routing.signal
2016-03-14 10:00:58 +00:00
2016-04-05 04:26:22 +00:00
init : ( Model, Effects.Effects Action )
init =
( model, Effects.none )
2016-03-14 10:00:58 +00:00
inputs : List (Signal.Signal Action)
inputs =
[ Layout.setupSizeChangeSignal LayoutAction
2016-04-05 04:26:22 +00:00
, routerSignal
2016-03-14 10:00:58 +00:00
]
app : StartApp.App Model
app =
2016-04-05 04:26:22 +00:00
StartApp.start
{ init = init
, view = view
, update = update
, inputs = inputs
}
2016-03-14 10:00:58 +00:00
main : Signal Html
main =
2016-04-05 04:26:22 +00:00
app.html
2016-03-14 10:00:58 +00:00
-- PORTS
port tasks : Signal (Task.Task Never ())
port tasks =
2016-04-05 04:26:22 +00:00
app.tasks
port routeRunTask : Task () ()
port routeRunTask =
Routing.run