Current section

Files

Jump to
lustre src lustre@event.erl
Raw

src/lustre@event.erl

-module(lustre@event).
-compile(no_auto_import).
-export([on/2, on_click/1, on_mouse_down/1, on_mouse_up/1, on_mouse_enter/1, on_mouse_leave/1, on_mouse_over/1, on_mouse_out/1, on_keypress/1, on_keydown/1, on_keyup/1, on_input/1, on_check/1, on_submit/1, on_focus/1, on_blur/1, value/1, checked/1, mouse_position/1]).
-spec on(binary(), fun((gleam@dynamic:dynamic(), fun((GHQ) -> nil)) -> nil)) -> lustre@attribute:attribute(GHQ).
on(Name, Handler) ->
lustre@attribute:event(Name, Handler).
-spec on_click(GHS) -> lustre@attribute:attribute(GHS).
on_click(Msg) ->
on(
<<"click"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_down(GHU) -> lustre@attribute:attribute(GHU).
on_mouse_down(Msg) ->
on(
<<"mouseDown"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_up(GHW) -> lustre@attribute:attribute(GHW).
on_mouse_up(Msg) ->
on(
<<"mouseUp"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_enter(GHY) -> lustre@attribute:attribute(GHY).
on_mouse_enter(Msg) ->
on(
<<"mouseEnter"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_leave(GIA) -> lustre@attribute:attribute(GIA).
on_mouse_leave(Msg) ->
on(
<<"mouseLeave"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_over(GIC) -> lustre@attribute:attribute(GIC).
on_mouse_over(Msg) ->
on(
<<"mouseOver"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_mouse_out(GIE) -> lustre@attribute:attribute(GIE).
on_mouse_out(Msg) ->
on(
<<"mouseOut"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_keypress(fun((binary()) -> GIG)) -> lustre@attribute:attribute(GIG).
on_keypress(Msg) ->
on(
<<"keyPress"/utf8>>,
fun(Event, Dispatch) ->
_pipe = Event,
_pipe@1 = (gleam@dynamic:field(
<<"key"/utf8>>,
fun gleam@dynamic:string/1
))(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Msg),
_pipe@3 = gleam@result:map(_pipe@2, Dispatch),
gleam@result:unwrap(_pipe@3, nil)
end
).
-spec on_keydown(fun((binary()) -> GII)) -> lustre@attribute:attribute(GII).
on_keydown(Msg) ->
on(
<<"keyDown"/utf8>>,
fun(Event, Dispatch) ->
_pipe = Event,
_pipe@1 = (gleam@dynamic:field(
<<"key"/utf8>>,
fun gleam@dynamic:string/1
))(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Msg),
_pipe@3 = gleam@result:map(_pipe@2, Dispatch),
gleam@result:unwrap(_pipe@3, nil)
end
).
-spec on_keyup(fun((binary()) -> GIK)) -> lustre@attribute:attribute(GIK).
on_keyup(Msg) ->
on(
<<"keyUp"/utf8>>,
fun(Event, Dispatch) ->
_pipe = Event,
_pipe@1 = (gleam@dynamic:field(
<<"key"/utf8>>,
fun gleam@dynamic:string/1
))(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Msg),
_pipe@3 = gleam@result:map(_pipe@2, Dispatch),
gleam@result:unwrap(_pipe@3, nil)
end
).
-spec on_input(fun((binary()) -> GIM)) -> lustre@attribute:attribute(GIM).
on_input(Msg) ->
on(
<<"change"/utf8>>,
fun(Event, Dispatch) ->
_pipe = Event,
_pipe@1 = value(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Msg),
_pipe@3 = gleam@result:map(_pipe@2, Dispatch),
gleam@result:unwrap(_pipe@3, nil)
end
).
-spec on_check(fun((boolean()) -> GIO)) -> lustre@attribute:attribute(GIO).
on_check(Msg) ->
on(
<<"change"/utf8>>,
fun(Event, Dispatch) ->
_pipe = Event,
_pipe@1 = (gleam@dynamic:field(
<<"target"/utf8>>,
gleam@dynamic:field(
<<"checked"/utf8>>,
fun gleam@dynamic:bool/1
)
))(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Msg),
_pipe@3 = gleam@result:map(_pipe@2, Dispatch),
gleam@result:unwrap(_pipe@3, nil)
end
).
-spec on_submit(GIQ) -> lustre@attribute:attribute(GIQ).
on_submit(Msg) ->
on(
<<"submit"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_focus(GIS) -> lustre@attribute:attribute(GIS).
on_focus(Msg) ->
on(
<<"focus"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec on_blur(GIU) -> lustre@attribute:attribute(GIU).
on_blur(Msg) ->
on(
<<"blur"/utf8>>,
fun(_, Dispatch) ->
Dispatch(Msg)
end
).
-spec value(gleam@dynamic:dynamic()) -> {ok, binary()} |
{error, list(gleam@dynamic:decode_error())}.
value(Event) ->
_pipe = Event,
(gleam@dynamic:field(
<<"target"/utf8>>,
gleam@dynamic:field(<<"value"/utf8>>, fun gleam@dynamic:string/1)
))(_pipe).
-spec checked(gleam@dynamic:dynamic()) -> {ok, boolean()} |
{error, list(gleam@dynamic:decode_error())}.
checked(Event) ->
_pipe = Event,
(gleam@dynamic:field(
<<"target"/utf8>>,
gleam@dynamic:field(<<"checked"/utf8>>, fun gleam@dynamic:bool/1)
))(_pipe).
-spec mouse_position(gleam@dynamic:dynamic()) -> {ok, {float(), float()}} |
{error, list(gleam@dynamic:decode_error())}.
mouse_position(Event) ->
gleam@result:then(
(gleam@dynamic:field(<<"clientX"/utf8>>, fun gleam@dynamic:float/1))(
Event
),
fun(X) ->
gleam@result:then(
(gleam@dynamic:field(
<<"clientY"/utf8>>,
fun gleam@dynamic:float/1
))(Event),
fun(Y) ->
{ok, {X, Y}}
end
)
end
).