diff --git a/assets/js/locate_me_hook.js b/assets/js/locate_me_hook.js index 07ce5d95..8fe27a45 100644 --- a/assets/js/locate_me_hook.js +++ b/assets/js/locate_me_hook.js @@ -1,8 +1,7 @@ export const CopyLink = { mounted() { this.el.addEventListener("click", () => { - const url = window.location.origin + this.el.dataset.url - navigator.clipboard.writeText(url).then(() => { + navigator.clipboard.writeText(window.location.href).then(() => { const orig = this.el.innerHTML this.el.innerHTML = 'Copied!' setTimeout(() => { this.el.innerHTML = orig }, 1500) diff --git a/lib/microwaveprop_web/live/path_live.ex b/lib/microwaveprop_web/live/path_live.ex index 4ae311a3..9bba152c 100644 --- a/lib/microwaveprop_web/live/path_live.ex +++ b/lib/microwaveprop_web/live/path_live.ex @@ -50,7 +50,8 @@ defmodule MicrowavepropWeb.PathLive do dst_height_ft: "30", tx_power_dbm: "20", src_gain_dbi: "30", - dst_gain_dbi: "30" + dst_gain_dbi: "30", + source_is_gps: false )} end @@ -58,16 +59,19 @@ defmodule MicrowavepropWeb.PathLive do def handle_params(params, _uri, socket) do p = Map.merge(@defaults, Map.take(params, @url_params)) + is_gps = p["source"] == "gps" + socket = assign(socket, - source: p["source"], + source: if(is_gps, do: socket.assigns.source, else: p["source"]), destination: p["destination"], band: p["band"], src_height_ft: p["src_height_ft"], dst_height_ft: p["dst_height_ft"], tx_power_dbm: p["tx_power_dbm"], src_gain_dbi: p["src_gain_dbi"], - dst_gain_dbi: p["dst_gain_dbi"] + dst_gain_dbi: p["dst_gain_dbi"], + source_is_gps: is_gps ) # If source=gps, request device location via JS @@ -156,19 +160,25 @@ defmodule MicrowavepropWeb.PathLive do {:compute_path, params["source"], params["destination"], String.to_integer(params["band"]), station_params} ) - url_params = params |> Map.take(@url_params) |> Enum.reject(fn {_k, v} -> v == "" end) |> Map.new() + url_params = + params + |> Map.take(@url_params) + |> then(fn p -> if socket.assigns.source_is_gps, do: Map.put(p, "source", "gps"), else: p end) + |> Enum.reject(fn {_k, v} -> v == "" end) + |> Map.new() + {:noreply, push_patch(socket, to: ~p"/path?#{url_params}", replace: true)} end def handle_event("gps_location", %{"lat" => lat, "lon" => lon}, socket) do - source = "#{Float.round(lat / 1, 6)},#{Float.round(lon / 1, 6)}" + coords = "#{Float.round(lat / 1, 6)},#{Float.round(lon / 1, 6)}" - # Replace gps with actual coords in URL and trigger calculation - socket = assign(socket, source: source, result: nil) + # Show coords in the input but keep source=gps in the URL + socket = assign(socket, source: coords, source_is_gps: true, result: nil) url_params = %{ - "source" => source, + "source" => "gps", "destination" => socket.assigns.destination, "band" => socket.assigns.band, "src_height_ft" => socket.assigns.src_height_ft, @@ -470,27 +480,6 @@ defmodule MicrowavepropWeb.PathLive do defp format_utc_hour(%DateTime{} = dt), do: Calendar.strftime(dt, "%H:%M") defp format_utc_hour(_), do: "?" - defp share_url(result, mode) do - source = - case mode do - :gps -> "gps" - :fixed -> "#{Float.round(result.source.lat, 4)},#{Float.round(result.source.lon, 4)}" - end - - params = %{ - "source" => source, - "destination" => result.destination.label, - "band" => to_string(result.band_mhz), - "src_height_ft" => to_string(result.station_params.src_height_ft), - "dst_height_ft" => to_string(result.station_params.dst_height_ft), - "tx_power_dbm" => to_string(result.station_params.tx_power_dbm), - "src_gain_dbi" => to_string(result.station_params.src_gain_dbi), - "dst_gain_dbi" => to_string(result.station_params.dst_gain_dbi) - } - - "/path?" <> URI.encode_query(params) - end - defp format_mw(mw) when mw >= 1000, do: "#{format_number(mw / 1000)} W" defp format_mw(mw) when mw >= 1, do: "#{format_number(mw)} mW" defp format_mw(mw), do: "#{format_number(mw * 1000)} uW" @@ -917,26 +906,16 @@ defmodule MicrowavepropWeb.PathLive do <% end %> - <%!-- Share links --%> -
+ <%!-- Share link --%> +
-
""" end