Skip to content

Commit

Permalink
use keyword lists internally
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Jan 30, 2024
1 parent 2fccd8a commit 024216c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion assets/js/phoenix_live_view/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let JS = {

// commands

exec_exec(eventType, phxEvent, view, sourceEl, el, [attr, to]){
exec_exec(eventType, phxEvent, view, sourceEl, el, {attr, to}){
let nodes = to ? DOM.all(document, to) : [sourceEl]
nodes.forEach(node => {
let encodedJS = node.getAttribute(attr)
Expand Down
2 changes: 1 addition & 1 deletion assets/test/js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ describe("JS", () => {
test("executes command", done => {
let view = setupView(`
<div id="modal" phx-remove='[["push", {"event": "clicked"}]]'>modal</div>
<div id="click" phx-click='[["exec",["phx-remove","#modal"]]]'></div>
<div id="click" phx-click='[["exec",{"attr": "phx-remove", "to": "#modal"}]]'></div>
`)
let click = document.querySelector("#click")
view.pushEvent = (eventType, sourceEl, targetCtx, event, meta) => {
Expand Down
63 changes: 30 additions & 33 deletions lib/phoenix_live_view/js.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ defmodule Phoenix.LiveView.JS do
|> put_target()
|> put_value()

put_op(js, "push", Enum.into(opts, %{event: event}))
put_op(js, "push", Keyword.put(opts, :event, event))
end

@doc """
Expand Down Expand Up @@ -223,12 +223,12 @@ defmodule Phoenix.LiveView.JS do
@doc "See `dispatch/2`."
def dispatch(%JS{} = js, event, opts) do
opts = validate_keys(opts, :dispatch, [:to, :detail, :bubbles])
args = %{event: event, to: opts[:to]}
args = [event: event, to: opts[:to]]

args =
case Keyword.fetch(opts, :bubbles) do
{:ok, val} when is_boolean(val) ->
Map.put(args, :bubbles, val)
Keyword.put(args, :bubbles, val)

{:ok, other} ->
raise ArgumentError, "expected :bubbles to be a boolean, got: #{inspect(other)}"
Expand All @@ -255,7 +255,7 @@ defmodule Phoenix.LiveView.JS do
"""

{_, {:ok, detail}} ->
Map.put(args, :detail, detail)
Keyword.put(args, :detail, detail)

{_, :error} ->
args
Expand Down Expand Up @@ -309,13 +309,13 @@ defmodule Phoenix.LiveView.JS do
out_classes = transition_class_names(opts[:out])
time = opts[:time]

put_op(js, "toggle", %{
put_op(js, "toggle",
to: opts[:to],
display: opts[:display],
ins: in_classes,
outs: out_classes,
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -360,12 +360,12 @@ defmodule Phoenix.LiveView.JS do
transition = transition_class_names(opts[:transition])
time = opts[:time]

put_op(js, "show", %{
put_op(js, "show",
to: opts[:to],
display: opts[:display],
transition: transition,
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -409,11 +409,11 @@ defmodule Phoenix.LiveView.JS do
transition = transition_class_names(opts[:transition])
time = opts[:time]

put_op(js, "hide", %{
put_op(js, "hide",
to: opts[:to],
transition: transition,
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -455,12 +455,12 @@ defmodule Phoenix.LiveView.JS do
opts = validate_keys(opts, :add_class, [:to, :transition, :time])
time = opts[:time]

put_op(js, "add_class", %{
put_op(js, "add_class",
to: opts[:to],
names: class_names(names),
transition: transition_class_names(opts[:transition]),
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -500,12 +500,12 @@ defmodule Phoenix.LiveView.JS do
opts = validate_keys(opts, :toggle_class, [:to, :transition, :time])
time = opts[:time]

put_op(js, "toggle_class", %{
put_op(js, "toggle_class",
to: opts[:to],
names: class_names(names),
transition: transition_class_names(opts[:transition]),
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -547,12 +547,12 @@ defmodule Phoenix.LiveView.JS do
opts = validate_keys(opts, :remove_class, [:to, :transition, :time])
time = opts[:time]

put_op(js, "remove_class", %{
put_op(js, "remove_class",
to: opts[:to],
names: class_names(names),
transition: transition_class_names(opts[:transition]),
time: time
})
)
end

@doc """
Expand Down Expand Up @@ -598,11 +598,11 @@ defmodule Phoenix.LiveView.JS do
opts = validate_keys(opts, :transition, [:to, :time])
time = opts[:time]

put_op(js, "transition", %{
put_op(js, "transition",
time: time,
to: opts[:to],
transition: transition_class_names(transition)
})
)
end

@doc """
Expand Down Expand Up @@ -632,7 +632,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `set_attribute/1`."
def set_attribute(%JS{} = js, {attr, val}, opts) when is_list(opts) do
opts = validate_keys(opts, :set_attribute, [:to])
put_op(js, "set_attr", %{to: opts[:to], attr: [attr, val]})
put_op(js, "set_attr", to: opts[:to], attr: [attr, val])
end

@doc """
Expand Down Expand Up @@ -662,7 +662,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `remove_attribute/1`."
def remove_attribute(%JS{} = js, attr, opts) when is_list(opts) do
opts = validate_keys(opts, :remove_attribute, [:to])
put_op(js, "remove_attr", %{to: opts[:to], attr: attr})
put_op(js, "remove_attr", to: opts[:to], attr: attr)
end

@doc """
Expand Down Expand Up @@ -707,12 +707,12 @@ defmodule Phoenix.LiveView.JS do
@doc "See `toggle_attribute/1`."
def toggle_attribute(%JS{} = js, {attr, val}, opts) when is_list(opts) do
opts = validate_keys(opts, :toggle_attribute, [:to])
put_op(js, "toggle_attr", %{to: opts[:to], attr: [attr, val]})
put_op(js, "toggle_attr", to: opts[:to], attr: [attr, val])
end

def toggle_attribute(%JS{} = js, {attr, val1, val2}, opts) when is_list(opts) do
opts = validate_keys(opts, :toggle_attribute, [:to])
put_op(js, "toggle_attr", %{to: opts[:to], attr: [attr, val1, val2]})
put_op(js, "toggle_attr", to: opts[:to], attr: [attr, val1, val2])
end

@doc """
Expand All @@ -734,7 +734,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `focus/1`."
def focus(%JS{} = js, opts) when is_list(opts) do
opts = validate_keys(opts, :focus, [:to])
put_op(js, "focus", %{to: opts[:to]})
put_op(js, "focus", to: opts[:to])
end

@doc """
Expand All @@ -756,7 +756,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `focus_first/1`."
def focus_first(%JS{} = js, opts) when is_list(opts) do
opts = validate_keys(opts, :focus_first, [:to])
put_op(js, "focus_first", %{to: opts[:to]})
put_op(js, "focus_first", to: opts[:to])
end

@doc """
Expand All @@ -779,7 +779,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `push_focus/1`."
def push_focus(%JS{} = js, opts) when is_list(opts) do
opts = validate_keys(opts, :push_focus, [:to])
put_op(js, "push_focus", %{to: opts[:to]})
put_op(js, "push_focus", to: opts[:to])
end

@doc """
Expand All @@ -790,7 +790,7 @@ defmodule Phoenix.LiveView.JS do
JS.pop_focus()
"""
def pop_focus(%JS{} = js \\ %JS{}) do
put_op(js, "pop_focus", %{})
put_op(js, "pop_focus", [])
end

@doc """
Expand Down Expand Up @@ -820,7 +820,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `navigate/1`."
def navigate(%JS{} = js, href, opts) when is_binary(href) and is_list(opts) do
opts = validate_keys(opts, :navigate, [:replace])
put_op(js, "navigate", %{href: href, replace: !!opts[:replace]})
put_op(js, "navigate", href: href, replace: !!opts[:replace])
end

@doc """
Expand Down Expand Up @@ -850,7 +850,7 @@ defmodule Phoenix.LiveView.JS do
@doc "See `patch/1`."
def patch(%JS{} = js, href, opts) when is_binary(href) and is_list(opts) do
opts = validate_keys(opts, :patch, [:replace])
put_op(js, "patch", %{href: href, replace: !!opts[:replace]})
put_op(js, "patch", href: href, replace: !!opts[:replace])
end

@doc """
Expand Down Expand Up @@ -884,22 +884,19 @@ defmodule Phoenix.LiveView.JS do
@doc "See `exec/1`."
def exec(%JS{} = js, attr, opts) when is_binary(attr) and is_list(opts) do
opts = validate_keys(opts, :exec, [:to])
ops = if to = opts[:to], do: [attr, to], else: [attr]
put_op(js, "exec", ops)
put_op(js, "exec", attr: attr, to: opts[:to])
end

defp put_op(%JS{ops: ops} = js, kind, args) do
args = drop_nil_values(args)
%JS{js | ops: ops ++ [[kind, args]]}
end

defp drop_nil_values(args) when is_map(args) do
defp drop_nil_values(args) when is_list(args) do
Enum.reject(args, fn {_k, v} -> is_nil(v) end)
|> Map.new()
end

defp drop_nil_values(args), do: args

defp class_names(nil), do: []

defp class_names(names) do
Expand Down
7 changes: 5 additions & 2 deletions test/phoenix_live_view/js_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ defmodule Phoenix.LiveView.JSTest do

describe "exec" do
test "with defaults" do
assert JS.exec("phx-remove") == %JS{ops: [["exec", ["phx-remove"]]]}
assert JS.exec("phx-remove", to: "#modal") == %JS{ops: [["exec", ["phx-remove", "#modal"]]]}
assert JS.exec("phx-remove") == %JS{ops: [["exec", %{attr: "phx-remove"}]]}

assert JS.exec("phx-remove", to: "#modal") == %JS{
ops: [["exec", %{attr: "phx-remove", to: "#modal"}]]
}
end
end

Expand Down

0 comments on commit 024216c

Please sign in to comment.