mirror of
https://github.com/correl/elm-mdl.git
synced 2024-12-18 03:00:11 +00:00
Removed native code, now depends on debois/elm-dom.
This commit is contained in:
parent
bba58eed2c
commit
d05539bb66
4 changed files with 25 additions and 149 deletions
|
@ -17,6 +17,7 @@
|
||||||
],
|
],
|
||||||
"native-modules": true,
|
"native-modules": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"debois/elm-dom": "1.0.0 <= v < 2.0.0",
|
||||||
"elm-lang/core": "3.0.0 <= v < 4.0.0",
|
"elm-lang/core": "3.0.0 <= v < 4.0.0",
|
||||||
"evancz/elm-effects": "2.0.1 <= v < 3.0.0",
|
"evancz/elm-effects": "2.0.1 <= v < 3.0.0",
|
||||||
"evancz/elm-html": "4.0.2 <= v < 5.0.0",
|
"evancz/elm-html": "4.0.2 <= v < 5.0.0",
|
||||||
|
|
|
@ -2,10 +2,7 @@ module Material.Aux where
|
||||||
|
|
||||||
import Html
|
import Html
|
||||||
import Html.Attributes
|
import Html.Attributes
|
||||||
import Html.Events
|
|
||||||
import Json.Decode as Json exposing ((:=))
|
|
||||||
import Effects exposing (Effects)
|
import Effects exposing (Effects)
|
||||||
import Native.Material
|
|
||||||
|
|
||||||
filter : (a -> List b -> c) -> a -> List (Maybe b) -> c
|
filter : (a -> List b -> c) -> a -> List (Maybe b) -> c
|
||||||
filter elem attr html =
|
filter elem attr html =
|
||||||
|
@ -23,18 +20,6 @@ mapWithIndex f xs =
|
||||||
loop 0 xs
|
loop 0 xs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
onClick' : Signal.Address a -> a -> Html.Attribute
|
|
||||||
onClick' address x =
|
|
||||||
Html.Events.onWithOptions
|
|
||||||
"click"
|
|
||||||
{ stopPropagation = True
|
|
||||||
, preventDefault = True
|
|
||||||
}
|
|
||||||
Json.value
|
|
||||||
(\_ -> Signal.message address x)
|
|
||||||
|
|
||||||
|
|
||||||
effect : Effects b -> a -> (a, Effects b)
|
effect : Effects b -> a -> (a, Effects b)
|
||||||
effect e x = (x, e)
|
effect e x = (x, e)
|
||||||
|
|
||||||
|
@ -47,63 +32,6 @@ clip : comparable -> comparable -> comparable -> comparable
|
||||||
clip lower upper k = Basics.max lower (Basics.min k upper)
|
clip lower upper k = Basics.max lower (Basics.min k upper)
|
||||||
|
|
||||||
|
|
||||||
type alias Rectangle =
|
|
||||||
{ width : Float
|
|
||||||
, height : Float
|
|
||||||
, top : Float
|
|
||||||
, right : Float
|
|
||||||
, bottom : Float
|
|
||||||
, left : Float
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
rectangleDecoder : Json.Decoder Rectangle
|
|
||||||
rectangleDecoder =
|
|
||||||
"boundingClientRect" :=
|
|
||||||
Json.object6 Rectangle
|
|
||||||
("width" := Json.float)
|
|
||||||
("height" := Json.float)
|
|
||||||
("top" := Json.float)
|
|
||||||
("right" := Json.float)
|
|
||||||
("bottom" := Json.float)
|
|
||||||
("left" := Json.float)
|
|
||||||
|
|
||||||
|
|
||||||
{-| Options for an event listener. If `stopPropagation` is true, it means the
|
|
||||||
event stops traveling through the DOM so it will not trigger any other event
|
|
||||||
listeners. If `preventDefault` is true, any built-in browser behavior related
|
|
||||||
to the event is prevented. For example, this is used with touch events when you
|
|
||||||
want to treat them as gestures of your own, not as scrolls. If `withGeometry`
|
|
||||||
is true, the event object will be augmented with geometry information for the
|
|
||||||
events target node; use `geometryDecoder` to decode.
|
|
||||||
-}
|
|
||||||
type alias Options =
|
|
||||||
{ stopPropagation : Bool
|
|
||||||
, preventDefault : Bool
|
|
||||||
, withGeometry : Bool
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
{-| Everything is `False` by default.
|
|
||||||
defaultOptions =
|
|
||||||
{ stopPropagation = False
|
|
||||||
, preventDefault = False
|
|
||||||
, withGeometry = False
|
|
||||||
}
|
|
||||||
-}
|
|
||||||
defaultOptions : Options
|
|
||||||
defaultOptions =
|
|
||||||
{ stopPropagation = False
|
|
||||||
, preventDefault = False
|
|
||||||
, withGeometry = False
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
on : String -> Options -> Json.Decoder a -> (a -> Signal.Message) -> Html.Attribute
|
|
||||||
on =
|
|
||||||
Native.Material.on
|
|
||||||
|
|
||||||
|
|
||||||
blurOn : String -> Html.Attribute
|
blurOn : String -> Html.Attribute
|
||||||
blurOn evt =
|
blurOn evt =
|
||||||
Html.Attributes.attribute ("on" ++ evt) <| "this.blur()"
|
Html.Attributes.attribute ("on" ++ evt) <| "this.blur()"
|
||||||
|
|
|
@ -7,13 +7,14 @@ import Json.Decode as Json exposing ((:=), at)
|
||||||
import Effects exposing (Effects, tick, none)
|
import Effects exposing (Effects, tick, none)
|
||||||
|
|
||||||
import Material.Aux exposing (Rectangle, rectangleDecoder, effect)
|
import Material.Aux exposing (Rectangle, rectangleDecoder, effect)
|
||||||
|
import DOM
|
||||||
|
|
||||||
|
|
||||||
-- MODEL
|
-- MODEL
|
||||||
|
|
||||||
|
|
||||||
type alias Metrics =
|
type alias Metrics =
|
||||||
{ rect : Rectangle
|
{ rect : DOM.Rectangle
|
||||||
, x : Float
|
, x : Float
|
||||||
, y : Float
|
, y : Float
|
||||||
}
|
}
|
||||||
|
@ -41,7 +42,7 @@ model =
|
||||||
|
|
||||||
|
|
||||||
type alias Geometry =
|
type alias Geometry =
|
||||||
{ rect : Rectangle
|
{ rect : DOM.Rectangle
|
||||||
, clientX : Maybe Float
|
, clientX : Maybe Float
|
||||||
, clientY : Maybe Float
|
, clientY : Maybe Float
|
||||||
, touchX : Maybe Float
|
, touchX : Maybe Float
|
||||||
|
@ -52,21 +53,22 @@ type alias Geometry =
|
||||||
geometryDecoder : Json.Decoder Geometry
|
geometryDecoder : Json.Decoder Geometry
|
||||||
geometryDecoder =
|
geometryDecoder =
|
||||||
Json.object5 Geometry
|
Json.object5 Geometry
|
||||||
rectangleDecoder
|
(DOM.target DOM.boundingClientRect)
|
||||||
(Json.maybe ("clientX" := Json.float))
|
(Json.maybe ("clientX" := Json.float))
|
||||||
(Json.maybe ("clientY" := Json.float))
|
(Json.maybe ("clientY" := Json.float))
|
||||||
(Json.maybe (at ["touches", "0", "clientX"] Json.float))
|
(Json.maybe (at ["touches", "0", "clientX"] Json.float))
|
||||||
(Json.maybe (at ["touches", "0", "clientY"] Json.float))
|
(Json.maybe (at ["touches", "0", "clientY"] Json.float))
|
||||||
|
|
||||||
|
|
||||||
computeMetrics : Geometry -> Metrics
|
computeMetrics : Geometry -> Maybe Metrics
|
||||||
computeMetrics g =
|
computeMetrics g =
|
||||||
let
|
let
|
||||||
rect = g.rect
|
rect = g.rect
|
||||||
set x y = (x - rect.left, y - rect.top)
|
set x y = (x - rect.left, y - rect.top) |> Just
|
||||||
(x,y) = case (g.clientX, g.clientY, g.touchX, g.touchY) of
|
in
|
||||||
|
(case (g.clientX, g.clientY, g.touchX, g.touchY) of
|
||||||
(Just 0.0, Just 0.0, _, _) ->
|
(Just 0.0, Just 0.0, _, _) ->
|
||||||
(rect.width / 2.0, rect.height / 2.0)
|
(rect.width / 2.0, rect.height / 2.0) |> Just
|
||||||
|
|
||||||
(Just x, Just y, _, _) ->
|
(Just x, Just y, _, _) ->
|
||||||
set x y
|
set x y
|
||||||
|
@ -75,12 +77,9 @@ computeMetrics g =
|
||||||
set x y
|
set x y
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
Debug.crash "Impossible value from geometryDecoder"
|
Nothing
|
||||||
in
|
|
||||||
{ rect = rect
|
) |> Maybe.map (\(x,y) -> Metrics rect x y)
|
||||||
, x = x
|
|
||||||
, y = y
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type Action
|
type Action
|
||||||
|
@ -95,7 +94,7 @@ update action model =
|
||||||
Down geometry ->
|
Down geometry ->
|
||||||
{ model
|
{ model
|
||||||
| animation = Frame 0
|
| animation = Frame 0
|
||||||
, metrics = computeMetrics geometry |> Just
|
, metrics = computeMetrics geometry
|
||||||
}
|
}
|
||||||
|> effect (tick <| \_ -> Tick)
|
|> effect (tick <| \_ -> Tick)
|
||||||
|
|
||||||
|
@ -118,12 +117,9 @@ update action model =
|
||||||
|
|
||||||
downOn : String -> Signal.Address Action -> Attribute
|
downOn : String -> Signal.Address Action -> Attribute
|
||||||
downOn name addr =
|
downOn name addr =
|
||||||
Material.Aux.on
|
--Material.Aux.on
|
||||||
|
Html.Events.on
|
||||||
name
|
name
|
||||||
{ preventDefault = False
|
|
||||||
, stopPropagation = False
|
|
||||||
, withGeometry = True
|
|
||||||
}
|
|
||||||
geometryDecoder
|
geometryDecoder
|
||||||
(Down >> Signal.message addr)
|
(Down >> Signal.message addr)
|
||||||
|
|
||||||
|
@ -144,7 +140,8 @@ styles m frame =
|
||||||
offset = "translate(" ++ toPx m.x ++ ", " ++ toPx m.y ++ ")"
|
offset = "translate(" ++ toPx m.x ++ ", " ++ toPx m.y ++ ")"
|
||||||
transformString = "translate(-50%, -50%) " ++ offset ++ scale
|
transformString = "translate(-50%, -50%) " ++ offset ++ scale
|
||||||
r = m.rect
|
r = m.rect
|
||||||
rippleSize = sqrt (r.width * r.width + r.height * r.height) * 2 + 2 |> toPx
|
rippleSize =
|
||||||
|
sqrt (r.width * r.width + r.height * r.height) * 2.0 + 2.0 |> toPx
|
||||||
in
|
in
|
||||||
[ ("width", rippleSize)
|
[ ("width", rippleSize)
|
||||||
, ("height", rippleSize)
|
, ("height", rippleSize)
|
||||||
|
@ -173,12 +170,12 @@ view addr attrs model =
|
||||||
:: attrs
|
:: attrs
|
||||||
)
|
)
|
||||||
[ span
|
[ span
|
||||||
[ classList
|
[ classList
|
||||||
[ ("mdl-ripple", True)
|
[ ("mdl-ripple", True)
|
||||||
, ("is-animating", model.animation /= Frame 0)
|
, ("is-animating", model.animation /= Frame 0)
|
||||||
, ("is-visible", model.animation /= Inert)
|
, ("is-visible", model.animation /= Inert)
|
||||||
]
|
|
||||||
, style styling
|
|
||||||
]
|
]
|
||||||
[]
|
, style styling
|
||||||
|
]
|
||||||
|
[]
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
/*
|
|
||||||
* Helper functions for accessing Google Material Design lite.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// ELM Boilerplate
|
|
||||||
Elm.Native.Material = {};
|
|
||||||
Elm.Native.Material.make = function(elm) {
|
|
||||||
elm.Native = elm.Native || {};
|
|
||||||
elm.Native.Material = elm.Native.Material || {};
|
|
||||||
if (elm.Native.Material.values) {
|
|
||||||
return elm.Native.Material.values;
|
|
||||||
}
|
|
||||||
|
|
||||||
var Signal = Elm.Native.Signal.make(elm);
|
|
||||||
var Json = Elm.Native.Json.make(elm);
|
|
||||||
|
|
||||||
function property(key, value) {
|
|
||||||
return { key: key, value: value };
|
|
||||||
}
|
|
||||||
|
|
||||||
function on(name, options, decoder, createMessage)
|
|
||||||
{
|
|
||||||
function eventHandler(event)
|
|
||||||
{
|
|
||||||
if (options.withGeometry)
|
|
||||||
{
|
|
||||||
event.boundingClientRect = event.currentTarget.getBoundingClientRect();
|
|
||||||
}
|
|
||||||
var value = A2(Json.runDecoderValue, decoder, event);
|
|
||||||
if (value.ctor === 'Ok')
|
|
||||||
{
|
|
||||||
if (options.stopPropagation)
|
|
||||||
{
|
|
||||||
event.stopPropagation();
|
|
||||||
}
|
|
||||||
if (options.preventDefault)
|
|
||||||
{
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
Signal.sendMessage(createMessage(value._0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return property('on' + name, eventHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
return elm.Native.Material.values = {
|
|
||||||
on : F4(on)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue