~ihabunek/triglav

6d637d65c2d423ef1fa266dacbaf3e0721b108bc — Ivan Habunek 1 year, 19 days ago b1ad0a3
Add josm remote to load all relations for a route
M lib/triglav/josm.ex => lib/triglav/josm.ex +25 -3
@@ 6,18 6,40 @@ defmodule Triglav.Josm do
  https://josm.openstreetmap.de/wiki/Help/RemoteControlCommands
  """
  alias Triglav.Schemas.Osm.Relation
  alias Triglav.Schemas.Osm.Way

  @type object :: Relation.t() | Way.t()
  @type option :: [new_layer: boolean()]
  @type options :: [option]

  @base "http://127.0.0.1:8111"

  def url(path, params) do
  defp url(path, params) do
    "#{@base}#{path}?#{URI.encode_query(params)}"
  end

  def load_object(%Relation{} = relation, new_layer \\ true) do
  @spec load_object(object, options) :: String.t()
  def load_object(object, opts \\ []) do
    load_objects([object], opts)
  end

  @spec load_objects([object], options) :: String.t()
  def load_objects(objects, opts \\ []) do
    new_layer = Keyword.get(opts, :new_layer, false)

    url("/load_object", %{
      objects: "r#{relation.id}",
      objects: object_ids(objects),
      new_layer: to_string(new_layer),
      relation_members: "true"
    })
  end

  defp object_ids(objects) do
    objects
    |> Enum.map(&object_id/1)
    |> Enum.join(",")
  end

  defp object_id(%Relation{} = relation), do: "r#{relation.id}"
  defp object_id(%Way{} = relation), do: "w#{relation.id}"
end

M lib/triglav_web/templates/zet/routes/detail.html.eex => lib/triglav_web/templates/zet/routes/detail.html.eex +4 -1
@@ 5,6 5,9 @@

  <section>
    <%= render_relation_hierarchy(@hierarchy) %>
    <a class="josm-remote" href="<%= Triglav.Josm.load_objects(@relations) %>" target="_blank">
      Open in JOSM
    </a>
  </section>

  <%= if length(@errors) > 0 or map_size(@rel_errors) > 0 do %>


@@ 23,7 26,7 @@
        <ul>
          <%= for {relation, errors} <- @rel_errors do %>
            <li>
              <%= osm_link(relation, tags: ["type"], name: true, josm: true) %>
              <%= osm_link(relation, tags: ["type"], name: true) %>
              <ul>
                <%= for error <- errors do %>
                  <li><span class="red"><%= error %></span></li>

M lib/triglav_web/views/osm_helpers.ex => lib/triglav_web/views/osm_helpers.ex +10 -0
@@ 43,4 43,14 @@ defmodule TriglavWeb.OsmHelpers do
    </span>
    """
  end

  def josm_load_objects(objects) do
    link = Triglav.Josm.load_objects(objects)

    ~E"""
    <a class="josm-remote" href="<%= link %>" target="_blank">
      Open in JOSM
    </a>
    """
  end
end