diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 65ec8687f..5e7842945 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -174,6 +174,7 @@ var destroy_non_idempotent_modules = function() { App.Datepicker.destroy(); App.HTMLEditor.destroy(); App.LegislationAnnotatable.destroy(); + App.Map.destroy(); App.SocialShare.destroy(); }; diff --git a/app/assets/javascripts/map.js b/app/assets/javascripts/map.js index d160d3a92..9d864d585 100644 --- a/app/assets/javascripts/map.js +++ b/app/assets/javascripts/map.js @@ -1,6 +1,7 @@ (function() { "use strict"; App.Map = { + maps: [], initialize: function() { $("*[data-map]:visible").each(function() { App.Map.initializeMap(this); @@ -11,6 +12,13 @@ } }); }, + destroy: function() { + App.Map.maps.forEach(function(map) { + map.off(); + map.remove(); + }); + App.Map.maps = []; + }, initializeMap: function(element) { var addMarkerInvestments, clearFormfields, createMarker, editable, getPopupContent, latitudeInputSelector, longitudeInputSelector, map, mapAttribution, mapCenterLatLng, mapCenterLatitude, mapCenterLongitude, mapTilesProvider, marker, markerIcon, markerLatitude, markerLongitude, moveOrPlaceMarker, openMarkerPopup, removeMarker, removeMarkerSelector, updateFormfields, zoom, zoomInputSelector; App.Map.cleanInvestmentCoordinates(element); @@ -88,6 +96,7 @@ }; mapCenterLatLng = new L.LatLng(mapCenterLatitude, mapCenterLongitude); map = L.map(element.id).setView(mapCenterLatLng, zoom); + App.Map.maps.push(map); L.tileLayer(mapTilesProvider, { attribution: mapAttribution }).addTo(map); diff --git a/app/assets/javascripts/settings.js b/app/assets/javascripts/settings.js index cea8fcda7..57be249ae 100644 --- a/app/assets/javascripts/settings.js +++ b/app/assets/javascripts/settings.js @@ -3,12 +3,8 @@ App.Settings = { initialize: function() { $("#settings-tabs").on("change.zf.tabs", function() { - var map_container; if ($("#tab-map-configuration:visible").length) { - map_container = L.DomUtil.get("admin-map"); - if (map_container !== null) { - map_container._leaflet_id = null; - } + App.Map.destroy(); App.Map.initialize(); } }); diff --git a/spec/shared/system/mappable.rb b/spec/shared/system/mappable.rb index 063b02851..455e5876e 100644 --- a/spec/shared/system/mappable.rb +++ b/spec/shared/system/mappable.rb @@ -79,6 +79,29 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, expect(page).to have_content "Map location can't be blank" end + describe "When restoring the page from browser history" do + scenario "map should not be duplicated", :js do + do_login_for user + visit send(mappable_new_path, arguments) + + 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(".leaflet-map-pane", count: 1) + end + end + end + scenario "Skip map", :js do do_login_for user visit send(mappable_new_path, arguments)