From b9ce68bc821bc2dba1cb8270d875f2e14a21d81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Tue, 11 Aug 2020 13:02:32 +0200 Subject: [PATCH] Set marker coordinates as map center when map location fields has valid coordinates When a user recovers a page from browser history where placed a marker in different map pane (visible map layer) marker was successfully added to the map but the map center is the one defined at Settings map properties so the marker was not visible to the user. Now when map_location form has valid coordinates we use them instead of default map center settings. This will avoid the user to have to rellocate the marker (or find the correct pane where the marker was added) if already placed. --- app/assets/javascripts/map.js | 21 +++++++++++++----- spec/shared/system/mappable.rb | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/map.js b/app/assets/javascripts/map.js index 0dc8d15d3..dc9c92ef1 100644 --- a/app/assets/javascripts/map.js +++ b/app/assets/javascripts/map.js @@ -20,14 +20,12 @@ App.Map.maps = []; }, initializeMap: function(element) { - var addMarkerInvestments, clearFormfields, createMarker, editable, formCoordinates, getPopupContent, - latitudeInputSelector, longitudeInputSelector, map, mapAttribution, mapCenterLatLng, + var addMarkerInvestments, clearFormfields, createMarker, dataCoordinates, editable, formCoordinates, + getPopupContent, latitudeInputSelector, longitudeInputSelector, map, mapAttribution, mapCenterLatLng, mapCenterLatitude, mapCenterLongitude, mapTilesProvider, marker, markerIcon, markerLatitude, markerLongitude, moveOrPlaceMarker, openMarkerPopup, removeMarker, removeMarkerSelector, updateFormfields, zoom, zoomInputSelector; App.Map.cleanInvestmentCoordinates(element); - mapCenterLatitude = $(element).data("map-center-latitude"); - mapCenterLongitude = $(element).data("map-center-longitude"); mapTilesProvider = $(element).data("map-tiles-provider"); mapAttribution = $(element).data("map-tiles-provider-attribution"); latitudeInputSelector = $(element).data("latitude-input-selector"); @@ -38,12 +36,23 @@ long: $(longitudeInputSelector).val(), zoom: $(zoomInputSelector).val() }; + dataCoordinates = { + lat: $(element).data("marker-latitude"), + long: $(element).data("marker-longitude") + }; if (App.Map.validCoordinates(formCoordinates)) { markerLatitude = formCoordinates.lat; markerLongitude = formCoordinates.long; + mapCenterLatitude = formCoordinates.lat; + mapCenterLongitude = formCoordinates.long; + } else if (App.Map.validCoordinates(dataCoordinates)) { + markerLatitude = dataCoordinates.lat; + markerLongitude = dataCoordinates.long; + mapCenterLatitude = dataCoordinates.lat; + mapCenterLongitude = dataCoordinates.lat; } else { - markerLatitude = $(element).data("marker-latitude"); - markerLongitude = $(element).data("marker-longitude"); + mapCenterLatitude = $(element).data("map-center-latitude"); + mapCenterLongitude = $(element).data("map-center-longitude"); } if (App.Map.validZoom(formCoordinates.zoom)) { zoom = formCoordinates.zoom; diff --git a/spec/shared/system/mappable.rb b/spec/shared/system/mappable.rb index 8d1df1aab..70c4266d7 100644 --- a/spec/shared/system/mappable.rb +++ b/spec/shared/system/mappable.rb @@ -134,6 +134,38 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, expect(page.execute_script("return App.Map.maps[0].getZoom();")).to eq(11) end end + + scenario "shows marker at map center", :js do + do_login_for user + visit send(mappable_new_path, arguments) + + within ".map_location" do + expect(page).not_to have_css(".map-icon") + end + + place_map_at(-68.592487, -62.391357) + find("#new_map_location").click + + within ".map_location" do + expect(page).to have_css(".map-icon") + end + + if management + click_link "Select user" + + expect(page).to have_content "User management" + else + click_link "Help" + + expect(page).to have_content "CONSUL is a platform for citizen participation" + end + + go_back + + within ".map_location" do + expect(page).to have_css(".map-icon") + end + end end scenario "Skip map", :js do @@ -335,3 +367,11 @@ def map_zoom_in sleep 0.01 end end + +def place_map_at(latitude, longitude) + page.execute_script("App.Map.maps[0].setView(new L.LatLng(#{latitude}, #{longitude}))") + + until page.execute_script("return App.Map.maps[0].getCenter().lat === #{latitude};") do + sleep 0.01 + end +end