Destroy maps before leaving the current page

If we do not do this a map could be initialized twice times or more
when restoring a page with a map causing weird UI effects and
loading some map layers also twice times or more.

Need to add a maps array to be able to store all initialized
(visible) maps so we can destroy them when needed. Notice that
we are destroying maps also when admin settings tabs changes
(only visible ones), this is again to avoid to re-initialize map more
than once when users navigate through settings tabs, another
option to the settings issue could be to detect if the map was
already initialized to skip uneeded initialization.
This commit is contained in:
Senén Rodero Rodríguez
2020-07-29 14:53:57 +02:00
parent 87339451de
commit 289426c1c3
4 changed files with 34 additions and 5 deletions

View File

@@ -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();
};

View File

@@ -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);

View File

@@ -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();
}
});