elm-mdl/Demo/Buttons.elm

145 lines
3.3 KiB
Elm
Raw Normal View History

2016-03-13 21:47:00 +00:00
module Demo.Buttons where
2016-03-08 16:30:09 +00:00
import Dict
import Html exposing (..)
import Html.Attributes exposing (..)
import Effects
2016-03-13 21:47:00 +00:00
import Material.Button as Button exposing (..)
2016-03-08 16:30:09 +00:00
import Material.Grid as Grid
import Material.Icon as Icon
-- MODEL
type alias Index = (Int, Int)
tabulate' : Int -> List a -> List (Int, a)
tabulate' i ys =
case ys of
[] -> []
y :: ys -> (i, y) :: tabulate' (i+1) ys
tabulate : List a -> List (Int, a)
tabulate = tabulate' 0
2016-03-13 21:47:00 +00:00
type alias View =
Signal.Address Button.Action -> Button.Model -> Coloring -> List Html -> Html
type alias View' =
Signal.Address Button.Action -> Button.Model -> Html
view' : View -> Coloring -> Html -> Signal.Address Button.Action -> Button.Model -> Html
view' view coloring elem addr model =
view addr model coloring [elem]
describe : String -> Bool -> Coloring -> String
describe kind ripple coloring =
let
c =
case coloring of
Plain -> "plain"
Colored -> "colored"
Primary -> "primary"
Accent -> "accent"
in
kind ++ ", " ++ c ++ if ripple then " w/ripple" else ""
row : (String, Html, View) -> Bool -> List (Int, (Bool, String, View'))
row (kind, elem, v) ripple =
2016-03-08 16:30:09 +00:00
[ Plain, Colored, Primary, Accent ]
2016-03-13 21:47:00 +00:00
|> List.map (\c -> (ripple, describe kind ripple c, view' v c elem))
2016-03-08 16:30:09 +00:00
|> tabulate
2016-03-13 21:47:00 +00:00
buttons : List (List (Index, (Bool, String, View')))
2016-03-08 16:30:09 +00:00
buttons =
2016-03-13 21:47:00 +00:00
[ ("flat", text "Flat Button", Button.flat)
, ("raised", text "Raised Button", Button.raised)
, ("FAB", Icon.i "add", Button.fab)
, ("mini-FAB", Icon.i "zoom_in", Button.minifab)
, ("icon", Icon.i "flight_land", Button.icon)
]
2016-03-08 16:30:09 +00:00
|> List.concatMap (\a -> [row a False, row a True])
|> tabulate
|> List.map (\(i, row) -> List.map (\(j, x) -> ((i,j), x)) row)
model : Model
model =
{ clicked = ""
, buttons =
buttons
2016-03-13 21:47:00 +00:00
|> List.concatMap (List.map <| \(idx, (ripple, _, _)) -> (idx, Button.model ripple))
2016-03-08 16:30:09 +00:00
|> Dict.fromList
}
-- ACTION, UPDATE
type Action = Action Index Button.Action
type alias Model =
{ clicked : String
, buttons : Dict.Dict Index Button.Model
}
update : Action -> Model -> (Model, Effects.Effects Action)
update (Action idx action) model =
Dict.get idx model.buttons
|> Maybe.map (\m0 ->
let
(m1, e) = Button.update action m0
in
({ model | buttons = Dict.insert idx m1 model.buttons }, Effects.map (Action idx) e)
)
|> Maybe.withDefault (model, Effects.none)
-- VIEW
view : Signal.Address Action -> Model -> Html
view addr model =
buttons |> List.concatMap (\row ->
2016-03-13 21:47:00 +00:00
row |> List.concatMap (\(idx, (ripple, description, view)) ->
2016-03-08 16:30:09 +00:00
let model' =
Dict.get idx model.buttons |> Maybe.withDefault (Button.model False)
in
2016-03-13 21:47:00 +00:00
[ Grid.cell
[ Grid.size Grid.All 3]
2016-03-08 16:30:09 +00:00
[ div
[ style
[ ("text-align", "center")
, ("margin-top", "1em")
, ("margin-bottom", "1em")
]
]
2016-03-13 21:47:00 +00:00
[ view
2016-03-08 16:30:09 +00:00
(Signal.forwardTo addr (Action idx))
model'
, div
[ style
[ ("font-size", "9pt")
, ("margin-top", "1em")
]
]
2016-03-13 21:47:00 +00:00
[ text description
]
2016-03-08 16:30:09 +00:00
]
]
2016-03-13 21:47:00 +00:00
]
2016-03-08 16:30:09 +00:00
)
)
2016-03-13 21:47:00 +00:00
|> Grid.grid