-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
window, document event listener interfaces +
- Loading branch information
Showing
5 changed files
with
199 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module BrowserApp.Document exposing (listenToEvent) | ||
|
||
{-| Helpers for `document` interaction as part of an [`Interface`](BrowserApp#Interface) | ||
@docs listenToEvent | ||
@docs resizeEventJsonDecoder | ||
-} | ||
|
||
import BrowserApp | ||
import Json.Decode | ||
import Json.Decode.Extra | ||
|
||
|
||
{-| An [`Interface`](BrowserApp#Interface) that listens for a specific `document` event | ||
like like keypress, keydown, keyup, click, mousemove, mousedown, mouseup | ||
-} | ||
listenToEvent : String -> BrowserApp.Interface Json.Decode.Value | ||
listenToEvent eventName = | ||
BrowserApp.DocumentEventListen { eventName = eventName, on = identity } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module BrowserApp.Window exposing | ||
( listenToEvent | ||
, listenToResize | ||
) | ||
|
||
{-| Helpers for `window` interaction as part of an [`Interface`](BrowserApp#Interface) | ||
@docs listenToEvent | ||
@docs resizeEventJsonDecoder | ||
-} | ||
|
||
import BrowserApp | ||
import Json.Decode | ||
import Json.Decode.Extra | ||
|
||
|
||
{-| An [`Interface`](BrowserApp#Interface) that listens for a specific `window` event | ||
-} | ||
listenToEvent : String -> BrowserApp.Interface Json.Decode.Value | ||
listenToEvent eventName = | ||
BrowserApp.WindowEventListen { eventName = eventName, on = identity } | ||
|
||
|
||
{-| An [`Interface`](BrowserApp#Interface) that listens changes to the inner window width and height. | ||
-} | ||
listenToResize : BrowserApp.Interface (Result Json.Decode.Error { width : Int, height : Int }) | ||
listenToResize = | ||
listenToEvent "resize" | ||
|> BrowserApp.on | ||
(\value -> | ||
value | ||
|> Json.Decode.decodeValue | ||
(Json.Decode.field "target" | ||
(Json.Decode.succeed (\width height -> { width = width, height = height }) | ||
|> Json.Decode.Extra.andMap (Json.Decode.field "innerWidth" Json.Decode.int) | ||
|> Json.Decode.Extra.andMap (Json.Decode.field "innerHeight" Json.Decode.int) | ||
) | ||
) | ||
) |