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