elm-mdl/examples/Demo.elm

230 lines
5.4 KiB
Elm
Raw Normal View History

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-03-17 12:31:43 +00:00
import Material.Color as Color
2016-03-14 10:00:58 +00:00
import Material.Layout as Layout exposing (defaultLayoutModel)
2016-03-29 15:25:33 +00:00
2016-03-17 12:31:43 +00:00
import Material exposing (lift, lift')
import Material.Style as Style
import Material.Icon as Icon
2016-03-14 10:00:58 +00:00
import Material
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-21 11:24:00 +00:00
--import Demo.Template
2016-03-14 10:00:58 +00:00
-- MODEL
2016-03-26 11:13:15 +00:00
x = 0 + "foo"
2016-03-14 10:00:58 +00:00
layoutModel : Layout.Model
layoutModel =
{ defaultLayoutModel
| state = Layout.initState (List.length tabs)
, mode = Layout.Waterfall True
, fixedHeader = False
}
2016-03-14 10:00:58 +00:00
type alias Model =
{ layout : Layout.Model
, buttons : Demo.Buttons.Model
, textfields : Demo.Textfields.Model
2016-03-17 12:31:43 +00:00
, snackbar : Demo.Snackbar.Model
2016-03-21 11:24:00 +00:00
--, template : Demo.Template.Model
2016-03-14 10:00:58 +00:00
}
model : Model
model =
{ layout = layoutModel
, buttons = Demo.Buttons.model
, textfields = Demo.Textfields.model
2016-03-17 12:31:43 +00:00
, snackbar = Demo.Snackbar.model
2016-03-21 11:24:00 +00:00
--, template = Demo.Template.model
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-03-21 11:24:00 +00:00
--| TemplateAction Demo.Template.Action
2016-03-14 10:00:58 +00:00
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
2016-03-17 12:31:43 +00:00
case Debug.log "Action: " action of
LayoutAction a -> lift .layout (\m x->{m|layout =x}) LayoutAction Layout.update a model
ButtonsAction a -> lift .buttons (\m x->{m|buttons =x}) ButtonsAction Demo.Buttons.update a model
TextfieldAction a -> lift' .textfields (\m x->{m|textfields=x}) Demo.Textfields.update a model
SnackbarAction a -> lift .snackbar (\m x->{m|snackbar =x}) SnackbarAction Demo.Snackbar.update a model
2016-03-21 11:24:00 +00:00
--TemplateAction a -> lift .template (\m x->{m|template =x}) TemplateAction Demo.Template.update a model
2016-03-14 10:00:58 +00:00
-- VIEW
type alias Addr = Signal.Address Action
drawer : List Html
drawer =
[ Layout.title "Example drawer"
, Layout.navigation
[ Layout.link
[ href "https://www.getmdl.io/components/index.html" ]
[ text "MDL" ]
2016-03-14 10:00:58 +00:00
, Layout.link
[ href "https://www.google.com/design/spec/material-design/introduction.html"]
[ text "Material Design"]
2016-03-14 10:00:58 +00:00
]
]
header : List Html
header =
[ Layout.row
[ Layout.title "elm-mdl"
, Layout.spacer
, Layout.navigation
[ Layout.link
[href "https://github.com/debois/elm-mdl"]
[span [] [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
]
]
tabs : List (String, Addr -> Model -> List Html)
tabs =
2016-03-17 12:31:43 +00:00
[ ("Snackbar", \addr model ->
[Demo.Snackbar.view (Signal.forwardTo addr SnackbarAction) model.snackbar])
2016-03-14 10:00:58 +00:00
, ("Textfields", \addr model ->
[Demo.Textfields.view (Signal.forwardTo addr TextfieldAction) model.textfields])
2016-03-17 12:31:43 +00:00
, ("Buttons", \addr model ->
[Demo.Buttons.view (Signal.forwardTo addr ButtonsAction) model.buttons])
2016-03-14 10:00:58 +00:00
, ("Grid", \addr model -> Demo.Grid.view)
2016-03-26 11:13:15 +00:00
, ("Badges", \addr model -> Demo.Badges.view )
2016-03-21 11:24:00 +00:00
{-
, ("Template", \addr model ->
[Demo.Template.view (Signal.forwardTo addr TemplateAction) model.template])
2016-03-21 11:24:00 +00:00
-}
2016-03-14 10:00:58 +00:00
]
tabViews : Array (Addr -> Model -> List Html)
tabViews = List.map snd tabs |> Array.fromList
tabTitles : List Html
tabTitles = List.map (fst >> text) tabs
stylesheet : Html
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-03-21 23:01:46 +00:00
p, blockquote {
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 =
let top =
div
[ style
2016-03-21 23:01:46 +00:00
[ ("margin", "auto")
2016-03-21 05:02:39 +00:00
, ("padding-left", "5%")
, ("padding-right", "5%")
2016-03-14 10:00:58 +00:00
]
]
((Array.get model.layout.selectedTab tabViews
|> Maybe.withDefault (\addr model ->
[div [] [text "This can't happen."]]
)
) addr model)
in
Layout.view (Signal.forwardTo addr LayoutAction) model.layout
2016-03-29 15:25:33 +00:00
<<<<<<< HEAD
2016-03-14 10:00:58 +00:00
{ header = Just header
, drawer = Just drawer
, tabs = Just tabTitles
, main = [ stylesheet, top ]
2016-03-29 15:25:33 +00:00
=======
{ header = header
, drawer = drawer
, tabs = tabTitles
2016-03-14 10:00:58 +00:00
, main = [ top ]
2016-03-29 15:25:33 +00:00
>>>>>>> waterfall
2016-03-14 10:00:58 +00:00
}
{- The following line is not needed when you manually set up
2016-03-15 16:47:47 +00:00
your html, as done with page.html. Removing it will then
2016-03-15 16:53:41 +00:00
fix the flicker you see on load.
-}
2016-03-19 22:47:21 +00:00
|> Material.topWithScheme Color.Teal Color.Red
2016-03-14 10:00:58 +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