Merge #17036 - Fix: Don't add multiple OpenLayers maps, remove listeners on dispose

Pull-request: #17036

Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
William Desportes 2021-08-14 15:57:52 +02:00
commit d7c2af5f1e
No known key found for this signature in database
GPG Key ID: 90A0EF1B8251A889
3 changed files with 18 additions and 14 deletions

View File

@ -21,6 +21,8 @@ var scale = defaultScale;
/** @type {SVGElement|undefined} */ /** @type {SVGElement|undefined} */
var gisSvg; var gisSvg;
/** @type {ol.Map|undefined} */
var map;
/** /**
* Zooms and pans the visualization. * Zooms and pans the visualization.
@ -129,16 +131,14 @@ function initGISVisualization () {
zoomAndPan(); zoomAndPan();
} }
function drawOpenLayerMap (openLayerCreate) { function drawOpenLayerMap () {
$('#placeholder').hide(); $('#placeholder').hide();
$('#openlayersmap').show(); $('#openlayersmap').show();
// Function doesn't work properly if #openlayersmap is hidden // Function doesn't work properly if #openlayersmap is hidden
if (!openLayerCreate) { if (typeof map !== 'object') {
// Draws openStreetMap with openLayers // Draws openStreetMap with openLayers
drawOpenLayers(); map = drawOpenLayers();
return 1;
} }
return 0;
} }
function getRelativeCoords (e) { function getRelativeCoords (e) {
@ -200,18 +200,21 @@ AJAX.registerTeardown('table/gis_visualization.js', function () {
onGisMouseWheel, onGisMouseWheel,
PASSIVE_EVENT_LISTENERS ? { passive: false } : undefined PASSIVE_EVENT_LISTENERS ? { passive: false } : undefined
); );
if (map) {
// Removes ol.Map's resize listener from window
map.setTarget(null);
map = undefined;
}
}); });
AJAX.registerOnload('table/gis_visualization.js', function () { AJAX.registerOnload('table/gis_visualization.js', function () {
var openLayerCreate = 0;
// If we are in GIS visualization, initialize it // If we are in GIS visualization, initialize it
if ($('#gis_div').length > 0) { if ($('#gis_div').length > 0) {
initGISVisualization(); initGISVisualization();
} }
if ($('#choice').prop('checked') === true) { if ($('#choice').prop('checked') === true) {
openLayerCreate = drawOpenLayerMap(openLayerCreate); drawOpenLayerMap();
} }
if (typeof ol === 'undefined') { if (typeof ol === 'undefined') {
@ -223,7 +226,7 @@ AJAX.registerOnload('table/gis_visualization.js', function () {
$('#placeholder').show(); $('#placeholder').show();
$('#openlayersmap').hide(); $('#openlayersmap').hide();
} else { } else {
openLayerCreate = drawOpenLayerMap(openLayerCreate); drawOpenLayerMap();
} }
}); });

View File

@ -485,7 +485,8 @@ class GisVisualization
{ {
$this->init(); $this->init();
$scale_data = $this->scaleDataSet($this->data); $scale_data = $this->scaleDataSet($this->data);
$output = 'if (typeof ol !== "undefined") {' $output = 'function drawOpenLayers() {'
. 'if (typeof ol !== "undefined") {'
. 'var olCss = "js/vendor/openlayers/theme/ol.css";' . 'var olCss = "js/vendor/openlayers/theme/ol.css";'
. '$(\'head\').append(\'<link rel="stylesheet" type="text/css" href=\'+olCss+\'>\');' . '$(\'head\').append(\'<link rel="stylesheet" type="text/css" href=\'+olCss+\'>\');'
. 'var vectorLayer = new ol.source.Vector({});' . 'var vectorLayer = new ol.source.Vector({});'
@ -510,6 +511,9 @@ class GisVisualization
. 'new ol.control.Attribution]' . 'new ol.control.Attribution]'
. '});'; . '});';
$output .= $this->prepareDataSet($this->data, $scale_data, 'ol', '') $output .= $this->prepareDataSet($this->data, $scale_data, 'ol', '')
. 'return map;'
. '}'
. 'return undefined;'
. '}'; . '}';
return $output; return $output;

View File

@ -71,10 +71,7 @@
<div id="openlayersmap"></div> <div id="openlayersmap"></div>
<input type="hidden" id="themeImagePath" value="{{ theme_image_path }}"> <input type="hidden" id="themeImagePath" value="{{ theme_image_path }}">
<script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript">
function drawOpenLayers() {{ draw_ol|raw }}
{
{{ draw_ol|raw }}
}
</script> </script>
</fieldset> </fieldset>
</div> </div>