Files
phoenix-ausblick/lib/outlook/internal_tree/internal_tree.ex
2023-01-14 22:05:48 +01:00

70 lines
1.9 KiB
Elixir

defmodule Outlook.InternalTree.InternalTree do
alias Outlook.InternalTree.{InternalNode,TranslationUnit}
def garnish([%TranslationUnit{} = node | rest], %{tunits: _} = options) do
[ set_attributes(node, options.tunits, options.tu_ids)
| garnish(rest, options) ]
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
[ %InternalNode{node |
content: garnish(node.content, options)
} | garnish(rest, options) ]
end
def garnish([node | rest], options) do
[ node | garnish(rest, options) ]
end
def garnish([], _), do: []
defp set_attributes(node, atts, ids, []) do
set_attributes(node, atts, ids)
end
defp set_attributes(node, atts, ids, names) do
if node.name in names do
set_attributes(node, atts, [])
else
set_attributes(node, atts, ids)
end
end
defp set_attributes(node, atts, []) do
set_attributes(node, atts)
end
defp set_attributes(node, atts, ids) do
if node.nid in ids do
set_attributes(node, atts)
else
node
end
end
defp set_attributes(node, atts) do
attributes = Map.get(atts, :atts, %{})
attributes = if Map.has_key?(atts, :phx) do
# TODO: for all keys in atts.phx create a respective entry
Map.put(attributes, "phx-click",atts.phx.click)
# TODO: only convert to string if present
|> Map.put("phx-target", atts.phx.target |> to_string)
|> Map.put("phx-value-nid", node.nid)
end
%{node | eph: Map.put(node.eph, :attributes, attributes)}
end
end