elm-mdl/Demo.elm

183 lines
3.6 KiB
Elm
Raw Normal View History

2016-03-08 16:30:09 +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)
2016-03-13 21:47:00 +00:00
import Array exposing (Array)
2016-03-08 16:30:09 +00:00
2016-03-13 21:47:00 +00:00
import Material.Layout as Layout exposing (defaultLayoutModel)
import Material
2016-03-08 16:30:09 +00:00
2016-03-13 21:47:00 +00:00
import Demo.Buttons
import Demo.Grid
import Demo.Textfields
2016-03-08 16:30:09 +00:00
-- MODEL
type alias Model =
{ layout : Layout.Model
2016-03-13 21:47:00 +00:00
, buttons : Demo.Buttons.Model
, textfields : Demo.Textfields.Model
2016-03-08 16:30:09 +00:00
}
layoutModel : Layout.Model
layoutModel =
2016-03-13 21:47:00 +00:00
{ defaultLayoutModel
| state = Layout.initState (List.length tabs)
2016-03-08 16:30:09 +00:00
}
model : Model
model =
{ layout = layoutModel
2016-03-13 21:47:00 +00:00
, buttons = Demo.Buttons.model
, textfields = Demo.Textfields.model
2016-03-08 16:30:09 +00:00
}
-- ACTION, UPDATE
type Action
= LayoutAction Layout.Action
2016-03-13 21:47:00 +00:00
| ButtonsAction Demo.Buttons.Action
| TextfieldAction Demo.Textfields.Action
2016-03-08 16:30:09 +00:00
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case action of
LayoutAction a ->
let
(l, e) = Layout.update a model.layout
in
({ model | layout = l }, Effects.map LayoutAction e)
ButtonsAction a ->
2016-03-13 21:47:00 +00:00
let
(b, e) = Demo.Buttons.update a model.buttons
2016-03-08 16:30:09 +00:00
in
({ model | buttons = b }, Effects.map ButtonsAction e)
2016-03-13 21:47:00 +00:00
TextfieldAction a ->
({ model | textfields = Demo.Textfields.update a model.textfields }
, Effects.none
)
2016-03-08 16:30:09 +00:00
-- VIEW
type alias Addr = Signal.Address Action
drawer : List Html
drawer =
2016-03-13 21:47:00 +00:00
[ Layout.title "Example drawer"
2016-03-08 16:30:09 +00:00
, Layout.navigation
2016-03-13 21:47:00 +00:00
[ Layout.link
[href "https://groups.google.com/forum/#!forum/elm-discuss"]
[text "Elm Discuss"]
, Layout.link
[href "http://elm-lang.org"]
[text "Elm"]
2016-03-08 16:30:09 +00:00
]
]
header : List Html
header =
[ Layout.title "elm-mdl"
, Layout.spacer
, Layout.navigation
[ Layout.link
[ href "https://www.getmdl.io/components/index.html" ]
[ text "MDL" ]
2016-03-13 21:47:00 +00:00
, Layout.link
[ href "https://www.google.com/design/spec/material-design/introduction.html"]
[ text "Material Design"]
2016-03-08 16:30:09 +00:00
]
]
2016-03-13 21:47:00 +00:00
tabs : List (String, Addr -> Model -> List Html)
tabs =
[ ("Buttons", \addr model ->
[Demo.Buttons.view (Signal.forwardTo addr ButtonsAction) model.buttons])
, ("Textfields", \addr model ->
[Demo.Textfields.view (Signal.forwardTo addr TextfieldAction) model.textfields])
, ("Grid", \addr model -> Demo.Grid.view)
2016-03-08 16:30:09 +00:00
]
2016-03-13 21:47:00 +00:00
tabViews : Array (Addr -> Model -> List Html)
tabViews = List.map snd tabs |> Array.fromList
2016-03-08 16:30:09 +00:00
2016-03-13 21:47:00 +00:00
tabTitles : List Html
tabTitles = List.map (fst >> text) tabs
2016-03-08 16:30:09 +00:00
view : Signal.Address Action -> Model -> Html
view addr model =
2016-03-13 21:47:00 +00:00
let top =
2016-03-08 16:30:09 +00:00
div
[ style
[ ("margin", "auto")
, ("width", "90%")
]
]
2016-03-13 21:47:00 +00:00
((Array.get model.layout.selectedTab tabViews
|> Maybe.withDefault (\addr model ->
[div [] [text "This can't happen."]]
)
) addr model)
2016-03-08 16:30:09 +00:00
in
2016-03-13 21:47:00 +00:00
Layout.view (Signal.forwardTo addr LayoutAction) model.layout
{ header = Just header
, drawer = Just drawer
, tabs = Just tabTitles
, main = [ top ]
}
|> Material.topWithColors Material.Teal Material.Red
2016-03-08 16:30:09 +00:00
init : (Model, Effects.Effects Action)
init = (model, Effects.none)
inputs : List (Signal.Signal Action)
inputs =
[ Layout.setupSizeChangeSignal LayoutAction
]
app : StartApp.App Model
app =
StartApp.start
{ init = init
, view = view
, update = update
, inputs = inputs
}
main : Signal Html
main =
app.html
-- PORTS
port tasks : Signal (Task.Task Never ())
port tasks =
app.tasks