From 0e68369d2d59c2c2d05a09e17b15ade7835c0621 Mon Sep 17 00:00:00 2001 From: sulphur Date: Wed, 14 Oct 2020 19:55:46 +0200 Subject: [PATCH 1/5] add custom html tag for show/edit page --- lib/kaffy/resource_form.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/kaffy/resource_form.ex b/lib/kaffy/resource_form.ex index 99f623bd..6a28fd0f 100644 --- a/lib/kaffy/resource_form.ex +++ b/lib/kaffy/resource_form.ex @@ -49,6 +49,7 @@ defmodule Kaffy.ResourceForm do opts end + opts = if options[:value], do: Keyword.put(opts, :value_fn, options.value), else: opts permission = case is_nil(changeset.data.id) do true -> Map.get(options, :create, :editable) @@ -197,6 +198,8 @@ defmodule Kaffy.ResourceForm do :utc_datetime_usec -> flatpickr_datetime_usec(form, field, opts) + :safe_html -> + {:safe, opts[:value_fn].(form.data)} _ -> text_input(form, field, opts) end From 4cbb4935197b0c42178aeabe9cd899f6ef18422c Mon Sep 17 00:00:00 2001 From: sulphur Date: Tue, 20 Oct 2020 16:21:55 +0200 Subject: [PATCH 2/5] add image_preview field --- lib/kaffy/resource_form.ex | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/kaffy/resource_form.ex b/lib/kaffy/resource_form.ex index 6a28fd0f..569cf41a 100644 --- a/lib/kaffy/resource_form.ex +++ b/lib/kaffy/resource_form.ex @@ -49,7 +49,6 @@ defmodule Kaffy.ResourceForm do opts end - opts = if options[:value], do: Keyword.put(opts, :value_fn, options.value), else: opts permission = case is_nil(changeset.data.id) do true -> Map.get(options, :create, :editable) @@ -62,6 +61,12 @@ defmodule Kaffy.ResourceForm do !is_nil(choices) -> select(form, field, choices, class: "custom-select") + type == :image_preview -> + build_image_preview(changeset.data, options) + + type == :custom_html -> + build_custom_html(changeset.data, options) + true -> build_html_input(changeset.data, form, field, type, opts, permission == :readonly) end @@ -72,6 +77,20 @@ defmodule Kaffy.ResourceForm do build_html_input(changeset.data, form, field, type, opts) end + defp build_image_preview(data, opts) do + url = extract_value(data, opts) + content_tag :img, "", src: url, style: Map.get(opts, :inline_css) + end + + defp build_custom_html(data, opts) do + value = extract_value(data, opts) + {:safe, value} + end + + defp extract_value(data, %{value_fn: fun}) when is_function(fun), do: fun.(data) + defp extract_value(_data, %{value: value}), do: value + defp extract_value(_data, _), do: raise (":value or :value_fn is missing") + defp build_html_input(schema, form, field, type, opts, readonly \\ false) do data = schema {conn, opts} = Keyword.pop(opts, :conn) @@ -198,8 +217,6 @@ defmodule Kaffy.ResourceForm do :utc_datetime_usec -> flatpickr_datetime_usec(form, field, opts) - :safe_html -> - {:safe, opts[:value_fn].(form.data)} _ -> text_input(form, field, opts) end From 2e17a9d0e1e52b7f5f7786d68b8826ab5f60b9a3 Mon Sep 17 00:00:00 2001 From: sulphur Date: Thu, 3 Dec 2020 17:56:29 +0100 Subject: [PATCH 3/5] change type names to image and html --- lib/kaffy/resource_form.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kaffy/resource_form.ex b/lib/kaffy/resource_form.ex index 569cf41a..f7c5885c 100644 --- a/lib/kaffy/resource_form.ex +++ b/lib/kaffy/resource_form.ex @@ -61,10 +61,10 @@ defmodule Kaffy.ResourceForm do !is_nil(choices) -> select(form, field, choices, class: "custom-select") - type == :image_preview -> + type == :image -> build_image_preview(changeset.data, options) - type == :custom_html -> + type == :html -> build_custom_html(changeset.data, options) true -> From 4bbfa8a3c24ff13cba8fdfc0a078b857d9f805ce Mon Sep 17 00:00:00 2001 From: sulphur Date: Mon, 21 Jun 2021 17:35:37 +0200 Subject: [PATCH 4/5] use String.to_atom --- lib/kaffy/utils.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kaffy/utils.ex b/lib/kaffy/utils.ex index 5542273e..0490edc9 100644 --- a/lib/kaffy/utils.ex +++ b/lib/kaffy/utils.ex @@ -352,7 +352,7 @@ defmodule Kaffy.Utils do end defp convert_to_atom(string) do - if is_binary(string), do: String.to_existing_atom(string), else: string + if is_binary(string), do: String.to_atom(string), else: string end defp setup_resources do From 72f07149f811925e247d119a0e7053d6a3e241a1 Mon Sep 17 00:00:00 2001 From: sulphur Date: Wed, 8 Sep 2021 14:50:19 +0200 Subject: [PATCH 5/5] relax phoenix_html dependency --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 47cb3fff..5389c052 100644 --- a/mix.exs +++ b/mix.exs @@ -31,7 +31,7 @@ defmodule Kaffy.MixProject do defp deps do [ {:phoenix, "~> 1.4"}, - {:phoenix_html, "~> 2.11"}, + {:phoenix_html, ">= 0.0.0"}, {:mock, "~> 0.3.0", only: :test}, {:ecto, "~> 3.0"}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}