Update garnish and render_doc functions
Now all element node attributes have to be threaded through the eph.attributes which is done by the garnish function.
This commit is contained in:
@ -33,6 +33,11 @@ defmodule Outlook.InternalTree do
|
|||||||
options,
|
options,
|
||||||
fn prop, opts -> Map.put_new(opts, prop, []) end
|
fn prop, opts -> Map.put_new(opts, prop, []) end
|
||||||
)
|
)
|
||||||
|
options = Enum.reduce(
|
||||||
|
~w(elements tunits)a,
|
||||||
|
options,
|
||||||
|
fn prop, opts -> Map.put_new(opts, prop, %{}) end
|
||||||
|
)
|
||||||
InternalTree.garnish(tree, options)
|
InternalTree.garnish(tree, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -2,23 +2,13 @@ defmodule Outlook.InternalTree.InternalTree do
|
|||||||
|
|
||||||
alias Outlook.InternalTree.{InternalNode,TranslationUnit}
|
alias Outlook.InternalTree.{InternalNode,TranslationUnit}
|
||||||
|
|
||||||
def garnish([%TranslationUnit{} = node | rest], %{tunits: _} = options) do
|
def garnish([%TranslationUnit{} = node | rest], options) do
|
||||||
[ set_attributes(node, options.tunits, options.tu_ids)
|
[ set_attributes(node, options.tunits, options.tu_ids)
|
||||||
| garnish(rest, options) ]
|
| garnish(rest, options) ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def garnish([%TranslationUnit{} = node | rest], options) do
|
|
||||||
[ node | garnish(rest, options) ]
|
|
||||||
end
|
|
||||||
|
|
||||||
def garnish([%InternalNode{type: :element} = node | rest], %{elements: _} = options) do
|
|
||||||
node = set_attributes(node, options.elements, options.el_ids, options.el_names)
|
|
||||||
[ %InternalNode{node |
|
|
||||||
content: garnish(node.content, options)
|
|
||||||
} | garnish(rest, options) ]
|
|
||||||
end
|
|
||||||
|
|
||||||
def garnish([%InternalNode{type: :element} = node | rest], options) do
|
def garnish([%InternalNode{type: :element} = node | rest], options) do
|
||||||
|
node = set_attributes(node, options.elements, options.el_ids, options.el_names)
|
||||||
[ %InternalNode{node |
|
[ %InternalNode{node |
|
||||||
content: garnish(node.content, options)
|
content: garnish(node.content, options)
|
||||||
} | garnish(rest, options) ]
|
} | garnish(rest, options) ]
|
||||||
@ -56,14 +46,16 @@ defmodule Outlook.InternalTree.InternalTree do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp set_attributes(node, atts) do
|
defp set_attributes(node, atts) do
|
||||||
attributes = Map.get(atts, :atts, %{})
|
node_atts = Map.get(node, :attributes, %{})
|
||||||
attributes = if Map.has_key?(atts, :phx) do
|
|> Map.merge(Map.get(node.eph, :attributes, %{}))
|
||||||
# TODO: for all keys in atts.phx create a respective entry
|
attributes = Enum.map(atts, fn {k,v} ->
|
||||||
Map.put(attributes, "phx-click",atts.phx.click)
|
nv = case is_function(v) do
|
||||||
# TODO: only convert to string if present
|
true -> v.(node)
|
||||||
|> Map.put("phx-target", atts.phx.target |> to_string)
|
false -> v
|
||||||
|> Map.put("phx-value-nid", node.nid)
|
end
|
||||||
end
|
{k,nv}
|
||||||
|
end)
|
||||||
|
|> Enum.into(node_atts)
|
||||||
%{node | eph: Map.put(node.eph, :attributes, attributes)}
|
%{node | eph: Map.put(node.eph, :attributes, attributes)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -11,9 +11,7 @@ defmodule OutlookWeb.HtmlDocComponent do
|
|||||||
|
|
||||||
def render_doc(%{tunit_tag: _} = assigns) do
|
def render_doc(%{tunit_tag: _} = assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<%= for node <- @tree do %>
|
<.dnode :for={node <- @tree} node={node} tunit_tag={@tunit_tag} />
|
||||||
<.dnode node={node} tunit_tag={@tunit_tag} />
|
|
||||||
<% end %>
|
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
def render_doc(assigns) do
|
def render_doc(assigns) do
|
||||||
@ -24,9 +22,8 @@ defmodule OutlookWeb.HtmlDocComponent do
|
|||||||
|
|
||||||
def dnode(%{node: %{status: status}} = assigns) do
|
def dnode(%{node: %{status: status}} = assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<.dynamic_tag name={@tunit_tag} class="tunit" nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}>
|
<.dynamic_tag name={@tunit_tag} nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}
|
||||||
<%= @node.content |> raw %>
|
><%= @node.content |> raw %></.dynamic_tag>
|
||||||
</.dynamic_tag>
|
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,11 +35,9 @@ defmodule OutlookWeb.HtmlDocComponent do
|
|||||||
|
|
||||||
def dnode(assigns) when assigns.node.type == :element do
|
def dnode(assigns) when assigns.node.type == :element do
|
||||||
~H"""
|
~H"""
|
||||||
<.dynamic_tag name={@node.name} nid={@node.nid} {@node.attributes |> Map.merge(Map.get(@node.eph, :attributes, %{}))}>
|
<.dynamic_tag name={@node.name} nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}
|
||||||
<%= for child_node <- @node.content do %>
|
><.dnode :for={child_node <- @node.content} node={child_node} tunit_tag={@tunit_tag}
|
||||||
<.dnode node={child_node} tunit_tag={@tunit_tag} />
|
/></.dynamic_tag>
|
||||||
<% end %>
|
|
||||||
</.dynamic_tag>
|
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user