diff --git a/app/packs/src/decidim/locations/map/controller/location.js b/app/packs/src/decidim/locations/map/controller/location.js index 0de3220..6559aa6 100644 --- a/app/packs/src/decidim/locations/map/controller/location.js +++ b/app/packs/src/decidim/locations/map/controller/location.js @@ -87,8 +87,8 @@ export default class ModelLocMapController extends MapController { }) } this.markers[markerId] = marker; - this.triggerEvent("markeradd", [marker, ev]); marker.addTo(this.map); + this.triggerEvent("markeradd", [marker, ev]); return markerId; } diff --git a/app/packs/src/decidim/locations/map/integration/forms.js b/app/packs/src/decidim/locations/map/integration/forms.js index aa6c3cd..eace0cd 100644 --- a/app/packs/src/decidim/locations/map/integration/forms.js +++ b/app/packs/src/decidim/locations/map/integration/forms.js @@ -124,7 +124,8 @@ export default () => { }, 300); }); - typeLocButton.addEventListener("click", () => { + typeLocButton.addEventListener("click", (event) => { + event.preventDefault() const markerId = ctrl.addMarker(typeLocCoords, "typeEv"); const addressData = { address: typeLocInput.value, position: {lat: typeLocCoords[0], lng: typeLocCoords[1]}, markerId }; typeLocInput.value = ""; @@ -174,16 +175,20 @@ export default () => { }); ctrl.setEventHandler("markeradd", (marker, ev) => { const markerId = marker.options.id; - const oldMarker = markerFieldContainer.querySelector(".marker-field"); - if (mapConfig && mapConfig === "single" && oldMarker && ev === "clickEv") { - ctrl.deleteMarker(oldMarker.dataset.markerId); - markerFieldContainer.querySelector(`[data-marker-id="${oldMarker.dataset.markerId}"]`).remove(); - } + + if (mapConfig && mapConfig === "single" && (ev === "typeEv" || ev === "clickEv")) { + const oldMarker = markerFieldContainer.querySelector(".marker-field"); + if (oldMarker) { + ctrl.deleteMarker(oldMarker.dataset.markerId); + markerFieldContainer.querySelector(`[data-marker-id="${oldMarker.dataset.markerId}"]`).remove(); + }; + }; addMarkerField(markerFieldContainer, markerId); if (ev === "clickEv") { ctrl.bindPopUp(markerId); $(mapEl).trigger("geocoder-reverse.decidim", [marker.getLatLng(), { markerId }]); - } + }; + marker.on("click", () => { editModalEl.dataset.markerId = markerId; diff --git a/spec/system/map_spec.rb b/spec/system/map_spec.rb index baabb91..ca2dd40 100644 --- a/spec/system/map_spec.rb +++ b/spec/system/map_spec.rb @@ -346,7 +346,7 @@ def drag_marker end context "when adding multiple locations and configuration limits it" do - let!(:map_configuration) { "single" } + let(:map_configuration) { "single" } it "adds a single location" do click_link "Type locations" @@ -357,7 +357,7 @@ def drag_marker ) find("#autoComplete_result_0").click click_button "Add" - expect(page).to have_css(".leaflet-marker-draggable") + expect(page).to have_css(".marker-field") expect(page).to have_css(".leaflet-marker-draggable", count: 1) expect(page).to have_field("dummy_locations__index__address", type: :hidden, with: "Veneentekijäntie 4, Finland") find("#dummy_address").set("veneen") @@ -367,13 +367,6 @@ def drag_marker find("#autoComplete_result_1").click click_button "Add" expect(page).to have_field("dummy_locations__index__address", type: :hidden, with: "Veneentekijäntie 6, Finland") - find("#dummy_address").set("veneen") - expect(page).to have_content( - "Veneentekijäntie 4, Finland\nVeneentekijäntie 6, Finland\nVeneentekijäntie 7, Finland" - ) - find("#autoComplete_result_2").click - click_button "Add" - expect(page).to have_field("dummy_locations__index__address", type: :hidden, with: "Veneentekijäntie 7, Finland") expect(page).to have_css(".leaflet-marker-draggable", count: 1) end end