~ihabunek/triglav

5bee7c999e50283fe87056046ad9ab42e362e70c — Ivan Habunek 1 year, 7 months ago 62b156b
Improve osm_link helper
M lib/triglav/schemas/osm/relation.ex => lib/triglav/schemas/osm/relation.ex +8 -0
@@ 15,4 15,12 @@ defmodule Triglav.Schemas.Osm.Relation do

    field :type, :string, virtual: true
  end

  def get_tag(relation, name) do
    Map.get(relation.tags, name)
  end

  def has_tag?(relation, name) do
    Map.has_key?(relation.tags, name)
  end
end

M lib/triglav_web/templates/zet/routes/detail.html.eex => lib/triglav_web/templates/zet/routes/detail.html.eex +4 -1
@@ 14,7 14,10 @@
        <%= for error <- @errors do %>
          <li>
            <%= case error do
              {relation, error} -> [osm_link(relation), error]
              {relation, error} -> [
                osm_link(relation, tags: ["type"]),
                error
              ]
              error -> error
            end %>
          </li>

M lib/triglav_web/templates/zet/routes/index.html.eex => lib/triglav_web/templates/zet/routes/index.html.eex +2 -7
@@ 33,7 33,7 @@
            <%= for error <- errors do %>
              <li>
                <%= case error do
                  {relation, error} -> [osm_link(relation), error]
                  {relation, error} -> [osm_link(relation, tags: ["type"]), error]
                  error -> error
                end %>
              </li>


@@ 55,12 55,7 @@
    <ul>
      <%= for relation <- @unmached_relations do %>
      <li>
        <%= osm_link(relation) %>
        <%= relation.tags["name"] %>
        <span class="gray">[
          type=<%= relation.tags["type"] %>
          ref=<%= relation.tags["ref"] %>
        ]</span>
        <%= osm_link(relation, tags: ["type", "ref"], name: true) %>
      </li>
      <% end %>
    </ul>

M lib/triglav_web/views/osm_helpers.ex => lib/triglav_web/views/osm_helpers.ex +11 -1
@@ 9,14 9,24 @@ defmodule TriglavWeb.OsmHelpers do
  @doc """
  Renders a link to the relation on OpenStreetMap website.
  """
  def osm_link(%Relation{} = relation) do
  def osm_link(%Relation{} = relation, opts \\ []) do
    image_url = Helpers.static_path(TriglavWeb.Endpoint, "/images/osm/relation.svg")
    target_url = "https://www.openstreetmap.org/relation/#{relation.id}"
    name? = Keyword.get(opts, :name)

    tags =
    for tag <- Keyword.get(opts, :tags, []), Relation.has_tag?(relation, tag) do
      {tag, Relation.get_tag(relation, tag)}
    end

    ~E"""
    <span class="osm-link">
      <img src="<%= image_url %>" alt="Relation" />
      <a href="<%= target_url %>"><%= relation.id %></a>
      <%= if name? do %>
        <%= relation.tags["name"] %>
      <% end %>
      <%= for {name, value} <- tags do %><span class="gray">[<%= name %>=<%= value %>]</span><% end %>
    </span>
    """
  end