From e316bfcd4841e79985a8efba17230febd3322cfd Mon Sep 17 00:00:00 2001 From: Adrian Kumpf Date: Sat, 14 Sep 2019 20:58:32 +0200 Subject: [PATCH] Update map when the car is driving --- assets/js/hooks.js | 26 ++++++++++--------- .../templates/car/summary.html.leex | 13 +++++----- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/assets/js/hooks.js b/assets/js/hooks.js index db1abd38..26274889 100644 --- a/assets/js/hooks.js +++ b/assets/js/hooks.js @@ -58,16 +58,7 @@ function createMap(opts) { export const SimpleMap = { mounted() { - const $position = this.el.parentElement.querySelector(".position"); - - let marker; - const setView = () => { - if (marker) map.removeLayer(marker); - const [lat, lng] = $position.value.split(","); - const location = new LatLng(lat, lng); - map.setView(location, 17); - marker = new Marker(location, { icon }).addTo(map); - }; + const $position = document.querySelector(`#position_${this.el.dataset.id}`); const map = createMap({ zoomControl: false, @@ -78,14 +69,25 @@ export const SimpleMap = { scrollWheelZoom: false, tap: false }); - $position.addEventListener("change", setView); + + let marker; + const setView = () => { + if (marker) map.removeLayer(marker); + const [lat, lng] = $position.value.split(","); + const location = new LatLng(lat, lng); + map.setView(location, 17); + marker = new Marker(location, { icon }).addTo(map); + }; + setView(); + + $position.addEventListener("change", setView); } }; export const TriggerChange = { updated() { - this.el.dispatchEvent(new CustomEvent("change", {})); + this.el.dispatchEvent(new CustomEvent("change")); } }; diff --git a/lib/teslamate_web/templates/car/summary.html.leex b/lib/teslamate_web/templates/car/summary.html.leex index c2610564..956c20b9 100644 --- a/lib/teslamate_web/templates/car/summary.html.leex +++ b/lib/teslamate_web/templates/car/summary.html.leex @@ -1,18 +1,19 @@
-
+
- <%= text_input :position, "#{@car.id}", value: "#{@summary.latitude},#{@summary.longitude}", - phx_hook: "TriggerChange", - class: "position is-hidden", - disabled: true - %>
+ <%= text_input :position, "#{@car.id}", value: "#{@summary.latitude},#{@summary.longitude}", + phx_hook: "TriggerChange", + class: "is-hidden", + disabled: true + %>