2021-11-26 01:43:42 +08:00
|
|
|
defmodule LivebookWeb.Output.InputComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(socket) do
|
2021-12-12 07:09:35 +08:00
|
|
|
{:ok, assign(socket, error: nil, local: false)}
|
2021-11-26 01:43:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
value = assigns.input_values[assigns.attrs.id]
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign(value: value, initial_value: value)
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<form phx-change="change" phx-submit="submit" phx-target={@myself}>
|
|
|
|
<div class="input-label">
|
|
|
|
<%= @attrs.label %>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<.input
|
|
|
|
id={"#{@id}-input"}
|
|
|
|
attrs={@attrs}
|
|
|
|
value={@value}
|
|
|
|
error={@error}
|
|
|
|
myself={@myself} />
|
|
|
|
|
|
|
|
<%= if @error do %>
|
|
|
|
<div class="input-error">
|
|
|
|
<%= @error %>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</form>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(%{attrs: %{type: :select}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<select
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2021-11-26 01:43:42 +08:00
|
|
|
class="input input-select"
|
|
|
|
name="value">
|
|
|
|
<%= for {{key, label}, idx} <- Enum.with_index(@attrs.options) do %>
|
|
|
|
<option value={idx} selected={@value == key}>
|
|
|
|
<%= label %>
|
|
|
|
</option>
|
|
|
|
<% end %>
|
|
|
|
</select>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(%{attrs: %{type: :checkbox}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="mt-1">
|
|
|
|
<.switch_checkbox
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2021-11-26 01:43:42 +08:00
|
|
|
name="value"
|
|
|
|
checked={@value} />
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(%{attrs: %{type: :range}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="flex items-center space-x-2">
|
|
|
|
<div><%= @attrs.min %></div>
|
|
|
|
<input type="range"
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2021-11-26 01:43:42 +08:00
|
|
|
class="input-range"
|
|
|
|
name="value"
|
|
|
|
value={@value}
|
|
|
|
phx-debounce="300"
|
2021-12-02 23:45:00 +08:00
|
|
|
phx-blur="blur"
|
|
|
|
phx-target={@myself}
|
2021-11-26 01:43:42 +08:00
|
|
|
spellcheck="false"
|
|
|
|
autocomplete="off"
|
|
|
|
min={@attrs.min}
|
|
|
|
max={@attrs.max}
|
|
|
|
step={@attrs.step} />
|
|
|
|
<div><%= @attrs.max %></div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(%{attrs: %{type: :textarea}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<textarea
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2022-02-02 21:12:50 +08:00
|
|
|
class="input min-h-[200px] tiny-scrollbar"
|
2021-11-26 01:43:42 +08:00
|
|
|
name="value"
|
|
|
|
phx-debounce="300"
|
2021-12-02 23:45:00 +08:00
|
|
|
phx-blur="blur"
|
|
|
|
phx-target={@myself}
|
2021-11-26 01:43:42 +08:00
|
|
|
spellcheck="false"><%= [?\n, @value] %></textarea>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(%{attrs: %{type: :password}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<.with_password_toggle id={"#{@id}-password-toggle"}>
|
|
|
|
<input type="password"
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2021-11-26 01:43:42 +08:00
|
|
|
class="input w-auto bg-gray-50"
|
|
|
|
name="value"
|
|
|
|
value={@value}
|
|
|
|
phx-debounce="300"
|
2021-12-02 23:45:00 +08:00
|
|
|
phx-blur="blur"
|
|
|
|
phx-target={@myself}
|
2021-11-26 01:43:42 +08:00
|
|
|
spellcheck="false"
|
|
|
|
autocomplete="off" />
|
|
|
|
</.with_password_toggle>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2021-12-02 23:45:00 +08:00
|
|
|
defp input(%{attrs: %{type: type}} = assigns) when type in [:number, :color, :url, :text] do
|
2021-11-26 01:43:42 +08:00
|
|
|
~H"""
|
|
|
|
<input type={html_input_type(@attrs.type)}
|
2022-04-04 18:19:11 +08:00
|
|
|
data-el-input
|
2021-11-26 01:43:42 +08:00
|
|
|
class={"input w-auto #{if(@error, do: "input--error")}"}
|
|
|
|
name="value"
|
|
|
|
value={to_string(@value)}
|
|
|
|
phx-debounce="300"
|
|
|
|
phx-blur="blur"
|
2021-12-02 23:45:00 +08:00
|
|
|
phx-target={@myself}
|
|
|
|
spellcheck="false"
|
|
|
|
autocomplete="off" />
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp input(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="text-red-600">
|
|
|
|
Unknown input type <%= @attrs.type %>
|
|
|
|
</div>
|
2021-11-26 01:43:42 +08:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp html_input_type(:number), do: "number"
|
|
|
|
defp html_input_type(:color), do: "color"
|
|
|
|
defp html_input_type(:url), do: "text"
|
|
|
|
defp html_input_type(:text), do: "text"
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("change", %{"value" => html_value}, socket) do
|
|
|
|
{:noreply, handle_html_value(socket, html_value)}
|
|
|
|
end
|
|
|
|
|
2021-12-02 23:45:00 +08:00
|
|
|
def handle_event("blur", %{"value" => html_value}, socket) do
|
|
|
|
socket = handle_html_value(socket, html_value)
|
|
|
|
|
2021-11-26 01:43:42 +08:00
|
|
|
if socket.assigns.error do
|
|
|
|
{:noreply, assign(socket, value: socket.assigns.initial_value, error: nil)}
|
|
|
|
else
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("submit", %{"value" => html_value}, socket) do
|
|
|
|
socket = handle_html_value(socket, html_value)
|
|
|
|
send(self(), {:queue_bound_cells_evaluation, socket.assigns.attrs.id})
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_html_value(socket, html_value) do
|
2021-12-02 23:45:00 +08:00
|
|
|
current_value = socket.assigns.value
|
|
|
|
|
2021-11-26 01:43:42 +08:00
|
|
|
case parse(html_value, socket.assigns.attrs) do
|
2021-12-02 23:45:00 +08:00
|
|
|
{:ok, ^current_value} ->
|
|
|
|
socket
|
|
|
|
|
2021-11-26 01:43:42 +08:00
|
|
|
{:ok, value} ->
|
2021-12-12 07:09:35 +08:00
|
|
|
send(
|
|
|
|
self(),
|
|
|
|
{:set_input_values, [{socket.assigns.attrs.id, value}], socket.assigns.local}
|
|
|
|
)
|
|
|
|
|
|
|
|
unless socket.assigns.local do
|
|
|
|
report_event(socket, value)
|
|
|
|
end
|
|
|
|
|
2021-11-26 01:43:42 +08:00
|
|
|
assign(socket, value: value, error: nil)
|
|
|
|
|
|
|
|
{:error, error, value} ->
|
|
|
|
assign(socket, value: value, error: error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :text}) do
|
|
|
|
{:ok, html_value}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :textarea}) do
|
|
|
|
# The browser may normalize newlines to \r\n, but we prefer just \n
|
|
|
|
value = String.replace(html_value, "\r\n", "\n")
|
|
|
|
{:ok, value}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :password}) do
|
|
|
|
{:ok, html_value}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :number}) do
|
|
|
|
if html_value == "" do
|
|
|
|
{:ok, nil}
|
|
|
|
else
|
|
|
|
case Integer.parse(html_value) do
|
|
|
|
{number, ""} ->
|
|
|
|
{:ok, number}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{number, ""} = Float.parse(html_value)
|
|
|
|
{:ok, number}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :url}) do
|
|
|
|
cond do
|
|
|
|
html_value == "" -> {:ok, nil}
|
|
|
|
Livebook.Utils.valid_url?(html_value) -> {:ok, html_value}
|
|
|
|
true -> {:error, "not a valid URL", html_value}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :select, options: options}) do
|
|
|
|
selected_idx = String.to_integer(html_value)
|
|
|
|
|
|
|
|
options
|
|
|
|
|> Enum.with_index()
|
|
|
|
|> Enum.find_value(fn {{key, _label}, idx} ->
|
|
|
|
idx == selected_idx && {:ok, key}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :checkbox}) do
|
|
|
|
{:ok, html_value == "true"}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :range}) do
|
|
|
|
{number, ""} = Float.parse(html_value)
|
|
|
|
{:ok, number}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse(html_value, %{type: :color}) do
|
|
|
|
{:ok, html_value}
|
|
|
|
end
|
2021-12-02 23:45:00 +08:00
|
|
|
|
|
|
|
defp report_event(socket, value) do
|
|
|
|
topic = socket.assigns.attrs.ref
|
|
|
|
event = %{value: value, origin: self(), type: :change}
|
|
|
|
send(socket.assigns.attrs.destination, {:event, topic, event})
|
|
|
|
end
|
2021-11-26 01:43:42 +08:00
|
|
|
end
|