-
Notifications
You must be signed in to change notification settings - Fork 111
/
Events.elm
265 lines (180 loc) · 5.78 KB
/
Events.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
module Element.Events exposing
( onClick, onDoubleClick, onMouseDown, onMouseUp, onMouseEnter, onMouseLeave, onMouseMove
, onFocus, onLoseFocus
-- , onClickCoords
-- , onClickPageCoords
-- , onClickScreenCoords
-- , onMouseCoords
-- , onMousePageCoords
-- , onMouseScreenCoords
)
{-|
## Mouse Events
@docs onClick, onDoubleClick, onMouseDown, onMouseUp, onMouseEnter, onMouseLeave, onMouseMove
## Focus Events
@docs onFocus, onLoseFocus
-}
import Element exposing (Attribute)
import Html.Events
import Internal.Model as Internal
import Json.Decode as Json
import VirtualDom
-- MOUSE EVENTS
{-| -}
onMouseDown : msg -> Attribute msg
onMouseDown =
Internal.Attr << Html.Events.onMouseDown
{-| -}
onMouseUp : msg -> Attribute msg
onMouseUp =
Internal.Attr << Html.Events.onMouseUp
{-| -}
onClick : msg -> Attribute msg
onClick =
Internal.Attr << Html.Events.onClick
{-| -}
onDoubleClick : msg -> Attribute msg
onDoubleClick =
Internal.Attr << Html.Events.onDoubleClick
{-| -}
onMouseEnter : msg -> Attribute msg
onMouseEnter =
Internal.Attr << Html.Events.onMouseEnter
{-| -}
onMouseLeave : msg -> Attribute msg
onMouseLeave =
Internal.Attr << Html.Events.onMouseLeave
{-| -}
onMouseMove : msg -> Attribute msg
onMouseMove msg =
on "mousemove" (Json.succeed msg)
-- onClickWith
-- { button = primary
-- , send = localCoords Button
-- }
-- type alias Click =
-- { button : Button
-- , send : Track
-- }
-- type Button = Primary | Secondary
-- type Track
-- = ElementCoords
-- | PageCoords
-- | ScreenCoords
-- |
{-| -}
onClickCoords : (Coords -> msg) -> Attribute msg
onClickCoords msg =
on "click" (Json.map msg localCoords)
{-| -}
onClickScreenCoords : (Coords -> msg) -> Attribute msg
onClickScreenCoords msg =
on "click" (Json.map msg screenCoords)
{-| -}
onClickPageCoords : (Coords -> msg) -> Attribute msg
onClickPageCoords msg =
on "click" (Json.map msg pageCoords)
{-| -}
onMouseCoords : (Coords -> msg) -> Attribute msg
onMouseCoords msg =
on "mousemove" (Json.map msg localCoords)
{-| -}
onMouseScreenCoords : (Coords -> msg) -> Attribute msg
onMouseScreenCoords msg =
on "mousemove" (Json.map msg screenCoords)
{-| -}
onMousePageCoords : (Coords -> msg) -> Attribute msg
onMousePageCoords msg =
on "mousemove" (Json.map msg pageCoords)
type alias Coords =
{ x : Int
, y : Int
}
screenCoords : Json.Decoder Coords
screenCoords =
Json.map2 Coords
(Json.field "screenX" Json.int)
(Json.field "screenY" Json.int)
{-| -}
localCoords : Json.Decoder Coords
localCoords =
Json.map2 Coords
(Json.field "offsetX" Json.int)
(Json.field "offsetY" Json.int)
pageCoords : Json.Decoder Coords
pageCoords =
Json.map2 Coords
(Json.field "pageX" Json.int)
(Json.field "pageY" Json.int)
-- FOCUS EVENTS
{-| -}
onLoseFocus : msg -> Attribute msg
onLoseFocus =
Internal.Attr << Html.Events.onBlur
{-| -}
onFocus : msg -> Attribute msg
onFocus =
Internal.Attr << Html.Events.onFocus
-- CUSTOM EVENTS
{-| Create a custom event listener. Normally this will not be necessary, but
you have the power! Here is how `onClick` is defined for example:
import Json.Decode as Json
onClick : msg -> Attribute msg
onClick message =
on "click" (Json.succeed message)
The first argument is the event name in the same format as with JavaScript's
[`addEventListener`][aEL] function.
The second argument is a JSON decoder. Read more about these [here][decoder].
When an event occurs, the decoder tries to turn the event object into an Elm
value. If successful, the value is routed to your `update` function. In the
case of `onClick` we always just succeed with the given `message`.
If this is confusing, work through the [Elm Architecture Tutorial][tutorial].
It really does help!
[aEL]: <https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener>
[decoder]: <http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode>
[tutorial]: <https://github.com/evancz/elm-architecture-tutorial/>
-}
on : String -> Json.Decoder msg -> Attribute msg
on event decode =
Internal.Attr <| Html.Events.on event decode
-- {-| Same as `on` but you can set a few options.
-- -}
-- onWithOptions : String -> Html.Events.Options -> Json.Decoder msg -> Attribute msg
-- onWithOptions event options decode =
-- Internal.Attr <| Html.Events.onWithOptions event options decode
-- COMMON DECODERS
{-| A `Json.Decoder` for grabbing `event.target.value`. We use this to define
`onInput` as follows:
import Json.Decode as Json
onInput : (String -> msg) -> Attribute msg
onInput tagger =
on "input" (Json.map tagger targetValue)
You probably will never need this, but hopefully it gives some insights into
how to make custom event handlers.
-}
targetValue : Json.Decoder String
targetValue =
Json.at [ "target", "value" ] Json.string
{-| A `Json.Decoder` for grabbing `event.target.checked`. We use this to define
`onCheck` as follows:
import Json.Decode as Json
onCheck : (Bool -> msg) -> Attribute msg
onCheck tagger =
on "input" (Json.map tagger targetChecked)
-}
targetChecked : Json.Decoder Bool
targetChecked =
Json.at [ "target", "checked" ] Json.bool
{-| A `Json.Decoder` for grabbing `event.keyCode`. This helps you define
keyboard listeners like this:
import Json.Decode as Json
onKeyUp : (Int -> msg) -> Attribute msg
onKeyUp tagger =
on "keyup" (Json.map tagger keyCode)
**Note:** It looks like the spec is moving away from `event.keyCode` and
towards `event.key`. Once this is supported in more browsers, we may add
helpers here for `onKeyUp`, `onKeyDown`, `onKeyPress`, etc.
-}
keyCode : Json.Decoder Int
keyCode =
Json.field "keyCode" Json.int